src/hbwidgets/widgets/hbcheckbox.cpp
changeset 30 80e4d18b72f5
parent 23 e6ad4ef83b23
equal deleted inserted replaced
28:b7da29130b0e 30:80e4d18b72f5
   107         mIconItem = HbStylePrivate::createPrimitive( HbStylePrivate::P_CheckBox_icon, q );
   107         mIconItem = HbStylePrivate::createPrimitive( HbStylePrivate::P_CheckBox_icon, q );
   108     }
   108     }
   109 }
   109 }
   110 
   110 
   111 /*!
   111 /*!
   112     @beta
       
   113     @hbwidgets
   112     @hbwidgets
   114     \class HbCheckBox
   113     \class HbCheckBox
   115     \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.
   116     
   115     
   117     \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 
   118 
   117     partially checked states as shown in the pictures below. Check boxes are 
   119     \image html checkbox_checked.png A checkbox with checked state.
   118     typically used to represent application features that can be enabled or 
   120 
   119     disabled without affecting other features, but you can also subclass it to 
   121     \image html checkbox_partiallychecked.png A checkbox with partiallychecked state.
   120     implement different behavior.
   122 
   121 
   123     A HbCheckBox is an option button that can be switched on (checked)
   122     \image html checkbox_unchecked.png A check box in unchecked state.
   124     or off (unchecked). Checkboxes are typically used to represent
   123 
   125     features in an application that can be enabled or disabled without
   124     \image html checkbox_checked.png A check box in checked state.
   126     affecting others, but different types of behavior can be
   125 
   127     implemented.
   126     \image html checkbox_partiallychecked.png A check box in partially checked state.
   128 
   127     
   129     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() 
   130     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 
   131     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 
   132     isChecked() to query whether or not a checkbox is checked.
   131     check box is checked or not.
   133 
   132 
   134     In addition to checked and unchecked states, HbCheckBox
   133     In addition to checked and unchecked states, %HbCheckBox provides 
   135     optionally provides a third state which is PartiallyChecked. This
   134     an optional partially checked state. This state is useful when you handle 
   136     is useful for Hierarchical list item. If you need this third state,
   135     hierarchical list items, for example. If you need the partially checked 
   137     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 
   138     toggle state.
   137     with checkState().
   139 
   138 
   140     Checkbox has default icon for checked,unchecked and partiallyChecked states.
   139     A check box has default icons for checked, unchecked, and partially checked 
   141     The text can be set in the constructor or with setText().For long text checkbox will show 
   140     states. The text can be set in the constructor or with setText(). If the 
   142     multiline text,default checkbox will have text up to three lines.
   141     check box text is long, it is shown in multiple lines. By default a check box 
   143 
   142     can have up to three lines of text.
   144     Example usage:
   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 
   145     \code
   150     \code
   146     HbCheckBox *checkbox = new HbCheckBox(QString("checkbox_text"));
   151     HbCheckBox *checkbox = new HbCheckBox(QString("checkbox_text"));
   147     layout->addItem(checkbox);
   152     layout->addItem(checkbox);
   148     \endcode
   153     \endcode
   149 
   154 
   150 */
   155 */
   151 
   156 
   152 
   157 
   153 /*!
   158 /*!
       
   159 
   154     \fn void HbCheckBox::stateChanged(int state)
   160     \fn void HbCheckBox::stateChanged(int state)
   155     This signal is emitted whenever the check box's state changes,
   161 
   156     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 
   157     \a state contains the check box's new Qt::CheckState.
   163     state change of the check box.
   158 */
   164     
   159 
   165     \param  state The new state (Qt::CheckState) of the check box.
   160 
   166 
   161 
   167     \sa Qt
   162 /*!
   168 
   163     @beta
   169 */
   164     Constructor an object of HbCheckBox with default parameters.
   170 
   165     By default the state of check box will be Unchecked, text will
   171 
   166     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     
   167 */
   181 */
   168 HbCheckBox::HbCheckBox( QGraphicsItem *parent )
   182 HbCheckBox::HbCheckBox( QGraphicsItem *parent )
   169     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   183     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   170 {
   184 {
   171     Q_D( HbCheckBox );
   185     Q_D( HbCheckBox );
   177 #endif
   191 #endif
   178 
   192 
   179 }
   193 }
   180 
   194 
   181 /*!
   195 /*!
   182     @beta
   196     
   183     Constructor an object of HbCheckBox with \a text passed.
   197     Constructs a check box with \a text and the following default values:
   184     By default the state of check box will be Unchecked.
   198     - \a state is \c Qt::CheckState::Unchecked.
   185     Tristate will be false by default.
   199     - \a tristate is \c false.
       
   200     
       
   201     \sa Qt
       
   202 
   186 */
   203 */
   187 HbCheckBox::HbCheckBox( const QString &text, QGraphicsItem *parent )
   204 HbCheckBox::HbCheckBox( const QString &text, QGraphicsItem *parent )
   188     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   205     : HbAbstractButton( *new HbCheckBoxPrivate, parent )
   189 {
   206 {
   190     Q_D( HbCheckBox );
   207     Q_D( HbCheckBox );
   200 HbCheckBox::~HbCheckBox( )
   217 HbCheckBox::~HbCheckBox( )
   201 {
   218 {
   202 }
   219 }
   203 
   220 
   204 /*!
   221 /*!
   205     @beta
   222 
   206     Sets the text of checkbox.
   223     Sets the \a text shown on a check box.
   207     UpdatePrimitive is called by this functions once the text is changed.
   224 
   208     \sa text()
   225     \sa text()
   209 */
   226 */
   210 void HbCheckBox::setText( const QString &text )
   227 void HbCheckBox::setText( const QString &text )
   211 {
   228 {
   212     Q_D( HbCheckBox );
   229     Q_D( HbCheckBox );
   220         }
   237         }
   221     }    
   238     }    
   222 }
   239 }
   223 
   240 
   224 /*!
   241 /*!
   225     @beta
   242     
   226     Returns the text of checkbox.
   243     Returns the text of a check box.
       
   244 
   227     \sa setText()
   245     \sa setText()
   228 */
   246 */
   229 QString HbCheckBox::text( ) const
   247 QString HbCheckBox::text( ) const
   230 {
   248 {
   231     Q_D( const HbCheckBox );
   249     Q_D( const HbCheckBox );
   232     return d->mText;
   250     return d->mText;
   233 }
   251 }
   234 
   252 
   235 /*!
   253 /*!
   236     @beta
   254     
   237     Sets tristate support for checkbox based upon \a flag.
   255     Sets a check box to:
   238     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     
   239     \sa isTristate( )
   259     \sa isTristate( )
   240 */
   260 */
   241 void HbCheckBox::setTristate( bool isTristate )
   261 void HbCheckBox::setTristate( bool isTristate )
   242 {
   262 {
   243     Q_D( HbCheckBox );
   263     Q_D( HbCheckBox );
   244     d->mTristate = isTristate;
   264     d->mTristate = isTristate;
   245 }
   265 }
   246 
   266 
   247 /*!
   267 /*!
   248     @beta
   268     
   249     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 
   250     \sa setTristate( )
   274     \sa setTristate( )
   251 */
   275 */
   252 bool HbCheckBox::isTristate( ) const
   276 bool HbCheckBox::isTristate( ) const
   253 {
   277 {
   254     Q_D( const HbCheckBox );
   278     Q_D( const HbCheckBox );
   255     return d->mTristate;
   279     return d->mTristate;
   256 }
   280 }
   257 
   281 
   258 /*!
   282 /*!
   259     @beta
   283     
   260     Returns the current state of HbCheckBox.
   284     Returns the current state (checked, unchecked, or partially checked) of a 
       
   285     check box.
       
   286 
   261 */
   287 */
   262 Qt::CheckState HbCheckBox::checkState() const
   288 Qt::CheckState HbCheckBox::checkState() const
   263 {
   289 {
   264     Q_D( const HbCheckBox );
   290     Q_D( const HbCheckBox );
   265     if ( d->mTristate &&  d->mNoChange ) {
   291     if ( d->mTristate &&  d->mNoChange ) {
   293             return 0;
   319             return 0;
   294     }
   320     }
   295 }
   321 }
   296 
   322 
   297 /*!
   323 /*!
   298     @beta
   324     
   299     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() 
   300     Calls updatePrimitive when state is changed.
   326     signal. If the check box state changes, updatePrimitives() is called
   301     Emits signal stateChanged(state).
   327     
   302 */
   328 */
   303 void HbCheckBox::setCheckState( Qt::CheckState state )
   329 void HbCheckBox::setCheckState( Qt::CheckState state )
   304 {
   330 {
   305     Q_D( HbCheckBox );
   331     Q_D( HbCheckBox );
   306     if (state == Qt::PartiallyChecked) {
   332     if (state == Qt::PartiallyChecked) {
   319         emit stateChanged( state );
   345         emit stateChanged( state );
   320     }
   346     }
   321 }
   347 }
   322 
   348 
   323 /*!
   349 /*!
   324     Updates the icon and text primitives.
   350     Updates the icon and the text primitives of a check box.
   325 */
   351 */
   326 void HbCheckBox::updatePrimitives( )
   352 void HbCheckBox::updatePrimitives( )
   327 {
   353 {
   328     HbWidget::updatePrimitives( );
   354     HbWidget::updatePrimitives( );
   329     Q_D( HbCheckBox );
   355     Q_D( HbCheckBox );
   341         HbStylePrivate::updatePrimitive( d->mTouchArea, HbStylePrivate::P_CheckBox_toucharea, &checkBoxOption );
   367         HbStylePrivate::updatePrimitive( d->mTouchArea, HbStylePrivate::P_CheckBox_toucharea, &checkBoxOption );
   342     }
   368     }
   343 }
   369 }
   344 
   370 
   345 /*!
   371 /*!
   346     Initializes the style for check box with the \a option passed.
   372     Initializes the style of a check box with the \a option style.
   347 */
   373 */
   348 void HbCheckBox::initStyleOption(HbStyleOptionCheckBox *option) const
   374 void HbCheckBox::initStyleOption(HbStyleOptionCheckBox *option) const
   349 {
   375 {
   350     Q_D( const HbCheckBox );
   376     Q_D( const HbCheckBox );
   351 
   377 
   359         option->state.operator = ( d->checked ? QStyle::State_On : QStyle::State_Off );
   385         option->state.operator = ( d->checked ? QStyle::State_On : QStyle::State_Off );
   360     }
   386     }
   361 }
   387 }
   362 
   388 
   363 /*!
   389 /*!
   364     Updates the primitive as per new size.
   390 
   365 */
   391 */
   366 void HbCheckBox::resizeEvent( QGraphicsSceneResizeEvent *event )
   392 void HbCheckBox::resizeEvent( QGraphicsSceneResizeEvent *event )
   367 {
   393 {
   368     HbAbstractButton::resizeEvent( event );
   394     HbAbstractButton::resizeEvent( event );
   369 }
   395 }
   370 
   396 
   371 /*!
   397 /*!
   372   Overloaded hit detection to include touch area
   398 
   373  */
   399  */
   374 bool HbCheckBox::hitButton( const QPointF &pos ) const
   400 bool HbCheckBox::hitButton( const QPointF &pos ) const
   375 {
   401 {
   376     Q_D(const HbCheckBox);
   402     Q_D(const HbCheckBox);
   377     QRectF compRect = d->mTouchArea->boundingRect( );
   403     QRectF compRect = d->mTouchArea->boundingRect( );
   378     compRect.translate( d->mTouchArea->pos( ) );
   404     compRect.translate( d->mTouchArea->pos( ) );
   379     return compRect.contains( pos );
   405     return compRect.contains( pos );
   380 }
   406 }
   381 
   407 
   382 /*!
   408 /*!
   383     \reimp.
   409 
   384 */
   410 */
   385 void HbCheckBox::checkStateSet( )
   411 void HbCheckBox::checkStateSet( )
   386 {
   412 {
   387     Q_D(HbCheckBox);
   413     Q_D(HbCheckBox);
   388     d->mNoChange = false;
   414     d->mNoChange = false;
   395         emit stateChanged( state );
   421         emit stateChanged( state );
   396     }
   422     }
   397 }
   423 }
   398 
   424 
   399 /*!
   425 /*!
   400     \reimp.
   426 
   401 */
   427 */
   402 void HbCheckBox::nextCheckState( )
   428 void HbCheckBox::nextCheckState( )
   403 {
   429 {
   404     if( checkState( ) == Qt::PartiallyChecked ) {
   430     if( checkState( ) == Qt::PartiallyChecked ) {
   405         HbAbstractButton::nextCheckState( );
   431         HbAbstractButton::nextCheckState( );
   408     HbCheckBox::checkStateSet( );
   434     HbCheckBox::checkStateSet( );
   409 }
   435 }
   410 
   436 
   411 #ifndef HB_GESTURE_FW
   437 #ifndef HB_GESTURE_FW
   412 /*!
   438 /*!
   413     \reimp.
   439 
   414 */
   440 */
   415 void HbCheckBox::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
   441 void HbCheckBox::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
   416 {
   442 {
   417     HbAbstractButton::mouseReleaseEvent( event );
   443     HbAbstractButton::mouseReleaseEvent( event );
   418     updatePrimitives();
   444     updatePrimitives();
   419 }
   445 }
   420 
   446 
   421 /*!
   447 /*!
   422     \reimp.
   448 
   423 */
   449 */
   424 void HbCheckBox::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
   450 void HbCheckBox::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
   425 {
   451 {
   426     Q_UNUSED( event );
   452     Q_UNUSED( event );
   427     // 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.
   438 {
   464 {
   439     HbAbstractButton::gestureEvent( event );
   465     HbAbstractButton::gestureEvent( event );
   440 }
   466 }
   441 #endif
   467 #endif
   442 /*!
   468 /*!
   443     \reimp.
   469 
   444 */
   470 */
   445 void HbCheckBox::keyPressEvent(QKeyEvent *keyEvent)
   471 void HbCheckBox::keyPressEvent(QKeyEvent *keyEvent)
   446 {
   472 {
   447     switch ( keyEvent->key( ) ) {
   473     switch ( keyEvent->key( ) ) {
   448     case Qt::Key_Select:
   474     case Qt::Key_Select:
   454             HbAbstractButton::keyPressEvent( keyEvent );
   480             HbAbstractButton::keyPressEvent( keyEvent );
   455     }
   481     }
   456 }
   482 }
   457 
   483 
   458 /*!
   484 /*!
   459     \reimp
   485 
   460  */
   486  */
   461 QVariant HbCheckBox::itemChange( GraphicsItemChange change, const QVariant &value )
   487 QVariant HbCheckBox::itemChange( GraphicsItemChange change, const QVariant &value )
   462 {
   488 {
   463     switch ( change ) {
   489     switch ( change ) {
   464         case ItemEnabledHasChanged:
   490         case ItemEnabledHasChanged: