From c3fb3df84012ec3f8fb78411a5f8478833ef366f Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 18 Jan 2013 08:47:15 +0100 Subject: [PATCH] * Try to fix search field behaviour on OSX. --- .../thirdparty/Qocoa/qsearchfield.cpp | 5 ++ .../thirdparty/Qocoa/qsearchfield.h | 10 +-- .../thirdparty/Qocoa/qsearchfield_mac.mm | 85 ++++++++++--------- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp b/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp index a1aa829e2..bf77d088a 100644 --- a/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp +++ b/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp @@ -145,4 +145,9 @@ void QSearchField::resizeEvent(QResizeEvent* e) } +bool QSearchField::eventFilter(QObject *o, QEvent *e) +{ + return QWidget::eventFilter(o, e); +} + #include "qsearchfield.moc" diff --git a/src/libtomahawk/thirdparty/Qocoa/qsearchfield.h b/src/libtomahawk/thirdparty/Qocoa/qsearchfield.h index 0b429972f..604a657a9 100644 --- a/src/libtomahawk/thirdparty/Qocoa/qsearchfield.h +++ b/src/libtomahawk/thirdparty/Qocoa/qsearchfield.h @@ -10,19 +10,16 @@ class QSearchFieldPrivate; class DLLEXPORT QSearchField : public QWidget { Q_OBJECT - - Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText); - public: explicit QSearchField(QWidget *parent); QString text() const; QString placeholderText() const; - void setFocus(Qt::FocusReason); + void setFocus(Qt::FocusReason reason); public slots: void setText(const QString &text); - void setPlaceholderText(const QString& text); + void setPlaceholderText(const QString &text); void clear(); void selectAll(); void setFocus(); @@ -34,10 +31,13 @@ signals: protected: void resizeEvent(QResizeEvent*); + bool eventFilter(QObject*, QEvent*); private: friend class QSearchFieldPrivate; QPointer pimpl; + + Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText); }; #endif // QSEARCHFIELD_H diff --git a/src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm b/src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm index 54fafd83c..9731ae103 100644 --- a/src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm +++ b/src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm @@ -1,6 +1,5 @@ /* Copyright (C) 2011 by Mike McQuaid -Copyright (C) 2011 by Leo Franchi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -22,20 +21,16 @@ THE SOFTWARE. */ #include "qsearchfield.h" -#include "moc_qsearchfield.cpp" #include "qocoa_mac.h" -#import +#import "Foundation/NSAutoreleasePool.h" +#import "Foundation/NSNotification.h" +#import "AppKit/NSSearchField.h" #include #include -#define KEYCODE_A 0 -#define KEYCODE_X 7 -#define KEYCODE_C 8 -#define KEYCODE_V 9 - class QSearchFieldPrivate : public QObject { public: @@ -81,7 +76,6 @@ public: } -(void)controlTextDidEndEditing:(NSNotification*)notification { - Q_UNUSED(notification); // No Q_ASSERT here as it is called on destruction. if (pimpl) pimpl->textDidEndEditing(); @@ -91,6 +85,15 @@ public: } @end +namespace { + +static const unsigned short kKeycodeA = 0; +static const unsigned short kKeycodeX = 7; +static const unsigned short kKeycodeC = 8; +static const unsigned short kKeycodeV = 9; + +} // namespace + @interface QocoaSearchField : NSSearchField -(BOOL)performKeyEquivalent:(NSEvent*)event; @end @@ -100,24 +103,24 @@ public: if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask) { const unsigned short keyCode = [event keyCode]; - if (keyCode == KEYCODE_A) + if (keyCode == kKeycodeA) // Cmd+a { [self performSelector:@selector(selectText:)]; return YES; } - else if (keyCode == KEYCODE_C) + else if (keyCode == kKeycodeC) // Cmd+c { QClipboard* clipboard = QApplication::clipboard(); clipboard->setText(toQString([self stringValue])); return YES; } - else if (keyCode == KEYCODE_V) + else if (keyCode == kKeycodeV) // Cmd+v { QClipboard* clipboard = QApplication::clipboard(); [self setStringValue:fromQString(clipboard->text())]; return YES; } - else if (keyCode == KEYCODE_X) + else if (keyCode == kKeycodeX) // Cmd+x { QClipboard* clipboard = QApplication::clipboard(); clipboard->setText(toQString([self stringValue])); @@ -145,10 +148,8 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent) setFixedHeight(24); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - layout()->setContentsMargins(2, 0, 2, 0); - setStyleSheet( "* { background: #DDE4EB; }" ); - [search release]; + [pool drain]; } @@ -163,7 +164,7 @@ void QSearchField::setText(const QString &text) [pool drain]; } -void QSearchField::setPlaceholderText(const QString& text) +void QSearchField::setPlaceholderText(const QString &text) { Q_ASSERT(pimpl); if (!pimpl) @@ -174,6 +175,27 @@ void QSearchField::setPlaceholderText(const QString& text) [pool drain]; } +QString QSearchField::placeholderText() const { + Q_ASSERT(pimpl); + NSString* placeholder = [[pimpl->nsSearchField cell] placeholderString]; + return toQString(placeholder); +} + +void QSearchField::setFocus(Qt::FocusReason reason) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + if ([pimpl->nsSearchField acceptsFirstResponder]) + [[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField]; +} + +void QSearchField::setFocus() +{ + setFocus(Qt::OtherFocusReason); +} + void QSearchField::clear() { Q_ASSERT(pimpl); @@ -202,31 +224,12 @@ QString QSearchField::text() const return toQString([pimpl->nsSearchField stringValue]); } -QString QSearchField::placeholderText() const -{ - Q_ASSERT(pimpl); - if (!pimpl) - return QString(); - - return toQString([[pimpl->nsSearchField cell] placeholderString]); -} - -void QSearchField::setFocus(Qt::FocusReason reason) -{ - Q_ASSERT(pimpl); - if (!pimpl) - return; - - if ([pimpl->nsSearchField acceptsFirstResponder]) - [[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField]; -} - -void QSearchField::setFocus() -{ - setFocus(Qt::OtherFocusReason); -} - void QSearchField::resizeEvent(QResizeEvent *resizeEvent) { QWidget::resizeEvent(resizeEvent); } + +bool QSearchField::eventFilter(QObject *o, QEvent *e) +{ + return QWidget::eventFilter(o, e); +}