src/gui/kernel/qsoftkeymanager.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 #ifdef Q_WS_S60
       
    45 #include "qstyle.h"
       
    46 #include "private/qt_s60_p.h"
       
    47 #endif
       
    48 #include "private/qsoftkeymanager_p.h"
       
    49 #include "private/qobject_p.h"
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 
       
    53 #ifdef Q_WS_S60
       
    54 static const int s60CommandStart = 6000;
       
    55 #endif
       
    56 
       
    57 class QSoftKeyManagerPrivate : public QObjectPrivate
       
    58 {
       
    59     Q_DECLARE_PUBLIC(QSoftKeyManager)
       
    60 
       
    61 public:
       
    62     static void updateSoftKeys_sys(const QList<QAction*> &softKeys);
       
    63 
       
    64 private:
       
    65     QHash<QAction*, Qt::Key> keyedActions;
       
    66     static QSoftKeyManager *self;
       
    67     static QWidget *softKeySource;
       
    68 };
       
    69 
       
    70 QWidget *QSoftKeyManagerPrivate::softKeySource = 0;
       
    71 QSoftKeyManager *QSoftKeyManagerPrivate::self = 0;
       
    72 
       
    73 const char *QSoftKeyManager::standardSoftKeyText(StandardSoftKey standardKey)
       
    74 {
       
    75     const char *softKeyText = 0;
       
    76     switch (standardKey) {
       
    77     case OkSoftKey:
       
    78         softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Ok");
       
    79         break;
       
    80     case SelectSoftKey:
       
    81         softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Select");
       
    82         break;
       
    83     case DoneSoftKey:
       
    84         softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Done");
       
    85         break;
       
    86     case MenuSoftKey:
       
    87         softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Options");
       
    88         break;
       
    89     case CancelSoftKey:
       
    90         softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Cancel");
       
    91         break;
       
    92     default:
       
    93         break;
       
    94     };
       
    95 
       
    96     return softKeyText;
       
    97 }
       
    98 
       
    99 QSoftKeyManager *QSoftKeyManager::instance()
       
   100 {
       
   101     if (!QSoftKeyManagerPrivate::self)
       
   102         QSoftKeyManagerPrivate::self = new QSoftKeyManager;
       
   103 
       
   104     return QSoftKeyManagerPrivate::self;
       
   105 }
       
   106 
       
   107 QSoftKeyManager::QSoftKeyManager() : QObject(*(new QSoftKeyManagerPrivate), 0)
       
   108 {
       
   109 }
       
   110 
       
   111 QAction *QSoftKeyManager::createAction(StandardSoftKey standardKey, QWidget *actionWidget)
       
   112 {
       
   113     const char* text = standardSoftKeyText(standardKey);
       
   114     QAction *action = new QAction(QSoftKeyManager::tr(text), actionWidget);
       
   115     QAction::SoftKeyRole softKeyRole = QAction::NoSoftKey;
       
   116     switch (standardKey) {
       
   117     case OkSoftKey:
       
   118     case SelectSoftKey:
       
   119     case DoneSoftKey:
       
   120     case MenuSoftKey:
       
   121         softKeyRole = QAction::PositiveSoftKey;
       
   122         break;
       
   123     case CancelSoftKey:
       
   124         softKeyRole = QAction::NegativeSoftKey;
       
   125         break;
       
   126     }
       
   127     action->setSoftKeyRole(softKeyRole);
       
   128     return action;
       
   129 }
       
   130 
       
   131 /*! \internal
       
   132 
       
   133   Creates a QAction and registers the 'triggered' signal to send the given key event to
       
   134   \a actionWidget as a convenience.
       
   135 
       
   136 */
       
   137 QAction *QSoftKeyManager::createKeyedAction(StandardSoftKey standardKey, Qt::Key key, QWidget *actionWidget)
       
   138 {
       
   139     QScopedPointer<QAction> action(createAction(standardKey, actionWidget));
       
   140 
       
   141     connect(action.data(), SIGNAL(triggered()), QSoftKeyManager::instance(), SLOT(sendKeyEvent()));
       
   142 
       
   143     QSoftKeyManager::instance()->d_func()->keyedActions.insert(action.data(), key);
       
   144     return action.take();
       
   145 }
       
   146 
       
   147 void QSoftKeyManager::sendKeyEvent()
       
   148 {
       
   149     Q_D(QSoftKeyManager);
       
   150     QAction *action = qobject_cast<QAction*>(sender());
       
   151 
       
   152     if (!action)
       
   153         return;
       
   154 
       
   155     Qt::Key keyToSend = d->keyedActions.value(action, Qt::Key_unknown);
       
   156 
       
   157     if (keyToSend != Qt::Key_unknown)
       
   158         QApplication::postEvent(action->parentWidget(),
       
   159                                 new QKeyEvent(QEvent::KeyPress, keyToSend, Qt::NoModifier));
       
   160 }
       
   161 
       
   162 void QSoftKeyManager::updateSoftKeys()
       
   163 {
       
   164     QEvent *event = new QEvent(QEvent::UpdateSoftKeys);
       
   165     QApplication::postEvent(QSoftKeyManager::instance(), event);
       
   166 }
       
   167 
       
   168 bool QSoftKeyManager::event(QEvent *e)
       
   169 {
       
   170     if (e->type() == QEvent::UpdateSoftKeys) {
       
   171         QList<QAction*> softKeys;
       
   172         QWidget *source = QApplication::focusWidget();
       
   173         do {
       
   174             if (source) {
       
   175                 QList<QAction*> actions = source->actions();
       
   176                 for (int i = 0; i < actions.count(); ++i) {
       
   177                     if (actions.at(i)->softKeyRole() != QAction::NoSoftKey)
       
   178                         softKeys.append(actions.at(i));
       
   179                 }
       
   180 
       
   181                 QWidget *parent = source->parentWidget();
       
   182                 if (parent && softKeys.isEmpty())
       
   183                     source = parent;
       
   184                 else
       
   185                     break;
       
   186             } else {
       
   187                 source = QApplication::activeWindow();
       
   188             }
       
   189         } while (source);
       
   190 
       
   191         QSoftKeyManagerPrivate::softKeySource = source;
       
   192         QSoftKeyManagerPrivate::updateSoftKeys_sys(softKeys);
       
   193         return true;
       
   194     }
       
   195     return false;
       
   196 }
       
   197 
       
   198 #ifdef Q_WS_S60
       
   199 void QSoftKeyManagerPrivate::updateSoftKeys_sys(const QList<QAction*> &softkeys)
       
   200 {
       
   201     CEikButtonGroupContainer* nativeContainer = S60->buttonGroupContainer();
       
   202     nativeContainer->DrawableWindow()->SetPointerCapturePriority(1); //keep softkeys available in modal dialog
       
   203     QT_TRAP_THROWING(nativeContainer->SetCommandSetL(R_AVKON_SOFTKEYS_EMPTY_WITH_IDS));
       
   204 
       
   205     int position = -1;
       
   206     int command;
       
   207     bool needsExitButton = true;
       
   208 
       
   209     for (int index = 0; index < softkeys.count(); index++) {
       
   210         const QAction* softKeyAction = softkeys.at(index);
       
   211         switch (softKeyAction->softKeyRole()) {
       
   212         // Positive Actions on the LSK
       
   213         case QAction::PositiveSoftKey:
       
   214             position = 0;
       
   215             break;
       
   216         case QAction::SelectSoftKey:
       
   217             position = 0;
       
   218             break;
       
   219         // Negative Actions on the RSK
       
   220         case QAction::NegativeSoftKey:
       
   221             needsExitButton = false;
       
   222             position = 2;
       
   223             break;
       
   224         default:
       
   225             break;
       
   226         }
       
   227 
       
   228         command = (softKeyAction->objectName().contains("_q_menuSoftKeyAction"))
       
   229                     ? EAknSoftkeyOptions
       
   230                     : s60CommandStart + index;
       
   231 
       
   232         if (position != -1) {
       
   233             const int underlineShortCut = QApplication::style()->styleHint(QStyle::SH_UnderlineShortcut);
       
   234             QString iconText = softKeyAction->iconText();
       
   235             TPtrC text = qt_QString2TPtrC( underlineShortCut ? softKeyAction->text() : iconText);
       
   236             QT_TRAP_THROWING(nativeContainer->SetCommandL(position, command, text));
       
   237         }
       
   238     }
       
   239 
       
   240     if (needsExitButton)
       
   241         QT_TRAP_THROWING(nativeContainer->SetCommandL(2, EAknSoftkeyExit, qt_QString2TPtrC(QSoftKeyManager::tr("Exit"))));
       
   242 
       
   243     nativeContainer->DrawDeferred(); // 3.1 needs an extra invitation
       
   244 }
       
   245 
       
   246 bool QSoftKeyManager::handleCommand(int command)
       
   247 {
       
   248     if (command >= s60CommandStart && QSoftKeyManagerPrivate::softKeySource) {
       
   249         int index = command - s60CommandStart;
       
   250         const QList<QAction*>& softKeys = QSoftKeyManagerPrivate::softKeySource->actions();
       
   251         for (int i = 0, j = 0; i < softKeys.count(); ++i) {
       
   252             QAction *action = softKeys.at(i);
       
   253             if (action->softKeyRole() != QAction::NoSoftKey) {
       
   254                 if (j == index) {
       
   255                     QWidget *parent = action->parentWidget();
       
   256                     if (parent && parent->isEnabled()) {
       
   257                         action->activate(QAction::Trigger);
       
   258                         return true;
       
   259                     }
       
   260                 }
       
   261                 j++;
       
   262             }
       
   263         }
       
   264     }
       
   265 
       
   266     return false;
       
   267 }
       
   268 
       
   269 #else
       
   270 
       
   271 void QSoftKeyManagerPrivate::updateSoftKeys_sys(const QList<QAction*> &)
       
   272 {
       
   273 }
       
   274 
       
   275 #endif
       
   276 
       
   277 QT_END_NAMESPACE
       
   278