From 7b4c46cf67062a5052e6898459b21b74bfd364b2 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 15 Oct 2015 17:45:41 +0200 Subject: [PATCH] Update to newer Qocoa versions of QSearchField. --- .../thirdparty/Qocoa/qsearchfield.cpp | 10 +- .../thirdparty/Qocoa/qsearchfield.h | 15 +- .../thirdparty/Qocoa/qsearchfield_mac.mm | 185 ++++++++++++------ 3 files changed, 138 insertions(+), 72 deletions(-) diff --git a/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp b/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp index 316fc24a0..13329bbc7 100644 --- a/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp +++ b/src/libtomahawk/thirdparty/Qocoa/qsearchfield.cpp @@ -138,16 +138,14 @@ void QSearchField::setFocus(Qt::FocusReason reason) pimpl.data()->lineEdit->setFocus(reason); } +void QSearchField::setMenu(QMenu *menu) +{ + //FIXME nop +} void QSearchField::resizeEvent(QResizeEvent* e) { QWidget::resizeEvent(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 604a657a9..2c29bbd17 100644 --- a/src/libtomahawk/thirdparty/Qocoa/qsearchfield.h +++ b/src/libtomahawk/thirdparty/Qocoa/qsearchfield.h @@ -3,6 +3,7 @@ #include #include +#include #include "DllMacro.h" @@ -10,12 +11,17 @@ class QSearchFieldPrivate; class DLLEXPORT QSearchField : public QWidget { Q_OBJECT + + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged USER true); + Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText); + public: explicit QSearchField(QWidget *parent); QString text() const; QString placeholderText() const; - void setFocus(Qt::FocusReason reason); + void setFocus(Qt::FocusReason); + void setMenu(QMenu *menu); public slots: void setText(const QString &text); @@ -29,15 +35,16 @@ signals: void editingFinished(); void returnPressed(); +private slots: + void popupMenu(); + protected: + void changeEvent(QEvent*); 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 d6aa69dc2..563961cec 100644 --- a/src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm +++ b/src/libtomahawk/thirdparty/Qocoa/qsearchfield_mac.mm @@ -24,13 +24,13 @@ THE SOFTWARE. #include "moc_qsearchfield.cpp" #include "qocoa_mac.h" -#include "utils/Logger.h" #import "Foundation/NSAutoreleasePool.h" #import "Foundation/NSNotification.h" #import "AppKit/NSSearchField.h" #include +#include #include class QSearchFieldPrivate : public QObject @@ -53,8 +53,27 @@ public: void returnPressed() { - if (qSearchField) + if (qSearchField) { emit qSearchField->returnPressed(); + QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier); + QApplication::postEvent(qSearchField, event); + } + } + + void keyDownPressed() + { + if (qSearchField) { + QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier); + QApplication::postEvent(qSearchField, event); + } + } + + void keyUpPressed() + { + if (qSearchField) { + QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier); + QApplication::postEvent(qSearchField, event); + } } QPointer qSearchField; @@ -78,6 +97,7 @@ public: } -(void)controlTextDidEndEditing:(NSNotification*)notification { + Q_UNUSED(notification); // No Q_ASSERT here as it is called on destruction. if (pimpl) pimpl->textDidEndEditing(); @@ -85,49 +105,60 @@ public: if ([[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement) pimpl->returnPressed(); } + +-(BOOL)control: (NSControl *)control textView: + (NSTextView *)textView doCommandBySelector: + (SEL)commandSelector { + Q_ASSERT(pimpl); + if (!pimpl) return NO; + + if (commandSelector == @selector(moveDown:)) { + pimpl->keyDownPressed(); + return YES; + } else if (commandSelector == @selector(moveUp:)) { + pimpl->keyUpPressed(); + return YES; + } + return NO; +} + @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 @implementation QocoaSearchField -(BOOL)performKeyEquivalent:(NSEvent*)event { - if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask) - { - const unsigned short keyCode = [event keyCode]; -/* if (keyCode == kKeycodeA) // Cmd+a + // First, check if we have the focus. + // If no, it probably means this event isn't for us. + NSResponder* firstResponder = [[NSApp keyWindow] firstResponder]; + if ([firstResponder isKindOfClass:[NSText class]] && + [(NSText*)firstResponder delegate] == self) { + + if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask) { - [self performSelector:@selector(selectText:)]; - return YES; - } - else*/ if (keyCode == kKeycodeC) // Cmd+c - { - QClipboard* clipboard = QApplication::clipboard(); - clipboard->setText(toQString([self stringValue])); - return YES; - } - else if (keyCode == kKeycodeV) // Cmd+v - { - QClipboard* clipboard = QApplication::clipboard(); - [self setStringValue:fromQString(clipboard->text())]; - return YES; - } - else if (keyCode == kKeycodeX) // Cmd+x - { - QClipboard* clipboard = QApplication::clipboard(); - clipboard->setText(toQString([self stringValue])); - [self setStringValue:@""]; - return YES; + QString keyString = toQString([event characters]); + if (keyString == "a") // Cmd+a + { + [self performSelector:@selector(selectText:)]; + return YES; + } + else if (keyString == "c") // Cmd+c + { + [[self currentEditor] copy: nil]; + return YES; + } + else if (keyString == "v") // Cmd+v + { + [[self currentEditor] paste: nil]; + return YES; + } + else if (keyString == "x") // Cmd+x + { + [[self currentEditor] cut: nil]; + return YES; + } } } @@ -155,6 +186,25 @@ QSearchField::QSearchField(QWidget *parent) : QWidget(parent) [pool drain]; } +void QSearchField::setMenu(QMenu *menu) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) + NSMenu *nsMenu = menu->macMenu(); +#else + NSMenu *nsMenu = menu->toNSMenu(); +#endif + + [[pimpl->nsSearchField cell] setSearchMenuTemplate:nsMenu]; +} + +void QSearchField::popupMenu() +{ +} + void QSearchField::setText(const QString &text) { Q_ASSERT(pimpl); @@ -177,27 +227,6 @@ 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); @@ -226,12 +255,44 @@ 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) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + if ([pimpl->nsSearchField acceptsFirstResponder]) + [[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField]; +} + +void QSearchField::setFocus() +{ + setFocus(Qt::OtherFocusReason); +} + +void QSearchField::changeEvent(QEvent* event) +{ + if (event->type() == QEvent::EnabledChange) { + Q_ASSERT(pimpl); + if (!pimpl) + return; + + const bool enabled = isEnabled(); + [pimpl->nsSearchField setEnabled: enabled]; + } + QWidget::changeEvent(event); +} + void QSearchField::resizeEvent(QResizeEvent *resizeEvent) { QWidget::resizeEvent(resizeEvent); } - -bool QSearchField::eventFilter(QObject *o, QEvent *e) -{ - return QWidget::eventFilter(o, e); -}