src/hbwidgets/sliders/hbratingslider.cpp
changeset 1 f7ac710697a9
parent 0 16d8024aca5e
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
    28 #include "hbratingslider_p.h"
    28 #include "hbratingslider_p.h"
    29 #include <hbtooltip.h>
    29 #include <hbtooltip.h>
    30 #include <hbstyleoptionratingslider.h>
    30 #include <hbstyleoptionratingslider.h>
    31 #include <QGraphicsItem>
    31 #include <QGraphicsItem>
    32 #include <QGraphicsSceneMouseEvent>
    32 #include <QGraphicsSceneMouseEvent>
       
    33 #include <hbtoucharea.h>
       
    34 
       
    35 #ifdef HB_GESTURE_FW
       
    36 #include <hbtapgesture.h>
       
    37 #include <hbpangesture.h>
       
    38 #endif 
    33 
    39 
    34 #ifdef HB_EFFECTS
    40 #ifdef HB_EFFECTS
    35 #include <hbeffect.h>
    41 #include <hbeffect.h>
    36 #include "hbeffectinternal_p.h"
    42 #include "hbeffectinternal_p.h"
    37 #define HB_RATINGSLIDER_ITEM_TYPE "HB_RATINGSLIDER"
    43 #define HB_RATINGSLIDER_ITEM_TYPE "HB_RATINGSLIDER"
    69 	mLayoutItem = q->style()->createPrimitive(HbStyle::P_RatingSlider_layout,q);
    75 	mLayoutItem = q->style()->createPrimitive(HbStyle::P_RatingSlider_layout,q);
    70     mFrame = q->style()->createPrimitive(HbStyle::P_RatingSlider_frame,mLayoutItem);
    76     mFrame = q->style()->createPrimitive(HbStyle::P_RatingSlider_frame,mLayoutItem);
    71     mTrack = q->style()->createPrimitive(HbStyle::P_RatingSlider_track,mFrame);
    77     mTrack = q->style()->createPrimitive(HbStyle::P_RatingSlider_track,mFrame);
    72     mTouchArea = q->style()->createPrimitive(HbStyle::P_RatingSlider_toucharea, q);
    78     mTouchArea = q->style()->createPrimitive(HbStyle::P_RatingSlider_toucharea, q);
    73 
    79 
    74 	HbStyle::setItemName(mLayoutItem, "frame");
       
    75 	HbStyle::setItemName(mTouchArea, "toucharea");
       
    76 	q->updatePrimitives();
    80 	q->updatePrimitives();
    77 
    81 
    78 	#ifdef HB_EFFECTS
    82 	#ifdef HB_EFFECTS
    79     HbEffectInternal::add(HB_RATINGSLIDER_ITEM_TYPE,"ratingslider_appear", "ratingslider_appear");
    83     HbEffectInternal::add(HB_RATINGSLIDER_ITEM_TYPE,"ratingslider_appear", "ratingslider_appear");
    80     HbEffectInternal::add(HB_RATINGSLIDER_ITEM_TYPE,"ratingslider_disappear", "ratingslider_disappear");
    84     HbEffectInternal::add(HB_RATINGSLIDER_ITEM_TYPE,"ratingslider_disappear", "ratingslider_disappear");
    81     #endif
    85     #endif
       
    86 
       
    87     #ifdef HB_GESTURE_FW
       
    88     q->grabGesture(Qt::TapGesture);
       
    89     q->grabGesture(Qt::PanGesture);
       
    90 
       
    91     if(QGraphicsObject *touchArea = mTouchArea->toGraphicsObject()) {
       
    92         touchArea->grabGesture(Qt::TapGesture);
       
    93         touchArea->grabGesture(Qt::PanGesture);
       
    94     }
       
    95     #endif 
    82 
    96 
    83 }
    97 }
    84 
    98 
    85 void HbRatingSliderPrivate::createLookupTable()
    99 void HbRatingSliderPrivate::createLookupTable()
    86 {
   100 {
   113 	}
   127 	}
   114 	else {
   128 	else {
   115         
   129         
   116 		count++;
   130 		count++;
   117 	}
   131 	}
       
   132 
       
   133 	if (pos > mLookupValues[mStepCount-1]) {
       
   134 		return -1;
       
   135 	}
   118 	
   136 	
   119 	return count;
   137 	return count;
   120 	
   138 	
   121 }
   139 }
   122 
   140 
   123 /*!
   141 /*!
   124     @beta
       
   125     @HbWidgets
       
   126     \class HbRatingSlider
   142     \class HbRatingSlider
   127     \brief HbRatingSlider widget provides a Rating control.
   143     \brief A control for user to do rating.
   128 
   144 
   129     A HbRatingSlider is used to rate a particular movie or a song.The user can drag over the 
   145     This is a general rating widget where user will be able to do different 
   130 	slider to rate. As soon as he releases the pointer from within the area of slider the rating is done.
   146 	ratings for things like Music ,Video etc. 
   131 	The Application can configure the RatingSlider to be ReadOnly/ReadWrite at any point using setReadOnly. 
   147 
   132 	The Application can configure a tooltip for rating assistance.
   148 	By default there are 5 ratings ( 5 stars ). This can be configured also.
   133 
   149 	The interval , number of icons etc can be configured.
   134 	By default there are 5 stars.User can rate in the range 1-5. By changinng the maximum it is possible to 
   150 
   135 	attain any number of ratings. Once the rating is done the HbRatingSlider emits the signal ratingChanged. 
   151 	Apart from rating the this can used for showing cumulative rating also.
   136 	Parameter of this signal is the new rating value. Which lies in the range min-max.
   152 
   137 */
   153     example code example:
   138 
   154     \code
   139 
   155     HbRatingSlider *object = new HbRatingSlider(parent);
   140 /*!
   156     \endcode
   141     @beta
   157 
   142     Constructs a RatingSlider  a \a parent.
   158     The below  code can be used to show some rating e.g. 2.5/5
       
   159 	by default the  stepcount =5
       
   160     
       
   161 	\code
       
   162 	HbRatingSlider *slider = new HbRatingSlider();
       
   163 	slider->setStepCount(100); //5 *20//
       
   164 	slider->setCurrentRating(50); //2.5*20 it shows 50 / 100 which is same as 2.5/5
       
   165     \endcode
       
   166 	
       
   167 	This will show as 2.5/5. Now if one the same ratingslider 
       
   168 	if the Application wants to configure a rating slider with range 1-5
       
   169 	on emitting the signal rating changed it can set to 
       
   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  */
       
   177 
       
   178 
       
   179 
       
   180 /*!
       
   181     @beta
       
   182 	Constructor of  RatingSlider.
       
   183     \param parent. Parent widget
       
   184 
   143 */
   185 */
   144 
   186 
   145 HbRatingSlider::HbRatingSlider(QGraphicsItem *parent) :
   187 HbRatingSlider::HbRatingSlider(QGraphicsItem *parent) :
   146 HbWidget(*new HbRatingSliderPrivate,parent)
   188 HbWidget(*new HbRatingSliderPrivate,parent)
   147 {
   189 {
   151 }
   193 }
   152 
   194 
   153 
   195 
   154 /*!
   196 /*!
   155     @beta
   197     @beta
   156     @HbWidgets
   198 	Constructor of  RatingSlider.
   157     \class HbRatingSlider
   199     \param parent. Parent widget
   158     \brief Constructs a basic Rating Slider
       
   159 */
   200 */
   160 HbRatingSlider::HbRatingSlider(HbRatingSliderPrivate &dd,QGraphicsItem *parent) : 
   201 HbRatingSlider::HbRatingSlider(HbRatingSliderPrivate &dd,QGraphicsItem *parent) : 
   161     HbWidget( dd,parent)
   202     HbWidget( dd,parent)
   162 {
   203 {
   163     Q_D( HbRatingSlider );
   204     Q_D( HbRatingSlider );
   173 {
   214 {
   174 }
   215 }
   175 
   216 
   176 /*!
   217 /*!
   177     @beta
   218     @beta
   178     Sets  the read only flag of the Rating slider. If the ReadOnly flag is true then Rating slider is not 
   219     Sets the read only property. It disables the interaction with widget
   179 	interactive.Once the Rating is done The application can decide it to allow rating again or not by setting this 
   220 
   180 	flag.
   221     \param value true or false.
   181 
   222 
   182 */
   223     \sa readOnly()
       
   224 */
       
   225 
   183 void HbRatingSlider::setReadOnly(bool value)
   226 void HbRatingSlider::setReadOnly(bool value)
   184 {
   227 {
   185 	Q_D(HbRatingSlider);
   228 	Q_D(HbRatingSlider);
   186 	d->mReadOnly = value;
   229 	d->mReadOnly = value;
   187 }
   230 }
   188 
   231 
   189 /*!
   232 /*!	
   190     @beta
   233 	
       
   234 	@beta  
   191     Sets the number of icons. In a Rating scenario you may have number of repeated icons. This API can be used to set 
   235     Sets the number of icons. In a Rating scenario you may have number of repeated icons. This API can be used to set 
   192 	the number of icons required. For Example the default image is "*" and you have 5 stars. You can set the number of 
   236 	the number of icons required. For Example the default image is "*" and you have 5 stars. You can set the number of 
   193 	stars  using this. By default this value is 5.
   237 	stars  using this. By default this value is 5.
   194 
   238 
       
   239     \param number. A value between 1 and 10 
       
   240 
       
   241     \sa numberOfIcons()
   195 */
   242 */
   196 
   243 
   197 void HbRatingSlider::setNumberOfIcons(int number)
   244 void HbRatingSlider::setNumberOfIcons(int number)
   198 {
   245 {
   199 	Q_D(HbRatingSlider);
   246 	Q_D(HbRatingSlider);
   203 	d->mNumberOfIcons = number;
   250 	d->mNumberOfIcons = number;
   204 	updatePrimitives();
   251 	updatePrimitives();
   205 	d->createLookupTable();
   252 	d->createLookupTable();
   206 }
   253 }
   207 
   254 
   208 /*!
   255 
   209     @beta
   256 /*!		
       
   257 	@beta  
   210     Returns the number of icons set.
   258     Returns the number of icons set.
   211 
   259 
   212 */
   260     \sa setNumberOfIcons()
       
   261 */
       
   262 
   213 int HbRatingSlider::numberOfIcons() const
   263 int HbRatingSlider::numberOfIcons() const
   214 {
   264 {
   215 	Q_D(const HbRatingSlider);
   265 	Q_D(const HbRatingSlider);
   216 	return d->mNumberOfIcons;
   266 	return d->mNumberOfIcons;
   217 }
   267 }
   219 /*!
   269 /*!
   220     @beta
   270     @beta
   221     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.
   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.
   222 	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 
   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 
   223 	case one rating will be one complete star. By default this value is 5.
   273 	case one rating will be one complete star. By default this value is 5.
       
   274 	
       
   275 	\param count. A value between 1 and 100. This can be considerd as the maximum rating possible. 
       
   276 
       
   277     \sa numberOfIcons()
   224 
   278 
   225 */
   279 */
   226 void HbRatingSlider::setStepCount(int count)
   280 void HbRatingSlider::setStepCount(int count)
   227 {
   281 {
   228 	Q_D(HbRatingSlider);
   282 	Q_D(HbRatingSlider);
   229 	if( (count <= 0) || (count >= 20) ) {
   283 	if( (count <= 0) || (count > 100) ) {
   230 		return;
   284 		return;
   231 	}
   285 	}
   232 	d->mStepCount = count;
   286 	d->mStepCount = count;
   233 	d->createLookupTable();	
   287 	d->createLookupTable();	
   234 	
   288 	
   261 	return d->mReadOnly;
   315 	return d->mReadOnly;
   262 }
   316 }
   263 
   317 
   264 /*!
   318 /*!
   265     @beta
   319     @beta
   266     Sets the current rating value.In future this will be qreal value :).
   320     It sets the current rating value.
       
   321 	\param count. A value between 1 and stepcount. 
       
   322 	\sa currentRating()
   267 
   323 
   268 */
   324 */
   269 void  HbRatingSlider::setCurrentRating(int rating)
   325 void  HbRatingSlider::setCurrentRating(int rating)
   270 {
   326 {
   271 	Q_D(HbRatingSlider);
   327 	Q_D(HbRatingSlider);
   295 	return d->mCurrentValue;
   351 	return d->mCurrentValue;
   296 }
   352 }
   297 
   353 
   298 /*!
   354 /*!
   299     @beta
   355     @beta
   300     It sets the unrated graphics name.This is the graphics shown when rating slider is displayed.
   356     
       
   357 	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 
       
   359 	setting number of stars.
       
   360 	
       
   361 	\param name. The graphics name along with the path. 
       
   362 	\sa unRatedIconName()
   301 */
   363 */
   302 void HbRatingSlider::setUnRatedIconName(const QString name)
   364 void HbRatingSlider::setUnRatedIconName(const QString name)
   303 {
   365 {
   304 	Q_D(HbRatingSlider);
   366 	Q_D(HbRatingSlider);
   305 	if(d->mUnratedIconName != name) {
   367 	if(d->mUnratedIconName != name) {
   323 
   385 
   324 }
   386 }
   325 
   387 
   326 /*!
   388 /*!
   327     @beta
   389     @beta
   328     It sets the rated graphics name.This is the graphics shown when rating is done. 
   390     
       
   391 	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 
       
   393 	setting number of stars.
       
   394 	
       
   395 	\param name. The graphics name along with the path. 
       
   396 	\sa unRatedIconName()
   329 */
   397 */
   330 void HbRatingSlider::setRatedIconName(const QString name)
   398 void HbRatingSlider::setRatedIconName(const QString name)
   331 {
   399 {
   332 	Q_D(HbRatingSlider);
   400 	Q_D(HbRatingSlider);
   333 	if(d->mRatedIconName != name) {
   401 	if(d->mRatedIconName != name) {
   346 {
   414 {
   347 	Q_D(const HbRatingSlider);
   415 	Q_D(const HbRatingSlider);
   348 	return d->mRatedIconName;
   416 	return d->mRatedIconName;
   349 }
   417 }
   350 
   418 
       
   419 #ifndef HB_GESTURE_FW
   351 /*!
   420 /*!
   352     \reimp
   421     \reimp
   353 */
   422 */
   354 void HbRatingSlider::mousePressEvent(QGraphicsSceneMouseEvent *event) 
   423 void HbRatingSlider::mousePressEvent(QGraphicsSceneMouseEvent *event) 
   355 {
   424 {
   448 			event->accept();
   517 			event->accept();
   449 			d->mMousePressed = false;
   518 			d->mMousePressed = false;
   450 		}
   519 		}
   451 	
   520 	
   452 	}		
   521 	}		
   453 
   522 }
   454 }
   523 #else
       
   524 void HbRatingSlider::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   525 {
       
   526     Q_UNUSED(event)
       
   527 }
       
   528 #endif
       
   529 
       
   530 #ifdef HB_GESTURE_FW
       
   531 void HbRatingSlider::gestureEvent(QGestureEvent *event)
       
   532 {
       
   533     Q_D (HbRatingSlider);
       
   534     if(event->gesture(Qt::TapGesture)) {
       
   535             HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture));
       
   536             switch(tap->state()) {
       
   537             case Qt::GestureStarted:
       
   538 				{
       
   539 				qreal xVal = mapFromScene(event->mapToGraphicsScene(tap->position( ))).x();
       
   540 				int rating = d->calculateProgressValue(xVal);
       
   541 				if(rating == -1) {
       
   542 					return;
       
   543 				}
       
   544                 if(d->mReadOnly) {	
       
   545                     event->ignore();
       
   546                     return;
       
   547                 }
       
   548 
       
   549                 d->mMousePressed = true;
       
   550                 event->accept();
       
   551 				}
       
   552                 break;
       
   553  
       
   554             case Qt::GestureFinished: // Reset state 
       
   555                 {
       
   556             qreal xVal = mapFromScene(event->mapToGraphicsScene(tap->position( ))).x();
       
   557             QRectF rect = d->mTouchArea->boundingRect();
       
   558             int rating=0;
       
   559             if(rect.contains(xVal,0 )) {
       
   560                 if(d->mReadOnly) {
       
   561                     event->ignore();
       
   562                     return;
       
   563                 }
       
   564 
       
   565 				if(!d->mMousePressed){
       
   566 					return;
       
   567 				}
       
   568 
       
   569                 if(xVal <0) {	
       
   570                     setCurrentRating(0);
       
   571 					emit ratingDone (d->mCurrentValue);
       
   572                     return;
       
   573                 }
       
   574 
       
   575                rating = d->calculateProgressValue(xVal);
       
   576 		
       
   577 			   if(toolTip() != QString()) {
       
   578                     HbToolTip::showText(toolTip(),this);
       
   579                 }	
       
   580                 setCurrentRating(rating);
       
   581                 if(d->mCurrentValue) {
       
   582                     emit ratingDone (d->mCurrentValue);
       
   583                 }
       
   584                 event->accept();
       
   585                 d->mMousePressed = false;
       
   586                 }
       
   587             }
       
   588             break;
       
   589             default: break;
       
   590             } 
       
   591     }else if(event->gesture(Qt::PanGesture)) {
       
   592                 HbPanGesture *pan = qobject_cast<HbPanGesture *>(event->gesture(Qt::PanGesture));
       
   593                 switch(pan->state()) {
       
   594                     case Qt::GestureUpdated:
       
   595                         {
       
   596                         if(!d->mMousePressed) {
       
   597 		                    return;
       
   598 	                    }
       
   599                         qreal xVal = mapFromScene(event->mapToGraphicsScene( pan->startPos()+pan->offset())).x();
       
   600 		                    QRectF rect = d->mTouchArea->boundingRect();
       
   601 		                    int rating=0;
       
   602 		                    if(rect.contains(xVal,0 )) {
       
   603                                 if(d->mReadOnly) {
       
   604 			                    event->ignore();
       
   605 			                    return;
       
   606 		                    }
       
   607                     		
       
   608 		                    if(xVal <0) {	
       
   609 			                    setCurrentRating(0);
       
   610 								return;
       
   611 		                    }
       
   612 
       
   613 			                    rating = d->calculateProgressValue(xVal);
       
   614 								
       
   615 			                    if(toolTip() != QString()) {
       
   616 				                    HbToolTip::showText(toolTip(),this);
       
   617 			                    }	
       
   618 			                    setCurrentRating(rating);
       
   619 			                    emit ratingChanged (d->mCurrentValue);
       
   620 			                    event->accept();
       
   621 		                    }
       
   622 	                        else {
       
   623 			                    setCurrentRating(0);
       
   624 		                    }
       
   625                         }
       
   626                         break;
       
   627 				    case Qt::GestureFinished: // Reset state 
       
   628 					{						  
       
   629 						 qreal xVal = mapFromScene(event->mapToGraphicsScene( pan->startPos()+pan->offset())).x();
       
   630                          QRectF rect = d->mTouchArea->boundingRect();
       
   631                          int rating=0;
       
   632                          if(rect.contains(xVal,0 )) {
       
   633                             if(d->mReadOnly) {
       
   634                                event->ignore();
       
   635                                return;
       
   636                              }
       
   637 						}
       
   638 
       
   639 						if(!d->mMousePressed) {
       
   640 							 return;
       
   641 						}
       
   642 
       
   643                        if(xVal <0) {	
       
   644                           setCurrentRating(0);
       
   645 						  emit ratingDone (d->mCurrentValue);
       
   646                           return;
       
   647                         }
       
   648 
       
   649                         rating = d->calculateProgressValue(xVal);
       
   650 					    setCurrentRating(rating);
       
   651                         if(d->mCurrentValue) {
       
   652                            emit ratingDone (d->mCurrentValue);
       
   653                         }
       
   654 					    d->mMousePressed = false;
       
   655                         event->accept();
       
   656 					 }
       
   657                      default:
       
   658                      break;
       
   659                 }
       
   660     }
       
   661 }
       
   662 #endif 
       
   663 
   455 /*!
   664 /*!
   456     \reimp
   665     \reimp
   457 */
   666 */
   458 void HbRatingSlider::setGeometry(const QRectF & rect)
   667 void HbRatingSlider::setGeometry(const QRectF & rect)
   459 {
   668 {
   476 		option->progressValue = d->mCurrentValue;
   685 		option->progressValue = d->mCurrentValue;
   477 	}
   686 	}
   478 }
   687 }
   479 
   688 
   480 /*!
   689 /*!
   481     @beta
   690 
   482     Returns the primitives.
   691     \deprecated HbRatingSlider::primitive(HbStyle::Primitive)
       
   692         is deprecated.
       
   693 
       
   694     Provides access to primitives of HbRatingSlider. 
       
   695     \param primitive is the type of the requested primitive. The available 
       
   696     primitives are P_RatingSlider_frame,P_RatingSlider_track and P_RatingSlider_layout.
       
   697 
   483 */
   698 */
   484 QGraphicsItem* HbRatingSlider::primitive(HbStyle::Primitive primitive) const
   699 QGraphicsItem* HbRatingSlider::primitive(HbStyle::Primitive primitive) const
   485 {
   700 {
   486 	Q_D(const HbRatingSlider);
   701 	Q_D(const HbRatingSlider);
   487     switch (primitive) {
   702     switch (primitive) {