radioapp/radiowidgets/src/radiofrequencyscanner.cpp
changeset 24 6df133bd92e1
child 28 075425b8d9a4
equal deleted inserted replaced
23:a2b50a479edf 24:6df133bd92e1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <HbProgressDialog>
       
    20 #include <QTimer>
       
    21 #include <HbLabel>
       
    22 #include <HbPushButton>
       
    23 
       
    24 // User includes
       
    25 #include "radiofrequencyscanner.h"
       
    26 #include "radioscannerengine.h"
       
    27 #include "radiouiengine.h"
       
    28 #include "radiologger.h"
       
    29 #include "radiostationmodel.h"
       
    30 #include "radiofrequencystrip.h"
       
    31 #include "radiostationcarousel.h"
       
    32 #include "radiouiutilities.h"
       
    33 #include "radiomainview.h"
       
    34 
       
    35 // Constants
       
    36 const int KExtraRoomToMaxValue = 100000;
       
    37 
       
    38 /*!
       
    39  *
       
    40  */
       
    41 RadioFrequencyScanner::RadioFrequencyScanner( RadioUiEngine& uiEngine, QObject* parent ) :
       
    42     QObject( parent ),
       
    43     mUiEngine( uiEngine ),
       
    44     mInMainView( parent->metaObject()->className() == RadioMainView::staticMetaObject.className() ),
       
    45     mScannerEngine( mUiEngine.scannerEngine() ),
       
    46     mStripScrollTime( 0 ),
       
    47     mCarouselScrollTime( 0 ),
       
    48     mIsAlive( false )
       
    49 {
       
    50     RadioUiUtilities::setFrequencyScanner( this );
       
    51 }
       
    52 
       
    53 /*!
       
    54  *
       
    55  */
       
    56 RadioFrequencyScanner::~RadioFrequencyScanner()
       
    57 {
       
    58 }
       
    59 
       
    60 /*!
       
    61  *
       
    62  */
       
    63 void RadioFrequencyScanner::startScanning()
       
    64 {
       
    65     mIsAlive = true;
       
    66     RadioFrequencyStrip* frequencyStrip = RadioUiUtilities::frequencyStrip();
       
    67     RadioStationCarousel* carousel = RadioUiUtilities::carousel();
       
    68 
       
    69     if ( mInMainView ) {
       
    70         mStripScrollTime = frequencyStrip->autoScrollTime();
       
    71         mCarouselScrollTime = carousel->autoScrollTime();
       
    72 
       
    73         carousel->setScanningMode( true );
       
    74         carousel->setAutoScrollTime( 1000 );
       
    75         frequencyStrip->setAutoScrollTime( 1100 );
       
    76 
       
    77         connectAndTest( carousel,               SIGNAL(scanAnimationFinished()),
       
    78                         this,                   SLOT(continueScanning()) );
       
    79 
       
    80         static_cast<RadioMainView*>( parent() )->setScanningMode( true );
       
    81         frequencyStrip->setScanningMode( true );
       
    82     } else {
       
    83         carousel->setCarouselModel( NULL );
       
    84 
       
    85         mScanningProgressNote.reset( new HbProgressDialog( HbProgressDialog::ProgressDialog ) ),
       
    86         mScanningProgressNote->setModal( true );
       
    87         mScanningProgressNote->setAutoClose( true );
       
    88 
       
    89         // Add some extra to the maximum value to allow room for the station at the low band edge
       
    90         mScanningProgressNote->setRange( mUiEngine.minFrequency(), mUiEngine.maxFrequency() + KExtraRoomToMaxValue );
       
    91         mScanningProgressNote->setProgressValue( mUiEngine.minFrequency() );
       
    92         mScanningProgressNote->setText( hbTrId( "txt_rad_info_searching_local_stations_please_wait" ) );
       
    93         mScanningProgressNote->show();
       
    94 
       
    95         connectAndTest( mScanningProgressNote.data(),   SIGNAL(cancelled()),
       
    96                         this,                           SLOT(cancelScanning()) );
       
    97     }
       
    98 
       
    99     connectAndTest( mScannerEngine.data(),  SIGNAL(stationFound(RadioStation)),
       
   100                     this,                   SLOT(updateScanProgress(RadioStation)) );
       
   101 
       
   102     QTimer::singleShot( 1000, this, SLOT(delayedStart()) );
       
   103 }
       
   104 
       
   105 /*!
       
   106  *
       
   107  */
       
   108 bool RadioFrequencyScanner::isAlive() const
       
   109 {
       
   110     return mIsAlive;
       
   111 }
       
   112 
       
   113 /*!
       
   114  * Public slot
       
   115  *
       
   116  */
       
   117 void RadioFrequencyScanner::cancelScanning()
       
   118 {
       
   119     finishScanning();
       
   120 }
       
   121 
       
   122 /*!
       
   123  * Private slot
       
   124  *
       
   125  */
       
   126 void RadioFrequencyScanner::delayedStart()
       
   127 {
       
   128     mScannerEngine->startScanning();
       
   129 }
       
   130 
       
   131 /*!
       
   132  * Private slot
       
   133  *
       
   134  */
       
   135 void RadioFrequencyScanner::updateScanProgress( const RadioStation& station )
       
   136 {
       
   137     if ( !station.isValid() ) {
       
   138         finishScanning();
       
   139         return;
       
   140     }
       
   141 
       
   142     const uint frequency = station.frequency();
       
   143     LOG_FORMAT( "RadioFrequencyScanner::updateScanAndSaveProgress frequency: %d", frequency );
       
   144 
       
   145     if ( mInMainView ) {
       
   146 
       
   147         RadioUiUtilities::frequencyStrip()->setFrequency( frequency, TuneReason::StationScan );
       
   148         RadioUiUtilities::carousel()->animateNewStation( station );
       
   149 
       
   150     } else {
       
   151         // Check for special case that can happen during scanning.
       
   152         // If there is a valid radio station at the low frequency band edge it will be reported last after
       
   153         // all of the higher frequencies. We don't update the progress value here because the value would
       
   154         // be lower than the previous one. The progress value is set to maximum when the scanner finishes.
       
   155         if ( frequency != mUiEngine.minFrequency() ) {
       
   156             mScanningProgressNote->setProgressValue( frequency );
       
   157         }
       
   158 
       
   159         mScannerEngine->continueScanning();
       
   160     }    
       
   161 }
       
   162 
       
   163 /*!
       
   164  * Private slot
       
   165  *
       
   166  */
       
   167 void RadioFrequencyScanner::continueScanning()
       
   168 {
       
   169     mScannerEngine->continueScanning();
       
   170 }
       
   171 
       
   172 /*!
       
   173  * Private slot
       
   174  *
       
   175  */
       
   176 void RadioFrequencyScanner::restoreUiControls()
       
   177 {
       
   178     if ( mInMainView ) {
       
   179         RadioUiUtilities::frequencyStrip()->setScanningMode( false );
       
   180         static_cast<RadioMainView*>( parent() )->setScanningMode( false );
       
   181         RadioUiUtilities::carousel()->setScanningMode( false );
       
   182     }
       
   183 
       
   184     deleteLater();
       
   185 }
       
   186 
       
   187 /*!
       
   188  *
       
   189  */
       
   190 void RadioFrequencyScanner::finishScanning()
       
   191 {
       
   192     mScannerEngine->cancel();
       
   193     RadioUiUtilities::setFrequencyScanner( NULL );
       
   194     mIsAlive = false;
       
   195     RadioFrequencyStrip* frequencyStrip = RadioUiUtilities::frequencyStrip();
       
   196     RadioStationCarousel* carousel = RadioUiUtilities::carousel();
       
   197 
       
   198     if ( mInMainView ) {
       
   199         RadioStationModel& model = mUiEngine.stationModel();
       
   200 
       
   201         // Scroll the carousel and frequency strip through all of the scanned stations
       
   202         const int stationCount = model.rowCount();
       
   203         if ( stationCount > 1 ) {
       
   204             frequencyStrip->setAutoScrollTime( 1000 );
       
   205             carousel->setAutoScrollTime( 1000 );
       
   206             const uint frequency = model.data( model.index( 0, 0 ), RadioStationModel::RadioStationRole ).value<RadioStation>().frequency();
       
   207             frequencyStrip->setFrequency( frequency, TuneReason::StationScan );
       
   208             carousel->setFrequency( frequency, TuneReason::StationScan );
       
   209 
       
   210             frequencyStrip->setAutoScrollTime( mStripScrollTime );
       
   211             carousel->setAutoScrollTime( mCarouselScrollTime );
       
   212         }
       
   213 
       
   214         QTimer::singleShot( 100, this, SLOT(restoreUiControls()) );
       
   215 
       
   216     } else {
       
   217         mScanningProgressNote->setProgressValue( mScanningProgressNote->maximum() );
       
   218         deleteLater();
       
   219 
       
   220         carousel->setCarouselModel( mUiEngine.carouselModel() );
       
   221     }
       
   222 
       
   223     disconnect( mScannerEngine.data(),  SIGNAL(stationFound(RadioStation)),
       
   224                 this,                   SLOT(updateScanAndSaveProgress(RadioStation)) );
       
   225 
       
   226     emit frequencyScannerFinished();
       
   227 }