From 93ae014d7ee06a6ebb701420fffef5895cd731c4 Mon Sep 17 00:00:00 2001 From: Alexis Menard Date: Wed, 31 Mar 2010 06:28:59 +0200 Subject: Add a QGraphicsItem::updateMicroFocus() to QGraphicsItem. QGraphicsItem is no more lacking a way to notify the input method of a changed micro focus. Reviewed-by:denis Reviewed-by:simon hausmann --- src/gui/graphicsview/qgraphicsitem.cpp | 34 ++++++++++++ src/gui/graphicsview/qgraphicsitem.h | 4 ++ src/gui/kernel/qapplication.h | 1 + tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp | 71 ++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 02a1ecb..71f248d 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -731,6 +731,9 @@ #include #include #include +#ifndef QT_NO_ACCESSIBILITY +# include "qaccessible.h" +#endif #include #include @@ -738,6 +741,7 @@ #include #include #include +#include #ifdef Q_WS_X11 #include @@ -7305,6 +7309,31 @@ void QGraphicsItem::setInputMethodHints(Qt::InputMethodHints hints) } /*! + Updates the item's micro focus. + + \since 4.7 + + \sa QInputContext +*/ +void QGraphicsItem::updateMicroFocus() +{ +#if !defined(QT_NO_IM) && (defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_OS_SYMBIAN)) + if (QWidget *fw = qApp->focusWidget()) { + if (qt_widget_private(fw)->ic || qApp->d_func()->inputContext) { + if (QInputContext *ic = fw->inputContext()) { + if (ic) + ic->update(); + } + } +#ifndef QT_NO_ACCESSIBILITY + // ##### is this correct + QAccessible::updateAccessibility(fw, 0, QAccessible::StateChanged); +#endif + } +#endif +} + +/*! This virtual function is called by QGraphicsItem to notify custom items that some part of the item's state changes. By reimplementing this function, your can react to a change, and in some cases, (depending on \a @@ -7580,6 +7609,11 @@ void QGraphicsObject::ungrabGesture(Qt::GestureType gesture) } } +void QGraphicsObject::updateMicroFocus() +{ + QGraphicsItem::updateMicroFocus(); +} + void QGraphicsItemPrivate::append(QDeclarativeListProperty *list, QGraphicsObject *item) { QGraphicsItemPrivate::get(item)->setParentItemHelper(static_cast(list->object), /*newParentVariant=*/0, /*thisPointerVariant=*/0); diff --git a/src/gui/graphicsview/qgraphicsitem.h b/src/gui/graphicsview/qgraphicsitem.h index 2c45b62..9cc75af 100644 --- a/src/gui/graphicsview/qgraphicsitem.h +++ b/src/gui/graphicsview/qgraphicsitem.h @@ -424,6 +424,7 @@ public: void removeSceneEventFilter(QGraphicsItem *filterItem); protected: + void updateMicroFocus(); virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event); virtual bool sceneEvent(QEvent *event); virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); @@ -571,6 +572,9 @@ public: void grabGesture(Qt::GestureType type, Qt::GestureFlags flags = Qt::GestureFlags()); void ungrabGesture(Qt::GestureType type); +protected Q_SLOTS: + void updateMicroFocus(); + Q_SIGNALS: void parentChanged(); void opacityChanged(); diff --git a/src/gui/kernel/qapplication.h b/src/gui/kernel/qapplication.h index ee74350..c21b982 100644 --- a/src/gui/kernel/qapplication.h +++ b/src/gui/kernel/qapplication.h @@ -376,6 +376,7 @@ private: Q_DECLARE_PRIVATE(QApplication) friend class QGraphicsWidget; + friend class QGraphicsItem; friend class QGraphicsScene; friend class QGraphicsScenePrivate; friend class QWidget; diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp index be50182..3d86c48 100644 --- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #include "../../shared/util.h" @@ -424,6 +425,7 @@ private slots: void modality_keyEvents(); void itemIsInFront(); void scenePosChange(); + void updateMicroFocus(); // task specific tests below me void task141694_textItemEnsureVisible(); @@ -9988,6 +9990,75 @@ void tst_QGraphicsItem::scenePosChange() QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); } +class MyInputContext : public QInputContext +{ +public: + MyInputContext() : nbUpdates(0) {} + ~MyInputContext() {} + + QString identifierName() { return QString(); } + QString language() { return QString(); } + + void reset() {} + + bool isComposing() const { return false; } + + void update() { nbUpdates++; } + + bool nbUpdates; +}; + +class MyInputWidget : public QGraphicsWidget +{ +public: + MyInputWidget() + { + setFlag(QGraphicsItem::ItemIsFocusable, true); + setFlag(QGraphicsItem::ItemAcceptsInputMethod, true); + } + void mousePressEvent(QGraphicsSceneMouseEvent *event) + { + event->accept(); + } + + void doUpdateMicroFocus() + { + updateMicroFocus(); + } +}; + +void tst_QGraphicsItem::updateMicroFocus() +{ + QGraphicsScene scene; + QWidget parent; + QGridLayout layout; + parent.setLayout(&layout); + QGraphicsView view(&scene); + QGraphicsView view2(&scene); + layout.addWidget(&view, 0, 0); + layout.addWidget(&view2, 0, 1); + MyInputContext ic2; + view2.setInputContext(&ic2); + MyInputContext ic; + view.setInputContext(&ic); + MyInputWidget input; + input.setPos(0, 0); + input.resize(150, 150); + scene.addItem(&input); + input.setFocus(); + parent.show(); + view.setFocus(); + qApp->setAutoSipEnabled(true); + QApplication::setActiveWindow(&parent); + QTest::qWaitForWindowShown(&parent); + QTRY_COMPARE(QApplication::activeWindow(), static_cast(&parent)); + input.doUpdateMicroFocus(); + QApplication::processEvents(); + QCOMPARE(ic.nbUpdates, 1); + //No update since view2 does not have the focus. + QCOMPARE(ic2.nbUpdates, 0); +} + void tst_QGraphicsItem::QTBUG_5418_textItemSetDefaultColor() { struct Item : public QGraphicsTextItem -- cgit v0.12