From 2e0acaff8d152732986f628dd9e198ca4b304f64 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 5 Aug 2011 08:43:24 +0200 Subject: Automatic closing of input panel on uikit when QML item looses focus. I.e. we remember which item had focus when we got the request to open the input panel, and close it when that looses focus, even if we don't get an explicit close request. Also set the "returnKey" to "Return" instead of "Done" if the focus item is a TextEdit. This adds a dependency of the platform plugin to declarative. --- src/plugins/platforms/uikit/platform.pro | 2 +- src/plugins/platforms/uikit/quikiteventloop.mm | 57 ++++++++++++++++++++-- .../platforms/uikit/quikitsoftwareinputhandler.h | 12 +++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/plugins/platforms/uikit/platform.pro b/src/plugins/platforms/uikit/platform.pro index 273c00d..726da06 100644 --- a/src/plugins/platforms/uikit/platform.pro +++ b/src/plugins/platforms/uikit/platform.pro @@ -2,7 +2,7 @@ TARGET = quikit include(../../qpluginbase.pri) QTDIR_build:DESTDIR = $$QT_BUILD_TREE/plugins/platforms -QT += opengl +QT += opengl declarative OBJECTIVE_SOURCES = main.mm \ quikitintegration.mm \ diff --git a/src/plugins/platforms/uikit/quikiteventloop.mm b/src/plugins/platforms/uikit/quikiteventloop.mm index 01ecf3f..7df7ec8 100644 --- a/src/plugins/platforms/uikit/quikiteventloop.mm +++ b/src/plugins/platforms/uikit/quikiteventloop.mm @@ -49,6 +49,8 @@ #include #include +#include +#include #include @interface QUIKitAppDelegate : NSObject { @@ -212,24 +214,69 @@ void QUIKitEventLoop::qtNeedsToProcessEvents() [mHelper performSelectorOnMainThread:@selector(processEvents) withObject:nil waitUntilDone:NO]; } +static UIReturnKeyType keyTypeForObject(QObject *obj) +{ + if (strcmp(obj->metaObject()->className(), "QDeclarativeTextEdit") == 0) + return UIReturnKeyDefault; + return UIReturnKeyDone; +} + bool QUIKitSoftwareInputHandler::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::RequestSoftwareInputPanel) { + UIReturnKeyType returnKeyType = UIReturnKeyDone; + if (QDeclarativeView *declarativeView = qobject_cast(obj)) { + // register on loosing the focus, so we can auto-remove the input panel again + QGraphicsScene *scene = declarativeView->scene(); + if (scene) { + if (mCurrentFocusObject) + disconnect(mCurrentFocusObject, 0, this, SLOT(activeFocusChanged(bool))); + QDeclarativeItem *focus = static_cast(scene->focusItem()); + mCurrentFocusObject = focus; + if (focus) { + connect(mCurrentFocusObject, SIGNAL(activeFocusChanged(bool)), this, SLOT(activeFocusChanged(bool))); + returnKeyType = keyTypeForObject(mCurrentFocusObject); + } + } + } QWidget *widget = qobject_cast(obj); if (widget) { + mCurrentFocusWidget = widget; QUIKitWindow *platformWindow = static_cast(widget->window()->platformWindow()); - if (platformWindow) [platformWindow->nativeView() becomeFirstResponder]; + if (platformWindow) { + platformWindow->nativeView().returnKeyType = returnKeyType; + [platformWindow->nativeView() becomeFirstResponder]; + } return true; } } else if (event->type() == QEvent::CloseSoftwareInputPanel) { QWidget *widget = qobject_cast(obj); - if (widget) { - QUIKitWindow *platformWindow = static_cast(widget->window()->platformWindow()); - if (platformWindow) [platformWindow->nativeView() resignFirstResponder]; - return true; + return closeSoftwareInputPanel(widget); + } + return false; +} + +bool QUIKitSoftwareInputHandler::closeSoftwareInputPanel(QWidget *widget) +{ + if (widget) { + QUIKitWindow *platformWindow = static_cast(widget->window()->platformWindow()); + if (platformWindow) { + [platformWindow->nativeView() resignFirstResponder]; + mCurrentFocusWidget = 0; + if (mCurrentFocusObject) + disconnect(mCurrentFocusObject, 0, this, SLOT(activeFocusChanged(bool))); + mCurrentFocusObject = 0; } + return true; } return false; } +void QUIKitSoftwareInputHandler::activeFocusChanged(bool focus) +{ + if (!focus && mCurrentFocusWidget && mCurrentFocusObject) { + closeSoftwareInputPanel(mCurrentFocusWidget); + } +} + QT_END_NAMESPACE diff --git a/src/plugins/platforms/uikit/quikitsoftwareinputhandler.h b/src/plugins/platforms/uikit/quikitsoftwareinputhandler.h index 7e4f8e9..3f64bf5 100644 --- a/src/plugins/platforms/uikit/quikitsoftwareinputhandler.h +++ b/src/plugins/platforms/uikit/quikitsoftwareinputhandler.h @@ -45,6 +45,8 @@ #define QUIKITSOFTWAREINPUTHANDLER_H #include +#include +#include QT_BEGIN_NAMESPACE @@ -53,7 +55,17 @@ class QUIKitSoftwareInputHandler : public QObject Q_OBJECT public: + QUIKitSoftwareInputHandler() : mCurrentFocusWidget(0), mCurrentFocusObject(0) {} bool eventFilter(QObject *obj, QEvent *event); + +private slots: + void activeFocusChanged(bool focus); + +private: + bool closeSoftwareInputPanel(QWidget *widget); + + QPointer mCurrentFocusWidget; + QPointer mCurrentFocusObject; }; QT_END_NAMESPACE -- cgit v0.12