src/gui/kernel/qsoftkeymanager_s60.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 "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() : cbaHasImage(4) // 4 since MSK position index is 3
       
    64 {
       
    65     cachedCbaIconSize[0] = QSize(0,0);
       
    66     cachedCbaIconSize[1] = QSize(0,0);
       
    67     cachedCbaIconSize[2] = QSize(0,0);
       
    68     cachedCbaIconSize[3] = QSize(0,0);
       
    69 }
       
    70 
       
    71 bool QSoftKeyManagerPrivateS60::skipCbaUpdate()
       
    72 {
       
    73     // Lets not update softkeys if
       
    74     // 1. We don't have application panes, i.e. cba
       
    75     // 2. Our CBA is not active, i.e. S60 native dialog or menu with custom CBA is shown
       
    76     //    2.1. Except if thre is no current CBA at all and WindowSoftkeysRespondHint is set
       
    77 
       
    78     // Note: Cannot use IsDisplayingMenuOrDialog since CBA update can be triggered before
       
    79     // menu/dialog CBA is actually displayed i.e. it is being costructed.
       
    80     CEikButtonGroupContainer *appUiCba = S60->buttonGroupContainer();
       
    81     // CEikButtonGroupContainer::Current returns 0 if CBA is not visible at all
       
    82     CEikButtonGroupContainer *currentCba = CEikButtonGroupContainer::Current();
       
    83     // Check if softkey need to be update even they are not visible
       
    84     bool cbaRespondsWhenInvisible = false;
       
    85     QWidget *window = QApplication::activeWindow();
       
    86     if (window && (window->windowFlags() & Qt::WindowSoftkeysRespondHint))
       
    87         cbaRespondsWhenInvisible = true;
       
    88 
       
    89     if (QApplication::testAttribute(Qt::AA_S60DontConstructApplicationPanes)
       
    90             || (appUiCba != currentCba && !cbaRespondsWhenInvisible)) {
       
    91         return true;
       
    92     }
       
    93     return false;
       
    94 }
       
    95 
       
    96 void QSoftKeyManagerPrivateS60::ensureCbaVisibilityAndResponsiviness(CEikButtonGroupContainer &cba)
       
    97 {
       
    98     RDrawableWindow *cbaWindow = cba.DrawableWindow();
       
    99     Q_ASSERT_X(cbaWindow, Q_FUNC_INFO, "Native CBA does not have window!");
       
   100     // Make sure CBA is visible, i.e. CBA window is on top
       
   101     cbaWindow->SetOrdinalPosition(0);
       
   102     // Qt shares same CBA instance between top-level widgets,
       
   103     // make sure we are not faded by underlying window.
       
   104     cbaWindow->SetFaded(EFalse, RWindowTreeNode::EFadeIncludeChildren);
       
   105     // Modal dialogs capture pointer events, but shared cba instance
       
   106     // shall stay responsive. Raise pointer capture priority to keep
       
   107     // softkeys responsive in modal dialogs
       
   108     cbaWindow->SetPointerCapturePriority(1);
       
   109 }
       
   110 
       
   111 void QSoftKeyManagerPrivateS60::clearSoftkeys(CEikButtonGroupContainer &cba)
       
   112 {
       
   113     QT_TRAP_THROWING(
       
   114         //Using -1 instead of EAknSoftkeyEmpty to avoid flickering.
       
   115         cba.SetCommandL(0, -1, KNullDesC);
       
   116         // TODO: Should we clear also middle SK?
       
   117         cba.SetCommandL(2, -1, KNullDesC);
       
   118     );
       
   119     realSoftKeyActions.clear();
       
   120 }
       
   121 
       
   122 QString QSoftKeyManagerPrivateS60::softkeyText(QAction &softkeyAction)
       
   123 {
       
   124     // In S60 softkeys and menu items do not support key accelerators (i.e.
       
   125     // CTRL+X). Therefore, removing the accelerator characters from both softkey
       
   126     // and menu item texts.
       
   127     const int underlineShortCut = QApplication::style()->styleHint(QStyle::SH_UnderlineShortcut);
       
   128     QString iconText = softkeyAction.iconText();
       
   129     return underlineShortCut ? softkeyAction.text() : iconText;
       
   130 }
       
   131 
       
   132 QAction *QSoftKeyManagerPrivateS60::highestPrioritySoftkey(QAction::SoftKeyRole role)
       
   133 {
       
   134     QAction *ret = NULL;
       
   135     // Priority look up is two level
       
   136     // 1. First widget with softkeys always has highest priority
       
   137     for (int level = 0; !ret; level++) {
       
   138         // 2. Highest priority action within widget
       
   139         QList<QAction*> actions = requestedSoftKeyActions.values(level);
       
   140         if (actions.isEmpty())
       
   141             break;
       
   142         qSort(actions.begin(), actions.end(), QSoftKeyManagerPrivateS60::actionPriorityMoreThan);
       
   143         foreach (QAction *action, actions) {
       
   144             if (action->softKeyRole() == role) {
       
   145                 ret = action;
       
   146                 break;
       
   147             }
       
   148         }
       
   149     }
       
   150     return ret;
       
   151 }
       
   152 
       
   153 bool QSoftKeyManagerPrivateS60::actionPriorityMoreThan(const QAction *firstItem, const QAction *secondItem)
       
   154 {
       
   155     return firstItem->priority() > secondItem->priority();
       
   156 }
       
   157 
       
   158 void QSoftKeyManagerPrivateS60::setNativeSoftkey(CEikButtonGroupContainer &cba,
       
   159                                               TInt position, TInt command, const TDesC &text)
       
   160 {
       
   161     // Calling SetCommandL causes CBA redraw
       
   162     QT_TRAP_THROWING(cba.SetCommandL(position, command, text));
       
   163 }
       
   164 
       
   165 QPoint QSoftKeyManagerPrivateS60::softkeyIconPosition(int position, QSize sourceSize, QSize targetSize)
       
   166 {
       
   167     QPoint iconPosition(0,0);
       
   168     switch( AknLayoutUtils::CbaLocation() )
       
   169         {
       
   170         case AknLayoutUtils::EAknCbaLocationBottom:
       
   171             // RSK must be moved to right, LSK in on correct position by default
       
   172             if (position == RSK_POSITION)
       
   173                 iconPosition.setX(targetSize.width() - sourceSize.width());
       
   174             break;
       
   175         case AknLayoutUtils::EAknCbaLocationRight:
       
   176         case AknLayoutUtils::EAknCbaLocationLeft:
       
   177             // Already in correct position
       
   178         default:
       
   179             break;
       
   180         }
       
   181 
       
   182     // Align horizontally to center
       
   183     iconPosition.setY((targetSize.height() - sourceSize.height()) >> 1);
       
   184     return iconPosition;
       
   185 }
       
   186 
       
   187 QPixmap QSoftKeyManagerPrivateS60::prepareSoftkeyPixmap(QPixmap src, int position, QSize targetSize)
       
   188 {
       
   189     QPixmap target(targetSize);
       
   190     target.fill(Qt::transparent);
       
   191     QPainter p;
       
   192     p.begin(&target);
       
   193     p.drawPixmap(softkeyIconPosition(position, src.size(), targetSize), src);
       
   194     p.end();
       
   195     return target;
       
   196 }
       
   197 
       
   198 bool QSoftKeyManagerPrivateS60::isOrientationLandscape()
       
   199 {
       
   200     // Hard to believe that there is no public API in S60 to
       
   201     // get current orientation. This workaround works with currently supported resolutions
       
   202     return  S60->screenHeightInPixels <  S60->screenWidthInPixels;
       
   203 }
       
   204 
       
   205 QSize QSoftKeyManagerPrivateS60::cbaIconSize(CEikButtonGroupContainer *cba, int position)
       
   206 {
       
   207 
       
   208     int index = position;
       
   209     index += isOrientationLandscape() ? 0 : 1;
       
   210     if(cachedCbaIconSize[index].isNull()) {
       
   211         // Only way I figured out to get CBA icon size without RnD SDK, was
       
   212         // to set some dummy icon to CBA first and then ask CBA button CCoeControl::Size()
       
   213         // The returned value is cached to avoid unnecessary icon setting every time.
       
   214         const bool left = (position == LSK_POSITION);
       
   215         if(position == LSK_POSITION || position == RSK_POSITION) {
       
   216             CEikImage* tmpImage = NULL;
       
   217             QT_TRAP_THROWING(tmpImage = new (ELeave) CEikImage);
       
   218             EikSoftkeyImage::SetImage(cba, *tmpImage, left); // Takes myimage ownership
       
   219             int command = S60_COMMAND_START + position;
       
   220             setNativeSoftkey(*cba, position, command, KNullDesC());
       
   221             cachedCbaIconSize[index] = qt_TSize2QSize(cba->ControlOrNull(command)->Size());
       
   222             EikSoftkeyImage::SetLabel(cba, left);
       
   223 
       
   224             if(cachedCbaIconSize[index] == QSize(138,72)) {
       
   225                 // Hack for S60 5.0 (5800) landscape orientation, which return wrong icon size
       
   226                 cachedCbaIconSize[index] = QSize(60,60);
       
   227             }
       
   228         }
       
   229     }
       
   230 
       
   231     return cachedCbaIconSize[index];
       
   232 }
       
   233 
       
   234 bool QSoftKeyManagerPrivateS60::setSoftkeyImage(CEikButtonGroupContainer *cba,
       
   235                                             QAction &action, int position)
       
   236 {
       
   237     bool ret = false;
       
   238 
       
   239     const bool left = (position == LSK_POSITION);
       
   240     if(position == LSK_POSITION || position == RSK_POSITION) {
       
   241         QIcon icon = action.icon();
       
   242         if (!icon.isNull()) {
       
   243             // Get size of CBA icon area based on button position and orientation
       
   244             QSize requiredIconSize = cbaIconSize(cba, position);
       
   245             // Get pixmap out of icon based on preferred size, the aspect ratio is kept
       
   246             QPixmap pmWihtAspectRatio = icon.pixmap(requiredIconSize);
       
   247             // Native softkeys require that pixmap size is exactly the same as requiredIconSize
       
   248             // prepareSoftkeyPixmap creates a new pixmap with requiredIconSize and blits the 'pmWihtAspectRatio'
       
   249             // to correct location of it
       
   250             QPixmap softkeyPixmap = prepareSoftkeyPixmap(pmWihtAspectRatio, position, requiredIconSize);
       
   251 
       
   252             QPixmap softkeyAlpha = softkeyPixmap.alphaChannel();
       
   253             // Alpha channel in 5.1 and older devices need to be inverted
       
   254             // TODO: Switch to use toSymbianCFbsBitmap with invert when available
       
   255             if(QSysInfo::s60Version() <= QSysInfo::SV_S60_5_1) {
       
   256                 QImage alphaImage = softkeyAlpha.toImage();
       
   257                 alphaImage.invertPixels();
       
   258                 softkeyAlpha = QPixmap::fromImage(alphaImage);
       
   259             }
       
   260 
       
   261             CFbsBitmap* nBitmap = softkeyPixmap.toSymbianCFbsBitmap();
       
   262             CFbsBitmap* nMask = softkeyAlpha.toSymbianCFbsBitmap();
       
   263 
       
   264             CEikImage* myimage = new (ELeave) CEikImage;
       
   265             myimage->SetPicture( nBitmap, nMask ); // nBitmap and nMask ownership transfered
       
   266 
       
   267             EikSoftkeyImage::SetImage(cba, *myimage, left); // Takes myimage ownership
       
   268             cbaHasImage[position] = true;
       
   269             ret = true;
       
   270         } else {
       
   271             // Restore softkey to text based
       
   272             if (cbaHasImage[position]) {
       
   273                 EikSoftkeyImage::SetLabel(cba, left);
       
   274                 cbaHasImage[position] = false;
       
   275             }
       
   276         }
       
   277     }
       
   278     return ret;
       
   279 }
       
   280 
       
   281 bool QSoftKeyManagerPrivateS60::setSoftkey(CEikButtonGroupContainer &cba,
       
   282                                         QAction::SoftKeyRole role, int position)
       
   283 {
       
   284     QAction *action = highestPrioritySoftkey(role);
       
   285     if (action) {
       
   286         setSoftkeyImage(&cba, *action, position);
       
   287         QString text = softkeyText(*action);
       
   288         TPtrC nativeText = qt_QString2TPtrC(text);
       
   289         int command = S60_COMMAND_START + position;
       
   290         setNativeSoftkey(cba, position, command, nativeText);
       
   291         const bool dimmed = !action->isEnabled() && !QSoftKeyManager::isForceEnabledInSofkeys(action);
       
   292         cba.DimCommand(command, dimmed);
       
   293         realSoftKeyActions.insert(command, action);
       
   294         return true;
       
   295     }
       
   296     return false;
       
   297 }
       
   298 
       
   299 bool QSoftKeyManagerPrivateS60::setLeftSoftkey(CEikButtonGroupContainer &cba)
       
   300 {
       
   301     return setSoftkey(cba, QAction::PositiveSoftKey, LSK_POSITION);
       
   302 }
       
   303 
       
   304 bool QSoftKeyManagerPrivateS60::setMiddleSoftkey(CEikButtonGroupContainer &cba)
       
   305 {
       
   306     // Note: In order to get MSK working, application has to have EAknEnableMSK flag set
       
   307     // Currently it is not possible very easily)
       
   308     // For more information see: http://wiki.forum.nokia.com/index.php/Middle_softkey_usage
       
   309     return setSoftkey(cba, QAction::SelectSoftKey, MSK_POSITION);
       
   310 }
       
   311 
       
   312 bool QSoftKeyManagerPrivateS60::setRightSoftkey(CEikButtonGroupContainer &cba)
       
   313 {
       
   314     if (!setSoftkey(cba, QAction::NegativeSoftKey, RSK_POSITION)) {
       
   315         Qt::WindowType windowType = Qt::Window;
       
   316         QAction *action = requestedSoftKeyActions.value(0);
       
   317         if (action) {
       
   318             QWidget *actionParent = action->parentWidget();
       
   319             Q_ASSERT_X(actionParent, Q_FUNC_INFO, "No parent set for softkey action!");
       
   320 
       
   321             QWidget *actionWindow = actionParent->window();
       
   322             Q_ASSERT_X(actionWindow, Q_FUNC_INFO, "Softkey action does not have window!");
       
   323             windowType = actionWindow->windowType();
       
   324         }
       
   325 
       
   326         if (windowType != Qt::Dialog && windowType != Qt::Popup) {
       
   327             QString text(QSoftKeyManager::tr("Exit"));
       
   328             TPtrC nativeText = qt_QString2TPtrC(text);
       
   329             if (cbaHasImage[RSK_POSITION]) {
       
   330                 EikSoftkeyImage::SetLabel(&cba, false);
       
   331                 cbaHasImage[RSK_POSITION] = false;
       
   332             }
       
   333             setNativeSoftkey(cba, RSK_POSITION, EAknSoftkeyExit, nativeText);
       
   334             cba.DimCommand(EAknSoftkeyExit, false);
       
   335             return true;
       
   336         }
       
   337     }
       
   338     return false;
       
   339 }
       
   340 
       
   341 void QSoftKeyManagerPrivateS60::setSoftkeys(CEikButtonGroupContainer &cba)
       
   342 {
       
   343     int requestedSoftkeyCount = requestedSoftKeyActions.count();
       
   344     const int maxSoftkeyCount = 2; // TODO: differs based on orientation ans S60 versions (some have MSK)
       
   345     if (requestedSoftkeyCount > maxSoftkeyCount) {
       
   346         // We have more softkeys than available slots
       
   347         // Put highest priority negative action to RSK and Options menu with rest of softkey actions to LSK
       
   348         // TODO: Build menu
       
   349         setLeftSoftkey(cba);
       
   350         if(AknLayoutUtils::MSKEnabled())
       
   351             setMiddleSoftkey(cba);
       
   352         setRightSoftkey(cba);
       
   353     } else {
       
   354         // We have less softkeys than available slots
       
   355         // Put softkeys to request slots based on role
       
   356         setLeftSoftkey(cba);
       
   357         if(AknLayoutUtils::MSKEnabled())
       
   358             setMiddleSoftkey(cba);
       
   359         setRightSoftkey(cba);
       
   360     }
       
   361 }
       
   362 
       
   363 void QSoftKeyManagerPrivateS60::updateSoftKeys_sys()
       
   364 {
       
   365     if (skipCbaUpdate())
       
   366         return;
       
   367 
       
   368     CEikButtonGroupContainer *nativeContainer = S60->buttonGroupContainer();
       
   369     Q_ASSERT_X(nativeContainer, Q_FUNC_INFO, "Native CBA does not exist!");
       
   370     ensureCbaVisibilityAndResponsiviness(*nativeContainer);
       
   371     clearSoftkeys(*nativeContainer);
       
   372     setSoftkeys(*nativeContainer);
       
   373 
       
   374     nativeContainer->DrawDeferred(); // 3.1 needs an extra invitation
       
   375 }
       
   376 
       
   377 bool QSoftKeyManagerPrivateS60::handleCommand(int command)
       
   378 {
       
   379     QAction *action = realSoftKeyActions.value(command);
       
   380     if (action) {
       
   381         QVariant property = action->property(MENU_ACTION_PROPERTY);
       
   382         if (property.isValid() && property.toBool()) {
       
   383             QT_TRAP_THROWING(S60->menuBar()->TryDisplayMenuBarL());
       
   384         } else if (action->menu()) {
       
   385             // TODO: This is hack, in order to use exising QMenuBar implementation for Symbian
       
   386             // menubar needs to have widget to which it is associated. Since we want to associate
       
   387             // menubar to action (which is inherited from QObejct), we create and associate QWidget
       
   388             // to action and pass that for QMenuBar. This associates the menubar to action, and we
       
   389             // can have own menubar for each action.
       
   390             QWidget *actionContainer = action->property("_q_action_widget").value<QWidget*>();
       
   391             if(!actionContainer) {
       
   392                 actionContainer = new QWidget(action->parentWidget());
       
   393                 QMenuBar *menuBar = new QMenuBar(actionContainer);
       
   394                 foreach(QAction *menuAction, action->menu()->actions()) {
       
   395                     QMenu *menu = menuAction->menu();
       
   396                     if(menu)
       
   397                         menuBar->addMenu(action->menu());
       
   398                     else
       
   399                         menuBar->addAction(menuAction);
       
   400                 }
       
   401                 QVariant v;
       
   402                 v.setValue(actionContainer);
       
   403                 action->setProperty("_q_action_widget", v);
       
   404             }
       
   405             qt_symbian_next_menu_from_action(actionContainer);
       
   406             QT_TRAP_THROWING(S60->menuBar()->TryDisplayMenuBarL());
       
   407         } else {
       
   408             Q_ASSERT(action->softKeyRole() != QAction::NoSoftKey);
       
   409             QWidget *actionParent = action->parentWidget();
       
   410             Q_ASSERT_X(actionParent, Q_FUNC_INFO, "No parent set for softkey action!");
       
   411             if (actionParent->isEnabled()) {
       
   412                 action->activate(QAction::Trigger);
       
   413                 return true;
       
   414             }
       
   415         }
       
   416     }
       
   417     return false;
       
   418 }
       
   419 
       
   420 QT_END_NAMESPACE
       
   421 #endif //QT_NO_SOFTKEYMANAGER