src/hbcore/gui/hbscrollbar.cpp
changeset 28 b7da29130b0e
parent 21 4633027730f5
child 30 80e4d18b72f5
equal deleted inserted replaced
23:e6ad4ef83b23 28:b7da29130b0e
    44 static const qreal THRESHOLD_VALUE = 2.0;
    44 static const qreal THRESHOLD_VALUE = 2.0;
    45 /*!
    45 /*!
    46     @stable
    46     @stable
    47     @hbcore
    47     @hbcore
    48     \class HbScrollBar
    48     \class HbScrollBar
    49     \brief HbScrollBar represents a scrollbar that can be used in different kinds of lists, options
    49     \brief The HbScrollBar class provides a vertical or horizontal scroll bar.
    50     menus and editors.
    50 
    51 
    51     Scroll bars indicate that more content is available than can fit within a container and
    52     A vertical scrollbar is created in this example to listview-parent and also shown:
    52     that the contents can be scrolled. A scroll bar consists of a groove and a handle (which
       
    53     is sometimes called a thumb). The position of the handle on the groove indicates the
       
    54     position of the visible content within the available contents. The size of the handle
       
    55     indicates the amount of content that is visible relative to the total. For example, a
       
    56     small handle indicates that there is a lot more content that is out of view.
       
    57 
       
    58     Scroll bars float above the content and do not need reserved space. There are two types of
       
    59     scroll bar:
       
    60 
       
    61     - <b>Indicative</b>. These scroll bars simply give an indication that the content can be
       
    62       scrolled and they do not provide features that enable scrolling (the ability to scroll
       
    63       is provided by the widget to which the scroll bar is attached). Indicative scroll bars are
       
    64       suitable for shorter lists and content that you expect the user to browse rather than to
       
    65       search for specific items. This is the default type of scroll bar.
       
    66 
       
    67     - <b>Interactive</b>. With this type of scroll bar, the user can drag the handle or press the
       
    68       groove to change the scroll position. When used in an item model view (classes derived
       
    69       from HbAbstractItemView), these scroll bars can provide index feedback (HbIndexFeedback
       
    70       class) when the user drags the scroll bar or taps on the groove. The feedback is a popup
       
    71       that shows, for example, the initial letter in an alphabetical list. Interactive scroll bars
       
    72       are particularly suitable for long lists.
       
    73 
       
    74     Call \link HbScrollBar::setInteractive() setInteractive(true)\endlink to make a scroll bar
       
    75     interactive. Call isInteractive() to discover whether a scroll bar is interactive.
       
    76 
       
    77     \image html hbscrollbar.png A list view with a vertical interactive scroll bar
       
    78 
       
    79     %HbScrollBar provides other properties that control the following aspects of a scroll bar:
       
    80 
       
    81     - <b>Value</b>. This is a real value in the range 0.0 to 1.0 that indicates how far the handle
       
    82       is from the start of the groove. For example, 0.5 means the handle is half way along the
       
    83       groove. Call setValue() to set this property.
       
    84 
       
    85     - <b>Page size</b>. This is a real value in the range 0.0 to 1.0 that indicates how much of the
       
    86       total contents are currently visible. This property also controls the size of the handle.
       
    87       For example, a page size of 0.1 indicates that one out of ten pages are visible. Call
       
    88       setPageSize() to set this property.
       
    89 
       
    90     - <b>Orientation</b>. This controls whether the scroll bar is horizontal or vertical. Call
       
    91       setOrientation() to set this property.
       
    92 
       
    93     \section _usecases_hbscrollbar Using the HbScrollBar class
       
    94 
       
    95     Although it is possible to create an HbScrollBar object directly, scroll bars are created
       
    96     automatically when you create an HbScrollArea object or one of the item view classes that
       
    97     are derived from it. This example demonstrates changing the default scroll bars
       
    98     created for an HbScrollArea object from indicative to interactive:
    53 
    99 
    54     \code
   100     \code
    55     //HbListView *listView is defined already
   101     scrollArea->verticalScrollBar()->setInteractive(true);
    56     HbScrollBar *scrollbar = new HbScrollBar(Qt::Vertical, listView);
   102     scrollArea->horizontalScrollBar()->setInteractive(true);
    57     scrollbar->show();
       
    58     \endcode
   103     \endcode
    59 
   104 
    60     By default scrollbar is hidden.
   105     You can replace the existing scroll bars by calling HbScrollArea::setHorizontalScrollBar()
    61 */
   106     and HbScrollArea::setVerticalScrollBar().
    62 
   107 
    63 /*!
   108     \sa HbScrollArea, HbAbstractItemView, HbIndexFeedback
    64     \reimp
   109  */
       
   110 
       
   111 /*!
    65     \fn int HbScrollBar::type() const
   112     \fn int HbScrollBar::type() const
    66  */
   113  */
    67 
   114 
    68 /*!
   115 /*!
    69     \fn void HbScrollBar::valueChanged( qreal value, Qt::Orientation orientation )
   116     \fn void HbScrollBar::valueChanged( qreal value, Qt::Orientation orientation );
    70 
   117 
    71     This signal is emitted when thumb position is changed by the user.
   118     This signal is emitted when the user changes the position of the handle in an interactive
    72 */
   119     scroll bar.
    73 
   120  */
    74 /*!
   121 
    75     \fn void valueChangeRequested( qreal value, Qt::Orientation orientation );
   122 /*!
    76 
   123     \fn void HbScrollBar::valueChangeRequested( qreal value, Qt::Orientation orientation );
    77     This signal is emitted when the user presses scrollbar groove.
   124 
    78 
   125     This signal is emitted when the user presses an interactive scroll bar's groove. The widget
    79     \param value, the new value of scrollbar after the change
   126     using the scroll bar must handle this signal (for example, by providing an animation that
    80   */
   127     scrolls to the target position).
       
   128 
       
   129     \param value The value to which the user wants to scroll.
       
   130     \param orientation The scroll bar's orientation.
       
   131 
       
   132     \sa HbScrollBar::setInteractive()
       
   133  */
    81 
   134 
    82 /*!
   135 /*!
    83     \primitives
   136     \primitives
    84     \primitive{groove} HbFrameItem representing the groove of a scrollbar.
   137     \primitive{groove} HbFrameItem representing the groove of a scroll bar.
    85     \primitive{handle} HbFrameItem representing the handle of a scrollbar.
   138     \primitive{handle} HbFrameItem representing the handle of a scroll bar.
    86     \primitive{toucharea} HbTouchArea representing the scrollbar toucharea.
   139     \primitive{toucharea} HbTouchArea representing the scroll bar toucharea.
    87   */
   140  */
    88 
   141 
    89 HbScrollBarPrivate::HbScrollBarPrivate():
   142 HbScrollBarPrivate::HbScrollBarPrivate():
    90         mOrientation(Qt::Vertical),
   143         mOrientation(Qt::Vertical),
    91         mCurrentPosition(0.5),
   144         mCurrentPosition(0.5),
    92         mPageSize(1.0),
   145         mPageSize(1.0),
   220     }
   273     }
   221 #endif
   274 #endif
   222 }
   275 }
   223 
   276 
   224 /*!
   277 /*!
   225     Constructs a scrollbar with \a parent.
   278     Constructs a scroll bar with the given \a parent.
   226 */
   279  */
   227 HbScrollBar::HbScrollBar( QGraphicsItem *parent ) :
   280 HbScrollBar::HbScrollBar( QGraphicsItem *parent ) :
   228         HbWidget(*new HbScrollBarPrivate, parent)
   281         HbWidget(*new HbScrollBarPrivate, parent)
   229 {
   282 {
   230     Q_D(HbScrollBar);
   283     Q_D(HbScrollBar);
   231     d->q_ptr = this;
   284     d->q_ptr = this;
   232     d->init();
   285     d->init();
   233 }
   286 }
   234 
   287 
   235 /*!
   288 /*!
   236     Constructs a scrollbar with \a orientation and \a parent.
   289     Constructs a scroll bar with the given \a orientation and \a parent.
   237 */
   290  */
   238 HbScrollBar::HbScrollBar( Qt::Orientation orientation, QGraphicsItem *parent ) :
   291 HbScrollBar::HbScrollBar( Qt::Orientation orientation, QGraphicsItem *parent ) :
   239         HbWidget( *new HbScrollBarPrivate, parent )
   292         HbWidget( *new HbScrollBarPrivate, parent )
   240 {
   293 {
   241     Q_D(HbScrollBar);
   294     Q_D(HbScrollBar);
   242     d->q_ptr = this;
   295     d->q_ptr = this;
   243     d->mOrientation = orientation;
   296     d->mOrientation = orientation;
   244     d->init();
   297     d->init();
   245 }
   298 }
   246 
   299 
   247 /*!
   300 /*!
   248     Destructor
   301     Destructor.
   249  */
   302  */
   250 HbScrollBar::~HbScrollBar()
   303 HbScrollBar::~HbScrollBar()
   251 {
   304 {
   252 }
   305 }
   253 
   306 
   254 /*!
   307 /*!
   255     Returns the value of the scrollbar. The value is in range of 0.0 to 1.0.
   308     Returns the scroll bar's \c value property. This indicates how far the handle is
   256     The value corresponds to the position of the thumb.
   309     from the start of the groove and can be in range of 0.0 to 1.0.
   257 
   310 
   258     \sa HbScrollBar::setValue()
   311     \sa HbScrollBar::setValue()
   259  */
   312  */
   260 qreal HbScrollBar::value() const
   313 qreal HbScrollBar::value() const
   261 {
   314 {
   262     Q_D( const HbScrollBar );
   315     Q_D( const HbScrollBar );
   263     return d->mCurrentPosition;
   316     return d->mCurrentPosition;
   264 }
   317 }
   265 
   318 
   266 /*!
   319 /*!
   267    Returns relative size of the visible area the scrollbar is attached to.
   320     Returns scroll bar's \c pageSize property. This is a real value in the range of 0.0 to
   268    For example if the pageSize is 0.2 it means that the overall size of the content
   321     1.0, which indicates the size of the visible content relative to the whole content.
   269    is five times larger than what is shown currently.
   322     For example, a page size of 0.2 indicates that the overall size of the content is five
   270 
   323     times larger than what is currently visible.
   271    The size is in range of 0.0 to 1.0.
   324 
   272    \sa HbScrollBar::setPageSize()
   325     \sa HbScrollBar::setPageSize()
   273  */
   326  */
   274 qreal HbScrollBar::pageSize() const
   327 qreal HbScrollBar::pageSize() const
   275 {
   328 {
   276     Q_D( const HbScrollBar );
   329     Q_D( const HbScrollBar );
   277     return d->mPageSize;
   330     return d->mPageSize;
   278 }
   331 }
   279 
   332 
   280 /*!
   333 /*!
   281     Returns the orientation of scrollbar.
   334     Returns the orientation of scroll bar.
   282 
   335 
   283     \sa HbScrollBar::setOrientation()
   336     \sa HbScrollBar::setOrientation()
   284  */
   337  */
   285 Qt::Orientation HbScrollBar::orientation() const
   338 Qt::Orientation HbScrollBar::orientation() const
   286 {
   339 {
   287     Q_D( const HbScrollBar );
   340     Q_D( const HbScrollBar );
   288     return d->mOrientation;
   341     return d->mOrientation;
   289 }
   342 }
   290 
   343 
   291 /*!
   344 /*!
   292     Returns whether scrollbar is in interactive mode.
   345     Returns true if the scroll bar is interactive and false if it is indicative.
   293 
   346 
   294     \sa HbScrollBar::setInteractive()
   347     \sa HbScrollBar::setInteractive()
   295 */
   348 */
   296 bool HbScrollBar::isInteractive() const
   349 bool HbScrollBar::isInteractive() const
   297 {
   350 {
   298     const Q_D(HbScrollBar);
   351     const Q_D(HbScrollBar);
   299     return d->mInteractive;
   352     return d->mInteractive;
   300 }
   353 }
   301 
   354 
   302 /*!
   355 /*!
   303     Sets the value of interactive property. If this value is set
   356     Sets the value of the scroll bar's \c interactive property, which controls whether the
   304     to true scrollbar is interactive, the user can change the scroll position by
   357     scroll bar is interactive or indicative (the default).
   305     dragging the thumb or pressing the groove. Dragging the thumb emits valueChanged
   358 
   306     signal and pressing the groove emits valueChangeRequested which means
   359     When the scroll bar is interactive, the user can drag the handle or press the groove to
   307     that the widget using scrollbars should handle the value change (for example
   360     change the scroll position. The following table lists the signals that an interactive
   308     by animating to the given target value).
   361     scroll bar emits when the user drags the handle or presses the groove.
   309 
   362 
   310     The default behavior is non interactive,
   363     <table border="1" style="border-collapse: collapse; border: solid;">
   311     which means that the scrollbar is just indicative.
   364     <tr><th>Signal</th><th>Description</th></tr>
       
   365     <tr><td>valueChanged()</td><td>This signal is emitted when the user drags the handle.</td></tr>
       
   366     <tr><td>valueChangeRequested()</td><td>This signal is emitted when the user presses the scroll
       
   367     bar's groove. The widget using the scroll bar must handle this signal (for example, by
       
   368     providing an animation that moves to the target position).</td></tr>
       
   369     </table>
       
   370 
       
   371     \param enabled A Boolean value; true for an interactive scroll bar, false for an indicative
       
   372            scroll bar.
   312 
   373 
   313     \sa HbScrollBar::isInteractive()
   374     \sa HbScrollBar::isInteractive()
   314 */
   375 */
   315 void HbScrollBar::setInteractive( bool enabled )
   376 void HbScrollBar::setInteractive( bool enabled )
   316 {
   377 {
   330         }
   391         }
   331     }
   392     }
   332 }
   393 }
   333 
   394 
   334 /*!
   395 /*!
   335     Sets the value of the scrollbar. The given \avalue should be from 0.0 to 1.0.
   396     Sets the scroll bar's \c value property, which controls how far the handle is from
   336     The value is used to position the thumb.
   397     the start of the groove.
       
   398 
       
   399     \param value A real value in the range 0.0 to 1.0. A value of 0.0 indicates that the
       
   400     handle is at the start of the groove and a value of 1.0 indicates that it is at the
       
   401     end.
   337 
   402 
   338     \sa HbScrollBar::value()
   403     \sa HbScrollBar::value()
   339 */
   404  */
   340 void HbScrollBar::setValue( qreal value )
   405 void HbScrollBar::setValue( qreal value )
   341 {
   406 {
   342     Q_D(HbScrollBar);        
   407     Q_D(HbScrollBar);        
   343 
   408 
   344     value = qBound(qreal(0.0), value, qreal(1.0));
   409     value = qBound(qreal(0.0), value, qreal(1.0));
   347         d->updatePosition();
   412         d->updatePosition();
   348     }
   413     }
   349 }
   414 }
   350 
   415 
   351 /*!
   416 /*!
   352     Sets the page size for the scrollbar. The page size is relative size of the visible area
   417     Sets the scroll bar's \c pageSize property, which indicates the size of the visible content
   353     the scrollbar is attached to. For example if the pageSize is 0.2 it means that the
   418     relative to the whole content. For example, a page size of 0.2 indicates that the
   354     overall size of the content is five times larger than what is shown currently.
   419     overall size of the content is five times larger than what is currently visible.
   355 
   420 
   356     \asize should be in the range of 0.0 to 1.0.
   421     \param size A real value in the range of 0.0 to 1.0.
   357     \sa HbScrollBar::pageSize()
   422     \sa HbScrollBar::pageSize()
   358 */
   423  */
   359 void HbScrollBar::setPageSize( qreal size )
   424 void HbScrollBar::setPageSize( qreal size )
   360 {
   425 {
   361     Q_D(HbScrollBar);
   426     Q_D(HbScrollBar);
   362     size = qBound(qreal(0.0), size, qreal(1.0));
   427     size = qBound(qreal(0.0), size, qreal(1.0));
   363 
   428 
   366         d->sizeHelper();
   431         d->sizeHelper();
   367     }
   432     }
   368 }
   433 }
   369 
   434 
   370 /*!
   435 /*!
   371     Sets the orientation of scrollbar.
   436     Sets the scroll bar's \c orientation property.
       
   437 
       
   438     \param orientation Set this to \c Qt::Horizontal for a horizontal scroll bar and
       
   439             \c Qt::Vertical for a vertical scroll bar.
   372 
   440 
   373     \sa HbScrollBar::orientation()
   441     \sa HbScrollBar::orientation()
   374 */
   442 */
   375 void HbScrollBar::setOrientation( Qt::Orientation orientation )
   443 void HbScrollBar::setOrientation( Qt::Orientation orientation )
   376 {
   444 {
   400         HbStylePrivate::updatePrimitive(d->mTouchArea, HbStylePrivate::P_ScrollBar_toucharea, &option);
   468         HbStylePrivate::updatePrimitive(d->mTouchArea, HbStylePrivate::P_ScrollBar_toucharea, &option);
   401     }
   469     }
   402 }
   470 }
   403 
   471 
   404 /*!
   472 /*!
   405     \reimp
   473     Reimplemented from QGraphicsWidget.
   406  */
   474  */
   407 void HbScrollBar::initStyleOption( HbStyleOptionScrollBar *option ) const
   475 void HbScrollBar::initStyleOption( HbStyleOptionScrollBar *option ) const
   408 {
   476 {
   409     Q_D(const HbScrollBar);
   477     Q_D(const HbScrollBar);
   410     HbWidget::initStyleOption(option);
   478     HbWidget::initStyleOption(option);
   415     option->groovePressed = d->mGroovePressed;
   483     option->groovePressed = d->mGroovePressed;
   416     option->interactive = d->mInteractive;
   484     option->interactive = d->mInteractive;
   417 }
   485 }
   418 
   486 
   419 /*!
   487 /*!
   420     \reimp
   488     Reimplemented from QGraphicsItem.
   421  */
   489  */
   422 void HbScrollBar::mousePressEvent( QGraphicsSceneMouseEvent *event )
   490 void HbScrollBar::mousePressEvent( QGraphicsSceneMouseEvent *event )
   423 {
   491 {
   424     Q_D(HbScrollBar);
   492     Q_D(HbScrollBar);
   425     QGraphicsWidget::mousePressEvent(event);
   493     QGraphicsWidget::mousePressEvent(event);
   489     }
   557     }
   490     event->accept();
   558     event->accept();
   491 }
   559 }
   492 
   560 
   493 /*!
   561 /*!
   494     \reimp
   562     Reimplemented from QGraphicsItem.
   495  */
   563  */
   496 void HbScrollBar::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
   564 void HbScrollBar::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
   497 {
   565 {
   498     Q_D(HbScrollBar);
   566     Q_D(HbScrollBar);
   499     QGraphicsWidget::mouseReleaseEvent(event);
   567     QGraphicsWidget::mouseReleaseEvent(event);
   519     d->emittedPos = false;
   587     d->emittedPos = false;
   520     event->accept();            
   588     event->accept();            
   521 }
   589 }
   522 
   590 
   523 /*!
   591 /*!
   524     \reimp
   592     Reimplemented from QGraphicsItem.
   525  */
   593  */
   526 void HbScrollBar::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
   594 void HbScrollBar::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
   527 {
   595 {
   528     Q_D(HbScrollBar);
   596     Q_D(HbScrollBar);
   529     QGraphicsWidget::mouseMoveEvent(event);
   597     QGraphicsWidget::mouseMoveEvent(event);
   559         }        
   627         }        
   560     }
   628     }
   561 }
   629 }
   562 
   630 
   563 /*!
   631 /*!
   564     \reimp
   632     Reimplemented from QGraphicsWidget.
   565  */
   633  */
   566 QRectF HbScrollBar::boundingRect() const
   634 QRectF HbScrollBar::boundingRect() const
   567 {
   635 {
   568     Q_D(const HbScrollBar);
   636     Q_D(const HbScrollBar);
   569     QRectF newBoundingRect = HbWidget::boundingRect();    
   637     QRectF newBoundingRect = HbWidget::boundingRect();    
   584     /* Workaround ends */
   652     /* Workaround ends */
   585     return newBoundingRect;
   653     return newBoundingRect;
   586 }
   654 }
   587 
   655 
   588 /*!
   656 /*!
   589     \reimp
   657     Reimplemented from QObject.
   590  */
   658  */
   591 void HbScrollBar::timerEvent( QTimerEvent *event )
   659 void HbScrollBar::timerEvent( QTimerEvent *event )
   592 {
   660 {
   593     Q_D(HbScrollBar);
   661     Q_D(HbScrollBar);
   594     if (event->timerId() == d->repeatActionTimer.timerId()) {
   662     if (event->timerId() == d->repeatActionTimer.timerId()) {
   680     HbWidget::polish(params);
   748     HbWidget::polish(params);
   681     d->sizeHelper();
   749     d->sizeHelper();
   682 }
   750 }
   683 
   751 
   684 /*!
   752 /*!
   685   \reimp
   753     \reimp
   686 */
   754 */
   687 void HbScrollBar::gestureEvent(QGestureEvent* event) 
   755 void HbScrollBar::gestureEvent(QGestureEvent* event) 
   688 {
   756 {
   689     Q_D(HbScrollBar);
   757     Q_D(HbScrollBar);
   690     if ( !d->mInteractive ) {
   758     if ( !d->mInteractive ) {