Check if Qt::ImEnabled is true before handling im events on Mac
authorTasuku Suzuki <tasuku.suzuki@nokia.com>
Mon, 19 Mar 2012 02:04:26 +0000 (11:04 +0900)
committerQt by Nokia <qt-info@nokia.com>
Fri, 23 Mar 2012 08:48:04 +0000 (09:48 +0100)
Key events were not sent to items on graphics view after the change
412dbdf410c765e75c60d1f48143dd6c02a69493. This is because the change
only checks if QGuiApplication::focusObject() exists. Qt::ImEnabled
needs to be checked too.

Change-Id: I2a78af717a7a1a5d84fbc8b521253bdc25b43286
Reviewed-by: Morten Johan Sørvig <morten.sorvig@nokia.com>
src/plugins/platforms/cocoa/qcocoainputcontext.h
src/plugins/platforms/cocoa/qcocoainputcontext.mm
src/plugins/platforms/cocoa/qnsview.mm

index 172c87e..1fda292 100644 (file)
@@ -59,7 +59,8 @@ public:
     virtual void reset();
 
 private Q_SLOTS:
-    void inputItemChanged();
+    void connectSignals();
+    void focusObjectChanged(QObject *focusObject);
 
 private:
     QPointer<QWindow> mWindow;
index db3488a..5ac7f72 100644 (file)
@@ -83,7 +83,7 @@ QCocoaInputContext::QCocoaInputContext()
     : QPlatformInputContext()
     , mWindow(QGuiApplication::focusWindow())
 {
-    connect(qApp->inputMethod(), SIGNAL(inputItemChanged()), this, SLOT(inputItemChanged()));
+    QMetaObject::invokeMethod(this, "connectSignals", Qt::QueuedConnection);
 }
 
 QCocoaInputContext::~QCocoaInputContext()
@@ -114,7 +114,13 @@ void QCocoaInputContext::reset()
     }
 }
 
-void QCocoaInputContext::inputItemChanged()
+void QCocoaInputContext::connectSignals()
+{
+    connect(qApp, SIGNAL(focusObjectChanged(QObject*)), this, SLOT(focusObjectChanged(QObject*)));
+    focusObjectChanged(qApp->focusObject());
+}
+
+void QCocoaInputContext::focusObjectChanged(QObject *focusObject)
 {
     mWindow = QGuiApplication::focusWindow();
 }
index d7f6310..d249158 100644 (file)
@@ -614,10 +614,11 @@ static QTouchDevice *touchDevice = 0;
 
         QObject *fo = QGuiApplication::focusObject();
         if (!m_keyEventsAccepted && fo) {
-            QInputMethodQueryEvent queryEvent(Qt::ImHints);
+            QInputMethodQueryEvent queryEvent(Qt::ImEnabled | Qt::ImHints);
             if (QCoreApplication::sendEvent(fo, &queryEvent)) {
+                bool imEnabled = queryEvent.value(Qt::ImEnabled).toBool();
                 Qt::InputMethodHints hints = static_cast<Qt::InputMethodHints>(queryEvent.value(Qt::ImHints).toUInt());
-                if (!(hints & Qt::ImhDigitsOnly || hints & Qt::ImhFormattedNumbersOnly || hints & Qt::ImhHiddenText))
+                if (imEnabled && !(hints & Qt::ImhDigitsOnly || hints & Qt::ImhFormattedNumbersOnly || hints & Qt::ImhHiddenText))
                     [self interpretKeyEvents:[NSArray arrayWithObject:nsevent]];
             }
         }
@@ -654,10 +655,15 @@ static QTouchDevice *touchDevice = 0;
     }
     QObject *fo = QGuiApplication::focusObject();
     if (fo) {
-        QInputMethodEvent e;
-        e.setCommitString(commitString);
-        QCoreApplication::sendEvent(fo, &e);
-        m_keyEventsAccepted = true;
+        QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
+        if (QCoreApplication::sendEvent(fo, &queryEvent)) {
+            if (queryEvent.value(Qt::ImEnabled).toBool()) {
+                QInputMethodEvent e;
+                e.setCommitString(commitString);
+                QCoreApplication::sendEvent(fo, &e);
+                m_keyEventsAccepted = true;
+            }
+        }
     }
 
     m_composingText.clear();
