radioapp/radiowidgets/src/radiofrequencystripbase.cpp
changeset 54 a8ba0c289b44
parent 47 74b7c6e79031
child 57 21be958eb3ce
equal deleted inserted replaced
51:bbebb0235466 54:a8ba0c289b44
    84     mRightButton( 0 ),
    84     mRightButton( 0 ),
    85     mManualSeekTimer( NULL ),
    85     mManualSeekTimer( NULL ),
    86     mManualSeekMode( false ),
    86     mManualSeekMode( false ),
    87     mLastReportedFrequency( 0 ),
    87     mLastReportedFrequency( 0 ),
    88     mManualSeekTimerId( 0 ),
    88     mManualSeekTimerId( 0 ),
    89     mForegroundColor( Qt::white )//HbColorScheme::color( FrequencyStrip::TEXT_COLOR_ATTRIBUTE ) )
    89     mForegroundColor( Qt::white ),//HbColorScheme::color( FrequencyStrip::TEXT_COLOR_ATTRIBUTE ) )
    90 {
    90     mIgnoreScrollingEnd( false )
    91     setScrollingStyle( HbScrollArea::PanWithFollowOn );
    91 {
    92     setItemSize( QSizeF( FrequencyStrip::ITEM_WIDTH + FrequencyStrip::PIXMAP_OVERLAP / 2, mItemHeight ) );
       
    93     setOverlap( FrequencyStrip::PIXMAP_OVERLAP / 2 );
       
    94 }
    92 }
    95 
    93 
    96 /*!
    94 /*!
    97  * Property
    95  * Property
    98  */
    96  */
   117     mMinFrequency       = minFrequency;
   115     mMinFrequency       = minFrequency;
   118     mMaxFrequency       = maxFrequency;
   116     mMaxFrequency       = maxFrequency;
   119     mFrequencyStepSize  = stepSize;
   117     mFrequencyStepSize  = stepSize;
   120     mFrequency          = frequency;
   118     mFrequency          = frequency;
   121 
   119 
       
   120     setScrollingStyle( HbScrollArea::PanWithFollowOn );
       
   121 
       
   122     HbDeviceProfile deviceProfile;
       
   123     mItemHeight = static_cast<int>( mItemHeight * deviceProfile.unitValue() );
       
   124     setItemSize( QSizeF( FrequencyStrip::ITEM_WIDTH + FrequencyStrip::PIXMAP_OVERLAP / 2, mItemHeight ) );
       
   125 
       
   126     setOverlap( FrequencyStrip::PIXMAP_OVERLAP / 2 );
       
   127 
   122     initModel();
   128     initModel();
   123     initEmptyItems();
   129     initEmptyItems();
   124     initPositions();
   130     initPositions();
   125 
       
   126     HbDeviceProfile deviceProfile;
       
   127     mItemHeight = static_cast<int>( mItemHeight * deviceProfile.unitValue() );
       
   128 
   131 
   129     mManualSeekTimer = new QTimer( this );
   132     mManualSeekTimer = new QTimer( this );
   130     mManualSeekTimer->setInterval( FrequencyStrip::MANUALSEEK_START_TIMEOUT );
   133     mManualSeekTimer->setInterval( FrequencyStrip::MANUALSEEK_START_TIMEOUT );
   131     mManualSeekTimer->setSingleShot( true );
   134     mManualSeekTimer->setSingleShot( true );
   132     connectAndCheck( mManualSeekTimer,   SIGNAL(timeout()),
   135     connectAndCheck( mManualSeekTimer,   SIGNAL(timeout()),
   340 /*!
   343 /*!
   341  * Private slot
   344  * Private slot
   342  */
   345  */
   343 void RadioFrequencyStripBase::handleScrollingEnd()
   346 void RadioFrequencyStripBase::handleScrollingEnd()
   344 {
   347 {
       
   348     if ( mIgnoreScrollingEnd ) {
       
   349         mIgnoreScrollingEnd = false;
       
   350         return;
       
   351     }
       
   352 
   345     // Check if the selector is in the invalid area where the strip loops around
   353     // Check if the selector is in the invalid area where the strip loops around
   346     const int selectorPosition = selectorPos();
   354     const int selectorPosition = selectorPos();
   347     if ( mManualSeekMode ) {
   355     if ( mManualSeekMode ) {
   348         if ( !mPositions.contains( selectorPosition ) ) {
   356         if ( !mPositions.contains( selectorPosition ) ) {
   349             if ( selectorPosition < mMaxWidth - FrequencyStrip::ITEM_WIDTH + mSeparatorPos ) {
   357             if ( selectorPosition < mMaxWidth - FrequencyStrip::ITEM_WIDTH + mSeparatorPos ) {
   384 void RadioFrequencyStripBase::scrollPosChanged()
   392 void RadioFrequencyStripBase::scrollPosChanged()
   385 {
   393 {
   386     if ( mManualSeekMode ) {
   394     if ( mManualSeekMode ) {
   387         const int pos = selectorPos();
   395         const int pos = selectorPos();
   388         const uint frequency = mPositions.value( pos );
   396         const uint frequency = mPositions.value( pos );
   389         if ( frequency > 0 ) {
   397         if ( frequency > 0 && mFrequency != frequency ) {
   390             mFrequency = mPositions.value( pos );
   398             mFrequency = frequency;
   391             emitFrequencyChanged( mFrequency, FrequencyStrip::ManualSeekUpdate, Scroll::Shortest );
   399             emitFrequencyChanged( mFrequency, FrequencyStrip::ManualSeekUpdate, Scroll::Shortest );
   392         }
   400         }
   393     }
   401     }
   394 }
   402 }
   395 
   403 
   465 /*!
   473 /*!
   466  * \reimp
   474  * \reimp
   467  */
   475  */
   468 void RadioFrequencyStripBase::gestureEvent( QGestureEvent* event )
   476 void RadioFrequencyStripBase::gestureEvent( QGestureEvent* event )
   469 {
   477 {
       
   478     mIgnoreScrollingEnd = true;
   470     HbSwipeGesture* swipeGesture = static_cast<HbSwipeGesture*>( event->gesture( Qt::SwipeGesture ) );
   479     HbSwipeGesture* swipeGesture = static_cast<HbSwipeGesture*>( event->gesture( Qt::SwipeGesture ) );
   471     if ( swipeGesture && !mManualSeekMode ) {
   480     if ( swipeGesture && !mManualSeekMode ) {
   472         if ( swipeGesture->state() == Qt::GestureFinished ) {
   481         if ( swipeGesture->state() == Qt::GestureFinished ) {
   473             if ( swipeGesture->horizontalDirection() == QSwipeGesture::Left ) {
   482             if ( swipeGesture->horizontalDirection() == QSwipeGesture::Left ) {
   474                 skipToNext();
   483                 skipToNext();
   500 {
   509 {
   501     const uint minFreq = uint( qreal(mMinFrequency) / FrequencyStrip::ONE_HERTZ + FrequencyStrip::ROUNDER );
   510     const uint minFreq = uint( qreal(mMinFrequency) / FrequencyStrip::ONE_HERTZ + FrequencyStrip::ROUNDER );
   502     const uint maxFreq = uint( qreal(mMaxFrequency) / FrequencyStrip::ONE_HERTZ + 0.9 ); // always round up
   511     const uint maxFreq = uint( qreal(mMaxFrequency) / FrequencyStrip::ONE_HERTZ + 0.9 ); // always round up
   503 
   512 
   504     QStringList list;
   513     QStringList list;
   505     QString freqText;
       
   506     for ( uint i = minFreq; i <= maxFreq; ++i ) {
   514     for ( uint i = minFreq; i <= maxFreq; ++i ) {
   507         freqText = QString::number( i );
   515         list.append( QString::number( i ) );
   508         list.append( freqText );
   516         mFrequencyItems.append( new RadioFrequencyItem( i ) );
   509         mFrequencyItems.append( new RadioFrequencyItem( freqText ) );
   517     }
   510     }
   518     mFrequencyItems.append( new RadioFrequencyItem( 0 ) );
   511     mFrequencyItems.append( new RadioFrequencyItem( "" ) );
       
   512     list.append( "" );
   519     list.append( "" );
   513 
   520 
   514     mMaxWidth = list.count() * FrequencyStrip::ITEM_WIDTH;
   521     mMaxWidth = list.count() * FrequencyStrip::ITEM_WIDTH;
   515 
   522 
   516     mSeparatorPos = qreal(FrequencyStrip::ITEM_WIDTH) / 2;
   523     mSeparatorPos = qreal(FrequencyStrip::ITEM_WIDTH) / 2;
   525 /*!
   532 /*!
   526  *
   533  *
   527  */
   534  */
   528 void RadioFrequencyStripBase::initSelector()
   535 void RadioFrequencyStripBase::initSelector()
   529 {
   536 {
   530     QPixmap selectorPixmap = QPixmap( QSize( FrequencyStrip::SELECTOR_WIDTH, (int)size().height() ) );
   537     QPixmap selectorPixmap = QPixmap( QSize( FrequencyStrip::SELECTOR_WIDTH, (int)size().height() - FrequencyStrip::SELECTOR_MARGIN * 2 ) );
   531     selectorPixmap.fill( Qt::red );
   538     selectorPixmap.fill( Qt::red );
   532     mSelectorImage->setPixmap( selectorPixmap );
   539     mSelectorImage->setPixmap( selectorPixmap );
   533     mSelectorImage->setZValue( FrequencyStrip::SELECTOR_Z_POS );
   540     mSelectorImage->setZValue( FrequencyStrip::SELECTOR_Z_POS );
       
   541     mSelectorImage->setPos( 0, FrequencyStrip::SELECTOR_MARGIN );
   534 }
   542 }
   535 
   543 
   536 /*!
   544 /*!
   537  *
   545  *
   538  */
   546  */
   668 }
   676 }
   669 
   677 
   670 /*!
   678 /*!
   671  *
   679  *
   672  */
   680  */
   673 QPixmap RadioFrequencyStripBase::drawPixmap( uint frequency, QList<FrequencyStrip::StationMarker> stations, RadioFrequencyItem* item )
   681 QPixmap RadioFrequencyStripBase::drawPixmap( uint mainFrequency, QList<FrequencyStrip::StationMarker> stations, RadioFrequencyItem* item )
   674 {
   682 {
   675     QPixmap pixmap( FrequencyStrip::PIXMAP_WIDTH, mItemHeight );
   683     QPixmap pixmap( FrequencyStrip::PIXMAP_WIDTH, mItemHeight );
   676     pixmap.fill( Qt::transparent );
   684     pixmap.fill( Qt::transparent );
       
   685 
       
   686     if ( mainFrequency == 0 ) {
       
   687         return pixmap;
       
   688     }
       
   689 
   677     QPainter painter( &pixmap );
   690     QPainter painter( &pixmap );
   678     QPen normalPen = painter.pen();
   691     QPen normalPen = painter.pen();
   679     QPen favoritePen = normalPen;
   692     QPen favoritePen = normalPen;
   680     normalPen.setColor( mForegroundColor );
   693     normalPen.setColor( mForegroundColor );
   681     painter.setPen( normalPen );
   694     painter.setPen( normalPen );
   682 
   695 
   683     if ( frequency == 0 ) {
   696     const uint frequencyIncrement = qMin( mFrequencyStepSize, FrequencyStrip::ONE_HUNDRED_KHZ );
   684         painter.drawLine( makeTab( mSeparatorPos - 1 + FrequencyStrip::ROUNDER, mItemHeight ) );
   697     const QString itemText = QString::number( mainFrequency / FrequencyStrip::ONE_HERTZ );
   685         painter.drawLine( makeTab( mSeparatorPos + FrequencyStrip::ROUNDER, mItemHeight ) );
   698     const uint startFrequency = mainFrequency - FrequencyStrip::HALF_HERTZ + frequencyIncrement;
   686         return pixmap;
   699     const uint endFrequency = mainFrequency + FrequencyStrip::HALF_HERTZ;
   687     }
       
   688 
       
   689     const QString itemText = QString::number( frequency / FrequencyStrip::ONE_HERTZ );
       
   690     const uint startFrequency = frequency - FrequencyStrip::HALF_HERTZ;
       
   691     const uint endFrequency = startFrequency + FrequencyStrip::ONE_HERTZ;
       
   692     const uint  roundedMin = int( qreal(mMinFrequency) / FrequencyStrip::ONE_HERTZ + FrequencyStrip::ROUNDER );
   700     const uint  roundedMin = int( qreal(mMinFrequency) / FrequencyStrip::ONE_HERTZ + FrequencyStrip::ROUNDER );
   693     const uint freq = frequency / FrequencyStrip::ONE_HERTZ;
   701     const uint freq = mainFrequency / FrequencyStrip::ONE_HERTZ;
   694     const int diff = freq - roundedMin;
   702     const int diff = freq - roundedMin;
   695     const qreal startPixel = diff * FrequencyStrip::ITEM_WIDTH;
   703     const qreal startPixel = diff * FrequencyStrip::ITEM_WIDTH;
   696     qreal pixels = 0.0;
   704     qreal pixels = 0.0;
   697     const qreal leftOverlap = FrequencyStrip::PIXMAP_OVERLAP / 2;
   705     const qreal leftOverlap = FrequencyStrip::PIXMAP_OVERLAP / 2;
   698 
   706 
   699     for ( uint frequency = startFrequency; frequency <= endFrequency; frequency += mFrequencyStepSize ) {
   707     for ( uint loopFrequency = startFrequency; loopFrequency <= endFrequency; loopFrequency += frequencyIncrement ) {
   700 
   708 
   701         if ( frequency < mMinFrequency || frequency > mMaxFrequency ) {
   709         pixels = qreal( loopFrequency - startFrequency ) / FrequencyStrip::PIXEL_IN_HZ;
   702             continue;
   710         if ( loopFrequency % FrequencyStrip::ONE_HERTZ == 0 ) {
   703         }
       
   704 
       
   705         pixels = qreal( frequency - startFrequency ) / FrequencyStrip::PIXEL_IN_HZ;
       
   706         if ( frequency % FrequencyStrip::ONE_HERTZ == 0 ) {
       
   707 
   711 
   708             // Draw the high frequency tab and the frequency text for the even number
   712             // Draw the high frequency tab and the frequency text for the even number
   709             normalPen.setWidth( 3 );
   713             normalPen.setWidth( 3 );
   710             painter.setPen( normalPen );
   714             painter.setPen( normalPen );
   711             painter.drawLine( makeTab( pixels + leftOverlap - 1, FrequencyStrip::TAB_HEIGHT_BIG ) );
   715             painter.drawLine( makeTab( pixels + leftOverlap - 1, FrequencyStrip::TAB_HEIGHT_BIG ) );
   712             normalPen.setWidth( 1 );
   716             normalPen.setWidth( 1 );
   713             painter.setPen( normalPen );
   717             painter.setPen( normalPen );
   714 
   718 
   715             // Draw the frequency text and its 00 decimals
   719             // Draw the frequency text and its decimals
   716             painter.setFont( FrequencyStrip::DECIMAL_FONT );
   720             painter.setFont( FrequencyStrip::DECIMAL_FONT );
   717             const int decimalWidth = painter.fontMetrics().width( FrequencyStrip::DECIMAL_TEXT );
   721             const int decimalWidth = painter.fontMetrics().width( FrequencyStrip::DECIMAL_TEXT );
   718             const int decimalSpace = painter.fontMetrics().leftBearing( '.' );
   722             const int decimalSpace = painter.fontMetrics().leftBearing( '.' );
   719             painter.setFont( FrequencyStrip::FREQUENCY_FONT );
   723             painter.setFont( FrequencyStrip::FREQUENCY_FONT );
   720             const int frequencyWidth = painter.fontMetrics().width( itemText );
   724             const int frequencyWidth = painter.fontMetrics().width( itemText );
   721             const int textPosX = int( pixels + leftOverlap - ( frequencyWidth + decimalSpace + decimalWidth ) / 2 );
   725             const int textPosX = int( pixels + leftOverlap - ( frequencyWidth + decimalSpace + decimalWidth ) / 2 );
   722             painter.drawText( QPoint( textPosX, FrequencyStrip::FREQUENCY_TEXT_Y_POS ), itemText );
   726             painter.drawText( QPoint( textPosX, FrequencyStrip::FREQUENCY_TEXT_Y_POS ), itemText );
   723             painter.setFont( FrequencyStrip::DECIMAL_FONT );
   727             painter.setFont( FrequencyStrip::DECIMAL_FONT );
   724             painter.drawText( QPoint( textPosX + frequencyWidth + decimalSpace, FrequencyStrip::FREQUENCY_TEXT_Y_POS ), FrequencyStrip::DECIMAL_TEXT );
   728             painter.drawText( QPoint( textPosX + frequencyWidth + decimalSpace, FrequencyStrip::FREQUENCY_TEXT_Y_POS ), FrequencyStrip::DECIMAL_TEXT );
   725 
   729 
   726         } else if ( frequency % FrequencyStrip::ONE_TAB_IN_HZ == 0 ) {
   730         } else if ( loopFrequency % FrequencyStrip::ONE_TAB_IN_HZ == 0 ) {
   727 
   731 
   728             // Draw the low frequency tab for the uneven number
   732             // Draw the low frequency tab for the uneven number
   729             painter.drawLine( makeTab( pixels + leftOverlap, FrequencyStrip::TAB_HEIGHT_SMALL ) );
   733             painter.drawLine( makeTab( pixels + leftOverlap, FrequencyStrip::TAB_HEIGHT_SMALL ) );
   730 
   734 
   731         }
   735         }
   732 
   736 
   733         addFrequencyPos( int( startPixel + pixels + FrequencyStrip::ROUNDER ), frequency, item );
   737         if ( ( loopFrequency - mMinFrequency ) % mFrequencyStepSize == 0  && loopFrequency >= mMinFrequency && loopFrequency <= mMaxFrequency ) {
       
   738             addFrequencyPos( int( startPixel + pixels + FrequencyStrip::ROUNDER ), loopFrequency, item );
       
   739         }
   734     }
   740     }
   735 
   741 
   736     // Draw favorites and local stations
   742     // Draw favorites and local stations
   737     favoritePen.setColor( Qt::yellow );
   743     favoritePen.setColor( Qt::yellow );
   738 
   744 
       
   745     const int markerYPos = mItemHeight - 18;
   739     foreach ( const FrequencyStrip::StationMarker& station, stations ) {
   746     foreach ( const FrequencyStrip::StationMarker& station, stations ) {
   740         const uint frequency = station.mFrequency;
   747         const uint frequency = station.mFrequency;
   741         pixels = qreal( frequency - startFrequency ) / FrequencyStrip::PIXEL_IN_HZ;
   748         pixels = qreal( frequency - startFrequency ) / FrequencyStrip::PIXEL_IN_HZ;
   742 
   749 
   743         if ( station.mIsFavorite ) {
   750         if ( station.mIsFavorite ) {
   744             favoritePen.setWidth( FrequencyStrip::PEN_WIDTH_FAVORITE );
   751             favoritePen.setWidth( FrequencyStrip::PEN_WIDTH_FAVORITE );
   745             painter.setPen( favoritePen );
   752             painter.setPen( favoritePen );
   746             painter.drawEllipse( int( pixels + leftOverlap - 3 ), FrequencyStrip::STATION_MARKER_Y_POS - 3, 6, 6 );
   753             painter.drawEllipse( int( pixels + leftOverlap - 3 ), markerYPos - 3, 6, 6 );
       
   754 //            painter.drawEllipse( int( pixels + leftOverlap - 3 ), FrequencyStrip::STATION_MARKER_Y_POS - 3, 6, 6 );
   747         } else {
   755         } else {
   748             favoritePen.setWidth( 1 );
   756             favoritePen.setWidth( 1 );
   749             painter.setPen( favoritePen );
   757             painter.setPen( favoritePen );
   750             painter.drawEllipse( int( pixels + leftOverlap - 4 ), FrequencyStrip::STATION_MARKER_Y_POS - 4, 8, 8 );
   758             painter.drawEllipse( int( pixels + leftOverlap - 4 ), markerYPos - 4, 8, 8 );
       
   759 //            painter.drawEllipse( int( pixels + leftOverlap - 4 ), FrequencyStrip::STATION_MARKER_Y_POS - 4, 8, 8 );
   751         }
   760         }
   752     }
   761     }
   753 
   762 
   754     return pixmap;
   763     return pixmap;
   755 }
   764 }