radioapp/radiowidgets/src/radiostripbase.cpp
branchGCC_SURGE
changeset 37 451b2e1545b2
parent 19 afea38384506
parent 32 189d20c34778
equal deleted inserted replaced
26:6bcf277166c1 37:451b2e1545b2
    32     mAutoScrollTime( 0 ),
    32     mAutoScrollTime( 0 ),
    33     mStripContainer( new HbWidget( this ) ),
    33     mStripContainer( new HbWidget( this ) ),
    34     mModel( 0 ),
    34     mModel( 0 ),
    35     mIsCyclic( true ),
    35     mIsCyclic( true ),
    36     mAutoCenter( false ),
    36     mAutoCenter( false ),
    37     mSpacing( 0 ),
    37     mOverlap( 0 ),
    38     mItemPoolParent( new QGraphicsWidget( NULL ) ),
    38     mItemPoolParent( new QGraphicsWidget( NULL ) ),
    39     mCurrentIndex( 0 ),
    39     mCurrentIndex( 0 ),
    40     mPressedIndex( 0 ),
    40     mPressedIndex( 0 ),
    41     mStripLength( 0 ),
    41     mStripLength( 0 ),
    42     mContentsLength( 0 )
    42     mContentsLength( 0 )
    51     setVerticalScrollBarPolicy( HbScrollArea::ScrollBarAlwaysOff );
    51     setVerticalScrollBarPolicy( HbScrollArea::ScrollBarAlwaysOff );
    52 
    52 
    53     // mItemParent is used to hold the unused QGraphicsItem's in the pool.  It's visibility is set to false
    53     // mItemParent is used to hold the unused QGraphicsItem's in the pool.  It's visibility is set to false
    54     // so the visibility of the items doesn't need to be modified.
    54     // so the visibility of the items doesn't need to be modified.
    55     mItemPoolParent->setVisible( false );
    55     mItemPoolParent->setVisible( false );
    56 
       
    57     connectAndTest( this,   SIGNAL(scrollPositionChanged(QPointF)),
       
    58                     this,   SLOT(scrollPositionChanged(QPointF)));
       
    59 }
    56 }
    60 
    57 
    61 /*!
    58 /*!
    62  *
    59  *
    63  */
    60  */
   122 }
   119 }
   123 
   120 
   124 /*!
   121 /*!
   125  *
   122  *
   126  */
   123  */
   127 void RadioStripBase::setSpacing( qreal spacing )
   124 void RadioStripBase::setOverlap( qreal overlap )
   128 {
   125 {
   129     if ( mSpacing != spacing )
   126     mOverlap = overlap;
   130     {
       
   131         mSpacing = spacing;
       
   132 
       
   133         prepareGeometryChange();
       
   134 
       
   135         populateAndLayout();
       
   136 
       
   137         update();
       
   138         updateGeometry();
       
   139     }
       
   140 }
   127 }
   141 
   128 
   142 /*!
   129 /*!
   143  *
   130  *
   144  */
   131  */
   225 
   212 
   226     HbScrollArea::mouseReleaseEvent( event );
   213     HbScrollArea::mouseReleaseEvent( event );
   227 }
   214 }
   228 
   215 
   229 /*!
   216 /*!
   230  * Private slot
       
   231  */
       
   232 void RadioStripBase::scrollPositionChanged( QPointF newPosition )
       
   233 {
       
   234     adjustItems();
       
   235     scrollPosChanged( newPosition );
       
   236 }
       
   237 
       
   238 /*!
       
   239  *
   217  *
   240  */
   218  */
   241 void RadioStripBase::moveAllItemsToPool()
   219 void RadioStripBase::moveAllItemsToPool()
   242 {
   220 {
   243     // set parent of all items to pool
   221     // set parent of all items to pool
   264         return;
   242         return;
   265     }
   243     }
   266 
   244 
   267     mStripLength = boundingRect().width();
   245     mStripLength = boundingRect().width();
   268     qreal itemSize = mItemSize.width();
   246     qreal itemSize = mItemSize.width();
   269     mContentsLength = mModel->rowCount() * (itemSize + mSpacing) + mSpacing;
   247     mContentsLength = mModel->rowCount() * (itemSize - mOverlap);
   270 
   248 
   271     if ( mIsCyclic )
   249     if ( mIsCyclic )
   272     {
   250     {
   273         // if treating the items cyclically, double the content area so it can
   251         // if treating the items cyclically, double the content area so it can
   274         // be shifted back and forth as you scroll
   252         // be shifted back and forth as you scroll
   275         mContentsLength = mModel->rowCount() * (itemSize + mSpacing);
   253         mContentsLength *= 2;
   276         mContentsLength *= 2.0;
   254     }
   277     }
   255 
   278 
   256     qreal currPos = -mOverlap;
   279     qreal currPos = mSpacing;
       
   280     for ( int i = 0; i < mModel->rowCount(); ++i ) {
   257     for ( int i = 0; i < mModel->rowCount(); ++i ) {
   281         if ( currPos > mStripLength )
   258         if ( currPos > mStripLength )
   282         {
   259         {
   283             break;
   260             break;
   284         }
   261         }
   285 
   262 
   286         QGraphicsItem* item = constructItem( i, true );
   263         QGraphicsItem* item = constructItem( i, true );
   287         if ( item )
   264         if ( item )
   288         {
   265         {
   289             item->setPos( QPointF( currPos, mSpacing ) );
   266             item->setPos( QPointF( currPos, 0 ) );
   290             currPos += itemSize + mSpacing;
   267             currPos += itemSize - mOverlap;
   291         }
   268         }
   292     }
   269     }
   293 
   270 
   294     QRectF contentsRect(0,0,0,0);
   271     mStripContainer->setPreferredSize( mContentsLength, mItemSize.height() );
   295     contentsRect.setBottom( itemSize + 2 * mSpacing );
       
   296     contentsRect.setRight( mContentsLength );
       
   297 
       
   298     mStripContainer->setGeometry( contentsRect );
       
   299 
   272 
   300     if ( mCurrentIndex >= 0 )
   273     if ( mCurrentIndex >= 0 )
   301     {
   274     {
   302         setIndex( mCurrentIndex, false );
   275         setIndex( mCurrentIndex, false );
   303     }
   276     }
   371 /*!
   344 /*!
   372  * Returns starting coordinate of the item with the specified index
   345  * Returns starting coordinate of the item with the specified index
   373  */
   346  */
   374 qreal RadioStripBase::indexToOffset( int index )
   347 qreal RadioStripBase::indexToOffset( int index )
   375 {
   348 {
   376     return index * ( mItemSize.width() + mSpacing ) + mSpacing;
   349     return index * ( mItemSize.width() - mOverlap ) - mOverlap;
   377 }
   350 }
   378 
   351 
   379 /*!
   352 /*!
   380  * Returns item index for specified offset amount into the content
   353  * Returns item index for specified offset amount into the content
   381  */
   354  */
   382 int RadioStripBase::offsetToIndex( qreal offset )
   355 int RadioStripBase::offsetToIndex( qreal offset )
   383 {
   356 {
   384     const int rows = mModel->rowCount();
   357     const int rows = mModel->rowCount();
   385     int index = (int)( ( offset - mSpacing) / ( mItemSize.width() + mSpacing ) );
   358     int index = (int)( offset / ( mItemSize.width() - mOverlap ) );
   386 
   359 
   387     if ( mIsCyclic )
   360     if ( mIsCyclic )
   388     {
   361     {
   389        return qBound( 0, index, 2 * rows - 1 );
   362        return qBound( 0, index, 2 * rows - 1 );
   390     }
   363     }
   477         for ( int i = firstVisibleIndex; i <= lastVisibleIndex; ++i )
   450         for ( int i = firstVisibleIndex; i <= lastVisibleIndex; ++i )
   478         {
   451         {
   479             QGraphicsItem* item = constructItem( i, true );
   452             QGraphicsItem* item = constructItem( i, true );
   480             if ( item )
   453             if ( item )
   481             {
   454             {
   482                 item->setPos( QPointF( indexToOffset( i ), mSpacing ) );
   455                 item->setPos( QPointF( indexToOffset( i ), 0 ) );
   483             }
   456             }
   484         }
   457         }
   485     }
   458     }
   486     else
   459     else
   487     {
   460     {
   490         for ( int i = firstItemToCreate; i >= firstVisibleIndex; --i )
   463         for ( int i = firstItemToCreate; i >= firstVisibleIndex; --i )
   491         {
   464         {
   492             QGraphicsItem* item = constructItem( i, false );
   465             QGraphicsItem* item = constructItem( i, false );
   493             if ( item )
   466             if ( item )
   494             {
   467             {
   495                 item->setPos( QPointF( indexToOffset( i ), mSpacing ) );
   468                 item->setPos( QPointF( indexToOffset( i ), 0 ) );
   496             }
   469             }
   497         }
   470         }
   498 
   471 
   499         // add missing items at the end
   472         // add missing items at the end
   500         firstItemToCreate = mIndexAtSlot.last()+1;
   473         firstItemToCreate = mIndexAtSlot.last()+1;
   501         for ( int i = firstItemToCreate; i <= lastVisibleIndex; ++i )
   474         for ( int i = firstItemToCreate; i <= lastVisibleIndex; ++i )
   502         {
   475         {
   503             QGraphicsItem* item = constructItem( i, true );
   476             QGraphicsItem* item = constructItem( i, true );
   504             if ( item )
   477             if ( item )
   505             {
   478             {
   506                 item->setPos( QPointF( indexToOffset( i ), mSpacing ) );
   479                 item->setPos( QPointF( indexToOffset( i ), 0 ) );
   507             }
   480             }
   508         }
   481         }
   509     }
   482     }
   510 }
   483 }
       
   484 
       
   485 /*!
       
   486  * \reimp
       
   487  */
       
   488 bool RadioStripBase::scrollByAmount( const QPointF& delta )
       
   489 {
       
   490     bool ret = HbScrollArea::scrollByAmount( delta );
       
   491 
       
   492     adjustItems();
       
   493     scrollPosChanged();
       
   494 
       
   495     return ret;
       
   496 }