src/hbwidgets/sliders/hbratingslider.cpp
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 7 923ff622b8b9
child 21 4633027730f5
child 34 ed14f46c0e55
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    20 **
    20 **
    21 ** If you have questions regarding the use of this file, please contact
    21 ** If you have questions regarding the use of this file, please contact
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 #include "hbratingslider_p.h"
    26 
       
    27 #include <hbratingslider.h>
    26 #include <hbratingslider.h>
    28 #include "hbratingslider_p.h"
       
    29 #include <hbtooltip.h>
    27 #include <hbtooltip.h>
    30 #include <hbstyleoptionratingslider_p.h>
    28 #include <hbstyleoptionratingslider_p.h>
    31 #include <QGraphicsItem>
    29 #include <QGraphicsItem>
    32 #include <QGraphicsSceneMouseEvent>
    30 #include <QGraphicsSceneMouseEvent>
    33 #include <hbtoucharea.h>
    31 #include <hbtoucharea.h>
       
    32 #include <hbwidgetfeedback.h>
    34 
    33 
    35 #ifdef HB_GESTURE_FW
    34 #ifdef HB_GESTURE_FW
    36 #include <hbtapgesture.h>
    35 #include <hbtapgesture.h>
    37 #include <hbpangesture.h>
    36 #include <hbpangesture.h>
    38 #endif 
    37 #endif 
   138     
   137     
   139 }
   138 }
   140 
   139 
   141 /*!
   140 /*!
   142     \class HbRatingSlider
   141     \class HbRatingSlider
   143     \brief A control for user to do rating.
   142     \brief This is a widget that enables a user to rate contents like videos , music etc.
   144 
   143     \image html ratingslider.png  "A Rating Slider with rating done"
   145     This is a general rating widget where user will be able to do different 
   144 
   146     ratings for things like Music ,Video etc. 
   145     The default version of Rating Slider contains 5 repeated icons drawn side by side, using a single themed graphics.
   147 
   146     The application can replace the themed graphic with a custom graphic.
   148     By default there are 5 ratings ( 5 stars ). This can be configured also.
   147     The custom graphics should contain only one icon (eg one star)  which will be multipled by the API \a setNumberOfIcons().
   149     The interval , number of icons etc can be configured.
   148     By default it is 5 and maximum number of icons are 10.
   150 
   149 
   151     Apart from rating the this can used for showing cumulative rating also.
   150     Along with the rating Rating Slider can be used to show the cumulative rating also.
   152 
   151 
   153     example code example:
   152     To use HbRatingSlider with default settings it just needs to be created.
       
   153     example code:
   154     \code
   154     \code
   155     HbRatingSlider *object = new HbRatingSlider(parent);
   155     HbRatingSlider *object = new HbRatingSlider(parent);
   156     \endcode
   156     \endcode
   157 
   157 
   158     The below  code can be used to show some rating e.g. 2.5/5
   158     HbRatingSlider emits below signals 
   159     by default the  stepcount =5
   159 
   160     
   160     void ratingDone(int ratingValue);
       
   161     void ratingChanged(int ratingValue);
       
   162     
       
   163     ratingDone is emitted when the user does the rating and releases the finger. 
       
   164     ratingChanged is emitted when the user presses and drags the finger on Rating Slider.
       
   165 
       
   166     To use HbRatingSlider with default settings it just needs to be created. 
       
   167     example code: 
       
   168     \code 
       
   169     HbMainWindow window;
       
   170     HbRatingSlider *rs = new HbRatingSlider();
       
   171     window.addView(rs);
       
   172     \endcode 
       
   173 
       
   174     HbRatingSlider supports integer ratings.But using the API \a setStepCount() fraction ratings can also be 
       
   175     shown on Rating Slider
       
   176 
       
   177     The below  code can be used to show some rating e.g. 2.5/5       
   161     \code
   178     \code
       
   179     //2.5/5 can be set as  25/50
   162     HbRatingSlider *slider = new HbRatingSlider();
   180     HbRatingSlider *slider = new HbRatingSlider();
   163     slider->setStepCount(100); //5 *20//
   181     slider->setStepCount(50); //5 *10//
   164     slider->setCurrentRating(50); //2.5*20 it shows 50 / 100 which is same as 2.5/5
   182     slider->setCurrentRating(25); //2.5*10 it shows 25/50 which is same as 2.5/5
   165     \endcode
   183     \endcode
   166     
   184     
   167     This will show as 2.5/5. Now if one the same ratingslider 
   185     This will show as 2.5/5. Now if on the same ratingslider 
   168     if the Application wants to configure a rating slider with range 1-5
   186     the Application wants to configure a Rating Slider with range 1-5
   169     on emitting the signal rating changed it can set to 
   187     on emitting the signal rating changed it can set to 5.
   170     slider->setStepCount(5);
       
   171     slider->setCurrentRating(0)
       
   172     
       
   173     When the rating is done it emits a signal called ratingDone and when rating is 
       
   174     changed by the user by draging the pointer ratingChanged signal is emitted.   
       
   175     
       
   176  */
   188  */
   177 
   189 
   178 
   190 
   179 
   191 /*!
   180 /*!
   192     @beta
   181     @beta
   193      Constructs a Rating Slider bar with the given parent.
   182     Constructor of  RatingSlider.
   194     \param parent Parent Item.
   183     \param parent. Parent widget
   195 
   184 
   196 */
   185 */
   197 
       
   198 
       
   199 
   186 
   200 
   187 HbRatingSlider::HbRatingSlider(QGraphicsItem *parent) :
   201 HbRatingSlider::HbRatingSlider(QGraphicsItem *parent) :
   188 HbWidget(*new HbRatingSliderPrivate,parent)
   202 HbWidget(*new HbRatingSliderPrivate,parent)
   189 {
   203 {
   190     Q_D( HbRatingSlider );
   204     Q_D( HbRatingSlider );
   193 }
   207 }
   194 
   208 
   195 
   209 
   196 /*!
   210 /*!
   197     @beta
   211     @beta
   198     Constructor of  RatingSlider.
   212     Protected constructor
   199     \param parent. Parent widget
       
   200 */
   213 */
   201 HbRatingSlider::HbRatingSlider(HbRatingSliderPrivate &dd,QGraphicsItem *parent) : 
   214 HbRatingSlider::HbRatingSlider(HbRatingSliderPrivate &dd,QGraphicsItem *parent) : 
   202     HbWidget( dd,parent)
   215     HbWidget( dd,parent)
   203 {
   216 {
   204     Q_D( HbRatingSlider );
   217     Q_D( HbRatingSlider );
   230 }
   243 }
   231 
   244 
   232 /*!    
   245 /*!    
   233     
   246     
   234     @beta  
   247     @beta  
   235     Sets the number of icons. In a Rating scenario you may have number of repeated icons. This API can be used to set 
   248     Sets the number of icons. There can be n number of repeated icons. This method can be used to set 
   236     the number of icons required. For Example the default image is "*" and you have 5 stars. You can set the number of 
   249     the number of icons required.The default image is "*" and has 5 stars.
   237     stars  using this. By default this value is 5.
       
   238 
   250 
   239     \param number. A value between 1 and 10 
   251     \param number. A value between 1 and 10 
   240 
   252 
   241     \sa numberOfIcons()
   253     \sa numberOfIcons()
   242 */
   254 */
   266     return d->mNumberOfIcons;
   278     return d->mNumberOfIcons;
   267 }
   279 }
   268 
   280 
   269 /*!
   281 /*!
   270     @beta
   282     @beta
   271     Sets the step count for the rating slider. If the number of icons is 5 and step count is 10 then it is possible to have 10 ratings.
   283     Sets the step count for the Rating Slider.This indicates the interval of the rating. Eg. If step count is 10
   272     one rating will be half star (by default). If the number of icons is 5 and step count is 5 then 5 ratings are possible. In this 
   284     then 10 rating is possible.
   273     case one rating will be one complete star. By default this value is 5.
       
   274     
   285     
   275     \param count. A value between 1 and 100. This can be considerd as the maximum rating possible. 
   286     \param count. A value between 1 and 100. This can be considerd as the maximum rating possible. 
   276 
   287 
   277     \sa numberOfIcons()
   288     \sa numberOfIcons()
   278 
   289 
   352 }
   363 }
   353 
   364 
   354 /*!
   365 /*!
   355     @beta
   366     @beta
   356     
   367     
   357     It sets the unrated graphics name.This is the graphics shown when rating slider is displayed.
   368     It sets the unrated graphics name.This is the graphics shown when Rating Slider is displayed.
   358     the grpahics can be a single star kind of or multi star image. If it is single star then use setNumberOfIcons for 
   369     the graphicscan be a single star kind of or multi star image. If it is single star then use setNumberOfIcons for 
   359     setting number of stars.
   370     setting number of stars.
   360     
   371     
   361     \param name. The graphics name along with the path. 
   372     \param name. The graphics name along with the path. 
   362     \sa unRatedIconName()
   373     \sa unRatedIconName()
   363 */
   374 */
   387 
   398 
   388 /*!
   399 /*!
   389     @beta
   400     @beta
   390     
   401     
   391     It sets the rated graphics name.This is the graphics shown when rating is on going.
   402     It sets the rated graphics name.This is the graphics shown when rating is on going.
   392     the grpahics can be a single star kind of or multi star image. If it is single star then use setNumberOfIcons for 
   403     the graphicscan be a single star kind of or multi star image. If it is single star then use setNumberOfIcons for 
   393     setting number of stars.
   404     setting number of stars.
   394     
   405     
   395     \param name. The graphics name along with the path. 
   406     \param name. The graphics name along with the path. 
   396     \sa unRatedIconName()
   407     \sa unRatedIconName()
   397 */
   408 */
   430             event->ignore();
   441             event->ignore();
   431             return;
   442             return;
   432         }
   443         }
   433         d->mMousePressed = true;
   444         d->mMousePressed = true;
   434         event->accept();
   445         event->accept();
       
   446         updatePrimitives();
   435 
   447 
   436     }
   448     }
   437 
   449 
   438 }
   450 }
   439 /*!
   451 /*!
   463         
   475         
   464         QRectF rect = d->mTouchArea->boundingRect();
   476         QRectF rect = d->mTouchArea->boundingRect();
   465         int rating=0;
   477         int rating=0;
   466         if(rect.contains(xVal,0 )) {
   478         if(rect.contains(xVal,0 )) {
   467             rating = d->calculateProgressValue(xVal);
   479             rating = d->calculateProgressValue(xVal);
   468             if(toolTip() != QString()) {
   480             if(!toolTip().isNull()) {
   469                 HbToolTip::showText(toolTip(),this);
   481                 HbToolTip::showText(toolTip(),this);
   470             }    
   482             }    
   471             setCurrentRating(rating);
   483             setCurrentRating(rating);
   472             emit ratingChanged (d->mCurrentValue);
   484             emit ratingChanged (d->mCurrentValue);
   473             event->accept();
   485             event->accept();
   505         
   517         
   506         QRectF rect = d->mTouchArea->boundingRect();
   518         QRectF rect = d->mTouchArea->boundingRect();
   507         int rating=0;
   519         int rating=0;
   508         if(rect.contains(xVal,0 )) {
   520         if(rect.contains(xVal,0 )) {
   509             rating = d->calculateProgressValue(xVal);
   521             rating = d->calculateProgressValue(xVal);
   510             if(toolTip() != QString()) {
   522             if(!toolTip().isNull()) {
   511                 HbToolTip::showText(toolTip(),this);
   523                 HbToolTip::showText(toolTip(),this);
   512             }    
   524             }    
   513             setCurrentRating(rating);
   525             setCurrentRating(rating);
   514             if(d->mCurrentValue) {
   526             if(d->mCurrentValue) {
   515                 emit ratingDone (d->mCurrentValue);
   527                 emit ratingDone (d->mCurrentValue);
   516             }
   528             }
   517             event->accept();
   529             event->accept();
   518             d->mMousePressed = false;
   530             d->mMousePressed = false;
   519         }
   531         }
       
   532         updatePrimitives();
   520     
   533     
   521     }        
   534     }        
   522 }
   535 }
   523 #else
   536 #else
       
   537 /*!
       
   538     \reimp
       
   539  */
   524 void HbRatingSlider::mousePressEvent(QGraphicsSceneMouseEvent *event)
   540 void HbRatingSlider::mousePressEvent(QGraphicsSceneMouseEvent *event)
   525 {
   541 {
   526     Q_UNUSED(event)
   542     Q_UNUSED(event)
   527 }
   543 }
   528 #endif
   544 #endif
   529 
   545 
   530 #ifdef HB_GESTURE_FW
   546 #ifdef HB_GESTURE_FW
       
   547 /*!
       
   548     \reimp
       
   549  */
   531 void HbRatingSlider::gestureEvent(QGestureEvent *event)
   550 void HbRatingSlider::gestureEvent(QGestureEvent *event)
   532 {
   551 {
   533     Q_D (HbRatingSlider);
   552     Q_D (HbRatingSlider);
   534     if(event->gesture(Qt::TapGesture)) {
   553     if(event->gesture(Qt::TapGesture)) {
   535             HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture));
   554             HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture));
   543                 }
   562                 }
   544                 if(d->mReadOnly) {    
   563                 if(d->mReadOnly) {    
   545                     event->ignore();
   564                     event->ignore();
   546                     return;
   565                     return;
   547                 }
   566                 }
   548 
   567                 QRectF rect = d->mTouchArea->boundingRect();
   549                 d->mMousePressed = true;
   568                 if(rect.contains(xVal,0 )) {
   550                 event->accept();
   569                     HbWidgetFeedback::triggered(this, Hb::InstantPressed);
       
   570                     d->mMousePressed = true;
       
   571                     updatePrimitives();
       
   572                     rating = d->calculateProgressValue(xVal);
       
   573                     setCurrentRating(rating);
       
   574                     event->accept();
       
   575                 }
       
   576                 else {
       
   577                     event->ignore();
       
   578                 }
       
   579                 
   551                 }
   580                 }
   552                 break;
   581                 break;
   553  
   582  
   554             case Qt::GestureFinished: // Reset state 
   583             case Qt::GestureFinished: // Reset state 
   555                 {
   584                 {
   572                     return;
   601                     return;
   573                 }
   602                 }
   574 
   603 
   575                rating = d->calculateProgressValue(xVal);
   604                rating = d->calculateProgressValue(xVal);
   576         
   605         
   577                if(toolTip() != QString()) {
   606                if(!toolTip().isNull()) {
   578                     HbToolTip::showText(toolTip(),this);
   607                     HbToolTip::showText(toolTip(),this);
   579                 }    
   608                 }    
   580                 setCurrentRating(rating);
   609                 setCurrentRating(rating);
       
   610                 HbWidgetFeedback::triggered(this, Hb::InstantReleased);
   581                 if(d->mCurrentValue) {
   611                 if(d->mCurrentValue) {
   582                     emit ratingDone (d->mCurrentValue);
   612                     emit ratingDone (d->mCurrentValue);
   583                 }
   613                 }
       
   614 
   584                 event->accept();
   615                 event->accept();
   585                 d->mMousePressed = false;
   616                 d->mMousePressed = false;
   586                 }
   617                 updatePrimitives();
       
   618             }            
       
   619             else {
       
   620 
       
   621                 d->mMousePressed = false;
       
   622                 updatePrimitives();
       
   623 
       
   624                 if(xVal <rect.x() )  {
       
   625 
       
   626                     setCurrentRating(0);
       
   627                     emit ratingDone (d->mCurrentValue);
       
   628                 }
       
   629             
       
   630             }
       
   631             
       
   632 
       
   633 
       
   634 
   587             }
   635             }
   588             break;
   636             break;
   589             default: break;
   637             default: break;
   590             } 
   638             } 
   591     }else if(event->gesture(Qt::PanGesture)) {
   639     }else if(event->gesture(Qt::PanGesture)) {
   610                                 return;
   658                                 return;
   611                             }
   659                             }
   612 
   660 
   613                                 rating = d->calculateProgressValue(xVal);
   661                                 rating = d->calculateProgressValue(xVal);
   614                                 
   662                                 
   615                                 if(toolTip() != QString()) {
   663                                 if(!toolTip().isNull()) {
   616                                     HbToolTip::showText(toolTip(),this);
   664                                     HbToolTip::showText(toolTip(),this);
   617                                 }    
   665                                 }    
   618                                 setCurrentRating(rating);
   666                                 setCurrentRating(rating);
       
   667                                 HbWidgetFeedback::continuousTriggered(this, Hb::ContinuousDragged);
   619                                 emit ratingChanged (d->mCurrentValue);
   668                                 emit ratingChanged (d->mCurrentValue);
   620                                 event->accept();
   669                                 event->accept();
   621                             }
   670                             }
   622                             else {
   671                             else {
   623                                 setCurrentRating(0);
   672                                 setCurrentRating(0);
   626                         break;
   675                         break;
   627                     case Qt::GestureFinished: // Reset state 
   676                     case Qt::GestureFinished: // Reset state 
   628                     {                          
   677                     {                          
   629                          qreal xVal = mapFromScene(event->mapToGraphicsScene( pan->startPos()+pan->offset())).x();
   678                          qreal xVal = mapFromScene(event->mapToGraphicsScene( pan->startPos()+pan->offset())).x();
   630                          QRectF rect = d->mTouchArea->boundingRect();
   679                          QRectF rect = d->mTouchArea->boundingRect();
       
   680                          d->mMousePressed = false;
       
   681                          updatePrimitives();
   631                          int rating=0;
   682                          int rating=0;
   632                          if(rect.contains(xVal,0 )) {
   683                          if(rect.contains(xVal,0 )) {
   633                             if(d->mReadOnly) {
   684                             if(d->mReadOnly) {
   634                                event->ignore();
   685                                event->ignore();
   635                                return;
   686                                return;
   636                              }
   687                              }
   637                         }
   688                          }
   638 
   689 
   639                         if(!d->mMousePressed) {
   690                          if(xVal <0) {    
   640                              return;
   691                             setCurrentRating(0);
   641                         }
   692                             emit ratingDone (d->mCurrentValue);
   642 
   693                             return;
   643                        if(xVal <0) {    
   694                           }
   644                           setCurrentRating(0);
   695 
   645                           emit ratingDone (d->mCurrentValue);
   696                           rating = d->calculateProgressValue(xVal);
   646                           return;
   697                           setCurrentRating(rating);
   647                         }
   698                           HbWidgetFeedback::triggered(this, Hb::InstantReleased);
   648 
   699                           if(d->mCurrentValue) {
   649                         rating = d->calculateProgressValue(xVal);
   700                              emit ratingDone (d->mCurrentValue);
   650                         setCurrentRating(rating);
   701                            }                       
   651                         if(d->mCurrentValue) {
   702                            event->accept();
   652                            emit ratingDone (d->mCurrentValue);
   703                         
   653                         }
   704                       }
   654                         d->mMousePressed = false;
   705                      
   655                         event->accept();
   706 					
   656                      }
   707 					default:
   657                      default:
   708                       break;
   658                      break;
       
   659                 }
   709                 }
   660     }
   710     }
   661 }
   711 }
   662 #endif 
   712 #endif 
   663 
   713 
   669     Q_D(HbRatingSlider);
   719     Q_D(HbRatingSlider);
   670     HbWidget::setGeometry(rect);
   720     HbWidget::setGeometry(rect);
   671     updatePrimitives();
   721     updatePrimitives();
   672     d->createLookupTable();
   722     d->createLookupTable();
   673 }
   723 }
   674 
   724 /*!
       
   725     \reimp
       
   726  */
   675 void HbRatingSlider::initStyleOption(HbStyleOption *hboption) const
   727 void HbRatingSlider::initStyleOption(HbStyleOption *hboption) const
   676 {
   728 {
   677     Q_D( const HbRatingSlider );
   729     Q_D( const HbRatingSlider );
   678      HbWidget::initStyleOption(hboption); 
   730      HbWidget::initStyleOption(hboption); 
   679     HbStyleOptionRatingSlider *option = 0;
   731     HbStyleOptionRatingSlider *option = 0;
   681         option->noOfStars = d->mNumberOfIcons;
   733         option->noOfStars = d->mNumberOfIcons;
   682         option->noOfIntervals = d->mStepCount;
   734         option->noOfIntervals = d->mStepCount;
   683         option->unRatedGraphicsName = d->mUnratedIconName;
   735         option->unRatedGraphicsName = d->mUnratedIconName;
   684         option->ratedGraphicsName = d->mRatedIconName;
   736         option->ratedGraphicsName = d->mRatedIconName;
   685         option->progressValue = d->mCurrentValue;
   737         option->progressValue = d->mCurrentValue;
       
   738         option->disableState = !isEnabled();
       
   739         option->pressedState = d->mMousePressed;
   686     }
   740     }
   687 }
   741 }
   688 
   742 
   689 /*!
   743 /*!
   690 
   744 
   708             return d->mLayoutItem;
   762             return d->mLayoutItem;
   709          default:
   763          default:
   710             return 0;
   764             return 0;
   711     }
   765     }
   712 }
   766 }
   713 
   767 /*!
       
   768     \reimp
       
   769  */
   714 void HbRatingSlider::changeEvent(QEvent *event)
   770 void HbRatingSlider::changeEvent(QEvent *event)
   715 {
   771 {
   716     HbWidget::changeEvent(event);
   772     HbWidget::changeEvent(event);
   717     switch (event->type()) {
   773     switch (event->type()) {
   718     case QEvent::LayoutDirectionChange:
   774     case QEvent::LayoutDirectionChange:
   719         updatePrimitives();
   775         updatePrimitives();
   720         break;
   776         break;
       
   777     case QEvent::EnabledChange:
       
   778          updatePrimitives();
       
   779           break;
   721     default:
   780     default:
   722         break;
   781         break;
   723     }
   782     }
   724 }
   783 }
       
   784 /*!
       
   785     \reimp
       
   786  */
   725 void HbRatingSlider::updatePrimitives()
   787 void HbRatingSlider::updatePrimitives()
   726 {
   788 {
   727     Q_D(HbRatingSlider);
   789     Q_D(HbRatingSlider);
   728     HbStyleOptionRatingSlider option;
   790     HbStyleOptionRatingSlider option;
   729     initStyleOption(&option);
   791     initStyleOption(&option);
   738     if (d->mTouchArea) {
   800     if (d->mTouchArea) {
   739         style()->updatePrimitive(d->mTouchArea, HbStyle::P_CheckBox_toucharea, &option);
   801         style()->updatePrimitive(d->mTouchArea, HbStyle::P_CheckBox_toucharea, &option);
   740     }
   802     }
   741     
   803     
   742 }
   804 }
   743 
   805 /*!
       
   806     \reimp
       
   807  */
   744 QVariant HbRatingSlider::itemChange(GraphicsItemChange change, const QVariant &value)
   808 QVariant HbRatingSlider::itemChange(GraphicsItemChange change, const QVariant &value)
   745 {
   809 {
   746     if(change == ItemVisibleHasChanged && value.toBool()){
   810     if(change == ItemVisibleHasChanged && value.toBool()){
   747         updatePrimitives();
   811         updatePrimitives();
   748     }
   812     }