src/hbcore/gui/hbmenu.cpp
changeset 5 627c4a0fd0e7
parent 3 11d3954df52a
child 6 c3690ec91ef8
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    44 HbMenuPrivate::HbMenuPrivate():
    44 HbMenuPrivate::HbMenuPrivate():
    45     HbPopupPrivate(),
    45     HbPopupPrivate(),
    46     menuItemView(0),
    46     menuItemView(0),
    47     subMenuAction(0),
    47     subMenuAction(0),
    48     activeSubMenu(0),
    48     activeSubMenu(0),
    49     resultAction(0),
       
    50     actionTriggered(false),
    49     actionTriggered(false),
    51     menuType(HbMenu::ContextMenu),
    50     menuType(HbMenu::ContextMenu),
    52     mSubMenuItem(0),
    51     mSubMenuItem(0),
    53     mRightMargin(0.0),
    52     mRightMargin(0.0),
    54     mDownMargin(0.0),
    53     mDownMargin(0.0),
    55     delayMenuConstruction(true),
    54     delayMenuConstruction(true),
    56     receiverToDisconnectOnClose(0)
    55     receiverToDisconnectOnClose(0),
       
    56     mNumberOfColumns(1)
    57 {
    57 {
    58 }
    58 }
    59 
    59 
    60 HbMenuPrivate::~HbMenuPrivate()
    60 HbMenuPrivate::~HbMenuPrivate()
    61 {
    61 {
    86         hasEffects = HbEffectInternal::add("HB_MENU", "menu_disappear", "disappear");
    86         hasEffects = HbEffectInternal::add("HB_MENU", "menu_disappear", "disappear");
    87 
    87 
    88         if (hasEffects) {
    88         if (hasEffects) {
    89             hasEffects = HbEffectInternal::add("HB_menuitem", "menuitem_press", "clicked");
    89             hasEffects = HbEffectInternal::add("HB_menuitem", "menuitem_press", "clicked");
    90         }
    90         }
    91         if (hasEffects ) {
       
    92             hasEffects = HbEffectInternal::add("HB_POPUP",
       
    93                                                "dialog_rotate",
       
    94                                                "orientationswitch");
       
    95         }
       
    96     }
    91     }
    97 #endif
    92 #endif
    98 }
    93 }
    99 
    94 
   100 void HbMenuPrivate::_q_triggerAction(HbMenuItem *currentItem)
    95 void HbMenuPrivate::_q_triggerAction(HbMenuItem *currentItem)
   106         if (hasEffects) {
   101         if (hasEffects) {
   107             HbEffect::start(currentItem, "HB_menuitem", "clicked");
   102             HbEffect::start(currentItem, "HB_menuitem", "clicked");
   108         }
   103         }
   109 #endif
   104 #endif
   110         HbAction *hbAction = qobject_cast<HbAction *>(currentItem->action());
   105         HbAction *hbAction = qobject_cast<HbAction *>(currentItem->action());
       
   106         q->setActiveAction(hbAction);
   111         if (hbAction && hbAction->menu() && !actionTriggered) {
   107         if (hbAction && hbAction->menu() && !actionTriggered) {
   112             hbAction->trigger();
   108             hbAction->trigger();
   113             stopTimeout();
   109             stopTimeout();
   114             openSubmenu(currentItem);
   110             openSubmenu(currentItem);
   115         } else {
   111         } else {
   116             q->close();
   112             q->close();
   117             
       
   118             resultAction = hbAction;
       
   119 
       
   120             if (!actionTriggered) { // prevent duplicate events
   113             if (!actionTriggered) { // prevent duplicate events
   121                 currentItem->action()->trigger();
   114                 currentItem->action()->trigger();
   122             }
   115             }
   123             actionTriggered = true;
   116             actionTriggered = true;
   124         }
   117         }
   255     if (!activeItem) {
   248     if (!activeItem) {
   256         activeAction(activeItem);
   249         activeAction(activeItem);
   257     }
   250     }
   258     if (activeItem && activeItem->action() && activeItem->action()->isEnabled()) {
   251     if (activeItem && activeItem->action() && activeItem->action()->isEnabled()) {
   259         HbAction *hbAction = qobject_cast<HbAction *>(activeItem->action());
   252         HbAction *hbAction = qobject_cast<HbAction *>(activeItem->action());
   260         q->setActiveAction(hbAction);
       
   261         if (!hbAction)
   253         if (!hbAction)
   262             return;
   254             return;
   263         HbMenu *subMenu = hbAction->menu();
   255         HbMenu *subMenu = hbAction->menu();
   264         if ( subMenu ) {
   256         if ( subMenu ) {
   265             subMenu->setLayoutDirection(q->layoutDirection());
   257             subMenu->setLayoutDirection(q->layoutDirection());
   289     } else {
   281     } else {
   290         startTimeout();
   282         startTimeout();
   291     }
   283     }
   292 }
   284 }
   293 
   285 
   294 void HbMenuPrivate::_q_handleMenuAfterOrientationChange()
   286 void HbMenuPrivate::_q_handleMenuClose()
   295 {
   287 {
   296     Q_Q(HbMenu);
   288     Q_Q(HbMenu);
   297     if ( menuType == HbMenu::ContextMenu || menuType == HbMenu::OptionsMenu ) {
   289     if ( menuType == HbMenu::ContextMenu || menuType == HbMenu::OptionsMenu ) {
   298         if(activeSubMenu){
   290         if(activeSubMenu){
   299             HbAction* action = activeSubMenu->activeAction();
   291             HbAction* action = activeSubMenu->activeAction();
   305         q->close();
   297         q->close();
   306     }
   298     }
   307 }
   299 }
   308 
   300 
   309 /*!
   301 /*!
   310  closes the menu after Orientation change
   302  Handles menu close
   311 */
   303 */
   312 void HbMenuPrivate::closeMenuAfterOrientationChange()
   304 void HbMenuPrivate::closeMenu()
   313 {
   305 {
   314     Q_Q(HbMenu);
   306     Q_Q(HbMenu);
   315     HbMainWindow* w(q->mainWindow());
   307     HbMainWindow* w(q->mainWindow());
   316     if ( w ){
   308     if ( w ){
   317         QObject::disconnect( w, SIGNAL(aboutToChangeOrientation()),
   309         QObject::disconnect( w, SIGNAL(aboutToChangeOrientation()),
   318                              q, SLOT(_q_handleMenuAfterOrientationChange()));
   310                              q, SLOT(_q_handleMenuClose()));
   319         QObject::connect( w, SIGNAL(aboutToChangeOrientation()),
   311         QObject::connect( w, SIGNAL(aboutToChangeOrientation()),
   320                  q, SLOT(_q_handleMenuAfterOrientationChange()));
   312                         q, SLOT(_q_handleMenuClose()));
       
   313         QObject::disconnect( w, SIGNAL(aboutToChangeView(HbView*, HbView*)),
       
   314                           q, SLOT(_q_handleMenuClose()));
       
   315         QObject::connect( w, SIGNAL(aboutToChangeView(HbView*, HbView*)),
       
   316                           q, SLOT(_q_handleMenuClose()));
   321     }
   317     }
   322 }
   318 }
   323 
   319 
   324 void HbMenuPrivate::closeMenuRecursively(HbAction* menuAction)
   320 void HbMenuPrivate::closeMenuRecursively(HbAction* menuAction)
   325 {
   321 {
   419     After you add an action to your menu, you specify a receiver object and its slot (you can also
   415     After you add an action to your menu, you specify a receiver object and its slot (you can also
   420     add an action and specify a receiver slot at the same time).
   416     add an action and specify a receiver slot at the same time).
   421     The receiver is notifed when the action is triggered (QAction::triggered()).
   417     The receiver is notifed when the action is triggered (QAction::triggered()).
   422     HbMenu also has a triggered() menu signal, which signals which HbAction was triggered in the menu.
   418     HbMenu also has a triggered() menu signal, which signals which HbAction was triggered in the menu.
   423 
   419 
   424     Context menu example:
   420     An example of how to create an option menu.
   425 
   421     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,2}
   426     A menu and a few actions are created. The triggered() signal of the menu is connected to
   422 
   427     the mute() function of the enclosing class (implementation not shown).
   423     An example of how to create and show a context menu from the gesture.
   428     The exec() function shows the menu.
   424     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,54}
   429 
       
   430     User needs to connect to "longPress" signal and implement corresponding slot. This enables
       
   431     longpress events to be received from list.
       
   432 
       
   433     \dontinclude decoratorlistdemo/contentwidget.cpp
       
   434     \skip // Create new menu
       
   435     \until ( coords );
       
   436 
   425 
   437     \sa HbDialog, HbView
   426     \sa HbDialog, HbView
   438 */
   427 */
   439 
   428 
   440 /*!
   429 /*!
   615 
   604 
   616     \sa insertSeparator
   605     \sa insertSeparator
   617  */
   606  */
   618 HbAction *HbMenu::addSeparator()
   607 HbAction *HbMenu::addSeparator()
   619 {
   608 {
   620     return insertSeparator(0);
   609     //functionality removed for now
       
   610     //return insertSeparator(0);
       
   611     return 0;
   621 }
   612 }
   622 
   613 
   623 /*!
   614 /*!
   624     Inserts a new separator action and inserts it into this menu's list of actions before \a action.
   615     Inserts a new separator action and inserts it into this menu's list of actions before \a action.
   625     \param before is the action before which the separator is inserted.
   616     \param before is the action before which the separator is inserted.
   627 
   618 
   628     \sa addSeparator
   619     \sa addSeparator
   629  */
   620  */
   630 HbAction *HbMenu::insertSeparator(HbAction *before)
   621 HbAction *HbMenu::insertSeparator(HbAction *before)
   631 {
   622 {
   632     HbAction *action = new HbAction(this);
   623     Q_UNUSED(before);
       
   624     //functionality removed for now
       
   625     /*HbAction *action = new HbAction(this);
   633     action->setSeparator(true);
   626     action->setSeparator(true);
   634     action->setEnabled(true);
   627     action->setEnabled(true);
   635     action->setVisible(true);
   628     action->setVisible(true);
   636     insertAction(before, action);
   629     insertAction(before, action);
   637     return action;
   630     return action;*/
       
   631     return 0;
   638 }
   632 }
   639 
   633 
   640 /*!
   634 /*!
   641     \return the current active action, or 0 if no action item is currently active.
   635     \return the current active action, or 0 if no action item is currently active.
   642  */
   636  */
   705 QVariant HbMenu::itemChange( GraphicsItemChange change, const QVariant & value )
   699 QVariant HbMenu::itemChange( GraphicsItemChange change, const QVariant & value )
   706 {
   700 {
   707     Q_D(HbMenu);
   701     Q_D(HbMenu);
   708 
   702 
   709     if (change == QGraphicsItem::ItemSceneHasChanged) {
   703     if (change == QGraphicsItem::ItemSceneHasChanged) {
   710         d->closeMenuAfterOrientationChange();
   704         d->closeMenu();
   711     }
   705     }
   712     if (change == QGraphicsItem::ItemVisibleChange) {
   706     if (change == QGraphicsItem::ItemVisibleChange) {
   713         if (value.toBool() && d->delayMenuConstruction) {
   707         if (value.toBool() && d->delayMenuConstruction) {
   714             d->delayedLayout();
   708             d->delayedLayout();
   715         }
   709         }
   716         if (value.toBool()) {
   710         if (value.toBool()) {
   717             d->resultAction = 0;
       
   718             d->actionTriggered = false;
   711             d->actionTriggered = false;
   719         }
   712         }
   720         else if (!value.toBool() && !d->menuItemView){
   713         else if (!value.toBool() && !d->menuItemView){
   721             d->delayMenuConstruction = true;
   714             d->delayMenuConstruction = true;
   722         }
   715         }
   755 }
   748 }
   756 
   749 
   757 void HbMenu::polish(HbStyleParameters &params)
   750 void HbMenu::polish(HbStyleParameters &params)
   758 {
   751 {
   759     Q_D(HbMenu);
   752     Q_D(HbMenu);
       
   753     const QString NumberOfCols = "number-of-columns";
       
   754     params.addParameter(NumberOfCols);
       
   755 
   760     if (d->mSubMenuItem) {
   756     if (d->mSubMenuItem) {
   761         const QString RightMargin = "submenu-right-offset";
   757         const QString RightMargin = "submenu-right-offset";
   762         const QString DownMargin = "submenu-bottom-margin";
   758         const QString DownMargin = "submenu-bottom-margin";
   763         params.addParameter(RightMargin);
   759         params.addParameter(RightMargin);
   764         params.addParameter(DownMargin);
   760         params.addParameter(DownMargin);
   773         }
   769         }
   774         d->setSubMenuPosition();
   770         d->setSubMenuPosition();
   775     } else {
   771     } else {
   776         HbPopup::polish(params);
   772         HbPopup::polish(params);
   777     }
   773     }
       
   774 
       
   775     if (!params.value(NumberOfCols).isNull()) {
       
   776         int cols = params.value(NumberOfCols).toInt();
       
   777         if (d->mNumberOfColumns != cols) {
       
   778             d->mNumberOfColumns = cols;
       
   779             if (d->menuItemView) {
       
   780                 d->menuItemView->updateContainer();
       
   781             }
       
   782         }
       
   783     }
   778 }
   784 }
   779 
   785 
   780 QPainterPath HbMenu::shape() const
   786 QPainterPath HbMenu::shape() const
   781 {
   787 {
   782     /*QRectF sceneRect = mapRectToScene(QRectF(-0.5, -0.5, boundingRect().width() + 0.5, boundingRect().height() + 0.5));
   788     /*QRectF rect = QRectF(-1.0, -1.0, boundingRect().width() + 1.0, boundingRect().height() + 1.0);
   783     QRectF clipRect = sceneRect.intersected(QRectF(pos().x() - 0.5, pos().y() - 0.5, size().width() + 0.5, size().height() + 0.5));
   789     QRectF clipRect = rect.intersected(mapRectFromParent(QRectF(pos().x() - 1.0, pos().y() - 1.0, size().width() + 1.0, size().height() + 1.0)));
   784 
   790 
   785     QPainterPath path;
   791     QPainterPath path;
   786     path.addRect(mapRectFromScene(clipRect));
   792     path.addRect(clipRect);
   787 
   793 
   788     return path.intersected(HbPopup::shape());*/
   794     return path;*/
   789 	return HbPopup::shape();
   795     return HbPopup::shape();
   790 }
   796 }
   791 
   797 
   792 /*!
   798 /*!
   793  
   799  
   794   Opens the menu and returns immediately.
   800   Opens the menu and returns immediately.