src/hbcore/gui/hbmenu.cpp
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 6 c3690ec91ef8
child 21 4633027730f5
child 34 ed14f46c0e55
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    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 /*!
   496     d->q_ptr = this;
   485     d->q_ptr = this;
   497     d->init();
   486     d->init();
   498     setTitle(title);
   487     setTitle(title);
   499 }
   488 }
   500 
   489 
       
   490 /*!
       
   491     Protected constructor.
       
   492 */
   501 HbMenu::HbMenu(HbMenuPrivate &dd, QGraphicsItem *parent) :
   493 HbMenu::HbMenu(HbMenuPrivate &dd, QGraphicsItem *parent) :
   502         HbPopup(dd, parent)
   494         HbPopup(dd, parent)
   503 {
   495 {
   504     Q_D(HbMenu);
   496     Q_D(HbMenu);
   505     d->q_ptr = this;
   497     d->q_ptr = this;
   615 
   607 
   616     \sa insertSeparator
   608     \sa insertSeparator
   617  */
   609  */
   618 HbAction *HbMenu::addSeparator()
   610 HbAction *HbMenu::addSeparator()
   619 {
   611 {
   620     return insertSeparator(0);
   612     //functionality removed for now
       
   613     //return insertSeparator(0);
       
   614     return 0;
   621 }
   615 }
   622 
   616 
   623 /*!
   617 /*!
   624     Inserts a new separator action and inserts it into this menu's list of actions before \a action.
   618     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.
   619     \param before is the action before which the separator is inserted.
   627 
   621 
   628     \sa addSeparator
   622     \sa addSeparator
   629  */
   623  */
   630 HbAction *HbMenu::insertSeparator(HbAction *before)
   624 HbAction *HbMenu::insertSeparator(HbAction *before)
   631 {
   625 {
   632     HbAction *action = new HbAction(this);
   626     Q_UNUSED(before);
       
   627     //functionality removed for now
       
   628     /*HbAction *action = new HbAction(this);
   633     action->setSeparator(true);
   629     action->setSeparator(true);
   634     action->setEnabled(true);
   630     action->setEnabled(true);
   635     action->setVisible(true);
   631     action->setVisible(true);
   636     insertAction(before, action);
   632     insertAction(before, action);
   637     return action;
   633     return action;*/
       
   634     return 0;
   638 }
   635 }
   639 
   636 
   640 /*!
   637 /*!
   641     \return the current active action, or 0 if no action item is currently active.
   638     \return the current active action, or 0 if no action item is currently active.
   642  */
   639  */
   679 {
   676 {
   680     menuAction()->setText(title);
   677     menuAction()->setText(title);
   681 }
   678 }
   682 
   679 
   683 /*!
   680 /*!
   684     \return the menu title. For a sub-menu, the title is the sub-menu action text.
   681     Returns the menu title. For a sub-menu, the title is the sub-menu action text.
       
   682 
       
   683     \return the menu title.
   685 
   684 
   686     \sa setTitle()
   685     \sa setTitle()
   687 */
   686 */
   688 QString HbMenu::title() const
   687 QString HbMenu::title() const
   689 {
   688 {
   690     return menuAction()->text();
   689     return menuAction()->text();
   691 }
   690 }
   692 
   691 
   693 /*!
   692 /*!
   694     \return the menu type. By default a menu is a context menu.
   693     Returns the menu type. By default a menu is a context menu.
       
   694 
       
   695     \return the menu type.
   695 */
   696 */
   696 HbMenu::MenuType HbMenu::menuType() const
   697 HbMenu::MenuType HbMenu::menuType() const
   697 {
   698 {
   698     Q_D(const HbMenu);
   699     Q_D(const HbMenu);
   699     return d->menuType;
   700     return d->menuType;
   705 QVariant HbMenu::itemChange( GraphicsItemChange change, const QVariant & value )
   706 QVariant HbMenu::itemChange( GraphicsItemChange change, const QVariant & value )
   706 {
   707 {
   707     Q_D(HbMenu);
   708     Q_D(HbMenu);
   708 
   709 
   709     if (change == QGraphicsItem::ItemSceneHasChanged) {
   710     if (change == QGraphicsItem::ItemSceneHasChanged) {
   710         d->closeMenuAfterOrientationChange();
   711         d->closeMenu();
   711     }
   712     }
   712     if (change == QGraphicsItem::ItemVisibleChange) {
   713     if (change == QGraphicsItem::ItemVisibleChange) {
   713         if (value.toBool() && d->delayMenuConstruction) {
   714         if (value.toBool() && d->delayMenuConstruction) {
   714             d->delayedLayout();
   715             d->delayedLayout();
   715         }
   716         }
   716         if (value.toBool()) {
   717         if (value.toBool()) {
   717             d->resultAction = 0;
       
   718             d->actionTriggered = false;
   718             d->actionTriggered = false;
   719         }
   719         }
   720         else if (!value.toBool() && !d->menuItemView){
   720         else if (!value.toBool() && !d->menuItemView){
   721             d->delayMenuConstruction = true;
   721             d->delayMenuConstruction = true;
   722         }
   722         }
   752     }
   752     }
   753 
   753 
   754     return HbPopup::event(event);
   754     return HbPopup::event(event);
   755 }
   755 }
   756 
   756 
       
   757 /*!
       
   758   \reimp
       
   759   */
   757 void HbMenu::polish(HbStyleParameters &params)
   760 void HbMenu::polish(HbStyleParameters &params)
   758 {
   761 {
   759     Q_D(HbMenu);
   762     Q_D(HbMenu);
       
   763     const QString NumberOfCols = "number-of-columns";
       
   764     params.addParameter(NumberOfCols);
       
   765 
   760     if (d->mSubMenuItem) {
   766     if (d->mSubMenuItem) {
   761         const QString RightMargin = "submenu-right-offset";
   767         const QString RightMargin = "submenu-right-offset";
   762         const QString DownMargin = "submenu-bottom-margin";
   768         const QString DownMargin = "submenu-bottom-margin";
   763         params.addParameter(RightMargin);
   769         params.addParameter(RightMargin);
   764         params.addParameter(DownMargin);
   770         params.addParameter(DownMargin);
   773         }
   779         }
   774         d->setSubMenuPosition();
   780         d->setSubMenuPosition();
   775     } else {
   781     } else {
   776         HbPopup::polish(params);
   782         HbPopup::polish(params);
   777     }
   783     }
   778 }
   784 
   779 
   785     if (!params.value(NumberOfCols).isNull()) {
       
   786         int cols = params.value(NumberOfCols).toInt();
       
   787         if (d->mNumberOfColumns != cols) {
       
   788             d->mNumberOfColumns = cols;
       
   789             if (d->menuItemView) {
       
   790                 d->menuItemView->updateContainer();
       
   791             }
       
   792         }
       
   793     }
       
   794 }
       
   795 
       
   796 /*!
       
   797   \reimp
       
   798   Returns the shape of this item as a QPainterPath.
       
   799  */
   780 QPainterPath HbMenu::shape() const
   800 QPainterPath HbMenu::shape() const
   781 {
   801 {
   782     /*
   802     /*QRectF rect = QRectF(-1.0, -1.0, boundingRect().width() + 1.0, boundingRect().height() + 1.0);
   783     QRectF sceneRect = mapRectToScene(QRectF(-0.5, -0.5, boundingRect().width() + 0.5, boundingRect().height() + 0.5));
   803     QRectF clipRect = rect.intersected(mapRectFromParent(QRectF(pos().x() - 1.0, pos().y() - 1.0, size().width() + 1.0, size().height() + 1.0)));
   784     QRectF clipRect = sceneRect.intersected(QRectF(pos().x() - 0.5, pos().y() - 0.5, size().width() + 0.5, size().height() + 0.5));
       
   785 
   804 
   786     QPainterPath path;
   805     QPainterPath path;
   787     path.addRect(mapRectFromScene(clipRect));
   806     path.addRect(clipRect);
   788 
   807 
   789     return path.intersected(HbPopup::shape());
   808     return path;*/
   790     */
       
   791     
       
   792     return HbPopup::shape();
   809     return HbPopup::shape();
   793 }
   810 }
   794 
   811 
   795 /*!
   812 /*!
   796  
   813