src/hbcore/gui/hbtoolbarextension.cpp
changeset 21 4633027730f5
parent 7 923ff622b8b9
child 23 e6ad4ef83b23
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    30 #include "hbtoolbutton_p.h"
    30 #include "hbtoolbutton_p.h"
    31 #include "hbdialog_p.h"
    31 #include "hbdialog_p.h"
    32 #include "hbdeviceprofile.h"
    32 #include "hbdeviceprofile.h"
    33 #include "hbtoolbar_p.h"
    33 #include "hbtoolbar_p.h"
    34 #include "hbmainwindow.h"
    34 #include "hbmainwindow.h"
       
    35 #include "hbstyle_p.h"
    35 #ifdef HB_EFFECTS
    36 #ifdef HB_EFFECTS
    36 #include <hbeffect.h>
    37 #include <hbeffect.h>
    37 #include <hbeffectinternal_p.h>
    38 #include <hbeffectinternal_p.h>
    38 bool HbToolBarExtensionPrivate::extensionEffectsLoaded = false;
    39 bool HbToolBarExtensionPrivate::extensionEffectsLoaded = false;
    39 #endif
    40 #endif
    40 
    41 
    41 #include <QDebug>
    42 #include <QDebug>
    42 #include <QGraphicsGridLayout>
    43 #include <QGraphicsGridLayout>
    43 #include <QEventLoop>
    44 #include <QEventLoop>
    44 #include <QGraphicsLinearLayout> 
    45 #include <QGraphicsLinearLayout>
    45 
    46 	
    46 /*!
    47 /*!
    47     @stable
    48   @stable
    48     @hbcore
    49   @hbcore
    49     \class HbToolBarExtension
    50   \class HbToolBarExtension
    50 
    51   \brief The HbToolBarExtension class provides a popup extension to a toolbar.
    51     \brief HbToolBarExtension is a popup style component that adds
    52 	
    52     extra functionality to an HbToolBar. A toolbar can contain more
    53   You can use a toolbar extension to extend the main toolbar
    53     than one toolbar extension.  An extension popup is opened when a
    54   (HbToolBar class) in a view with additional actions that are placed
    54     toolbar button with associated with the extension is triggered.
    55   in a subsidiary toolbar. Alternatively, a toolbar extension can
    55 
    56   contain a widget, such as a list or grid. This is useful, for
    56     A toolbar extension uses the QGraphicsWidget action API to manage
    57   example, for providing navigation between views when there are more
    57     buttons. In addition the HbDialog API can be used to fill the
    58   views than can fit as actions on the toolbar itself.
    58     extension popup with custom widgets (e.g.  list or line edit). If
    59 	
    59     custom content widget is set, buttons generated based on actions
    60   A toolbar can contain more than one toolbar extension. A toolbar
    60     will not be visible.
    61   extension opens when the user triggers the toolbar action that is
    61 
    62   associated with the extension, usually by tapping it. The user
    62     An example of how to add a toolbar extension button to the toolbar:
    63   dismisses the toolbar extension by selecting an option (which runs a
    63     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,27}
    64   command) or by tapping outside the extension.
    64 */
    65 	
    65 
    66   The following image shows a toolbar that has two extensions: the
    66 /*!
    67   leftmost one contains a list widget and the rightmost one contains
    67     \reimp
    68   three standard actions.
    68     \fn int HbToolBarExtension::type() const
    69 	
    69  */
    70   \image html toolbarextension.png A toolbar that has two extensions
    70 
    71 	
    71 /*!
    72   Use addAction() to create an action and add it to the toolbar
       
    73   extension. There are several overloads of this function, which allow
       
    74   you to specify both a text and image or just a text and also to
       
    75   connect the action's \link HbAction::triggered() triggered()\endlink
       
    76   signal to a slot on a receiver object. Use the insertAction(),
       
    77   addActions() and insertActions() methods (which are inherited from
       
    78   QGraphicsWidget) to add existing actions to the toolbar
       
    79   extension. Use clearActions() to clear all of the actions and
       
    80   removeAction() to remove individual actions.
       
    81 	
       
    82   The order of the actions within the toolbar extension controls the
       
    83   order of the buttons that the user sees. addAction() and
       
    84   addActions() append the actions to the end of the toolbar and
       
    85   insertAction() and insertActions() enable you to specify the
       
    86   required position.
       
    87 	
       
    88   You can use the HbDialog API to fill the toolbar extension popup
       
    89   with widgets (such as a list, grid or line edit). If you do this,
       
    90   any actions that you add to the toolbar extension will not be
       
    91   visible.
       
    92 	
       
    93   \section _usecases_hbtoolbarextension Using the HbToolBarExtension class
       
    94 	
       
    95   \subsection _uc_001_hbtoolbarextension Creating a toolbar extension containing actions
       
    96   The following example demonstrates how to add a toolbar extension button to the toolbar
       
    97   and how to add actions to the toolbar extension.
       
    98   \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,27}
       
    99 	
       
   100   \subsection _uc_002_hbtoolbarextension Creating a toolbar extension containing a widget
       
   101 	
       
   102   The following example demonstrates creating a toolbar extension containing a single-selection
       
   103   list widget.
       
   104 	
       
   105   \code
       
   106   // Create the toolbar.
       
   107   HbToolBar *toolBar = new HbToolBar();
       
   108 	
       
   109   // Add the action that will open the toolbar extension.
       
   110   HbAction *radioAction = toolBar->addAction("Channel");
       
   111  	
       
   112   // Create the toolbar extension.
       
   113   HbToolBarExtension *radioExtension = new HbToolBarExtension();
       
   114  	
       
   115   // Set the heading.
       
   116   HbLabel* heading = new HbLabel(QString("Channel"));
       
   117   radioExtension->setHeadingWidget(heading);
       
   118  	
       
   119   // Create a list widget.
       
   120   HbListWidget *list = new HbListWidget();
       
   121   list->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
       
   122  	
       
   123   // Make the list single selection.
       
   124   list->setSelectionMode(HbAbstractItemView::SingleSelection);
       
   125  	
       
   126   // Add list items.
       
   127   list->addItem("574.7");
       
   128   list->addItem("976.5");
       
   129   list->addItem("108.67");
       
   130  	
       
   131   // Add the list widget to the toolbar extension object.
       
   132   radioExtension->setContentWidget(list);
       
   133  	
       
   134   // Add the toolbar extension to the toolbar action that will open it.
       
   135   radioAction->setToolBarExtension(radioExtension);
       
   136   \endcode
       
   137 */
       
   138  	
       
   139 /*!
       
   140   \fn int HbToolBarExtension::type() const
       
   141 */
       
   142  	
       
   143 /*
    72   \primitives
   144   \primitives
    73   \primitive{background} HbFrameItem representing the extension background.
   145   \primitive{background} HbFrameItem representing the extension background.
    74   */
   146 */
    75 
   147  	
    76 HbToolBarExtensionPrivate::HbToolBarExtensionPrivate() :
   148 HbToolBarExtensionPrivate::HbToolBarExtensionPrivate() :
    77         HbDialogPrivate(),
   149     HbDialogPrivate(),
    78         mToolButtons(),
   150     mToolButtons(),
    79         mLayout(0),
   151     mLayout(0),
    80         extensionAction(0),
   152     extensionAction(0),
    81         mAlignment(Qt::AlignTop),
   153     mAlignment(Qt::AlignTop),
    82         mDefaultContentWidget(false),
   154     mDefaultContentWidget(false),
    83         // default values, in case CSS parsing fails
   155     // default values, in case CSS parsing fails
    84         mMargins(0),
   156     mMargins(0),
    85         mRowsPortrait(4),
   157     mRowsPortrait(4),
    86         mRowsLandscape(3),
   158     mRowsLandscape(3),
    87         mColsPortrait(3),
   159     mColsPortrait(3),
    88         mColsLandscape(4),
   160     mColsLandscape(4),
    89         lazyInitDone(false),
   161     lazyInitDone(false),
    90         orientationConnectDone(false),
   162     orientationConnectDone(false),
    91         //
   163     //
    92         mExtendedButton(0),
   164     mExtendedButton(0),
    93         mToolBar(0)
   165     mToolBar(0)
    94 {
   166 {
    95 }
   167 }
    96 
   168 
    97 HbToolBarExtensionPrivate::~HbToolBarExtensionPrivate()
   169 HbToolBarExtensionPrivate::~HbToolBarExtensionPrivate()
    98 {
   170 {
   110 }
   182 }
   111 
   183 
   112 void HbToolBarExtensionPrivate::doLazyInit()
   184 void HbToolBarExtensionPrivate::doLazyInit()
   113 {
   185 {
   114     if ( !lazyInitDone ) {
   186     if ( !lazyInitDone ) {
   115         setBackgroundItem(HbStyle::P_ToolBarExtension_background);
   187         setBackgroundItem(HbStylePrivate::P_ToolBarExtension_background);
   116 #ifdef HB_EFFECTS
   188 #ifdef HB_EFFECTS
   117         if (!extensionEffectsLoaded){
   189         if (!extensionEffectsLoaded){
   118             HbEffectInternal::add("HB_TBE", "tbe_button_click", "clicked");
   190             HbEffectInternal::add("HB_TBE", "tbe_button_click", "clicked");
   119             extensionEffectsLoaded = true;
   191             extensionEffectsLoaded = true;
   120         }
   192         }
   223         QObject::connect(event->action(), SIGNAL(changed()), button, SLOT(_q_actionChanged()));
   295         QObject::connect(event->action(), SIGNAL(changed()), button, SLOT(_q_actionChanged()));
   224     }
   296     }
   225 
   297 
   226     if ((hbAction && !hbAction->icon().isNull()) || !event->action()->icon().isNull()) {
   298     if ((hbAction && !hbAction->icon().isNull()) || !event->action()->icon().isNull()) {
   227         if (HbToolButtonPrivate::d_ptr(button)->action->text().isEmpty()) {
   299         if (HbToolButtonPrivate::d_ptr(button)->action->text().isEmpty()) {
   228             button->setToolButtonStyle(HbToolButton::ToolButtonIcon);
   300             button->setProperty(BUTTONSTYLE, HbToolButtonPrivate::ToolButtonIcon);
   229         } else {
   301         } else {
   230             button->setToolButtonStyle(HbToolButton::ToolButtonTextAndIcon);
   302             button->setProperty(BUTTONSTYLE, HbToolButtonPrivate::ToolButtonTextAndIcon);
   231         }
   303         }
   232     } else {
   304     } else {
   233         button->setToolButtonStyle(HbToolButton::ToolButtonText);
   305         button->setProperty(BUTTONSTYLE, HbToolButtonPrivate::ToolButtonText);
   234     }
   306     }
   235 
   307 
   236     button->setProperty("toolbutton_extension_layout", true);
   308     button->setProperty("toolbutton_extension_layout", true);
   237     button->setSizePolicy( QSizePolicy( QSizePolicy::Preferred,
   309     button->setSizePolicy( QSizePolicy( QSizePolicy::Preferred,
   238                                         QSizePolicy::Preferred) );
   310                                         QSizePolicy::Preferred) );
   338     Destructor.
   410     Destructor.
   339  */
   411  */
   340 HbToolBarExtension::~HbToolBarExtension()
   412 HbToolBarExtension::~HbToolBarExtension()
   341 {
   413 {
   342 }
   414 }
   343 
   415  	
   344 /*!
   416  	
   345     \overload
   417 /*!  
   346 
   418   Creates a new action with the given \a text and adds the action
   347     Creates a new action with the given \a text. 
   419   to the end of the toolbar extension, provided space is
   348     This action is added to the end of the toolbar extension.
   420   available. The space available in a toolbar extension depends on the
   349     TODO: If the grid is already full, this call will be ignored.
   421   screen size and orientation. When there is no free space, this
   350     TODO: Find a way to notificate the caller.
   422   function does nothing. There is currently no notification when there
       
   423   is no free space.
       
   424  	
       
   425   \overload
       
   426   \return The new action.
   351 */
   427 */
   352 HbAction *HbToolBarExtension::addAction( const QString &text )
   428 HbAction *HbToolBarExtension::addAction( const QString &text )
   353 {
   429 {
   354     HbAction *action = new HbAction( text, this );
   430     HbAction *action = new HbAction( text, this );
   355     addAction(action);
   431     addAction(action);
   356     return action;
   432     return action;
   357 }
   433 }
   358 
   434  	
   359 /*!
   435  	
   360     \overload
   436 /*!  
   361 
   437   Creates a new action with the given \a icon and \a text and adds
   362     Creates a new action with the given \a icon and \a text.
   438   the action to the end of the toolbar extension, provided space is
   363     This action is added to the end of the toolbar extension.
   439   available. The space available in a toolbar extension depends on the
   364     TODO: If the grid is already full, this call will be ignored.
   440   screen size and orientation. When there is no free space, this
   365     TODO: Find a way to notificate the caller.
   441   function does nothing. There is currently no notification when there
       
   442   is no free space.
       
   443  	
       
   444   \overload
       
   445   \return The new action.
   366 */
   446 */
   367 HbAction *HbToolBarExtension::addAction( const HbIcon &icon, 
   447 HbAction *HbToolBarExtension::addAction( const HbIcon &icon, 
   368                                          const QString &text )
   448                                          const QString &text )
   369 {
   449 {
   370     HbAction *action = new HbAction( icon, text, this );
   450     HbAction *action = new HbAction( icon, text, this );
   371     addAction(action);
   451     addAction(action);
   372     return action;
   452     return action;
   373 }
   453 }
   374 
   454  	
   375 /*!
   455  	
   376     \overload
   456 /*!  
   377 
   457   Creates a new action with the given \a text, adds the action to
   378     Creates a new action with the given \a text. 
   458   the end of the toolbar extension (provided space is available), and
   379     This action is added to the end of the toolbar extension.
   459   connects the action's \link HbAction::triggered()
   380     TODO: If the grid is already full, this call will be ignored.
   460   triggered()\endlink signal to a receiver object's slot.
   381     The action's \link HbAction::triggered()
   461  	
   382     triggered()\endlink signal is connected to \a member in \a
   462   The space available in a toolbar extension depends on the screen
   383     receiver.
   463   size and orientation.  When there is no free space, this function
   384     TODO: Find a way to notificate the caller.
   464   does not add the action to the toolbar extension. There is currently
   385 */
   465   no notification when there is no free space.
   386 HbAction *HbToolBarExtension::addAction( const QString &text, 
   466  	
   387                                          const QObject *receiver, 
   467   \overload
       
   468   \param text The text for the new action.
       
   469   \param receiver The object that is to receive the new action's signal.
       
   470   \param member The slot on the receiver to which the action's signal is to connect.
       
   471   \return The new action.
       
   472 */
       
   473 HbAction *HbToolBarExtension::addAction( const QString &text,
       
   474                                          const QObject *receiver,
   388                                          const char *member )
   475                                          const char *member )
   389 {
   476 {
   390     HbAction *action = new HbAction( text, this );
   477     HbAction *action = new HbAction( text, this );
   391     QObject::connect( action, SIGNAL( triggered(bool) ), receiver, member );
   478     QObject::connect( action, SIGNAL( triggered(bool) ), receiver, member );
   392     addAction(action);
   479     addAction(action);
   393     return action;
   480     return action;
   394 }
   481 }
   395 
   482  	
   396 /*!
   483 /*!  
   397     \overload
   484   Creates a new action with the given \a icon and \a text, adds the
   398 
   485   action to the end of the toolbar extension (provided space is
   399     Creates a new action with the given  \a icon and \a text. 
   486   available), and connects the action's \link HbAction::triggered()
   400     This action is added to the end of the toolbar extension.
   487   triggered()\endlink signal to a receiver object's slot.
   401     TODO: If the grid is already full, this call will be ignored.
   488  	
   402     The action's \link HbAction::triggered()
   489   The space available in a toolbar extension depends on the screen
   403     triggered()\endlink signal is connected to \a member in \a
   490   size and orientation.  When there is no free space, this function
   404     receiver.
   491   does not add the action to the toolbar extension. There is currently
   405     TODO: Find a way to notificate the caller.
   492   no notification when there is no free space.
       
   493  	
       
   494   \overload
       
   495   \param icon The image for the new action.
       
   496   \param text The text for the new action.
       
   497   \param receiver The object that is to receive the new action's signal.
       
   498   \param member The slot on the receiver to which the action's signal is to connect.
       
   499   \return The new action.
   406 */
   500 */
   407 HbAction *HbToolBarExtension::addAction( const HbIcon &icon, 
   501 HbAction *HbToolBarExtension::addAction( const HbIcon &icon, 
   408                                          const QString &text, 
   502                                          const QString &text, 
   409                                          const QObject *receiver, 
   503                                          const QObject *receiver, 
   410                                          const char *member )
   504                                          const char *member )
   412     HbAction *action = new HbAction( icon, text, this );
   506     HbAction *action = new HbAction( icon, text, this );
   413     QObject::connect( action, SIGNAL( triggered(bool) ), receiver, member );
   507     QObject::connect( action, SIGNAL( triggered(bool) ), receiver, member );
   414     addAction(action);
   508     addAction(action);
   415     return action;
   509     return action;
   416 }
   510 }
   417 
   511  	
   418 /*!
   512 /*!  
   419     Returns the action associated with this extension.
   513   Returns the action associated with this toolbar extension. This
   420  */
   514   is the action in the toolbar to which this toolbar extension belongs
       
   515   that opens this toolbar extension when triggered.
       
   516 */
   421 HbAction *HbToolBarExtension::extensionAction() const
   517 HbAction *HbToolBarExtension::extensionAction() const
   422 {
   518 {
   423     Q_D( const HbToolBarExtension );
   519     Q_D( const HbToolBarExtension );
   424     return d->extensionAction;
   520     return d->extensionAction;
   425 }
   521 }
   431         HbDialog( dd, parent )
   527         HbDialog( dd, parent )
   432 {
   528 {
   433 }
   529 }
   434 
   530 
   435 /*!
   531 /*!
   436     \reimp
   532  	
   437  */
   533  */
   438 bool HbToolBarExtension::event( QEvent *event )
   534 bool HbToolBarExtension::event( QEvent *event )
   439 {
   535 {
   440     Q_D( HbToolBarExtension );    
   536     Q_D( HbToolBarExtension );    
   441     if ( event->type() == QEvent::ActionAdded ) {
   537     if ( event->type() == QEvent::ActionAdded ) {
   453     }
   549     }
   454     return HbDialog::event(event);
   550     return HbDialog::event(event);
   455 }
   551 }
   456 
   552 
   457 /*!
   553 /*!
   458   \reimp
   554  	
   459   */
   555  */
   460 void HbToolBarExtension::polish( HbStyleParameters &params )
   556 void HbToolBarExtension::polish( HbStyleParameters &params )
   461 {    
   557 {
   462     Q_D(HbToolBarExtension);  
   558     if (isVisible()) {
   463     d->doLazyInit();
   559         Q_D(HbToolBarExtension);
   464     const QString Margins       = "content-margins";
   560         d->doLazyInit();
   465     const QString RowsPortrait  = "max-rows-portrait";
   561         const QLatin1String Margins("content-margins");
   466     const QString RowsLandscape = "max-rows-landscape";
   562         const QLatin1String RowsPortrait("max-rows-portrait");
   467     const QString ColsPortrait  = "max-columns-portrait";
   563         const QLatin1String RowsLandscape("max-rows-landscape");
   468     const QString ColsLandscape = "max-columns-landscape";
   564         const QLatin1String ColsPortrait("max-columns-portrait");
   469 
   565         const QLatin1String ColsLandscape("max-columns-landscape");
   470     params.addParameter( Margins );
   566 
   471     params.addParameter( RowsPortrait );
   567         params.addParameter( Margins );
   472     params.addParameter( RowsLandscape );
   568         params.addParameter( RowsPortrait );
   473     params.addParameter( ColsPortrait );
   569         params.addParameter( RowsLandscape );
   474     params.addParameter( ColsLandscape );
   570         params.addParameter( ColsPortrait );
   475     d->initialiseContent();
   571         params.addParameter( ColsLandscape );
   476     if (d->mDefaultContentWidget) {       
   572         d->initialiseContent();
   477         HbDialog::polish(params);
   573         if (d->mDefaultContentWidget) {
   478         if ( params.value( Margins ).isValid() 
   574             HbDialog::polish(params);
   479              && params.value( RowsPortrait ).isValid() 
   575             if ( params.value( Margins ).isValid()
   480              && params.value( RowsLandscape ).isValid() 
   576                 && params.value( RowsPortrait ).isValid()
   481              && params.value( ColsPortrait ).isValid() 
   577                 && params.value( RowsLandscape ).isValid()
   482              && params.value( ColsLandscape ).isValid() ) {
   578                 && params.value( ColsPortrait ).isValid()
   483             d->mMargins = params.value( Margins ).toReal();
   579                 && params.value( ColsLandscape ).isValid() ) {
   484             d->mRowsPortrait  = params.value( RowsPortrait ).toInt();
   580                 d->mMargins = params.value( Margins ).toReal();
   485             d->mRowsLandscape = params.value( RowsLandscape ).toInt();
   581                 d->mRowsPortrait  = params.value( RowsPortrait ).toInt();
   486             d->mColsPortrait  = params.value( ColsPortrait ).toInt();
   582                 d->mRowsLandscape = params.value( RowsLandscape ).toInt();
   487             d->mColsLandscape = params.value( ColsLandscape ).toInt();
   583                 d->mColsPortrait  = params.value( ColsPortrait ).toInt();
   488             d->doLayout();
   584                 d->mColsLandscape = params.value( ColsLandscape ).toInt();
   489         }
   585                 d->doLayout();
   490     } else {
   586             }
   491         HbDialog::polish(params);
   587             return;
   492     }
   588         }
       
   589     }
       
   590     HbDialog::polish(params);
   493 }
   591 }
   494 
   592 
   495 QVariant HbToolBarExtension::itemChange(GraphicsItemChange change, 
   593 QVariant HbToolBarExtension::itemChange(GraphicsItemChange change, 
   496                                         const QVariant &value)
   594                                         const QVariant &value)
   497 {
   595 {