src/hbcore/gui/hbtoolbar.cpp
changeset 21 4633027730f5
parent 6 c3690ec91ef8
child 23 e6ad4ef83b23
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    53 
    53 
    54 /*!
    54 /*!
    55     @beta
    55     @beta
    56     @hbcore
    56     @hbcore
    57     \class HbToolBar
    57     \class HbToolBar
    58     \brief HbToolBar is a toolbar decorator.
    58     \brief The HbToolBar class provides a widget that gives quick access to commands
    59 
    59     associated with the view.
    60     The HbToolBar class represents an HbView toolbar. It provides the
    60 
    61     interface for adding actions to the toolbar.
    61     organizing a set of commands.
    62 
    62 
    63     Toolbar actions are added using one of the addAction() methods.
    63     A toolbar provides similar functions to an options menu (HbMenu class). Like an options
    64     Calling addAction() adds an HbToolButton to the toolbar and
    64     menu, a toolbar represents commands that apply to the entire view and not to individual
    65     triggers the action when the button is pressed. The image and text
    65     items in the view. However, there are some important differences. A toolbar shows the
    66     specified with the action are applied to the toolbar button.
    66     options as buttons (each of which can have an image and a label) whereas a menu shows the
    67 
    67     options in a simple list. Typically the toolbar gives access to the view's most important
    68     HbToolBar also provides methods for adding pop-up toolbar
    68     commands (called first order commands) and menus provide access to second order commands.
    69     extensions, represented by HbToolBarExtension objects.
    69     In an application that has multiple views (HbView class), typically the toolbar also
    70 
    70     enables the user to switch quickly between views.
    71     Example usage:
    71 
    72     \dontinclude ultimatecodesnippet/ultimatecodesnippet.cpp
    72     \image html toolbar.png A toolbar that has five buttons
    73     \skip Start of snippet 1
    73 
    74     \until End of snippet 1
    74     Toolbars contain \b actions, which are objects of class HbAction. A toolbar can contain
    75 
    75     actions that represent a button and actions that open a popup, called a toolbar extension
    76     Note: calling hide() or setVisible(bool) on toolbar is not
    76     (HbToolBarExtension class). This can contain additional actions or a widget, such as a list.
    77     recommended.  Instead, use \a HbView::setItemVisible(), as in 
    77 
    78     this example:
    78     Use addAction() to create an action and add it to the toolbar. There are several overloads
       
    79     of this function, which allow you to specify both a text and image or just a text and also to
       
    80     connect the action's \link HbAction::triggered() triggered()\endlink signal to a slot on
       
    81     a receiver object. The image and text, if specified, are applied to the toolbar button.
       
    82     Using an image is recommended. Use the insertAction(), addActions() and insertActions() methods
       
    83     (which are inherited from QGraphicsWidget) to add existing actions to the toolbar. Use
       
    84     clearActions() to clear all of the actions from a toolbar and removeAction() to remove
       
    85     individual actions.
       
    86 
       
    87     Typically a toolbar can accommodate up to five actions. If you add more actions than can
       
    88     fit, the additional actions are hidden behind the endmost button, which activates
       
    89     a popup containing the extra actions. However, usability guidelines suggest that this
       
    90     should be avoided. The order of the actions within the toolbar controls the order of the
       
    91     buttons that the user sees.
       
    92 
       
    93     When you add the actions directly to the toolbar, addAction() and addActions() append
       
    94     the actions to the end of the toolbar and insertAction() and insertActions() enable you to
       
    95     specify the required position. However, there is an alternative approach to ordering the
       
    96     action items. That is to call HbView::addAction() to add actions to the \b view and
       
    97     let the view distribute them to the options menu or toolbar, depending on the preference
       
    98     set, the UI command distribution template, and taking into account the available space in
       
    99     the toolbar. The menu and toolbar then order the actions according to their defined roles
       
   100     and the UI command container template. This approach makes it easier to create consistent
       
   101     user interfaces and applications that work well on a variety of different devices.
       
   102 
       
   103     After you add an action item to a toolbar, you can connect its \link HbAction::triggered()
       
   104     triggered()\endlink signal to a slot on a receiver object. Alternatively you can use one of
       
   105     the addAction() overloads that allow you to add an action and specify a receiver slot at
       
   106     the same time. The receiver is notified when the action is \link HbAction::triggered()
       
   107     triggered()\endlink, which means that the user has selected the button; for example, by
       
   108     tapping it.
       
   109 
       
   110     By default, the toolbar is horizontal and positioned at the bottom of the view. Use
       
   111     setOrientation() to change the orientation to vertical. The toolbar is then positioned on
       
   112     the right of the view.
       
   113 
       
   114     You add the toolbar to the view by calling HbView::setToolBar(). If you want to hide the
       
   115     toolbar, it is recommended that you call HbView::setItemVisible() rather than calling
       
   116     \link QGraphicsItem::hide() hide()\endlink or \link QGraphicsItem::setVisible()
       
   117     setVisible()\endlink on the toolbar itself.
       
   118 
       
   119     You can disable a toolbar button by calling \c setEnabled(false) on the action. This
       
   120     property is inherited from QAction. Other properties inherited from QAction enable
       
   121     you to specify that an action is checkable (which means it has an on/off state), to
       
   122     set a keyboard shortcut, and so on.
       
   123 
       
   124     \b Note: When you add an action to the toolbar, the HbToolBar implementation creates an
       
   125     internal HbToolButton object. This is not accessible and is an implementation detail that
       
   126     might change in the future.
       
   127 
       
   128     \section _usecases_hbtoolbar Using the HbToolBar class
       
   129 
       
   130     \subsection _uc_001_hbtoolbar Creating a toolbar
       
   131 
       
   132     The following example demonstrates creating a toolbar and adding an action
       
   133     to it and then adding the toolbar to the view:
       
   134 
       
   135     \code
       
   136     // Create the toolbar object.
       
   137     HbToolBar *toolBar = new HbToolBar();
       
   138 
       
   139     HbAction *closeAction = toolBar->addAction(tr("Close"));
       
   140 
       
   141     // Add the toolbar to the view.
       
   142     myView->setToolBar(toolBar);
       
   143     \endcode
       
   144 
       
   145     \subsection _uc_002_hbtoolbar Connecting a toolbar action's signal to a slot
       
   146 
       
   147     You must connect the action's \link HbAction::triggered() triggered()\endlink
       
   148     signal to a suitable slot on the receiver object that is to carry out the command.
       
   149     For example:
       
   150 
       
   151     \code
       
   152     QObject::connect(closeAction, SIGNAL(triggered(bool)), this, SLOT(closeAccount(bool)));
       
   153     \endcode
       
   154 
       
   155     Alternatively you can add the action and connect its signal to a slot by using one of the
       
   156     the convenience \link HbToolBar::addAction(const QString &, const QObject *, const char *)
       
   157     addAction() \endlink overloads that let you add the action to the toolbar and connect its
       
   158     signal to a suitable slot in one call. For example:
       
   159 
       
   160     \code
       
   161     toolBar->addAction(tr("Open"), d, SLOT(openAccount(bool)));
       
   162     \endcode
       
   163 
       
   164     \subsection _uc_003_hbtoolbar Adding actions to the view
       
   165 
       
   166     As mentioned above, there are advantages in terms of consistency and portability to
       
   167     adding toolbar actions to the view rather than to the toolbar itself. The following example
       
   168     demonstrates this. It creates two action items, specifies their command roles, and then
       
   169     adds them to the view, specifying the toolbar as the preferred container. The view takes
       
   170     this preference into account but may place them in the options menu if, for example, the
       
   171     toolbar is already full. The menu and toolbar order the actions according to their defined
       
   172     command roles.
       
   173 
       
   174     \code
       
   175     HbAction *actionCircle = new HbAction();
       
   176     actionCircle->setToolTip("Circle");
       
   177     actionCircle->setIcon(HbIcon("circle.png"));
       
   178     actionCircle->setCommandRole(HbAction::OtherRole);
       
   179 
       
   180     HbAction *actionStar = new HbAction();
       
   181     actionStar->setToolTip("Star");
       
   182     actionStar->setIcon(HbIcon("star.png"));
       
   183     actionStar->setCommandRole(HbAction::OtherRole);
       
   184 
       
   185     // Add actions to the view.
       
   186     myView->addAction(actionStar, HbView::ToolBar);
       
   187     myView->addAction(actionCircle, HbView::ToolBar);
       
   188     \endcode
       
   189 
       
   190     \subsection _uc_004_hbtoolbar Hiding the toolbar
       
   191 
       
   192     The next example demonstrates calling HbView::setItemVisible() to hide the
       
   193     toolbar. This is the recommended way of hiding a toolbar:
    79     \dontinclude ultimatecodesnippet/ultimatecodesnippet.cpp
   194     \dontinclude ultimatecodesnippet/ultimatecodesnippet.cpp
    80     \skip Start of snippet 59
   195     \skip Start of snippet 59
    81     \until End of snippet 59
   196     \until End of snippet 59
    82 
   197 
    83    
   198     \sa HbToolBarExtension, HbView, HbMenu, HbAction
    84 */
   199 */
    85 
   200 
    86 /*!
   201 /*!
    87     \reimp
       
    88     \fn int HbToolBar::type() const
   202     \fn int HbToolBar::type() const
    89  */
   203 */
    90 
       
    91 /*!
       
    92     \fn void HbToolBar::addAction(QAction *action)
       
    93     Adds a new action to the toolbar. It's appended to the end
       
    94     of the toolbar. Toolbar doesn't take ownership of the QAction.
       
    95   */
       
    96 
   204 
    97 // ======== MEMBER FUNCTIONS ========
   205 // ======== MEMBER FUNCTIONS ========
    98 
   206 
    99 /*!
   207 /*!
   100     Constructs a tool bar with \a parent.
   208     Constructs a toolbar with the given \a parent.
   101 */
   209 */
   102 
   210 
   103 HbToolBar::HbToolBar( QGraphicsItem *parent )
   211 HbToolBar::HbToolBar( QGraphicsItem *parent )
   104     : HbWidget(*new HbToolBarPrivate, parent)
   212     : HbWidget(*new HbToolBarPrivate, parent)
   105 {
   213 {
   138         }
   246         }
   139     }
   247     }
   140 }
   248 }
   141 
   249 
   142 /*!
   250 /*!
       
   251     Creates a new action with the given \a text and adds the action to
       
   252     the end of the toolbar. The toolbar takes ownership of the new action.
       
   253 
   143     \overload
   254     \overload
   144 
   255     \return The new action.
   145     Creates a new action with the given \a text. This action is added to
       
   146     the end of the toolbar. Toolbar retains ownership of the action.
       
   147 */
   256 */
   148 HbAction *HbToolBar::addAction( const QString &text )
   257 HbAction *HbToolBar::addAction( const QString &text )
   149 {
   258 {
   150     HbAction *action = new HbAction(text, this);
   259     HbAction *action = new HbAction(text, this);
   151     addAction(action);
   260     addAction(action);
   152     return action;
   261     return action;
   153 }
   262 }
   154 
   263 
   155 /*!
   264 /*!
       
   265     Creates a new action with the given \a icon and \a text and adds the
       
   266     action to the end of the toolbar. The toolbar takes ownership of the new action.
       
   267 
   156     \overload
   268     \overload
   157 
   269     \return The new action.
   158     Creates a new action with the given \a icon and \a text. This
       
   159     action is added to the end of the toolbar. Toolbar retains ownership of the action.
       
   160 */
   270 */
   161 HbAction *HbToolBar::addAction( const HbIcon &icon, const QString &text )
   271 HbAction *HbToolBar::addAction( const HbIcon &icon, const QString &text )
   162 {
   272 {
   163     HbAction *action = new HbAction(icon, text, this);
   273     HbAction *action = new HbAction(icon, text, this);
   164     addAction(action);
   274     addAction(action);
   165     return action;
   275     return action;
   166 }
   276 }
   167 
   277 
   168 /*!
   278 /*!
       
   279     Creates a new action with the given \a text, adds the action to the
       
   280     end of the toolbar, and connects the action's \link HbAction::triggered()
       
   281     triggered()\endlink signal to a receiver object's slot. The toolbar takes ownership
       
   282     of the new action.
       
   283 
   169     \overload
   284     \overload
   170 
   285     \param text The text for the new action.
   171     Creates a new action with the given \a text. This action is added to
   286     \param receiver The object that is to receive the new action's signal.
   172     the end of the toolbar. The action's \link HbAction::triggered()
   287     \param member The slot on the receiver to which the action's signal is to connect.
   173     triggered()\endlink signal is connected to \a member in \a
   288     \return The new action.
   174     receiver. Toolbar retains ownership of the action.
       
   175 */
   289 */
   176 HbAction *HbToolBar::addAction( const QString &text, const QObject *receiver, const char *member )
   290 HbAction *HbToolBar::addAction( const QString &text, const QObject *receiver, const char *member )
   177 {
   291 {
   178     HbAction *action = new HbAction(text, this);
   292     HbAction *action = new HbAction(text, this);
   179     QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
   293     QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
   180     addAction(action);
   294     addAction(action);
   181     return action;
   295     return action;
   182 }
   296 }
   183 
   297 
   184 /*!
   298 /*!
       
   299     Creates a new action with the given \a icon and \a text, adds the action
       
   300     to the end of the toolbar, and connects the action's \link HbAction::triggered()
       
   301     triggered()\endlink signal to a receiver object's slot. The toolbar takes ownership
       
   302     of the new action.
       
   303 
   185     \overload
   304     \overload
   186 
   305     \param icon The image for the new action.
   187     Creates a new action with the icon \a icon and text \a text. This
   306     \param text The text for the new action.
   188     action is added to the end of the toolbar. The action's \link
   307     \param receiver The object that is to receive the new action's signal.
   189     HbAction::triggered() triggered()\endlink signal is connected to \a
   308     \param member The slot on the receiver to which the action's signal is to connect.
   190     member in \a receiver. Toolbar retains ownership of the action.
   309     \return The new action.
   191 */
   310 */
   192 HbAction *HbToolBar::addAction( const HbIcon &icon, const QString &text, const QObject *receiver, const char *member )
   311 HbAction *HbToolBar::addAction( const HbIcon &icon, const QString &text, const QObject *receiver, const char *member )
   193 {
   312 {
   194     HbAction *action = new HbAction(icon, text, this);
   313     HbAction *action = new HbAction(icon, text, this);
   195     QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
   314     QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
   196     addAction(action);
   315     addAction(action);
   197     return action;
   316     return action;
   198 }
   317 }
   199 
   318 
   200 /*!
   319 /*!
   201     This convenience function adds the \a extension as an extension popup
   320     Adds \a extension as a toolbar extension at the end of the toolbar.
   202     to the toolbar. Returns the HbAction triggering the \a extension.
   321 
       
   322     \return The action that opens the new extension.
       
   323 
       
   324     \sa insertExtension()
   203 */
   325 */
   204 HbAction *HbToolBar::addExtension( HbToolBarExtension *extension )
   326 HbAction *HbToolBar::addExtension( HbToolBarExtension *extension )
   205 {
   327 {
   206     return insertExtension(0, extension);
   328     return insertExtension(0, extension);
   207 }
   329 }
   208 
   330 
   209 /*!
   331 /*!
   210     This convenience function inserts the \a extension as an extension popup
   332     Inserts \a extension as a toolbar extension in front of the \a before action, provided
   211     before the action \a before.
   333     it is a valid toolbar action. If \a before is 0 or is not valid, this function adds
   212 
   334     the extension to the end of the toolbar.
   213     It appends the action if \a before is 0 or \a before is not a valid
   335 
   214     action for this widget.
   336     \return The action that opens the new extension.
   215 
   337 
   216     Returns the HbAction triggering the \a extension.
   338     \sa addExtension()
   217 */
   339 */
   218 HbAction *HbToolBar::insertExtension( HbAction *before, HbToolBarExtension *extension )
   340 HbAction *HbToolBar::insertExtension( HbAction *before, HbToolBarExtension *extension )
   219 {    
   341 {    
   220     Q_D(HbToolBar);
   342     Q_D(HbToolBar);
   221 
   343 
   223 
   345 
   224     insertAction(before, extension->extensionAction());
   346     insertAction(before, extension->extensionAction());
   225     return extension->extensionAction();
   347     return extension->extensionAction();
   226 }
   348 }
   227 
   349 
   228 /*! @beta
   350 /*!
   229     \brief  Returns the orientation of the tool bar.
   351     Returns the orientation of the toolbar.
   230 
   352 
   231     \sa setOrientation
   353     @beta
   232  */
   354 
       
   355     \sa setOrientation()
       
   356 */
   233 Qt::Orientation HbToolBar::orientation() const
   357 Qt::Orientation HbToolBar::orientation() const
   234 {
   358 {
   235     Q_D(const HbToolBar);
   359     Q_D(const HbToolBar);
   236     return d->mOrientation;
   360     return d->mOrientation;
   237 }
   361 }
   238 
   362 
   239 /*! @beta
   363 /*!
   240     Sets the \a orientation of the tool bar.
   364     Sets the orientation of the toolbar.
   241 
   365 
   242     \sa orientation
   366     @beta
   243  */
   367 
       
   368     \b Example:
       
   369 
       
   370     \code
       
   371     HbToolBar *toolBar = new HbToolBar();
       
   372     toolBar->setOrientation(Qt::Vertical);
       
   373     \endcode
       
   374 
       
   375     \sa orientation()
       
   376 */
   244 void HbToolBar::setOrientation( Qt::Orientation orientation )
   377 void HbToolBar::setOrientation( Qt::Orientation orientation )
   245 {
   378 {
   246     Q_D(HbToolBar);
   379     Q_D(HbToolBar);
   247 
   380 
   248     d->setOrientation ( orientation );
   381     d->setOrientation ( orientation );
   249     d->minimumToolButtonSize = QSizeF();
   382     d->minimumToolButtonSize = QSizeF();
   250 }
   383 }
   251 
   384 
   252 /*!
   385 /*!
   253     Emits areaChanged() whenever the tool bar's visibility or position changes.
   386     \reimp
   254 */
   387  */
   255 QVariant HbToolBar::itemChange( GraphicsItemChange change, const QVariant &value )
   388 QVariant HbToolBar::itemChange( GraphicsItemChange change, const QVariant &value )
   256 {
   389 {
   257     Q_D(HbToolBar);
   390     Q_D(HbToolBar);
   258     QVariant result = HbWidget::itemChange(change, value);
   391     QVariant result = HbWidget::itemChange(change, value);
   259 
   392 
   266         break;
   399         break;
   267     case ItemVisibleChange:
   400     case ItemVisibleChange:
   268         if (d->mOrientationEffectsRunning)
   401         if (d->mOrientationEffectsRunning)
   269             return result;
   402             return result;
   270         if (value.toBool()) {
   403         if (value.toBool()) {
   271             if (d->mDoLayout && d->mDoLayoutPending) {
   404             if (d->mDoLayoutPending && d->polished) {
   272                 d->doLayout();
   405                 d->doLayout();
   273             }
   406             }
   274             if (!d->mDialogToolBar) {
   407             if (!d->mDialogToolBar) {
   275                 d->doLazyInit();
   408                 d->doLazyInit();
   276                 d->delayedStartEffects = d->mDoLayoutPending && !d->mSuppressNextAppearEffect;
   409                 d->delayedStartEffects = d->mDoLayoutPending && !d->mSuppressNextAppearEffect;
   277                 if (!d->delayedStartEffects && d->hasEffects && !d->mSuppressNextAppearEffect) {
   410                 if (!d->delayedStartEffects && d->hasEffects && !d->mSuppressNextAppearEffect) {
   278                     d->startAppearEffect();
   411                     d->startAppearEffect();
   279                 }
   412                 }
   280                 d->mSuppressNextAppearEffect = false;
       
   281                 d->delayedHide = d->hasEffects;
   413                 d->delayedHide = d->hasEffects;
   282             }
   414             }
   283         } else {
   415         } else {
       
   416             d->mSuppressNextAppearEffect = false;
   284             if(d->moreExtension && d->moreExtension->isVisible()){
   417             if(d->moreExtension && d->moreExtension->isVisible()){
   285                d->moreExtension->setVisible(false);
   418                d->moreExtension->setVisible(false);
   286             }
   419             }
   287             bool hideDelayed = d->delayedHide;
   420             bool hideDelayed = d->delayedHide;
   288             if (d->delayedHide && d->hasEffects) { // about to hide and we wanna delay hiding
   421             if (d->delayedHide && d->hasEffects) { // about to hide and we wanna delay hiding
   321     }
   454     }
   322     return result;
   455     return result;
   323 }
   456 }
   324 
   457 
   325 /*!
   458 /*!
   326     Reimplemented from QGraphicsWidget::changeEvent().
   459     \reimp
   327  */
   460  */
   328 void HbToolBar::changeEvent( QEvent *event )
   461 void HbToolBar::changeEvent( QEvent *event )
   329 {
   462 {
   330     Q_D(HbToolBar);
   463     Q_D(HbToolBar);
   331     if (event->type() == QEvent::LayoutDirectionChange) {
   464     if (event->type() == QEvent::LayoutDirectionChange) {
   335 
   468 
   336     QGraphicsWidget::changeEvent(event);
   469     QGraphicsWidget::changeEvent(event);
   337 }
   470 }
   338 
   471 
   339 /*!
   472 /*!
   340     Reimplemented from QGraphicsWidget::resizeEvent().
   473     \reimp
   341  */
   474  */
   342 void HbToolBar::resizeEvent(QGraphicsSceneResizeEvent *event)
   475 void HbToolBar::resizeEvent(QGraphicsSceneResizeEvent *event)
   343 {
   476 {
   344     Q_D(HbToolBar);
   477     Q_D(HbToolBar);
   345     HbWidget::resizeEvent(event);
   478     HbWidget::resizeEvent(event);
   346     if (isVisible() && d->mDoLayout) {
   479     if (isVisible()) {
   347         d->updateToolBarForSizeChange();
   480         d->updateToolBarForSizeChange();
   348     }
   481     }
   349 }
   482 }
   350 
   483 
   351 /*!
   484 /*!
   354 void HbToolBar::hideEvent(QHideEvent *event)
   487 void HbToolBar::hideEvent(QHideEvent *event)
   355 {
   488 {
   356     HbWidget::hideEvent(event);
   489     HbWidget::hideEvent(event);
   357 }
   490 }
   358 
   491 
   359 /*!
       
   360   \reimp
       
   361   */
       
   362 /*!
   492 /*!
   363     \reimp
   493     \reimp
   364  */
   494  */
   365 bool HbToolBar::event(QEvent *event)
   495 bool HbToolBar::event(QEvent *event)
   366 {
   496 {
   392 void HbToolBar::gestureEvent(QGestureEvent *)
   522 void HbToolBar::gestureEvent(QGestureEvent *)
   393 {
   523 {
   394 
   524 
   395 }
   525 }
   396 
   526 
       
   527 /*!
       
   528     \reimp
       
   529  */
   397 void HbToolBar::updatePrimitives()
   530 void HbToolBar::updatePrimitives()
   398 {
   531 {
   399     Q_D(HbToolBar);
   532     Q_D(HbToolBar);
   400     for (int i = 0; i < d->mVisibleToolButtons.count(); i++) {
   533     for (int i = 0; i < d->mVisibleToolButtons.count(); i++) {
   401         d->mVisibleToolButtons.at(i)->updatePrimitives();
   534         d->mVisibleToolButtons.at(i)->updatePrimitives();
   404         d->moreExtensionButton->updatePrimitives();
   537         d->moreExtensionButton->updatePrimitives();
   405     }
   538     }
   406 }
   539 }
   407 
   540 
   408 /*!
   541 /*!
   409   \reimp
   542     \reimp
   410   */
   543  */
   411 void HbToolBar::polish(HbStyleParameters &params)
   544 void HbToolBar::polish(HbStyleParameters &params)
   412 {
   545 {
   413     Q_D(HbToolBar);
   546     Q_D(HbToolBar);
       
   547     if (d->mDoLayoutPending && isVisible()) {
       
   548         d->doLayout();
       
   549     }
   414     HbWidget::polish(params);
   550     HbWidget::polish(params);
   415     if (d->mDoLayoutPending) {
       
   416         d->doLayout();
       
   417     }
       
   418 }
   551 }
   419 
   552 
   420 #include "moc_hbtoolbar.cpp"
   553 #include "moc_hbtoolbar.cpp"