src/gui/kernel/qsoftkeymanager_s60.cpp
branchRCL_3
changeset 5 d3bac044e0f0
child 7 3f74d0d4af4c
equal deleted inserted replaced
4:3b1da2848fc7 5:d3bac044e0f0
       
     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 "qstyle.h"
       
    46 #include "qmenubar.h"
       
    47 #include "private/qt_s60_p.h"
       
    48 #include "private/qmenu_p.h"
       
    49 #include "private/qsoftkeymanager_p.h"
       
    50 #include "private/qsoftkeymanager_s60_p.h"
       
    51 #include "private/qobject_p.h"
       
    52 //#include <eiksoftkeyimage.h>
       
    53 #include <eikcmbut.h>
       
    54 
       
    55 #ifndef QT_NO_SOFTKEYMANAGER
       
    56 QT_BEGIN_NAMESPACE
       
    57 
       
    58 const int S60_COMMAND_START = 6000;
       
    59 const int LSK_POSITION = 0;
       
    60 const int MSK_POSITION = 3;
       
    61 const int RSK_POSITION = 2;
       
    62 
       
    63 QSoftKeyManagerPrivateS60::QSoftKeyManagerPrivateS60()
       
    64 {
       
    65     cachedCbaIconSize[0] = QSize(0,0);
       
    66     cachedCbaIconSize[1] = QSize(0,0);
       
    67     skipNextUpdate = false;
       
    68 }
       
    69 
       
    70 bool QSoftKeyManagerPrivateS60::skipCbaUpdate()
       
    71 {
       
    72     // lets not update softkeys if
       
    73     // 1. We don't have application panes, i.e. cba
       
    74     // 2. S60 native dialog or menu is shown
       
    75     if (QApplication::testAttribute(Qt::AA_S60DontConstructApplicationPanes) ||
       
    76         CCoeEnv::Static()->AppUi()->IsDisplayingMenuOrDialog() || skipNextUpdate) {
       
    77         skipNextUpdate = false;
       
    78         return true;
       
    79     }
       
    80     return false;
       
    81 }
       
    82 
       
    83 void QSoftKeyManagerPrivateS60::ensureCbaVisibilityAndResponsiviness(CEikButtonGroupContainer &cba)
       
    84 {
       
    85     RDrawableWindow *cbaWindow = cba.DrawableWindow();
       
    86     Q_ASSERT_X(cbaWindow, Q_FUNC_INFO, "Native CBA does not have window!");
       
    87     // Make sure CBA is visible, i.e. CBA window is on top
       
    88     cbaWindow->SetOrdinalPosition(0);
       
    89     // Qt shares same CBA instance between top-level widgets,
       
    90     // make sure we are not faded by underlying window.
       
    91     cbaWindow->SetFaded(EFalse, RWindowTreeNode::EFadeIncludeChildren);
       
    92     // Modal dialogs capture pointer events, but shared cba instance
       
    93     // shall stay responsive. Raise pointer capture priority to keep
       
    94     // softkeys responsive in modal dialogs
       
    95     cbaWindow->SetPointerCapturePriority(1);
       
    96 }
       
    97 
       
    98 void QSoftKeyManagerPrivateS60::clearSoftkeys(CEikButtonGroupContainer &cba)
       
    99 {
       
   100     QT_TRAP_THROWING(
       
   101         //Using -1 instead of EAknSoftkeyEmpty to avoid flickering.
       
   102         cba.SetCommandL(0, -1, KNullDesC);
       
   103         // TODO: Should we clear also middle SK?
       
   104         cba.SetCommandL(2, -1, KNullDesC);
       
   105     );
       
   106     realSoftKeyActions.clear();
       
   107 }
       
   108 
       
   109 QString QSoftKeyManagerPrivateS60::softkeyText(QAction &softkeyAction)
       
   110 {
       
   111     // In S60 softkeys and menu items do not support key accelerators (i.e.
       
   112     // CTRL+X). Therefore, removing the accelerator characters from both softkey
       
   113     // and menu item texts.
       
   114     const int underlineShortCut = QApplication::style()->styleHint(QStyle::SH_UnderlineShortcut);
       
   115     QString iconText = softkeyAction.iconText();
       
   116     return underlineShortCut ? softkeyAction.text() : iconText;
       
   117 }
       
   118 
       
   119 QAction *QSoftKeyManagerPrivateS60::highestPrioritySoftkey(QAction::SoftKeyRole role)
       
   120 {
       
   121     QAction *ret = NULL;
       
   122     // Priority look up is two level
       
   123     // 1. First widget with softkeys always has highest priority
       
   124     for (int level = 0; !ret; level++) {
       
   125         // 2. Highest priority action within widget
       
   126         QList<QAction*> actions = requestedSoftKeyActions.values(level);
       
   127         if (actions.isEmpty())
       
   128             break;
       
   129         qSort(actions.begin(), actions.end(), QSoftKeyManagerPrivateS60::actionPriorityMoreThan);
       
   130         foreach (QAction *action, actions) {
       
   131             if (action->softKeyRole() == role) {
       
   132                 ret = action;
       
   133                 break;
       
   134             }
       
   135         }
       
   136     }
       
   137     return ret;
       
   138 }
       
   139 
       
   140 bool QSoftKeyManagerPrivateS60::actionPriorityMoreThan(const QAction *firstItem, const QAction *secondItem)
       
   141 {
       
   142     return firstItem->priority() > secondItem->priority();
       
   143 }
       
   144 
       
   145 void QSoftKeyManagerPrivateS60::setNativeSoftkey(CEikButtonGroupContainer &cba,
       
   146                                               TInt position, TInt command, const TDesC &text)
       
   147 {
       
   148     // Calling SetCommandL causes CBA redraw
       
   149     QT_TRAP_THROWING(cba.SetCommandL(position, command, text));
       
   150 }
       
   151 
       
   152 bool QSoftKeyManagerPrivateS60::isOrientationLandscape()
       
   153 {
       
   154     // Hard to believe that there is no public API in S60 to
       
   155     // get current orientation. This workaround works with currently supported resolutions
       
   156     return  S60->screenHeightInPixels <  S60->screenWidthInPixels;
       
   157 }
       
   158 
       
   159 QSize QSoftKeyManagerPrivateS60::cbaIconSize(CEikButtonGroupContainer *cba, int position)
       
   160 {
       
   161     Q_UNUSED(cba);
       
   162     Q_UNUSED(position);
       
   163 
       
   164     // Will be implemented when EikSoftkeyImage usage license wise is OK
       
   165 /*
       
   166     const int index = isOrientationLandscape() ? 0 : 1;
       
   167     if(cachedCbaIconSize[index].isNull()) {
       
   168         // Only way I figured out to get CBA icon size without RnD SDK, was
       
   169         // Only way I figured out to get CBA icon size without RnD SDK, was
       
   170         // to set some dummy icon to CBA first and then ask CBA button CCoeControl::Size()
       
   171         // The returned value is cached to avoid unnecessary icon setting every time.
       
   172         const bool left = (position == LSK_POSITION);
       
   173         if(position == LSK_POSITION || position == RSK_POSITION) {
       
   174             CEikImage* tmpImage = NULL;
       
   175             QT_TRAP_THROWING(tmpImage = new (ELeave) CEikImage);
       
   176             EikSoftkeyImage::SetImage(cba, *tmpImage, left); // Takes myimage ownership
       
   177             int command = S60_COMMAND_START + position;
       
   178             setNativeSoftkey(*cba, position, command, KNullDesC());
       
   179             cachedCbaIconSize[index] = qt_TSize2QSize(cba->ControlOrNull(command)->Size());
       
   180             EikSoftkeyImage::SetLabel(cba, left);
       
   181         }
       
   182     }
       
   183 
       
   184     return cachedCbaIconSize[index];
       
   185 */
       
   186     return QSize();
       
   187 }
       
   188 
       
   189 bool QSoftKeyManagerPrivateS60::setSoftkeyImage(CEikButtonGroupContainer *cba,
       
   190                                             QAction &action, int position)
       
   191 {
       
   192     bool ret = false;
       
   193     Q_UNUSED(cba);
       
   194     Q_UNUSED(action);
       
   195     Q_UNUSED(position);
       
   196 
       
   197     // Will be implemented when EikSoftkeyImage usage license wise is OK
       
   198     /*
       
   199     const bool left = (position == LSK_POSITION);
       
   200     if(position == LSK_POSITION || position == RSK_POSITION) {
       
   201         QIcon icon = action.icon();
       
   202         if (!icon.isNull()) {
       
   203             QPixmap pm = icon.pixmap(cbaIconSize(cba, position));
       
   204             pm = pm.scaled(cbaIconSize(cba, position));
       
   205             QBitmap mask = pm.mask();
       
   206             if (mask.isNull()) {
       
   207                 mask = QBitmap(pm.size());
       
   208                 mask.fill(Qt::color1);
       
   209             }
       
   210 
       
   211             CFbsBitmap* nBitmap = pm.toSymbianCFbsBitmap();
       
   212             CFbsBitmap* nMask = mask.toSymbianCFbsBitmap();
       
   213 
       
   214             CEikImage* myimage = new (ELeave) CEikImage;
       
   215             myimage->SetPicture( nBitmap, nMask ); // nBitmap and nMask ownership transfered
       
   216 
       
   217             EikSoftkeyImage::SetImage(cba, *myimage, left); // Takes myimage ownership
       
   218             ret = true;
       
   219         } else {
       
   220             // Restore softkey to text based
       
   221             EikSoftkeyImage::SetLabel(cba, left);
       
   222         }
       
   223     }
       
   224     */
       
   225     return ret;
       
   226 }
       
   227 
       
   228 bool QSoftKeyManagerPrivateS60::setSoftkey(CEikButtonGroupContainer &cba,
       
   229                                         QAction::SoftKeyRole role, int position)
       
   230 {
       
   231     QAction *action = highestPrioritySoftkey(role);
       
   232     if (action) {
       
   233         setSoftkeyImage(&cba, *action, position);
       
   234         QString text = softkeyText(*action);
       
   235         TPtrC nativeText = qt_QString2TPtrC(text);
       
   236         int command = S60_COMMAND_START + position;
       
   237         setNativeSoftkey(cba, position, command, nativeText);
       
   238         cba.DimCommand(command, !action->isEnabled());
       
   239         realSoftKeyActions.insert(command, action);
       
   240         return true;
       
   241     }
       
   242     return false;
       
   243 }
       
   244 
       
   245 bool QSoftKeyManagerPrivateS60::setLeftSoftkey(CEikButtonGroupContainer &cba)
       
   246 {
       
   247     return setSoftkey(cba, QAction::PositiveSoftKey, LSK_POSITION);
       
   248 }
       
   249 
       
   250 bool QSoftKeyManagerPrivateS60::setMiddleSoftkey(CEikButtonGroupContainer &cba)
       
   251 {
       
   252     // Note: In order to get MSK working, application has to have EAknEnableMSK flag set
       
   253     // Currently it is not possible very easily)
       
   254     // For more information see: http://wiki.forum.nokia.com/index.php/Middle_softkey_usage
       
   255     return setSoftkey(cba, QAction::SelectSoftKey, MSK_POSITION);
       
   256 }
       
   257 
       
   258 bool QSoftKeyManagerPrivateS60::setRightSoftkey(CEikButtonGroupContainer &cba)
       
   259 {
       
   260     if (!setSoftkey(cba, QAction::NegativeSoftKey, RSK_POSITION)) {
       
   261         Qt::WindowType windowType = Qt::Window;
       
   262         QAction *action = requestedSoftKeyActions.value(0);
       
   263         if (action) {
       
   264             QWidget *actionParent = action->parentWidget();
       
   265             Q_ASSERT_X(actionParent, Q_FUNC_INFO, "No parent set for softkey action!");
       
   266 
       
   267             QWidget *actionWindow = actionParent->window();
       
   268             Q_ASSERT_X(actionWindow, Q_FUNC_INFO, "Softkey action does not have window!");
       
   269             windowType = actionWindow->windowType();
       
   270         }
       
   271 
       
   272         if (windowType != Qt::Dialog && windowType != Qt::Popup) {
       
   273             QString text(QSoftKeyManager::tr("Exit"));
       
   274             TPtrC nativeText = qt_QString2TPtrC(text);
       
   275             setNativeSoftkey(cba, RSK_POSITION, EAknSoftkeyExit, nativeText);
       
   276             return true;
       
   277         }
       
   278     }
       
   279     return false;
       
   280 }
       
   281 
       
   282 void QSoftKeyManagerPrivateS60::setSoftkeys(CEikButtonGroupContainer &cba)
       
   283 {
       
   284     int requestedSoftkeyCount = requestedSoftKeyActions.count();
       
   285     const int maxSoftkeyCount = 2; // TODO: differs based on orientation ans S60 versions (some have MSK)
       
   286     if (requestedSoftkeyCount > maxSoftkeyCount) {
       
   287         // We have more softkeys than available slots
       
   288         // Put highest priority negative action to RSK and Options menu with rest of softkey actions to LSK
       
   289         // TODO: Build menu
       
   290         setLeftSoftkey(cba);
       
   291         if(AknLayoutUtils::MSKEnabled())
       
   292             setMiddleSoftkey(cba);
       
   293         setRightSoftkey(cba);
       
   294     } else {
       
   295         // We have less softkeys than available slots
       
   296         // Put softkeys to request slots based on role
       
   297         setLeftSoftkey(cba);
       
   298         if(AknLayoutUtils::MSKEnabled())
       
   299             setMiddleSoftkey(cba);
       
   300         setRightSoftkey(cba);
       
   301     }
       
   302 }
       
   303 
       
   304 void QSoftKeyManagerPrivateS60::updateSoftKeys_sys()
       
   305 {
       
   306     //bool status = CCoeEnv::Static()->AppUi()->IsDisplayingMenuOrDialog();
       
   307     if (skipCbaUpdate())
       
   308         return;
       
   309 
       
   310     CEikButtonGroupContainer *nativeContainer = S60->buttonGroupContainer();
       
   311     Q_ASSERT_X(nativeContainer, Q_FUNC_INFO, "Native CBA does not exist!");
       
   312     ensureCbaVisibilityAndResponsiviness(*nativeContainer);
       
   313     clearSoftkeys(*nativeContainer);
       
   314     setSoftkeys(*nativeContainer);
       
   315 
       
   316     nativeContainer->DrawDeferred(); // 3.1 needs an extra invitation
       
   317 }
       
   318 
       
   319 bool QSoftKeyManagerPrivateS60::handleCommand(int command)
       
   320 {
       
   321     QAction *action = realSoftKeyActions.value(command);
       
   322     if (action) {
       
   323         QVariant property = action->property(MENU_ACTION_PROPERTY);
       
   324         if (property.isValid() && property.toBool()) {
       
   325             QT_TRAP_THROWING(S60->menuBar()->TryDisplayMenuBarL());
       
   326         } else if (action->menu()) {
       
   327             // TODO: This is hack, in order to use exising QMenuBar implementation for Symbian
       
   328             // menubar needs to have widget to which it is associated. Since we want to associate
       
   329             // menubar to action (which is inherited from QObejct), we create and associate QWidget
       
   330             // to action and pass that for QMenuBar. This associates the menubar to action, and we
       
   331             // can have own menubar for each action.
       
   332             QWidget *actionContainer = action->property("_q_action_widget").value<QWidget*>();
       
   333             if(!actionContainer) {
       
   334                 actionContainer = new QWidget(action->parentWidget());
       
   335                 QMenuBar *menuBar = new QMenuBar(actionContainer);
       
   336                 foreach(QAction *menuAction, action->menu()->actions()) {
       
   337                     QMenu *menu = menuAction->menu();
       
   338                     if(menu)
       
   339                         menuBar->addMenu(action->menu());
       
   340                     else
       
   341                         menuBar->addAction(menuAction);
       
   342                 }
       
   343                 QVariant v;
       
   344                 v.setValue(actionContainer);
       
   345                 action->setProperty("_q_action_widget", v);
       
   346             }
       
   347             qt_symbian_next_menu_from_action(actionContainer);
       
   348             QT_TRAP_THROWING(S60->menuBar()->TryDisplayMenuBarL());
       
   349             // TODO: hack remove, it can happen that IsDisplayingMenuOrDialog return false
       
   350             // in updateSoftKeys_sys, and we will override menu CBA with our own
       
   351             skipNextUpdate = true;
       
   352         } else {
       
   353             Q_ASSERT(action->softKeyRole() != QAction::NoSoftKey);
       
   354             QWidget *actionParent = action->parentWidget();
       
   355             Q_ASSERT_X(actionParent, Q_FUNC_INFO, "No parent set for softkey action!");
       
   356             if (actionParent->isEnabled()) {
       
   357                 action->activate(QAction::Trigger);
       
   358                 return true;
       
   359             }
       
   360         }
       
   361     }
       
   362     return false;
       
   363 }
       
   364 
       
   365 QT_END_NAMESPACE
       
   366 #endif //QT_NO_SOFTKEYMANAGER