src/hbcore/gui/hbtoolbutton.cpp
changeset 21 4633027730f5
parent 7 923ff622b8b9
child 23 e6ad4ef83b23
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    29 #include "hbstyleoptiontoolbutton_p.h"
    29 #include "hbstyleoptiontoolbutton_p.h"
    30 #include "hbtoolbarextension.h"
    30 #include "hbtoolbarextension.h"
    31 #include "hbtoolbarextension_p.h"
    31 #include "hbtoolbarextension_p.h"
    32 #include "hbaction.h"
    32 #include "hbaction.h"
    33 #include "hbaction_p.h"
    33 #include "hbaction_p.h"
       
    34 #include "hbstyle_p.h"
    34 #include <hbglobal.h>
    35 #include <hbglobal.h>
    35 #include "hbcolorscheme.h"
    36 #include "hbcolorscheme.h"
    36 #include "hbtextitem.h"
    37 #include "hbtextitem.h"
    37 #include "hbiconitem.h"
    38 #include "hbiconitem.h"
    38 #include "hbview.h"
    39 #include "hbview.h"
    62     Tool buttons are normally created indirectly when actions are added to a
    63     Tool buttons are normally created indirectly when actions are added to a
    63     toolbar with HbToolBar::addAction(). It is also possible to
    64     toolbar with HbToolBar::addAction(). It is also possible to
    64     construct tool buttons directly in the same way as any other widget, and
    65     construct tool buttons directly in the same way as any other widget, and
    65     arrange them alongside other widgets in layouts.
    66     arrange them alongside other widgets in layouts.
    66 
    67 
    67     The style of a tool button is adjustable with setToolButtonStyle().
       
    68     By default a tool button shows only an icon.
       
    69 
       
    70     A tool button's background is set as HbIcon. This makes it possible to
    68     A tool button's background is set as HbIcon. This makes it possible to
    71     specify different images for the normal and pressed states.
    69     specify different images for the normal and pressed states.
    72 
    70 
    73     Example usage:
    71     Example usage:
    74     \code
    72     \code
    77     layout->addItem(toolButton);
    75     layout->addItem(toolButton);
    78     \endcode
    76     \endcode
    79 
    77 
    80     \sa HbAction, HbPushButton
    78     \sa HbAction, HbPushButton
    81 */
    79 */
    82 
       
    83 /*!
       
    84     \enum HbToolButton::ToolButtonStyle
       
    85     \deprecated HbToolButton::ToolButtonStyle
       
    86 
       
    87     This enum defines available tool button styles.
       
    88 
       
    89     The tool button style describes how the button's text and icon should be displayed.
       
    90  */
       
    91 
       
    92 /*!
       
    93     \var HbToolButton::ToolButtonIcon
       
    94     \deprecated HbToolButton::ToolButtonIcon
       
    95 
       
    96     Only display the icon.
       
    97  */
       
    98 
       
    99 /*!
       
   100     \var HbToolButton::ToolButtonText
       
   101     \deprecated HbToolButton::ToolButtonText
       
   102 
       
   103     Only display the text.
       
   104  */
       
   105 
       
   106 /*!
       
   107     \var HbToolButton::ToolButtonTextAndIcon
       
   108     \deprecated HbToolButton::ToolButtonTextAndIcon
       
   109 
       
   110     Display both text and icon.
       
   111  */
       
   112 
    80 
   113 /*!
    81 /*!
   114     \fn void HbToolButton::triggered(HbAction *action)
    82     \fn void HbToolButton::triggered(HbAction *action)
   115 
    83 
   116     This signal is emitted when the \a action is triggered.
    84     This signal is emitted when the \a action is triggered.
   133     textItem(0),
   101     textItem(0),
   134     iconItem(0),
   102     iconItem(0),
   135     frameItem(0),
   103     frameItem(0),
   136     customBackground(),
   104     customBackground(),
   137     backgroundVisible(true),
   105     backgroundVisible(true),
   138     buttonStyle(HbToolButton::ToolButtonIcon),
       
   139     toolBarPosition(HbStyleOptionToolButton::TB_None),
   106     toolBarPosition(HbStyleOptionToolButton::TB_None),
   140     orientation(Qt::Vertical),
   107     orientation(Qt::Vertical),
   141     mDialogToolBar(false),
   108     mDialogToolBar(false),
   142     toolbarExtensionFrame(false),
   109     toolbarExtensionFrame(false),
   143     mButtonSize(QSizeF())
   110     mButtonSize(QSizeF())
   151 void HbToolButtonPrivate::createPrimitives()
   118 void HbToolButtonPrivate::createPrimitives()
   152 {
   119 {
   153     Q_Q(HbToolButton);
   120     Q_Q(HbToolButton);
   154     if (backgroundVisible) {
   121     if (backgroundVisible) {
   155         if (!frameItem){
   122         if (!frameItem){
   156             frameItem = q->style()->createPrimitive(HbStyle::P_ToolButton_frame, q);
   123             frameItem = HbStylePrivate::createPrimitive(HbStylePrivate::P_ToolButton_frame, q);
   157         }
   124         }
   158     } else if (frameItem) {
   125     } else if (frameItem) {
   159         delete frameItem;
   126         delete frameItem;
   160         frameItem = 0;
   127         frameItem = 0;
   161     }
   128     }
   162 
   129     if (!textItem) {
   163     if (action && !action->text().isEmpty()) {
   130             textItem = static_cast<HbTextItem *>(HbStylePrivate::createPrimitive(HbStylePrivate::P_ToolButton_text, q));            
   164         if (!textItem) {
   131         textItem->setTextWrapping(Hb::TextWordWrap);
   165             textItem = static_cast<HbTextItem *>(q->style()->createPrimitive(HbStyle::P_ToolButton_text, q));            
   132     } // I dare you to optimize this
   166             textItem->setTextWrapping(Hb::TextWordWrap);
   133     if (!iconItem) {
   167         }
   134             iconItem = HbStylePrivate::createPrimitive(HbStylePrivate::P_ToolButton_icon, q);
   168         textItem->setVisible(buttonStyle & HbToolButton::ToolButtonText);
       
   169     } else if (textItem) {
       
   170         delete textItem;
       
   171         textItem = 0;
       
   172     }
       
   173 
       
   174     if (action && (buttonStyle & HbToolButton::ToolButtonIcon)) {
       
   175         if (!iconItem) {
       
   176             iconItem = q->style()->createPrimitive(HbStyle::P_ToolButton_icon, q);
       
   177         }
       
   178     } else if (iconItem){
       
   179         delete iconItem;
       
   180         iconItem = 0;
       
   181     }
   135     }
   182 }
   136 }
   183 
   137 
   184 void HbToolButtonPrivate::setOrientation(Qt::Orientation orientation)
   138 void HbToolButtonPrivate::setOrientation(Qt::Orientation orientation)
   185 {
   139 {
   186     if (this->orientation != orientation) {
   140     if (this->orientation != orientation) {
   187         this->orientation = orientation;
   141         this->orientation = orientation;
   188         Q_Q(HbToolButton);        
   142         Q_Q(HbToolButton);
   189         if (q->isVisible() && polished) {
   143         q->repolish();
   190             q->repolish();
       
   191         }
       
   192     }    
   144     }    
   193 }
   145 }
   194 
   146 
   195 void HbToolButtonPrivate::setToolBarPosition(HbStyleOptionToolButton::ToolBarPosition position)
   147 void HbToolButtonPrivate::setToolBarPosition(HbStyleOptionToolButton::ToolBarPosition position)
   196 {
   148 {
   231 
   183 
   232 void HbToolButtonPrivate::setLayoutProperty(const char *name, bool value)
   184 void HbToolButtonPrivate::setLayoutProperty(const char *name, bool value)
   233 {
   185 {
   234     Q_Q(HbToolButton);
   186     Q_Q(HbToolButton);
   235     q->setProperty(name, value);
   187     q->setProperty(name, value);
   236     if (q->isVisible() && polished) {
   188     q->repolish();
   237         q->repolish();
       
   238     }
       
   239 }
   189 }
   240 
   190 
   241 QSizeF HbToolButtonPrivate::getMinimumSize()
   191 QSizeF HbToolButtonPrivate::getMinimumSize()
   242 {
   192 {
   243     Q_Q(HbToolButton);
   193     Q_Q(HbToolButton);
   245     polishPending = false;
   195     polishPending = false;
   246     //Workaround (causing extra polish)
   196     //Workaround (causing extra polish)
   247     mSizeHintPolish = false;
   197     mSizeHintPolish = false;
   248     //workaround ends
   198     //workaround ends
   249     q->updateGeometry();
   199     q->updateGeometry();
   250     QCoreApplication::sendPostedEvents(q, QEvent::LayoutRequest);
       
   251     QSizeF size = q->minimumSize();
   200     QSizeF size = q->minimumSize();
   252     return size;
   201     return size;
   253 }
   202 }
   254 
   203 
   255 void HbToolButtonPrivate::_q_actionTriggered()
   204 void HbToolButtonPrivate::_q_actionTriggered()
   264 
   213 
   265 void HbToolButtonPrivate::_q_actionChanged()
   214 void HbToolButtonPrivate::_q_actionChanged()
   266 {
   215 {
   267     Q_Q(HbToolButton);
   216     Q_Q(HbToolButton);
   268     HbAction *hbAction = qobject_cast<HbAction *>(action);
   217     HbAction *hbAction = qobject_cast<HbAction *>(action);
       
   218     int currentProperty = q->property(BUTTONSTYLE).toInt();
   269     if ((hbAction && !hbAction->icon().isNull()) || !action->icon().isNull()) {
   219     if ((hbAction && !hbAction->icon().isNull()) || !action->icon().isNull()) {
   270         if (orientation == Qt::Horizontal) {
   220         if (orientation == Qt::Horizontal) {
   271             buttonStyle = HbToolButton::ToolButtonIcon;
   221             q->setProperty(BUTTONSTYLE, HbToolButtonPrivate::ToolButtonIcon);
   272         } else if (!action->text().isEmpty()) {
   222         } else if (!action->text().isEmpty()) {
   273             buttonStyle = HbToolButton::ToolButtonTextAndIcon;
   223             q->setProperty(BUTTONSTYLE, HbToolButtonPrivate::ToolButtonTextAndIcon);
   274         } else {
   224         } else {
   275             buttonStyle = HbToolButton::ToolButtonIcon;
   225             q->setProperty(BUTTONSTYLE, HbToolButtonPrivate::ToolButtonIcon);
   276         }
   226         }
   277     } else {
   227     } else {
   278         buttonStyle = HbToolButton::ToolButtonText;
   228         q->setProperty(BUTTONSTYLE, HbToolButtonPrivate::ToolButtonText);
   279     }
   229     }
   280     // action text/icon may have changed,            
   230     // action text/icon may have changed,
   281     if (polished) {
   231     q->prepareGeometryChange();
   282         q->repolish();        
   232     if (currentProperty != q->property(BUTTONSTYLE).toInt())
   283         QCoreApplication::sendPostedEvents(q, QEvent::Polish);
   233         q->repolish();
       
   234     else if (polished) {
       
   235         q->updatePrimitives();
   284     }
   236     }
   285 }
   237 }
   286 
   238 
   287 void HbToolButtonPrivate::showToolTip()
   239 void HbToolButtonPrivate::showToolTip()
   288 {
   240 {
   355     d->action = action;
   307     d->action = action;
   356     if (d->action) {
   308     if (d->action) {
   357         connect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
   309         connect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
   358         connect(action, SIGNAL(changed()), this, SLOT(_q_actionChanged()));
   310         connect(action, SIGNAL(changed()), this, SLOT(_q_actionChanged()));
   359     }
   311     }
   360 
   312     // If action was null then there is a chance that the iconitem is not yet created.
       
   313     // If the new action is null then we may need to get rid of the icon completely.
       
   314     if ((!oldAction && action) || (oldAction && !action)) {
       
   315         repolish(); // will call createPrimitives()
       
   316     }
   361     if (isVisible() && d->polished) {
   317     if (isVisible() && d->polished) {
   362         // If action was null then there is a chance that the iconitem is not yet created.
       
   363         // If the new action is null then we may need to get rid of the icon completely.
       
   364         if ((!oldAction && action) || (oldAction && !action)) {
       
   365             repolish(); // will call createPrimitives()
       
   366         }
       
   367         updatePrimitives();
   318         updatePrimitives();
   368     }
   319     }
   369 }
   320 }
   370 
   321 
   371 /*!
   322 /*!
   388         if (isVisible() || d->polished)
   339         if (isVisible() || d->polished)
   389             updatePrimitives();
   340             updatePrimitives();
   390     }
   341     }
   391 }
   342 }
   392 
   343 
   393 /*!
       
   394  \deprecated HbToolButton::toolButtonStyle() const
       
   395          is deprecated.
       
   396 
       
   397     @beta
       
   398     Returns the tool button style.
       
   399 
       
   400     The default value is \b HbToolButton::ToolButtonIcon.
       
   401 
       
   402     \sa setToolButtonStyle()
       
   403  */
       
   404 HbToolButton::ToolButtonStyle HbToolButton::toolButtonStyle() const
       
   405 {
       
   406     HB_DEPRECATED("HbToolButton::toolButtonStyle() is deprecated.");
       
   407 
       
   408     Q_D(const HbToolButton);
       
   409     return d->buttonStyle;
       
   410 }
       
   411 
       
   412 /*!
       
   413  \deprecated HbToolButton::setToolButtonStyle(HbToolButton::ToolButtonStyle)
       
   414          is deprecated.
       
   415 
       
   416     @beta
       
   417     Sets the tool button style.
       
   418 
       
   419     \sa toolButtonStyle()
       
   420  */
       
   421 void HbToolButton::setToolButtonStyle(HbToolButton::ToolButtonStyle style)
       
   422 {    
       
   423     HB_DEPRECATED("HbToolButton::setToolButtonStyle(HbToolButton::ToolButtonStyle style) is deprecated.");
       
   424 
       
   425     Q_D(HbToolButton);
       
   426     if (d->buttonStyle != style) {
       
   427         d->buttonStyle = style;
       
   428 
       
   429         // action text/icon may have changed,
       
   430         // primitives might need to be created/cleaned up
       
   431         if (size() != QSize(0, 0)) {
       
   432             repolish();
       
   433         }
       
   434     }
       
   435 }
       
   436 
   344 
   437 /*!
   345 /*!
   438 
   346 
   439     \deprecated HbToolButton::primitive(HbStyle::Primitive)
   347     \deprecated HbToolButton::primitive(HbStyle::Primitive)
   440          is deprecated.
   348          is deprecated.
   443  */
   351  */
   444 QGraphicsItem *HbToolButton::primitive(HbStyle::Primitive primitive) const
   352 QGraphicsItem *HbToolButton::primitive(HbStyle::Primitive primitive) const
   445 {
   353 {
   446     Q_D(const HbToolButton);
   354     Q_D(const HbToolButton);
   447     switch (primitive) {
   355     switch (primitive) {
   448         case HbStyle::P_ToolButton_frame:
   356         case HbStylePrivate::P_ToolButton_frame:
   449             return d->frameItem;
   357             return d->frameItem;
   450         case HbStyle::P_ToolButton_icon:
   358         case HbStylePrivate::P_ToolButton_icon:
   451             return d->iconItem;
   359             return d->iconItem;
   452         case HbStyle::P_ToolButton_text:
   360         case HbStylePrivate::P_ToolButton_text:
   453             return d->textItem;
   361             return d->textItem;
   454         default:
   362         default:
   455             return 0;
   363             return 0;
   456     }
   364     }
   457 }
   365 }
   486     }
   394     }
   487 
   395 
   488     initStyleOption(&option);
   396     initStyleOption(&option);
   489     setProperty("dialogtoolbar", d->mDialogToolBar);
   397     setProperty("dialogtoolbar", d->mDialogToolBar);
   490     if (d->frameItem) {
   398     if (d->frameItem) {
   491         style()->updatePrimitive(d->frameItem, HbStyle::P_ToolButton_frame, &option);
   399         HbStylePrivate::updatePrimitive(d->frameItem, HbStylePrivate::P_ToolButton_frame, &option);
   492     }
   400     }
   493     if (d->textItem) {
   401     if (d->textItem) {
   494         style()->updatePrimitive(d->textItem, HbStyle::P_ToolButton_text, &option);
   402         bool itemHasNoContents = false;
       
   403         if (option.text.isEmpty() || option.text.isNull() ||
       
   404             (property(BUTTONSTYLE).toInt() == HbToolButtonPrivate::ToolButtonIcon)) {
       
   405             itemHasNoContents = true;
       
   406         }
       
   407         d->textItem->setFlag(QGraphicsItem::ItemHasNoContents, itemHasNoContents);
       
   408         if(!itemHasNoContents)
       
   409             HbStylePrivate::updatePrimitive(d->textItem, HbStylePrivate::P_ToolButton_text, &option);
   495     }
   410     }
   496     if (d->iconItem) {
   411     if (d->iconItem) {
   497         style()->updatePrimitive(d->iconItem, HbStyle::P_ToolButton_icon, &option);
   412         bool itemHasNoContents = false;
   498         HbAction *hbAction = qobject_cast<HbAction *>(d->action);
   413         if (option.icon.isNull() || (property(BUTTONSTYLE).toInt() == HbToolButtonPrivate::ToolButtonText)) {
   499         if (hbAction) {
   414             itemHasNoContents = true;
   500             if (hbAction->icon().flags() & HbIcon::Colorized) {
   415         } else {
       
   416             HbStylePrivate::updatePrimitive(d->iconItem, HbStylePrivate::P_ToolButton_icon, &option);
       
   417             if (option.icon.flags() & HbIcon::Colorized) {
   501                 static_cast<HbIconItem *>(d->iconItem)->setFlags(HbIcon::Colorized);
   418                 static_cast<HbIconItem *>(d->iconItem)->setFlags(HbIcon::Colorized);
   502             }
   419             }
   503             if (hbAction->icon().mirroringMode() != HbIcon::Default) {
   420             if (option.icon.mirroringMode() != HbIcon::Default) {
   504                 HbIconItem *iconItem = static_cast<HbIconItem *>(d->iconItem);
   421                 HbIconItem *iconItem = static_cast<HbIconItem *>(d->iconItem);
   505                 iconItem->setMirroringMode( hbAction->icon().mirroringMode() );
   422                 iconItem->setMirroringMode( option.icon.mirroringMode() );
   506             }
   423             }
   507         }
   424         }
   508 
   425         d->iconItem->setFlag(QGraphicsItem::ItemHasNoContents, itemHasNoContents);
   509     }
   426     }
   510 }
   427 }
   511 
   428 
   512 /*!
   429 /*!
   513     Initializes \a option with the values from this HbToolButton. This method is useful for
   430     Initializes \a option with the values from this HbToolButton. This method is useful for