src/hbwidgets/sliders/hbsliderhandle.cpp
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 7 923ff622b8b9
child 21 4633027730f5
child 34 ed14f46c0e55
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    38 #define HB_SLIDERHANDLE_TYPE "HB_SLIDERHANDLE"
    38 #define HB_SLIDERHANDLE_TYPE "HB_SLIDERHANDLE"
    39 #endif
    39 #endif
    40 #ifdef HB_GESTURE_FW
    40 #ifdef HB_GESTURE_FW
    41 #include <hbtapgesture.h>
    41 #include <hbtapgesture.h>
    42 #include <hbpangesture.h>
    42 #include <hbpangesture.h>
       
    43 #include <hbtapandholdgesture.h>
    43 #endif
    44 #endif
    44 /*!
    45 /*!
    45     This is internal class for HbSlider
    46     This is internal class for HbSlider
    46     this created slider handle
    47     this created slider handle
    47     It also has event handling for handle press/move and release
    48     It also has event handling for handle press/move and release
    61     mHandleItem(0)
    62     mHandleItem(0)
    62 {
    63 {
    63     // create handle icon item
    64     // create handle icon item
    64     gItem = slider->style()->createPrimitive(HbStyle::P_Slider_thumb, this); 
    65     gItem = slider->style()->createPrimitive(HbStyle::P_Slider_thumb, this); 
    65     HbStyle::setItemName(gItem , "icon");
    66     HbStyle::setItemName(gItem , "icon");
       
    67     setFiltersChildEvents(true) ;
    66 
    68 
    67     // create touch area for handle
    69     // create touch area for handle
    68     touchItem = slider->style()->createPrimitive(HbStyle::P_SliderElement_touchhandle, this); 
    70     touchItem = slider->style()->createPrimitive(HbStyle::P_SliderElement_touchhandle, this); 
    69     HbStyle::setItemName(touchItem , "toucharea");
    71     HbStyle::setItemName(touchItem , "toucharea");
    70 
    72 
    71     setZValue(slider->zValue() + 1);
    73     setZValue(slider->zValue() + 1);
    72 #ifdef HB_GESTURE_FW    
    74 #ifdef HB_GESTURE_FW    
    73     grabGesture(Qt::TapGesture);
    75     grabGesture(Qt::TapGesture);
    74     grabGesture(Qt::PanGesture);
    76     grabGesture(Qt::PanGesture);
       
    77     grabGesture(Qt::TapAndHoldGesture);
    75     if(touchItem) {
    78     if(touchItem) {
    76         if(QGraphicsObject *touchArea = touchItem->toGraphicsObject()) {
    79         if(QGraphicsObject *touchArea = touchItem->toGraphicsObject()) {
    77             Q_UNUSED(touchArea);
    80             touchArea->grabGesture(Qt::PanGesture);
    78                touchArea->grabGesture(Qt::PanGesture);
    81             touchArea->grabGesture(Qt::TapGesture);
    79                touchArea->grabGesture(Qt::TapGesture);
    82             touchArea->grabGesture(Qt::TapAndHoldGesture);
    80         }
    83         }
    81     }
    84     }
    82 #endif 
    85 #endif 
    83 #ifdef HB_EFFECTS
    86 #ifdef HB_EFFECTS
    84     // horizontal thumb press
    87     // horizontal thumb press
    93     //HbEffectInternal::add(HB_SLIDERHANDLE_TYPE,"sliderhandle_h_outofbound", "h_outofbound");
    96     //HbEffectInternal::add(HB_SLIDERHANDLE_TYPE,"sliderhandle_h_outofbound", "h_outofbound");
    94     //vertical out of bound
    97     //vertical out of bound
    95     //HbEffectInternal::add(HB_SLIDERHANDLE_TYPE,"sliderhandle_v_outofbound", "v_outofbound");
    98     //HbEffectInternal::add(HB_SLIDERHANDLE_TYPE,"sliderhandle_v_outofbound", "v_outofbound");
    96 #endif
    99 #endif
    97 }
   100 }
       
   101 
       
   102 
    98 
   103 
    99 /*!
   104 /*!
   100     Destroys the slider handle.
   105     Destroys the slider handle.
   101 */
   106 */
   102 HbSliderHandle::~HbSliderHandle()
   107 HbSliderHandle::~HbSliderHandle()
   160 
   165 
   161     if(scene()) {
   166     if(scene()) {
   162         mHandleItem->removeSceneEventFilter(this);
   167         mHandleItem->removeSceneEventFilter(this);
   163         mHandleItem->installSceneEventFilter(this);
   168         mHandleItem->installSceneEventFilter(this);
   164     }
   169     }
   165 	mHandleItem->setFlag(QGraphicsItem::ItemIsMovable,true);//required as the press has to be accepted to filter move events.
       
   166 
   170 
   167 }
   171 }
   168 
   172 
   169 QGraphicsItem *HbSliderHandle::handleItem() const
   173 QGraphicsItem *HbSliderHandle::handleItem() const
   170 {
   174 {
   186            
   190            
   187     }
   191     }
   188 }
   192 }
   189 bool HbSliderHandle::sceneEventFilter(QGraphicsItem *obj,QEvent *event)
   193 bool HbSliderHandle::sceneEventFilter(QGraphicsItem *obj,QEvent *event)
   190 {
   194 {
   191     //TODO: touch area doesnt work with the current filtering mechanism. find better solution
   195     //TODO: touch area does not work with the current filtering mechanism. find better solution
   192 	if( obj == mHandleItem) {
   196 	if( obj == mHandleItem) {
   193 		if(event->type() == QEvent::GraphicsSceneMouseMove){
   197         if(event->type() == QEvent::GraphicsSceneMouseMove){
   194 			mouseMoveEvent ( (QGraphicsSceneMouseEvent *) event ) ;
   198             mouseMoveEvent ((QGraphicsSceneMouseEvent *) event) ;
   195 			return true;
   199             return true;
   196 		}
   200         } else if (event->type() == QEvent::GraphicsSceneMousePress){
   197 		else if (event->type() == QEvent::GraphicsSceneMousePress){
   201             mousePressEvent((QGraphicsSceneMouseEvent *) event);
   198 			mousePressEvent((QGraphicsSceneMouseEvent *) event);
   202             return true;
   199 		}
   203         } else if (event->type() == QEvent::GraphicsSceneMouseRelease){
   200 		else if (event->type() == QEvent::GraphicsSceneMouseRelease){
   204             mouseReleaseEvent((QGraphicsSceneMouseEvent *) event);
   201 			mouseReleaseEvent((QGraphicsSceneMouseEvent *) event);
   205             return true;
   202 		}
   206         }
   203 	} 
   207 	} 
   204 	return false;
   208     if( obj == touchItem ) {
       
   209         if (!isEnabled() ) {
       
   210             return false;
       
   211         }
       
   212 	    if (event->type() == QEvent::Gesture){
       
   213             gestureEvent( (QGestureEvent *) (event));
       
   214             return true;
       
   215         }
       
   216     }
       
   217     return false;
   205 }
   218 }
   206  
   219  
   207 /*!
   220 /*!
   208   reimp
   221   reimp
   209 
   222 
   210 */
   223 */
   211 void HbSliderHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
   224 void HbSliderHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
   212 {
   225 {
       
   226 #ifndef HB_GESTURE_FW
   213     qreal span = 0;
   227     qreal span = 0;
   214     HbWidget::mousePressEvent(event);
   228     HbWidget::mousePressEvent(event);
   215     QRectF sliderBounds=sliderControl->boundingRect();
   229     QRectF sliderBounds=sliderControl->boundingRect();
   216     QRectF handleBounds=boundingRect();
   230     QRectF handleBounds=boundingRect();
   217     sliderControl->setSliderDown(true);
   231     sliderControl->setSliderDown(true);
   237         handleDragPos= QStyle::sliderPositionFromValue(opt.minimum, opt.maximum,
   251         handleDragPos= QStyle::sliderPositionFromValue(opt.minimum, opt.maximum,
   238             sliderControl->sliderPosition(), static_cast<int>(span),opt.upsideDown);
   252             sliderControl->sliderPosition(), static_cast<int>(span),opt.upsideDown);
   239         updatePrimitives();
   253         updatePrimitives();
   240     }
   254     }
   241     event->accept();
   255     event->accept();
       
   256 #else
       
   257     Q_UNUSED(event)
       
   258 #endif 
   242 }
   259 }
   243 
   260 
   244 /*!
   261 /*!
   245   reimp
   262   reimp
   246 
   263 
   247 */
   264 */
   248 void HbSliderHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
   265 void HbSliderHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
   249 {
   266 {
       
   267 #ifndef HB_GESTURE_FW
   250     HbWidget::mouseReleaseEvent(event);
   268     HbWidget::mouseReleaseEvent(event);
   251 #ifdef HB_EFFECTS
   269 #ifdef HB_EFFECTS
   252     if( sliderControl->orientation() == Qt::Horizontal ) {
   270     if( sliderControl->orientation() == Qt::Horizontal ) {
   253         HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "h_thumbrelease");
   271         HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "h_thumbrelease");
   254     }
   272     }
   268     event->accept();
   286     event->accept();
   269     if( sliderControl->snappingMode()!= HbSliderControl::NoSnapping ){
   287     if( sliderControl->snappingMode()!= HbSliderControl::NoSnapping ){
   270         sliderControl->updateSliderPosToTick();
   288         sliderControl->updateSliderPosToTick();
   271     }
   289     }
   272     updatePrimitives();
   290     updatePrimitives();
       
   291 #else
       
   292     Q_UNUSED(event)
       
   293 #endif 
   273 }
   294 }
   274 
   295 
   275 /*!
   296 /*!
   276   reimp
   297   reimp
   277 
   298 
   278 */
   299 */
   279 void HbSliderHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
   300 void HbSliderHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
   280 {
   301 {
       
   302 #ifndef HB_GESTURE_FW
   281     qreal span = 0;
   303     qreal span = 0;
   282     HbWidget::mouseMoveEvent(event);
   304     HbWidget::mouseMoveEvent(event);
   283     QRectF sliderBounds=sliderControl->boundingRect();
   305     QRectF sliderBounds=sliderControl->boundingRect();
   284     QRectF handleBounds=boundingRect();
   306     QRectF handleBounds=boundingRect();
   285     HbStyleOptionSlider opt;
   307     HbStyleOptionSlider opt;
   310     }
   332     }
   311     int pressValue= QStyle::sliderValueFromPosition(opt.minimum, opt.maximum,
   333     int pressValue= QStyle::sliderValueFromPosition(opt.minimum, opt.maximum,
   312         sliderPos, static_cast<int>(span),opt.upsideDown);
   334         sliderPos, static_cast<int>(span),opt.upsideDown);
   313     sliderControl->setSliderPosition(pressValue);
   335     sliderControl->setSliderPosition(pressValue);
   314     sliderControl->showToolTip();
   336     sliderControl->showToolTip();
       
   337 #else
       
   338     Q_UNUSED(event)
       
   339 #endif 
   315 }
   340 }
   316 
   341 
   317 /*!
   342 /*!
   318   reimp
   343   reimp
   319 
   344 
   320 */
   345 */
   321 void HbSliderHandle::gestureEvent(QGestureEvent *event)
   346 void HbSliderHandle::gestureEvent(QGestureEvent *event)
   322 {
   347 { 
   323     Q_UNUSED(event);
   348     if(HbTapAndHoldGesture *tapandHold= qobject_cast<HbTapAndHoldGesture *>(event->gesture(Qt::TapAndHoldGesture))) {
   324     // HbWidgetBase::gestureEvent() ignores, overriding to accept
   349         if(tapandHold->state() == Qt::GestureStarted) {
       
   350             sliderControl->showToolTip();
       
   351         }
       
   352     }
       
   353     if(HbTapGesture *tap = qobject_cast<HbTapGesture *>(event->gesture(Qt::TapGesture))) {
       
   354         switch(tap->state()) {
       
   355         case Qt::GestureStarted: {
       
   356             if (!sliderControl->isSliderDown( ) ) {
       
   357                 sliderControl->setSliderDown(true);
       
   358                 updatePrimitives();
       
   359             }
       
   360             HbWidgetFeedback::triggered(sliderControl, Hb::InstantPressed, Hb::ModifierSliderHandle);
       
   361             if (scene()){
       
   362                 HbStyleOptionSlider opt;
       
   363                 sliderControl->initStyleOption(&opt);
       
   364                 if (sliderControl->orientation() == Qt::Horizontal) {
       
   365 #ifdef HB_EFFECTS
       
   366                     HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "h_thumbpress");
       
   367 #endif
       
   368                 } else {
       
   369 #ifdef HB_EFFECTS
       
   370                     HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "v_thumbpress");
       
   371 #endif
       
   372                 }
       
   373                 event->accept();
       
   374             }
       
   375         }
       
   376         break;
       
   377         case Qt::GestureFinished:
       
   378          {
       
   379 #ifdef HB_EFFECTS
       
   380             if( sliderControl->orientation() == Qt::Horizontal ) {
       
   381                 HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "h_thumbrelease");
       
   382             }  else {
       
   383                 HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "v_thumbrelease");
       
   384             }
       
   385 #endif
       
   386             HbWidgetFeedback::triggered(sliderControl, Hb::InstantReleased, Hb::ModifierSliderHandle);
       
   387             sliderControl->setSliderDown(false);
       
   388             updatePrimitives();
       
   389             break;
       
   390         }
       
   391         default:
       
   392             break;
       
   393                                   
       
   394         }
       
   395     }
       
   396     if (HbPanGesture *panGesture = qobject_cast<HbPanGesture*>(event->gesture(Qt::PanGesture))) {
       
   397         switch(panGesture->state( )) {
       
   398         case Qt::GestureStarted: 
       
   399         case Qt::GestureUpdated:{
       
   400             QPointF eventScenePos =panGesture->sceneOffset( )+panGesture->sceneStartPos( );
       
   401             QPointF relativePos = mapToParent( mapFromScene(eventScenePos ));
       
   402             qreal span = 0;
       
   403             QRectF sliderBounds=sliderControl->boundingRect();
       
   404             QRectF handleBounds=boundingRect();
       
   405             HbStyleOptionSlider opt;
       
   406             sliderControl->initStyleOption(&opt);
       
   407             int sliderPos;
       
   408             if ( sliderControl->orientation() == Qt::Horizontal ) {
       
   409 #ifdef HB_EFFECTS
       
   410                 if( sliderBounds.topLeft().x() > relativePos.rx( ) ||
       
   411                     sliderBounds.bottomRight().x() < relativePos.rx()) {
       
   412                     HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "h_outofbound");
       
   413                 }
       
   414 #endif
       
   415                 sliderBounds.adjust(0, 0, -handleBounds.width(), 0);
       
   416                 span = sliderBounds.width();
       
   417                 sliderPos = relativePos.rx();
       
   418                 sliderPos-=handleBounds.width()/2;
       
   419             } else {
       
   420 #ifdef HB_EFFECTS
       
   421                 if( sliderBounds.topLeft().y() > relativePos.ry() ||
       
   422                     sliderBounds.bottomRight().y() < relativePos.ry()) {
       
   423                     HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "v_outofbound");
       
   424                 }
       
   425 #endif
       
   426                 sliderBounds.adjust(0, 0, 0, -handleBounds.height());
       
   427                 span = sliderBounds.height();
       
   428                 sliderPos = relativePos.ry();
       
   429                 sliderPos -=  handleBounds.height() / 2;
       
   430                     
       
   431             }
       
   432             int pressValue= QStyle::sliderValueFromPosition(opt.minimum, opt.maximum,
       
   433                 sliderPos, static_cast<int>(span),opt.upsideDown);
       
   434             sliderControl->setSliderPosition(pressValue);
       
   435             sliderControl->showToolTip();
       
   436             break;
       
   437         }
       
   438         case Qt::GestureFinished:
       
   439         case Qt::GestureCanceled: {
       
   440 #ifdef HB_EFFECTS
       
   441             if( sliderControl->orientation() == Qt::Horizontal ) {
       
   442                 HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "h_thumbrelease");
       
   443             }  else {
       
   444                 HbEffect::start(gItem, HB_SLIDERHANDLE_TYPE, "v_thumbrelease");
       
   445             }
       
   446 #endif
       
   447             HbWidgetFeedback::triggered(sliderControl, Hb::InstantReleased, Hb::ModifierSliderHandle);
       
   448             sliderControl->setSliderDown(false);
       
   449             QRectF controlRect = sliderControl->sceneBoundingRect();
       
   450             if(sliderControl->singleStep() != 0) {
       
   451                 HbWidgetFeedback::continuousStopped(sliderControl, Hb::ContinuousDragged);
       
   452             }
       
   453             if( sliderControl->snappingMode()!= HbSliderControl::NoSnapping ){
       
   454                 sliderControl->updateSliderPosToTick();
       
   455             }
       
   456             updatePrimitives();
       
   457             break;
       
   458             }
       
   459             default:
       
   460                 break;
       
   461         }
       
   462     }
       
   463 
   325 }
   464 }
   326 
   465 
   327 /*!
   466 /*!
   328   reimp
   467   reimp
   329 
   468