util/src/gui/kernel/qsoftkeymanager.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qapplication.h"
       
    43 #include "qevent.h"
       
    44 #include "qbitmap.h"
       
    45 #include "private/qsoftkeymanager_p.h"
       
    46 #include "private/qobject_p.h"
       
    47 #include "private/qsoftkeymanager_common_p.h"
       
    48 
       
    49 #ifdef Q_WS_S60
       
    50 #include "private/qsoftkeymanager_s60_p.h"
       
    51 #endif
       
    52 
       
    53 #ifndef QT_NO_SOFTKEYMANAGER
       
    54 QT_BEGIN_NAMESPACE
       
    55 
       
    56 QSoftKeyManager *QSoftKeyManagerPrivate::self = 0;
       
    57 
       
    58 QString QSoftKeyManager::standardSoftKeyText(StandardSoftKey standardKey)
       
    59 {
       
    60     QString softKeyText;
       
    61     switch (standardKey) {
       
    62     case OkSoftKey:
       
    63         softKeyText = QSoftKeyManager::tr("Ok");
       
    64         break;
       
    65     case SelectSoftKey:
       
    66         softKeyText = QSoftKeyManager::tr("Select");
       
    67         break;
       
    68     case DoneSoftKey:
       
    69         softKeyText = QSoftKeyManager::tr("Done");
       
    70         break;
       
    71     case MenuSoftKey:
       
    72         softKeyText = QSoftKeyManager::tr("Options");
       
    73         break;
       
    74     case CancelSoftKey:
       
    75         softKeyText = QSoftKeyManager::tr("Cancel");
       
    76         break;
       
    77     default:
       
    78         break;
       
    79     };
       
    80 
       
    81     return softKeyText;
       
    82 }
       
    83 
       
    84 QSoftKeyManager *QSoftKeyManager::instance()
       
    85 {
       
    86     if (!QSoftKeyManagerPrivate::self)
       
    87         QSoftKeyManagerPrivate::self = new QSoftKeyManager;
       
    88 
       
    89     return QSoftKeyManagerPrivate::self;
       
    90 }
       
    91 
       
    92 QSoftKeyManager::QSoftKeyManager() :
       
    93 #ifdef Q_WS_S60
       
    94     QObject(*(new QSoftKeyManagerPrivateS60), 0)
       
    95 #else
       
    96     QObject(*(new QSoftKeyManagerPrivate), 0)
       
    97 #endif
       
    98 {
       
    99 }
       
   100 
       
   101 QAction *QSoftKeyManager::createAction(StandardSoftKey standardKey, QWidget *actionWidget)
       
   102 {
       
   103     QAction *action = new QAction(standardSoftKeyText(standardKey), actionWidget);
       
   104     QAction::SoftKeyRole softKeyRole = QAction::NoSoftKey;
       
   105     switch (standardKey) {
       
   106     case MenuSoftKey: // FALL-THROUGH
       
   107         action->setProperty(MENU_ACTION_PROPERTY, QVariant(true)); // TODO: can be refactored away to use _q_action_menubar
       
   108     case OkSoftKey:
       
   109     case SelectSoftKey:
       
   110     case DoneSoftKey:
       
   111         softKeyRole = QAction::PositiveSoftKey;
       
   112         break;
       
   113     case CancelSoftKey:
       
   114         softKeyRole = QAction::NegativeSoftKey;
       
   115         break;
       
   116     }
       
   117     action->setSoftKeyRole(softKeyRole);
       
   118     action->setVisible(false);
       
   119     setForceEnabledInSoftkeys(action);
       
   120     return action;
       
   121 }
       
   122 
       
   123 /*! \internal
       
   124 
       
   125   Creates a QAction and registers the 'triggered' signal to send the given key event to
       
   126   \a actionWidget as a convenience.
       
   127 
       
   128 */
       
   129 QAction *QSoftKeyManager::createKeyedAction(StandardSoftKey standardKey, Qt::Key key, QWidget *actionWidget)
       
   130 {
       
   131 #ifndef QT_NO_ACTION
       
   132     QScopedPointer<QAction> action(createAction(standardKey, actionWidget));
       
   133 
       
   134     connect(action.data(), SIGNAL(triggered()), QSoftKeyManager::instance(), SLOT(sendKeyEvent()));
       
   135     connect(action.data(), SIGNAL(destroyed(QObject*)), QSoftKeyManager::instance(), SLOT(cleanupHash(QObject*)));
       
   136     QSoftKeyManager::instance()->d_func()->keyedActions.insert(action.data(), key);
       
   137     return action.take();
       
   138 #endif //QT_NO_ACTION
       
   139 }
       
   140 
       
   141 void QSoftKeyManager::cleanupHash(QObject *obj)
       
   142 {
       
   143     Q_D(QSoftKeyManager);
       
   144     QAction *action = qobject_cast<QAction*>(obj);
       
   145     d->keyedActions.remove(action);
       
   146 }
       
   147 
       
   148 void QSoftKeyManager::sendKeyEvent()
       
   149 {
       
   150     Q_D(QSoftKeyManager);
       
   151     QAction *action = qobject_cast<QAction*>(sender());
       
   152 
       
   153     if (!action)
       
   154         return;
       
   155 
       
   156     Qt::Key keyToSend = d->keyedActions.value(action, Qt::Key_unknown);
       
   157 
       
   158     if (keyToSend != Qt::Key_unknown)
       
   159         QApplication::postEvent(action->parentWidget(),
       
   160                                 new QKeyEvent(QEvent::KeyPress, keyToSend, Qt::NoModifier));
       
   161 }
       
   162 
       
   163 void QSoftKeyManager::updateSoftKeys()
       
   164 {
       
   165     QEvent *event = new QEvent(QEvent::UpdateSoftKeys);
       
   166     QApplication::postEvent(QSoftKeyManager::instance(), event);
       
   167 }
       
   168 
       
   169 bool QSoftKeyManager::appendSoftkeys(const QWidget &source, int level)
       
   170 {
       
   171     Q_D(QSoftKeyManager);
       
   172     bool ret = false;
       
   173     foreach(QAction *action, source.actions()) {
       
   174         if (action->softKeyRole() != QAction::NoSoftKey
       
   175             && (action->isVisible() || isForceEnabledInSofkeys(action))) {
       
   176             d->requestedSoftKeyActions.insert(level, action);
       
   177             ret = true;
       
   178         }
       
   179     }
       
   180     return ret;
       
   181 }
       
   182 
       
   183 
       
   184 static bool isChildOf(const QWidget *c, const QWidget *p)
       
   185 {
       
   186     while (c) {
       
   187         if (c == p)
       
   188             return true;
       
   189         c = c->parentWidget();
       
   190     }
       
   191     return false;
       
   192 }
       
   193 
       
   194 QWidget *QSoftKeyManager::softkeySource(QWidget *previousSource, bool& recursiveMerging)
       
   195 {
       
   196     Q_D(QSoftKeyManager);
       
   197     QWidget *source = NULL;
       
   198     if (!previousSource) {
       
   199         // Initial source is primarily focuswidget and secondarily activeWindow
       
   200         QWidget *focus = QApplication::focusWidget();
       
   201         QWidget *popup = QApplication::activePopupWidget();
       
   202         if (popup) {
       
   203             if (isChildOf(focus, popup))
       
   204                 source = focus;
       
   205             else
       
   206                 source = popup;
       
   207         }
       
   208         if (!source) {
       
   209             QWidget *modal = QApplication::activeModalWidget();
       
   210             if (modal) {
       
   211                 if (isChildOf(focus, modal))
       
   212                     source = focus;
       
   213                 else
       
   214                     source = modal;
       
   215             }
       
   216         }
       
   217         if (!source) {
       
   218             source = focus;
       
   219             if (!source)
       
   220                 source = QApplication::activeWindow();
       
   221         }
       
   222     } else {
       
   223         // Softkey merging is based on four criterias
       
   224         // 1. Implicit merging is used whenever focus widget does not specify any softkeys
       
   225         bool implicitMerging = d->requestedSoftKeyActions.isEmpty();
       
   226         // 2. Explicit merging with parent is used whenever WA_MergeSoftkeys widget attribute is set
       
   227         bool explicitMerging = previousSource->testAttribute(Qt::WA_MergeSoftkeys);
       
   228         // 3. Explicit merging with all parents
       
   229         recursiveMerging |= previousSource->testAttribute(Qt::WA_MergeSoftkeysRecursively);
       
   230         // 4. Implicit and explicit merging always stops at window boundary
       
   231         bool merging = (implicitMerging || explicitMerging || recursiveMerging) && !previousSource->isWindow();
       
   232 
       
   233         source = merging ? previousSource->parentWidget() : NULL;
       
   234     }
       
   235     return source;
       
   236 }
       
   237 
       
   238 bool QSoftKeyManager::handleUpdateSoftKeys()
       
   239 {
       
   240     Q_D(QSoftKeyManager);
       
   241     int level = 0;
       
   242     d->requestedSoftKeyActions.clear();
       
   243     bool recursiveMerging = false;
       
   244     QWidget *source = softkeySource(NULL, recursiveMerging);
       
   245     while (source) {
       
   246         if (appendSoftkeys(*source, level))
       
   247             ++level;
       
   248         source = softkeySource(source, recursiveMerging);
       
   249     }
       
   250 
       
   251     d->updateSoftKeys_sys();
       
   252     return true;
       
   253 }
       
   254 
       
   255 void QSoftKeyManager::setForceEnabledInSoftkeys(QAction *action)
       
   256 {
       
   257     action->setProperty(FORCE_ENABLED_PROPERTY, QVariant(true));
       
   258 }
       
   259 
       
   260 bool QSoftKeyManager::isForceEnabledInSofkeys(QAction *action)
       
   261 {
       
   262     bool ret = false;
       
   263     QVariant property = action->property(FORCE_ENABLED_PROPERTY);
       
   264     if (property.isValid() && property.toBool())
       
   265         ret = true;
       
   266     return ret;
       
   267 }
       
   268 
       
   269 bool QSoftKeyManager::event(QEvent *e)
       
   270 {
       
   271 #ifndef QT_NO_ACTION
       
   272     if (e->type() == QEvent::UpdateSoftKeys)
       
   273         return handleUpdateSoftKeys();
       
   274 #endif //QT_NO_ACTION
       
   275     return false;
       
   276 }
       
   277 
       
   278 #ifdef Q_WS_S60
       
   279 bool QSoftKeyManager::handleCommand(int command)
       
   280 {
       
   281     return static_cast<QSoftKeyManagerPrivateS60*>(QSoftKeyManager::instance()->d_func())->handleCommand(command);
       
   282 }
       
   283 #endif
       
   284 
       
   285 QT_END_NAMESPACE
       
   286 #endif //QT_NO_SOFTKEYMANAGER