src/hbwidgets/widgets/hbcheckbox.cpp
changeset 34 ed14f46c0e55
parent 7 923ff622b8b9
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    38 
    38 
    39 #ifdef HB_GESTURE_FW
    39 #ifdef HB_GESTURE_FW
    40 #include <hbtapgesture.h>
    40 #include <hbtapgesture.h>
    41 #endif
    41 #endif
    42 
    42 
    43 
       
    44 /*
       
    45     Spacer class.
       
    46 */
       
    47 class HbCheckBoxSpacer: public HbWidgetBase
       
    48 {
       
    49 
       
    50 public:
       
    51     HbCheckBoxSpacer( QGraphicsItem *parent = 0 );
       
    52     QSizeF sizeHint(  Qt::SizeHint which, const QSizeF &constraint = QSizeF( ) ) const;
       
    53 };
       
    54 
       
    55 HbCheckBoxSpacer::HbCheckBoxSpacer( QGraphicsItem *parent )
       
    56     : HbWidgetBase( parent )
       
    57 {
       
    58 #if QT_VERSION >= 0x040600
       
    59     setFlag(QGraphicsItem::ItemHasNoContents, true);
       
    60 #endif
       
    61 }
       
    62 
       
    63 QSizeF HbCheckBoxSpacer::sizeHint( Qt::SizeHint which, const QSizeF &constraint ) const
       
    64 {
       
    65     Q_UNUSED( constraint );
       
    66 
       
    67     if ( which == Qt::MaximumSize ) {
       
    68         return QSizeF( QWIDGETSIZE_MAX, QWIDGETSIZE_MAX );
       
    69     }
       
    70     return QSizeF( 0.f, 0.f );
       
    71 }
       
    72 
       
    73 
       
    74 /*
    43 /*
    75   private class
    44   private class
    76 */
    45 */
    77 class HB_AUTOTEST_EXPORT HbCheckBoxPrivate : public HbAbstractButtonPrivate
    46 class HB_AUTOTEST_EXPORT HbCheckBoxPrivate : public HbAbstractButtonPrivate
    78 {
    47 {
   124 */
    93 */
   125 void HbCheckBoxPrivate::createPrimitives( )
    94 void HbCheckBoxPrivate::createPrimitives( )
   126 {
    95 {
   127     Q_Q( HbCheckBox );
    96     Q_Q( HbCheckBox );
   128     if( !mTouchArea ) {
    97     if( !mTouchArea ) {
   129         mTouchArea = q->style( )->createPrimitive( HbStyle::P_CheckBox_toucharea, q );
    98         mTouchArea = HbStylePrivate::createPrimitive( HbStylePrivate::P_CheckBox_toucharea, q );
   130         if( QGraphicsObject *ta = qgraphicsitem_cast<QGraphicsObject*>( mTouchArea ) ) {
    99         if( QGraphicsObject *ta = qgraphicsitem_cast<QGraphicsObject*>( mTouchArea ) ) {
   131             ta->grabGesture( Qt::TapGesture );
   100             ta->grabGesture( Qt::TapGesture );
   132         }
   101         }
   133     }
   102     }
   134     if ( !mTextItem ) {
   103     if ( !mTextItem ) {
   135         mTextItem = q->style( )->createPrimitive( HbStyle::P_CheckBox_text, q );
   104         mTextItem = HbStylePrivate::createPrimitive( HbStylePrivate::P_CheckBox_text, q );
   136         }
   105         }
   137     if ( !mIconItem ) {
   106     if ( !mIconItem ) {
   138         mIconItem = q->style( )->createPrimitive( HbStyle::P_CheckBox_icon, q );
   107         mIconItem = HbStylePrivate::createPrimitive( HbStylePrivate::P_CheckBox_icon, q );
   139     }
   108     }
   140 }
   109 }
   141 
   110 
   142 /*!
   111 /*!
   143     @beta
       
   144     @hbwidgets
   112     @hbwidgets
   145     \class HbCheckBox
   113     \class HbCheckBox
   146     \brief The HbCheckBox widget provides a checkbox with a text label.
   114     \brief The HbCheckBox class provides a check box widget with a text label.
   147     
   115     
   148     \image html checkbox_unchecked.png A checkbox with unchecked state.
   116     A check box is an option button that can be in checked, unchecked, and 
   149 
   117     partially checked states as shown in the pictures below. Check boxes are 
   150     \image html checkbox_checked.png A checkbox with checked state.
   118     typically used to represent application features that can be enabled or 
   151 
   119     disabled without affecting other features, but you can also subclass it to 
   152     \image html checkbox_partiallychecked.png A checkbox with partiallychecked state.
   120     implement different behavior.
   153 
   121 
   154     A HbCheckBox is an option button that can be switched on (checked)
   122     \image html checkbox_unchecked.png A check box in unchecked state.
   155     or off (unchecked). Checkboxes are typically used to represent
   123 
   156     features in an application that can be enabled or disabled without
   124     \image html checkbox_checked.png A check box in checked state.
   157     affecting others, but different types of behavior can be
   125 
   158     implemented.
   126     \image html checkbox_partiallychecked.png A check box in partially checked state.
   159 
   127     
   160     Whenever a checkbox is checked or cleared it emits the signal
   128     When a user checks or unchecks a check box, it emits the stateChanged() 
   161     stateChanged(). Connect to this signal if you want to trigger an
   129     signal. Connect a slot to this signal if you want to trigger an action every 
   162     action each time the checkbox changes state. You can use
   130     time the check box state changes. You can use isChecked() to query whether a 
   163     isChecked() to query whether or not a checkbox is checked.
   131     check box is checked or not.
   164 
   132 
   165     In addition to checked and unchecked states, HbCheckBox
   133     In addition to checked and unchecked states, %HbCheckBox provides 
   166     optionally provides a third state which is PartiallyChecked. This
   134     an optional partially checked state. This state is useful when you handle 
   167     is useful for Hierarchical list item. If you need this third state,
   135     hierarchical list items, for example. If you need the partially checked 
   168     enable it with setTristate(), and use checkState() to query the current
   136     state, enable it with setTristate() and query the state of the check box 
   169     toggle state.
   137     with checkState().
   170 
   138 
   171     Checkbox has default icon for checked,unchecked and partiallyChecked states.
   139     A check box has default icons for checked, unchecked, and partially checked 
   172     The text can be set in the constructor or with setText().
   140     states. The text can be set in the constructor or with setText(). If the 
   173 
   141     check box text is long, it is shown in multiple lines. By default a check box 
   174     Example usage:
   142     can have up to three lines of text.
       
   143 
       
   144     \section _usecases_hbcheckbox Using the HbCheckBox class
       
   145     
       
   146     \subsection _uc_hbcheckbox_001 Creating a check box.
       
   147     
       
   148     The following code snippet creates a check box in the layout.
       
   149 
   175     \code
   150     \code
   176     HbCheckBox *checkbox = new HbCheckBox(QString("checkbox_text"));
   151     HbCheckBox *checkbox = new HbCheckBox(QString("checkbox_text"));
   177     layout->addItem(checkbox);
   152     layout->addItem(checkbox);
   178     \endcode
   153     \endcode
   179 
   154 
   180 */
   155 */
   181 
   156 
   182 
   157 
   183 /*!
   158 /*!
       
   159 
   184     \fn void HbCheckBox::stateChanged(int state)
   160     \fn void HbCheckBox::stateChanged(int state)
   185     This signal is emitted whenever the check box's state changes,
   161 
   186     i.e. whenever the user checks or unchecks it.
   162     This signal is emitted when the user checks or unchecks a check box causing a 
   187     \a state contains the check box's new Qt::CheckState.
   163     state change of the check box.
   188 */
   164     
   189 
   165     \param  state The new state (Qt::CheckState) of the check box.
   190 
   166 
   191 
   167     \sa Qt
   192 /*!
   168 
   193     @beta
   169 */
   194     Constructor an object of HbCheckBox with default parameters.
   170 
   195     By default the state of check box will be Unchecked, text will
   171 
   196     be empty and tristate will be false.
   172 /*!
       
   173     
       
   174     Constructs a check box with the following default values:
       
   175     - \a state is \c Qt::CheckState::Unchecked.
       
   176     - \a text is empty.
       
   177     - \a tristate is \c false.
       
   178     
       
   179     \sa Qt
       
   180     
   197 */
   181 */
   198 HbCheckBox::HbCheckBox( QGraphicsItem *parent )
   182 HbCheckBox::HbCheckBox( QGraphicsItem *parent )
   199     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   183     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   200 {
   184 {
   201     Q_D( HbCheckBox );
   185     Q_D( HbCheckBox );
   202     d->q_ptr = this;
   186     d->q_ptr = this;
   203     setCheckable( true );
   187     setCheckable( true );
   204     d->createPrimitives( );
   188     d->createPrimitives( );
   205     // creattion of top and bottom spacer.
       
   206     HbStyle::setItemName( new HbCheckBoxSpacer( this ), "topSpacer" );
       
   207     HbStyle::setItemName( new HbCheckBoxSpacer( this ), "bottomSpacer" );
       
   208 #ifdef HB_GESTURE_FW
   189 #ifdef HB_GESTURE_FW
   209     grabGesture( Qt::TapGesture );
   190     grabGesture( Qt::TapGesture );
   210 #endif
   191 #endif
   211 
   192 
   212 }
   193 }
   213 
   194 
   214 /*!
   195 /*!
   215     @beta
   196     
   216     Constructor an object of HbCheckBox with \a text passed.
   197     Constructs a check box with \a text and the following default values:
   217     By default the state of check box will be Unchecked.
   198     - \a state is \c Qt::CheckState::Unchecked.
   218     Tristate will be false by default.
   199     - \a tristate is \c false.
       
   200     
       
   201     \sa Qt
       
   202 
   219 */
   203 */
   220 HbCheckBox::HbCheckBox( const QString &text, QGraphicsItem *parent )
   204 HbCheckBox::HbCheckBox( const QString &text, QGraphicsItem *parent )
   221     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   205     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   222 {
   206 {
   223     Q_D( HbCheckBox );
   207     Q_D( HbCheckBox );
   224     d->q_ptr = this;
   208     d->q_ptr = this;
   225     d->mText = text;
   209     d->mText = text;
   226     setCheckable( true );
   210     setCheckable( true );
   227     d->createPrimitives( );
   211     d->createPrimitives( );
   228     // creattion of top and bottom spacer.
       
   229     HbStyle::setItemName( new HbCheckBoxSpacer(this), "topSpacer");
       
   230     HbStyle::setItemName( new HbCheckBoxSpacer(this), "bottomSpacer");
       
   231 
       
   232 }
   212 }
   233 
   213 
   234 /*!
   214 /*!
   235     Destructor.
   215     Destructor.
   236 */
   216 */
   237 HbCheckBox::~HbCheckBox( )
   217 HbCheckBox::~HbCheckBox( )
   238 {
   218 {
   239 }
   219 }
   240 
   220 
   241 /*!
   221 /*!
   242     @beta
   222 
   243     Sets the text of checkbox.
   223     Sets the \a text shown on a check box.
   244     UpdatePrimitive is called by this functions once the text is changed.
   224 
   245     \sa text()
   225     \sa text()
   246 */
   226 */
   247 void HbCheckBox::setText( const QString &text )
   227 void HbCheckBox::setText( const QString &text )
   248 {
   228 {
   249     Q_D( HbCheckBox );
   229     Q_D( HbCheckBox );
   251         d->mText = text;
   231         d->mText = text;
   252         //updatePrimitives();
   232         //updatePrimitives();
   253         if( d->mTextItem ){
   233         if( d->mTextItem ){
   254             HbStyleOptionCheckBox checkBoxOption;
   234             HbStyleOptionCheckBox checkBoxOption;
   255             initStyleOption( &checkBoxOption );
   235             initStyleOption( &checkBoxOption );
   256             style( )->updatePrimitive( d->mTextItem, HbStyle::P_CheckBox_text, &checkBoxOption );
   236             HbStylePrivate::updatePrimitive( d->mTextItem, HbStylePrivate::P_CheckBox_text, &checkBoxOption );
   257         }
   237         }
   258     }    
   238     }    
   259 }
   239 }
   260 
   240 
   261 /*!
   241 /*!
   262     @beta
   242     
   263     Returns the text of checkbox.
   243     Returns the text of a check box.
       
   244 
   264     \sa setText()
   245     \sa setText()
   265 */
   246 */
   266 QString HbCheckBox::text( ) const
   247 QString HbCheckBox::text( ) const
   267 {
   248 {
   268     Q_D( const HbCheckBox );
   249     Q_D( const HbCheckBox );
   269     return d->mText;
   250     return d->mText;
   270 }
   251 }
   271 
   252 
   272 /*!
   253 /*!
   273     @beta
   254     
   274     Sets tristate support for checkbox based upon \a flag.
   255     Sets a check box to:
   275     By default tristate is false and checkbox has got only two states.
   256     - tristate-enabled mode if \a isTristate is \c true.
       
   257     - twostate-enabled mode if \a isTristate is \c false (default).
       
   258     
   276     \sa isTristate( )
   259     \sa isTristate( )
   277 */
   260 */
   278 void HbCheckBox::setTristate( bool isTristate )
   261 void HbCheckBox::setTristate( bool isTristate )
   279 {
   262 {
   280     Q_D( HbCheckBox );
   263     Q_D( HbCheckBox );
   281     d->mTristate = isTristate;
   264     d->mTristate = isTristate;
   282 }
   265 }
   283 
   266 
   284 /*!
   267 /*!
   285     @beta
   268     
   286     Returns whether tristate is enabled for checkbox or not.
   269     Checks whether a check box can have two or three states.
       
   270     
       
   271     \return \c true if a check box is tristate-enabled.
       
   272     \return \c false if a check box is twostate-enabled.
       
   273 
   287     \sa setTristate( )
   274     \sa setTristate( )
   288 */
   275 */
   289 bool HbCheckBox::isTristate( ) const
   276 bool HbCheckBox::isTristate( ) const
   290 {
   277 {
   291     Q_D( const HbCheckBox );
   278     Q_D( const HbCheckBox );
   292     return d->mTristate;
   279     return d->mTristate;
   293 }
   280 }
   294 
   281 
   295 /*!
   282 /*!
   296     @beta
   283     
   297     Returns the current state of HbCheckBox.
   284     Returns the current state (checked, unchecked, or partially checked) of a 
       
   285     check box.
       
   286 
   298 */
   287 */
   299 Qt::CheckState HbCheckBox::checkState() const
   288 Qt::CheckState HbCheckBox::checkState() const
   300 {
   289 {
   301     Q_D( const HbCheckBox );
   290     Q_D( const HbCheckBox );
   302     if ( d->mTristate &&  d->mNoChange ) {
   291     if ( d->mTristate &&  d->mNoChange ) {
   318 QGraphicsItem *HbCheckBox::primitive( HbStyle::Primitive primitive ) const
   307 QGraphicsItem *HbCheckBox::primitive( HbStyle::Primitive primitive ) const
   319 {
   308 {
   320     Q_D( const HbCheckBox );
   309     Q_D( const HbCheckBox );
   321 
   310 
   322     switch ( primitive ) {
   311     switch ( primitive ) {
   323         case HbStyle::P_CheckBox_text:
   312         case HbStylePrivate::P_CheckBox_text:
   324             return d->mTextItem;
   313             return d->mTextItem;
   325         case HbStyle::P_CheckBox_toucharea:
   314         case HbStylePrivate::P_CheckBox_toucharea:
   326             return d->mTouchArea;
   315             return d->mTouchArea;
   327         case HbStyle::P_CheckBox_icon:
   316         case HbStylePrivate::P_CheckBox_icon:
   328             return d->mIconItem;
   317             return d->mIconItem;
   329         default:
   318         default:
   330             return 0;
   319             return 0;
   331     }
   320     }
   332 }
   321 }
   333 
   322 
   334 /*!
   323 /*!
   335     @beta
   324     
   336     Sets the state of HbCheckBox to the \a state passed.
   325     Sets the state of a check box to \a state and emits the stateChanged() 
   337     Calls updatePrimitive when state is changed.
   326     signal. If the check box state changes, updatePrimitives() is called
   338     Emits signal stateChanged(state).
   327     
   339 */
   328 */
   340 void HbCheckBox::setCheckState( Qt::CheckState state )
   329 void HbCheckBox::setCheckState( Qt::CheckState state )
   341 {
   330 {
   342     Q_D( HbCheckBox );
   331     Q_D( HbCheckBox );
   343     if (state == Qt::PartiallyChecked) {
   332     if (state == Qt::PartiallyChecked) {
   356         emit stateChanged( state );
   345         emit stateChanged( state );
   357     }
   346     }
   358 }
   347 }
   359 
   348 
   360 /*!
   349 /*!
   361     Updates the icon and text primitives.
   350     Updates the icon and the text primitives of a check box.
   362 */
   351 */
   363 void HbCheckBox::updatePrimitives( )
   352 void HbCheckBox::updatePrimitives( )
   364 {
   353 {
   365     HbWidget::updatePrimitives( );
   354     HbWidget::updatePrimitives( );
   366     Q_D( HbCheckBox );
   355     Q_D( HbCheckBox );
   367 
   356 
   368     HbStyleOptionCheckBox checkBoxOption;
   357     HbStyleOptionCheckBox checkBoxOption;
   369     initStyleOption( &checkBoxOption );
   358     initStyleOption( &checkBoxOption );
   370 
   359 
   371     if ( d->mTextItem ) {
   360     if ( d->mTextItem ) {
   372         style( )->updatePrimitive( d->mTextItem, HbStyle::P_CheckBox_text, &checkBoxOption );
   361         HbStylePrivate::updatePrimitive( d->mTextItem, HbStylePrivate::P_CheckBox_text, &checkBoxOption );
   373     }
   362     }
   374     if ( d->mIconItem ) {
   363     if ( d->mIconItem ) {
   375         style( )->updatePrimitive( d->mIconItem, HbStyle::P_CheckBox_icon, &checkBoxOption );
   364         HbStylePrivate::updatePrimitive( d->mIconItem, HbStylePrivate::P_CheckBox_icon, &checkBoxOption );
   376     }
   365     }
   377     if (d->mTouchArea) {
   366     if (d->mTouchArea) {
   378         style( )->updatePrimitive( d->mTouchArea, HbStyle::P_CheckBox_toucharea, &checkBoxOption );
   367         HbStylePrivate::updatePrimitive( d->mTouchArea, HbStylePrivate::P_CheckBox_toucharea, &checkBoxOption );
   379     }
   368     }
   380 }
   369 }
   381 
   370 
   382 /*!
   371 /*!
   383     Initializes the style for check box with the \a option passed.
   372     Initializes the style of a check box with the \a option style.
   384 */
   373 */
   385 void HbCheckBox::initStyleOption(HbStyleOptionCheckBox *option) const
   374 void HbCheckBox::initStyleOption(HbStyleOptionCheckBox *option) const
   386 {
   375 {
   387     Q_D( const HbCheckBox );
   376     Q_D( const HbCheckBox );
   388 
   377 
   396         option->state.operator = ( d->checked ? QStyle::State_On : QStyle::State_Off );
   385         option->state.operator = ( d->checked ? QStyle::State_On : QStyle::State_Off );
   397     }
   386     }
   398 }
   387 }
   399 
   388 
   400 /*!
   389 /*!
   401     Updates the primitive as per new size.
   390 
   402 */
   391 */
   403 void HbCheckBox::resizeEvent( QGraphicsSceneResizeEvent *event )
   392 void HbCheckBox::resizeEvent( QGraphicsSceneResizeEvent *event )
   404 {
   393 {
   405     HbAbstractButton::resizeEvent( event );
   394     HbAbstractButton::resizeEvent( event );
   406 }
   395 }
   407 
   396 
   408 /*!
   397 /*!
   409   Overloaded hit detection to include touch area
   398 
   410  */
   399  */
   411 bool HbCheckBox::hitButton( const QPointF &pos ) const
   400 bool HbCheckBox::hitButton( const QPointF &pos ) const
   412 {
   401 {
   413     Q_D(const HbCheckBox);
   402     Q_D(const HbCheckBox);
   414     QRectF compRect = d->mTouchArea->boundingRect( );
   403     QRectF compRect = d->mTouchArea->boundingRect( );
   415     compRect.translate( d->mTouchArea->pos( ) );
   404     compRect.translate( d->mTouchArea->pos( ) );
   416     return compRect.contains( pos );
   405     return compRect.contains( pos );
   417 }
   406 }
   418 
   407 
   419 /*!
   408 /*!
   420     \reimp.
   409 
   421 */
   410 */
   422 void HbCheckBox::checkStateSet( )
   411 void HbCheckBox::checkStateSet( )
   423 {
   412 {
   424     Q_D(HbCheckBox);
   413     Q_D(HbCheckBox);
   425     d->mNoChange = false;
   414     d->mNoChange = false;
   432         emit stateChanged( state );
   421         emit stateChanged( state );
   433     }
   422     }
   434 }
   423 }
   435 
   424 
   436 /*!
   425 /*!
   437     \reimp.
   426 
   438 */
   427 */
   439 void HbCheckBox::nextCheckState( )
   428 void HbCheckBox::nextCheckState( )
   440 {
   429 {
   441     if( checkState( ) == Qt::PartiallyChecked ) {
   430     if( checkState( ) == Qt::PartiallyChecked ) {
   442         HbAbstractButton::nextCheckState( );
   431         HbAbstractButton::nextCheckState( );
   445     HbCheckBox::checkStateSet( );
   434     HbCheckBox::checkStateSet( );
   446 }
   435 }
   447 
   436 
   448 #ifndef HB_GESTURE_FW
   437 #ifndef HB_GESTURE_FW
   449 /*!
   438 /*!
   450     \reimp.
   439 
   451 */
   440 */
   452 void HbCheckBox::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
   441 void HbCheckBox::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
   453 {
   442 {
   454     HbAbstractButton::mouseReleaseEvent( event );
   443     HbAbstractButton::mouseReleaseEvent( event );
   455     updatePrimitives();
   444     updatePrimitives();
   456 }
   445 }
   457 
   446 
   458 /*!
   447 /*!
   459     \reimp.
   448 
   460 */
   449 */
   461 void HbCheckBox::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
   450 void HbCheckBox::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
   462 {
   451 {
   463     Q_UNUSED( event );
   452     Q_UNUSED( event );
   464     // To show the tooltip on press,move outside and come back to same button.
   453     // To show the tooltip on press,move outside and come back to same button.
   475 {
   464 {
   476     HbAbstractButton::gestureEvent( event );
   465     HbAbstractButton::gestureEvent( event );
   477 }
   466 }
   478 #endif
   467 #endif
   479 /*!
   468 /*!
   480     \reimp.
   469 
   481 */
   470 */
   482 void HbCheckBox::keyPressEvent(QKeyEvent *keyEvent)
   471 void HbCheckBox::keyPressEvent(QKeyEvent *keyEvent)
   483 {
   472 {
   484     switch ( keyEvent->key( ) ) {
   473     switch ( keyEvent->key( ) ) {
   485     case Qt::Key_Select:
   474     case Qt::Key_Select:
   491             HbAbstractButton::keyPressEvent( keyEvent );
   480             HbAbstractButton::keyPressEvent( keyEvent );
   492     }
   481     }
   493 }
   482 }
   494 
   483 
   495 /*!
   484 /*!
   496     \reimp
   485 
   497  */
   486  */
   498 QVariant HbCheckBox::itemChange( GraphicsItemChange change, const QVariant &value )
   487 QVariant HbCheckBox::itemChange( GraphicsItemChange change, const QVariant &value )
   499 {
   488 {
   500     switch ( change ) {
   489     switch ( change ) {
   501         case ItemEnabledHasChanged:
   490         case ItemEnabledHasChanged: