src/hbwidgets/widgets/hbcheckbox.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include <hbcheckbox.h>
       
    27 #include <hbstyleoptioncheckbox.h>
       
    28 #include "hbabstractbutton_p.h"
       
    29 #include "hbtooltip.h"
       
    30 
       
    31 #include <QGraphicsSceneMouseEvent>
       
    32 #include <QGraphicsItem>
       
    33 
       
    34 #ifdef HB_EFFECTS
       
    35 #include "hbeffect.h"
       
    36 #include "hbeffectinternal_p.h"
       
    37 #endif
       
    38 
       
    39 /*
       
    40     Spacer class.
       
    41 */
       
    42 class HbCheckBoxSpacer: public HbWidgetBase
       
    43 {
       
    44 
       
    45 public:
       
    46     HbCheckBoxSpacer( QGraphicsItem *parent = 0 );
       
    47     QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
       
    48 };
       
    49 
       
    50 HbCheckBoxSpacer::HbCheckBoxSpacer( QGraphicsItem *parent )
       
    51     : HbWidgetBase( parent )
       
    52 {
       
    53     // seems to be 4.6 only?
       
    54     //    setFlag( QGraphicsItem::ItemHasNoContents, true );
       
    55 }
       
    56 
       
    57 QSizeF HbCheckBoxSpacer::sizeHint(Qt::SizeHint which, const QSizeF &constraint ) const
       
    58 {
       
    59     Q_UNUSED( constraint );
       
    60 
       
    61     if ( which == Qt::MaximumSize ) {
       
    62         return QSizeF( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
       
    63     }
       
    64     return QSizeF(0.f,0.f);
       
    65 }
       
    66 
       
    67 
       
    68 /*
       
    69   private class
       
    70 */
       
    71 class HB_AUTOTEST_EXPORT HbCheckBoxPrivate : public HbAbstractButtonPrivate
       
    72 {
       
    73     Q_DECLARE_PUBLIC(HbCheckBox)
       
    74 
       
    75 public:
       
    76     HbCheckBoxPrivate();
       
    77     virtual ~HbCheckBoxPrivate();
       
    78 
       
    79     void createPrimitives();
       
    80 
       
    81 public:
       
    82     QString mText;
       
    83     QGraphicsItem *mTextItem;
       
    84     QGraphicsItem *mIconItem;
       
    85     QGraphicsItem *mTouchArea;
       
    86 
       
    87     bool mTristate;
       
    88     bool mNoChange;
       
    89     Qt::CheckState mPublishedState;
       
    90 };
       
    91 
       
    92 /*
       
    93   private class constructor
       
    94 */
       
    95 HbCheckBoxPrivate::HbCheckBoxPrivate():
       
    96     mTextItem(0),
       
    97     mIconItem(0), 
       
    98     mTouchArea(0),
       
    99     mTristate(false),
       
   100     mNoChange(false),
       
   101     mPublishedState(Qt::Unchecked)
       
   102 {
       
   103     //adding effect for checkbox
       
   104 #ifdef HB_EFFECTS
       
   105     HbEffectInternal::add(HB_CHECKBOX_TYPE,"checkbox_selected", "pressed");
       
   106 #endif
       
   107 }
       
   108 
       
   109 /*
       
   110   private class destructor
       
   111 */
       
   112 HbCheckBoxPrivate::~HbCheckBoxPrivate()
       
   113 {
       
   114 }
       
   115 
       
   116 /*
       
   117   create primitive
       
   118 */
       
   119 void HbCheckBoxPrivate::createPrimitives()
       
   120 {
       
   121     Q_Q(HbCheckBox);
       
   122     if(!mTouchArea) {
       
   123         mTouchArea = q->style()->createPrimitive(HbStyle::P_CheckBox_toucharea, q);
       
   124     }
       
   125     if (!mTextItem) {
       
   126         mTextItem = q->style()->createPrimitive(HbStyle::P_CheckBox_text, q);
       
   127         }
       
   128     if (!mIconItem) {
       
   129         mIconItem = q->style()->createPrimitive(HbStyle::P_CheckBox_icon, q);
       
   130     }
       
   131 }
       
   132 
       
   133 /*!
       
   134     @beta
       
   135     @hbwidgets
       
   136     \class HbCheckBox
       
   137     \brief The HbCheckBox widget provides a checkbox with a text label.
       
   138     
       
   139     \image html checkbox_unchecked.png A checkbox with unchecked state.
       
   140 
       
   141     \image html checkbox_checked.png A checkbox with checked state.
       
   142 
       
   143     \image html checkbox_partiallychecked.png A checkbox with partiallychecked state.
       
   144 
       
   145     A HbCheckBox is an option button that can be switched on (checked)
       
   146     or off (unchecked). Checkboxes are typically used to represent
       
   147     features in an application that can be enabled or disabled without
       
   148     affecting others, but different types of behavior can be
       
   149     implemented.
       
   150 
       
   151     Whenever a checkbox is checked or cleared it emits the signal
       
   152     stateChanged(). Connect to this signal if you want to trigger an
       
   153     action each time the checkbox changes state. You can use
       
   154     isChecked() to query whether or not a checkbox is checked.
       
   155 
       
   156     In addition to checked and unchecked states, HbCheckBox
       
   157     optionally provides a third state which is PartiallyChecked. This
       
   158     is useful for Hierarchical list item. If you need this third state,
       
   159     enable it with setTristate(), and use checkState() to query the current
       
   160     toggle state.
       
   161 
       
   162     Checkbox has default icon for checked,unchecked and partiallyChecked states.
       
   163     The text can be set in the constructor or with setText().
       
   164 
       
   165     Example usage:
       
   166     \code
       
   167     HbCheckBox *checkbox = new HbCheckBox(QString("checkbox_text"));
       
   168     layout->addItem(checkbox);
       
   169     \endcode
       
   170 
       
   171 */
       
   172 
       
   173 
       
   174 /*!
       
   175     \fn void HbCheckBox::stateChanged(int state)
       
   176     This signal is emitted whenever the check box's state changes,
       
   177     i.e. whenever the user checks or unchecks it.
       
   178     \a state contains the check box's new Qt::CheckState.
       
   179 */
       
   180 
       
   181 
       
   182 
       
   183 /*!
       
   184     @beta
       
   185     Constructor an object of HbCheckBox with default parameters.
       
   186     By default the state of check box will be Unchecked, text will
       
   187     be empty and tristate will be false.
       
   188 */
       
   189 HbCheckBox::HbCheckBox(QGraphicsItem *parent)
       
   190     : HbAbstractButton(*new HbCheckBoxPrivate, parent)
       
   191 {
       
   192     Q_D(HbCheckBox);
       
   193     d->q_ptr = this;
       
   194     setCheckable(true);
       
   195     d->createPrimitives();
       
   196     // creattion of top and bottom spacer.
       
   197     HbStyle::setItemName( new HbCheckBoxSpacer(this), "topSpacer" );
       
   198     HbStyle::setItemName( new HbCheckBoxSpacer(this), "bottomSpacer" );
       
   199 
       
   200 }
       
   201 
       
   202 /*!
       
   203     @beta
       
   204     Constructor an object of HbCheckBox with \a text passed.
       
   205     By default the state of check box will be Unchecked.
       
   206     Tristate will be false by default.
       
   207 */
       
   208 HbCheckBox::HbCheckBox(const QString &text, QGraphicsItem *parent )
       
   209     : HbAbstractButton(*new HbCheckBoxPrivate, parent)
       
   210 {
       
   211     Q_D(HbCheckBox);
       
   212     d->q_ptr = this;
       
   213     d->mText = text;
       
   214     setCheckable(true);
       
   215     d->createPrimitives();
       
   216     // creattion of top and bottom spacer.
       
   217     HbStyle::setItemName( new HbCheckBoxSpacer(this), "topSpacer");
       
   218     HbStyle::setItemName( new HbCheckBoxSpacer(this), "bottomSpacer");
       
   219 
       
   220 }
       
   221 
       
   222 /*!
       
   223     Destructor.
       
   224 */
       
   225 HbCheckBox::~HbCheckBox()
       
   226 {
       
   227 }
       
   228 
       
   229 /*!
       
   230     @beta
       
   231     Sets the text of checkbox.
       
   232     UpdatePrimitive is called by this functions once the text is changed.
       
   233     \sa text()
       
   234 */
       
   235 void HbCheckBox::setText( const QString &text )
       
   236 {
       
   237     Q_D(HbCheckBox);
       
   238     if(d->mText != text){
       
   239         d->mText = text;
       
   240         //updatePrimitives();
       
   241         if( d->mTextItem ){
       
   242             HbStyleOptionCheckBox checkBoxOption;
       
   243             initStyleOption(&checkBoxOption);
       
   244             style()->updatePrimitive(d->mTextItem, HbStyle::P_CheckBox_text, &checkBoxOption);
       
   245         }
       
   246     }    
       
   247 }
       
   248 
       
   249 /*!
       
   250     @beta
       
   251     Returns the text of checkbox.
       
   252     \sa setText()
       
   253 */
       
   254 QString HbCheckBox::text() const
       
   255 {
       
   256     Q_D(const HbCheckBox);
       
   257     return d->mText;
       
   258 }
       
   259 
       
   260 /*!
       
   261     @beta
       
   262     Sets tristate support for checkbox based upon \a flag.
       
   263     By default tristate is false and checkbox has got only two states.
       
   264     \sa isTristate( )
       
   265 */
       
   266 void HbCheckBox::setTristate( bool isTristate )
       
   267 {
       
   268     Q_D(HbCheckBox);
       
   269     d->mTristate = isTristate;
       
   270 }
       
   271 
       
   272 /*!
       
   273     @beta
       
   274     Returns whether tristate is enabled for checkbox or not.
       
   275     \sa setTristate( )
       
   276 */
       
   277 bool HbCheckBox::isTristate( ) const
       
   278 {
       
   279     Q_D(const HbCheckBox);
       
   280     return d->mTristate;
       
   281 }
       
   282 
       
   283 /*!
       
   284     @beta
       
   285     Returns the current state of HbCheckBox.
       
   286 */
       
   287 Qt::CheckState HbCheckBox::checkState() const
       
   288 {
       
   289     Q_D(const HbCheckBox);
       
   290     if (d->mTristate &&  d->mNoChange) {
       
   291         return Qt::PartiallyChecked;
       
   292     }
       
   293     return d->checked ? Qt::Checked : Qt::Unchecked;
       
   294 }
       
   295 
       
   296 /*!
       
   297     Returns the pointer for \a primitive passed.
       
   298     Will return NULL if \a primitive passed is icon because user cannot
       
   299     configure the check and unchecked icons. Style needs to be changed if
       
   300     user wants different icons.
       
   301 */
       
   302 QGraphicsItem* HbCheckBox::primitive(HbStyle::Primitive primitive) const
       
   303 {
       
   304     Q_D(const HbCheckBox);
       
   305 
       
   306     switch (primitive) {
       
   307         case HbStyle::P_CheckBox_text:
       
   308             return d->mTextItem;
       
   309         case HbStyle::P_CheckBox_toucharea:
       
   310             return d->mTouchArea;
       
   311         case HbStyle::P_CheckBox_icon:
       
   312             return d->mIconItem;
       
   313         default:
       
   314             return 0;
       
   315     }
       
   316 }
       
   317 
       
   318 /*!
       
   319     @beta
       
   320     Sets the state of HbCheckBox to the \a state passed.
       
   321     Calls updatePrimitive when state is changed.
       
   322     Emits signal stateChanged(state).
       
   323 */
       
   324 void HbCheckBox::setCheckState( Qt::CheckState state )
       
   325 {
       
   326     Q_D(HbCheckBox);
       
   327     if (state == Qt::PartiallyChecked) {
       
   328         d->mTristate = true;
       
   329         d->mNoChange = true;
       
   330     } else {
       
   331         d->mNoChange = false;
       
   332     }
       
   333     d->blockRefresh = true;
       
   334     setChecked(state != Qt::Unchecked);
       
   335     d->blockRefresh = false;
       
   336     d->refresh();
       
   337 
       
   338     if (state != d->mPublishedState) {
       
   339         d->mPublishedState = state;
       
   340         emit stateChanged(state);
       
   341     }
       
   342 }
       
   343 
       
   344 /*!
       
   345     Updates the icon and text primitives.
       
   346 */
       
   347 void HbCheckBox::updatePrimitives()
       
   348 {
       
   349     HbWidget::updatePrimitives();
       
   350     Q_D(HbCheckBox);
       
   351 
       
   352     HbStyleOptionCheckBox checkBoxOption;
       
   353     initStyleOption(&checkBoxOption);
       
   354 
       
   355     if (d->mTextItem) {
       
   356         style()->updatePrimitive(d->mTextItem, HbStyle::P_CheckBox_text, &checkBoxOption);
       
   357     }
       
   358     if (d->mIconItem) {
       
   359         style()->updatePrimitive(d->mIconItem, HbStyle::P_CheckBox_icon, &checkBoxOption);
       
   360     }
       
   361     if (d->mTouchArea) {
       
   362         style()->updatePrimitive(d->mTouchArea, HbStyle::P_CheckBox_toucharea, &checkBoxOption);
       
   363     }
       
   364 }
       
   365 
       
   366 /*!
       
   367     Initializes the style for check box with the \a option passed.
       
   368 */
       
   369 void HbCheckBox::initStyleOption(HbStyleOptionCheckBox *option) const
       
   370 {
       
   371     Q_D(const HbCheckBox);
       
   372 
       
   373     HbAbstractButton::initStyleOption(option);
       
   374 
       
   375     Q_ASSERT(option);
       
   376     option->text = d->mText;
       
   377     if (d->mTristate && d->mNoChange){
       
   378         option->state.operator = (QStyle::State_NoChange);
       
   379     } else {
       
   380         option->state.operator = (d->checked ? QStyle::State_On : QStyle::State_Off);
       
   381     }
       
   382 }
       
   383 
       
   384 /*!
       
   385     Updates the primitive as per new size.
       
   386 */
       
   387 void HbCheckBox::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   388 {
       
   389     HbAbstractButton::resizeEvent(event);
       
   390 }
       
   391 
       
   392 /*!
       
   393   Overloaded hit detection to include touch area
       
   394  */
       
   395 bool HbCheckBox::hitButton( const QPointF &pos ) const
       
   396 {
       
   397     Q_D(const HbCheckBox);
       
   398     QRectF compRect = d->mTouchArea->boundingRect();
       
   399     compRect.translate(d->mTouchArea->pos());
       
   400     return compRect.contains(pos);
       
   401 }
       
   402 
       
   403 /*!
       
   404     \reimp.
       
   405 */
       
   406 void HbCheckBox::checkStateSet( )
       
   407 {
       
   408     Q_D(HbCheckBox);
       
   409     d->mNoChange = false;
       
   410     Qt::CheckState state = checkState();
       
   411     if (state != d->mPublishedState) {
       
   412         #ifdef HB_EFFECTS
       
   413             HbEffect::start(d->mIconItem, HB_CHECKBOX_TYPE, "pressed");
       
   414         #endif
       
   415         d->mPublishedState = state;
       
   416         emit stateChanged(state);
       
   417     }
       
   418 }
       
   419 
       
   420 /*!
       
   421     \reimp.
       
   422 */
       
   423 void HbCheckBox::nextCheckState( )
       
   424 {
       
   425     if( checkState() == Qt::PartiallyChecked ) {
       
   426         HbAbstractButton::nextCheckState();
       
   427     }
       
   428     HbAbstractButton::nextCheckState();
       
   429     HbCheckBox::checkStateSet();
       
   430 }
       
   431 
       
   432 /*!
       
   433     \reimp.
       
   434 */
       
   435 void HbCheckBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   436 {
       
   437     HbAbstractButton::mouseReleaseEvent(event);
       
   438     updatePrimitives();
       
   439 }
       
   440 
       
   441 /*!
       
   442     \reimp.
       
   443 */
       
   444 void HbCheckBox::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   445 {
       
   446     Q_UNUSED(event);
       
   447     // To show the tooltip on press,move outside and come back to same button.
       
   448     // check for hit pos 
       
   449     bool hit = hitButton( event->pos( ) );
       
   450     if ( hit ) {
       
   451         HbToolTip::showText(toolTip(), this);
       
   452     }   
       
   453 }
       
   454 
       
   455 /*!
       
   456     \reimp.
       
   457 */
       
   458 void HbCheckBox::keyPressEvent(QKeyEvent *keyEvent)
       
   459 {
       
   460     switch (keyEvent->key()) {
       
   461     case Qt::Key_Select:
       
   462     case Qt::Key_Enter:
       
   463     case Qt::Key_Return:
       
   464             HbAbstractButton::keyPressEvent(keyEvent);
       
   465         break;
       
   466     default:
       
   467             HbAbstractButton::keyPressEvent(keyEvent);
       
   468     }
       
   469 }
       
   470 
       
   471 /*!
       
   472     \reimp
       
   473  */
       
   474 QVariant HbCheckBox::itemChange(GraphicsItemChange change, const QVariant &value)
       
   475 {
       
   476     switch ( change ) {
       
   477         case ItemEnabledHasChanged:
       
   478         case ItemVisibleChange: {
       
   479                 updatePrimitives( );
       
   480             }
       
   481             break;
       
   482         default:
       
   483             break;
       
   484     }
       
   485     return HbAbstractButton::itemChange( change, value );
       
   486 }
       
   487 #include "moc_hbcheckbox.cpp"
       
   488