@@ -715,9 +721,14 @@ static QTouchDevice *touchDevice = 0;
 
     QObject *fo = QGuiApplication::focusObject();
     if (fo) {
-        QInputMethodEvent e(preeditString, attrs);
-        QCoreApplication::sendEvent(fo, &e);
-        m_keyEventsAccepted = true;
+        QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
+        if (QCoreApplication::sendEvent(fo, &queryEvent)) {
+            if (queryEvent.value(Qt::ImEnabled).toBool()) {
+                QInputMethodEvent e(preeditString, attrs);
+                QCoreApplication::sendEvent(fo, &e);
+                m_keyEventsAccepted = true;
+            }
+        }
     }
 }
 
@@ -726,9 +737,14 @@ static QTouchDevice *touchDevice = 0;
     if (!m_composingText.isEmpty()) {
         QObject *fo = QGuiApplication::focusObject();
         if (fo) {
-            QInputMethodEvent e;
-            e.setCommitString(m_composingText);
-            QCoreApplication::sendEvent(fo, &e);
+            QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
+            if (QCoreApplication::sendEvent(fo, &queryEvent)) {
+                if (queryEvent.value(Qt::ImEnabled).toBool()) {
+                    QInputMethodEvent e;
+                    e.setCommitString(m_composingText);
+                    QCoreApplication::sendEvent(fo, &e);
+                }
+            }
         }
     }
     m_composingText.clear();
@@ -749,9 +765,11 @@ static QTouchDevice *touchDevice = 0;
     QObject *fo = QGuiApplication::focusObject();
     if (!fo)
         return nil;
-    QInputMethodQueryEvent queryEvent(Qt::ImCurrentSelection);
+    QInputMethodQueryEvent queryEvent(Qt::ImEnabled | Qt::ImCurrentSelection);
     if (!QCoreApplication::sendEvent(fo, &queryEvent))
         return nil;
+    if (!queryEvent.value(Qt::ImEnabled).toBool())
+        return nil;
 
     QString selectedText = queryEvent.value(Qt::ImCurrentSelection).toString();
     if (selectedText.isEmpty())
@@ -785,9 +803,12 @@ static QTouchDevice *touchDevice = 0;
     QObject *fo = QGuiApplication::focusObject();
     if (!fo)
         return selRange;
-    QInputMethodQueryEvent queryEvent(Qt::ImCurrentSelection);
+    QInputMethodQueryEvent queryEvent(Qt::ImEnabled | Qt::ImCurrentSelection);
     if (!QCoreApplication::sendEvent(fo, &queryEvent))
         return selRange;
+    if (!queryEvent.value(Qt::ImEnabled).toBool())
+        return selRange;
+
     QString selectedText = queryEvent.value(Qt::ImCurrentSelection).toString();
 
     if (!selectedText.isEmpty()) {
@@ -804,6 +825,12 @@ static QTouchDevice *touchDevice = 0;
     if (!fo)
         return NSZeroRect;
 
+    QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
+    if (!QCoreApplication::sendEvent(fo, &queryEvent))
+        return NSZeroRect;
+    if (!queryEvent.value(Qt::ImEnabled).toBool())
+        return NSZeroRect;
+
     if (!m_window)
         return NSZeroRect;
 
@@ -828,10 +855,19 @@ static QTouchDevice *touchDevice = 0;
 
 - (NSArray*) validAttributesForMarkedText
 {
+    if (m_window != QGuiApplication::focusWindow())
+        return nil;
+
     QObject *fo = QGuiApplication::focusObject();
     if (!fo)
         return nil;
 
+    QInputMethodQueryEvent queryEvent(Qt::ImEnabled);
+    if (!QCoreApplication::sendEvent(fo, &queryEvent))
+        return nil;
+    if (!queryEvent.value(Qt::ImEnabled).toBool())
+        return nil;
+
     // Support only underline color/style.
     return [NSArray arrayWithObjects:NSUnderlineColorAttributeName,
                                      NSUnderlineStyleAttributeName, nil];