radioapp/radiowidgets/src/radiomainview.cpp
branchRCL_3
changeset 19 cce62ebc198e
equal deleted inserted replaced
18:1a6714c53019 19:cce62ebc198e
       
     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 <QTimer>
       
    20 #include <HbPushButton>
       
    21 #include <HbAction>
       
    22 #include <HbMenu>
       
    23 #include <HbApplication>
       
    24 #include <HbActivityManager>
       
    25 #include <QPixmap>
       
    26 #include <HbLabel>
       
    27 #include <HbFrameItem>          // Temporarily here until docml supports frame items
       
    28 
       
    29 // User includes
       
    30 #include "radiowindow.h"
       
    31 #include "radiomainview.h"
       
    32 #include "radiofrequencystrip.h"
       
    33 #include "radiouiengine.h"
       
    34 #include "radiologger.h"
       
    35 #include "radiostationcarousel.h"
       
    36 #include "radiouiloader.h"
       
    37 #include "radioutil.h"
       
    38 #include "radiostationmodel.h"
       
    39 #include "radiofrequencyscanner.h"
       
    40 
       
    41 // Constants
       
    42 const QLatin1String RADIO_MAINVIEW_ACTIVITY_ID( "FMRadioMainView" );
       
    43 
       
    44 /*!
       
    45  * Temporary convenience function to set frame background until DOCML supports frame items
       
    46  */
       
    47 static void initFrameBackground( RadioUiLoader& uiLoader,
       
    48                                  const QString backgroundName,
       
    49                                  HbFrameDrawer::FrameType frameType )
       
    50 {
       
    51     if ( HbLabel* backgroundLabel = uiLoader.findObject<HbLabel>( backgroundName ) ) {
       
    52         QString backgroundName = backgroundLabel->icon().iconName();
       
    53         HbFrameItem* frameItem = new HbFrameItem( backgroundName, frameType, backgroundLabel );
       
    54         backgroundLabel->setBackgroundItem( frameItem );
       
    55     }
       
    56 }
       
    57 
       
    58 
       
    59 /*!
       
    60  *
       
    61  */
       
    62 RadioMainView::RadioMainView() :
       
    63     RadioViewBase( false ),
       
    64     mCarousel( NULL ),
       
    65     mFrequencyStrip( NULL ),
       
    66     mSkippingAction( NULL ),
       
    67     mAlternateSkipping( false )
       
    68 {
       
    69 }
       
    70 
       
    71 /*!
       
    72  *
       
    73  */
       
    74 RadioMainView::~RadioMainView()
       
    75 {
       
    76 }
       
    77 
       
    78 /*!
       
    79  * \reimp
       
    80  *
       
    81  */
       
    82 void RadioMainView::setScanningMode( bool scanning )
       
    83 {
       
    84     if ( scanning ) {
       
    85         loadSection( DOCML::FILE_MAINVIEW, DOCML::MV_SECTION_SCANNING );
       
    86     } else {
       
    87         loadSection( DOCML::FILE_MAINVIEW, DOCML::MV_SECTION_NORMAL );
       
    88         updateFavoriteButton();
       
    89         mFrequencyScanner.take();
       
    90 
       
    91         const bool firsTimeStart = mUiEngine->isFirstTimeStart();
       
    92         const int rowCount = mUiEngine->stationModel().rowCount();
       
    93         if ( firsTimeStart && rowCount != 0 ) {
       
    94             mUiEngine->setFirstTimeStartPerformed( true );
       
    95         }
       
    96     }
       
    97 }
       
    98 
       
    99 /*!
       
   100  * \reimp
       
   101  *
       
   102  */
       
   103 void RadioMainView::preLazyLoadInit()
       
   104 {
       
   105     initFrameBackground( *mUiLoader, DOCML::MV_NAME_FREQUENCY_BACKGROUND, HbFrameDrawer::NinePieces );
       
   106 
       
   107     initFrameBackground( *mUiLoader, DOCML::MV_NAME_CAROUSEL_BACKGROUND, HbFrameDrawer::NinePieces );
       
   108 }
       
   109 
       
   110 /*!
       
   111  * \reimp
       
   112  *
       
   113  */
       
   114 void RadioMainView::init()
       
   115 {
       
   116     LOG_METHOD;
       
   117     loadSection( DOCML::FILE_MAINVIEW, DOCML::SECTION_LAZY_LOAD );
       
   118     initFrameBackground( *mUiLoader, DOCML::MV_NAME_CAROUSEL_OVERLAY, HbFrameDrawer::NinePieces );
       
   119 
       
   120     mCarousel = mUiLoader->findObject<RadioStationCarousel>( DOCML::MV_NAME_STATION_CAROUSEL );
       
   121     mCarousel->init( *mUiLoader, mUiEngine.data() );
       
   122     mCarousel->setLandscape( mMainWindow->orientation() == Qt::Horizontal );
       
   123 
       
   124     mFrequencyStrip = mUiLoader->findObject<RadioFrequencyStrip>( DOCML::MV_NAME_FREQUENCY_STRIP );
       
   125     mFrequencyStrip->init( mUiEngine.data(), *mUiLoader );
       
   126 
       
   127     RadioStationModel* stationModel = &mUiEngine->stationModel();
       
   128 
       
   129     Radio::connect( mFrequencyStrip,            SIGNAL(frequencyChanged(uint,int,int)),
       
   130                     this,                       SLOT(setFrequencyFromWidget(uint,int,int)) );
       
   131     Radio::connect( mCarousel,                  SIGNAL(frequencyChanged(uint,int,int)),
       
   132                     this,                       SLOT(setFrequencyFromWidget(uint,int,int)) );
       
   133     Radio::connect( mUiEngine.data(),           SIGNAL(tunedToFrequency(uint,int)),
       
   134                     this,                       SLOT(setFrequencyFromEngine(uint,int)) );
       
   135     Radio::connect( mFrequencyStrip,            SIGNAL(manualSeekChanged(bool)),
       
   136                     this,                       SLOT(setManualSeekMode(bool)) );
       
   137 
       
   138     Radio::connect( mFrequencyStrip,            SIGNAL(skipRequested(int)),
       
   139                     this,                       SLOT(skip(int)) );
       
   140     Radio::connect( mCarousel,                  SIGNAL(skipRequested(int)),
       
   141                     this,                       SLOT(skip(int)) );
       
   142     Radio::connect( mFrequencyStrip,            SIGNAL(seekRequested(int)),
       
   143                     mUiEngine.data(),           SLOT(seekStation(int)) );
       
   144 
       
   145     Radio::connect( mUiEngine.data(),           SIGNAL(seekingStarted(int)),
       
   146                     this,                       SLOT(seekingStarted()) );
       
   147     Radio::connect( mUiEngine.data(),           SIGNAL(antennaStatusChanged(bool)),
       
   148                     this,                       SLOT(updateAntennaStatus(bool)) );
       
   149     Radio::connect( mUiEngine.data(),           SIGNAL(audioRouteChanged(bool)),
       
   150                     this,                       SLOT(updateAudioRoute(bool)) );
       
   151 
       
   152     Radio::connect( stationModel,               SIGNAL(favoriteChanged(RadioStation)),
       
   153                     this,                       SLOT(handleFavoriteChange(RadioStation)) );
       
   154 
       
   155     connectXmlElement( DOCML::MV_NAME_STATIONS_BUTTON,  SIGNAL(clicked()),
       
   156                        mMainWindow,                     SLOT(activateStationsView()) );
       
   157 
       
   158     connectXmlElement( DOCML::MV_NAME_FAVORITE_BUTTON,  SIGNAL(clicked()),
       
   159                        this,                            SLOT(toggleFavorite()) );
       
   160 
       
   161     connectXmlElement( DOCML::MV_NAME_SPEAKER_BUTTON,   SIGNAL(clicked()),
       
   162                        mUiEngine.data(),                SLOT(toggleAudioRoute()) );
       
   163 
       
   164     connectXmlElement( DOCML::MV_NAME_SCAN_ACTION,      SIGNAL(triggered()),
       
   165                        this,                            SLOT(toggleScanning()) );
       
   166 
       
   167     updateFavoriteButton();
       
   168 
       
   169     connectCommonMenuItem( MenuItem::Exit );
       
   170 
       
   171     // "Play history" menu item
       
   172     connectViewChangeMenuItem( DOCML::MV_NAME_HISTORYVIEW_ACTION, SLOT(activateHistoryView()) );
       
   173 
       
   174     updateAudioRoute( mUiEngine->isUsingLoudspeaker() );
       
   175 
       
   176     // Add "back" navigation action to put the application to background
       
   177     HbAction* backAction = new HbAction( Hb::BackNaviAction, this );
       
   178 #ifdef BUILD_WIN32
       
   179     Radio::connect( backAction,     SIGNAL(triggered()),
       
   180                     this,           SLOT(quit()) );
       
   181 #else
       
   182     Radio::connect( backAction,     SIGNAL(triggered()),
       
   183                     mMainWindow,    SLOT(lower()) );
       
   184 #endif // BUILD_WIN32
       
   185     setNavigationAction( backAction );
       
   186 
       
   187     const bool firsTimeStart = mUiEngine->isFirstTimeStart();
       
   188     const int rowCount = mUiEngine->stationModel().rowCount();
       
   189 
       
   190    if ( firsTimeStart && rowCount == 0 ){
       
   191             QTimer::singleShot( 4500, this, SLOT(startFirstTimeScanning()) );
       
   192         }
       
   193 
       
   194 
       
   195     Radio::connect( static_cast<HbApplication*>( qApp ),    SIGNAL(aboutToQuit()),
       
   196                     this,                                   SLOT(saveActivity()) );
       
   197 
       
   198     emit applicationReady();
       
   199 }
       
   200 
       
   201 /*!
       
   202  * \reimp
       
   203  *
       
   204  */
       
   205 void RadioMainView::setOrientation()
       
   206 {
       
   207     loadSection( DOCML::FILE_MAINVIEW, mMainWindow->orientationSection() );
       
   208     if ( mCarousel && mFrequencyStrip ) {
       
   209         mCarousel->setFrequency( mFrequencyStrip->frequency(), TuneReason::Unspecified );
       
   210         mCarousel->setLandscape( mMainWindow->orientation() == Qt::Horizontal );
       
   211     }
       
   212 }
       
   213 
       
   214 /*!
       
   215  * \reimp
       
   216  *
       
   217  */
       
   218 void RadioMainView::userAccepted()
       
   219 {
       
   220     mFrequencyScanner.reset( new RadioFrequencyScanner( *mUiEngine, this ) );
       
   221     mFrequencyScanner->startScanning();
       
   222 }
       
   223 
       
   224 /*!
       
   225  * \reimp
       
   226  *
       
   227  */
       
   228 bool RadioMainView::eventFilter( QObject* watched, QEvent* event )
       
   229 {
       
   230     if ( event->type() == QEvent::ApplicationDeactivate ) {
       
   231         mFrequencyStrip->cancelManualSeek();
       
   232     }
       
   233 
       
   234     return RadioViewBase::eventFilter( watched, event );
       
   235 }
       
   236 
       
   237 /*!
       
   238  * Private slot
       
   239  */
       
   240 void RadioMainView::setFrequencyFromWidget( uint frequency, int reason, int direction )
       
   241 {
       
   242 //    LOG_FORMAT( "RadioMainView::setFrequencyFromWidget: %u, reason = %d", frequency, reason );
       
   243     if ( !RadioUtil::isScannerAlive() ) {
       
   244         if ( reason == TuneReason::FrequencyStrip ) {
       
   245 //            mCarousel->setFrequency( frequency, reason, direction );
       
   246 //            mUiEngine->tuneWithDelay( frequency, reason );
       
   247         } else if ( reason == TuneReason::ManualSeekUpdate ) {
       
   248             mCarousel->setFrequency( frequency, reason, RadioUtil::scrollDirection( direction ) );
       
   249         } else if ( reason == TuneReason::ManualSeekTune ) {
       
   250             LOG_FORMAT( "--------Manual seek tune: %u", frequency );
       
   251             mUiEngine->setFrequency( frequency, reason );
       
   252         } else if ( reason == TuneReason::StationCarousel ) {
       
   253             mFrequencyStrip->setFrequency( frequency, reason, RadioUtil::scrollDirection( direction ) );
       
   254             mUiEngine->setFrequency( frequency, reason );
       
   255         }
       
   256     }
       
   257 }
       
   258 
       
   259 /*!
       
   260  * Private slot
       
   261  */
       
   262 void RadioMainView::setFrequencyFromEngine( uint frequency, int reason )
       
   263 {
       
   264     LOG_FORMAT( "RadioMainView::setFrequencyFromEngine reason: %d", reason );
       
   265     if ( RadioUtil::scanStatus() != Scan::ScanningInMainView && !mFrequencyStrip->isInManualSeekMode() ) {
       
   266         mCarousel->clearInfoText();
       
   267         updateFavoriteButton();
       
   268         if ( reason != TuneReason::FrequencyStrip &&
       
   269              reason != TuneReason::StationCarousel &&
       
   270              reason != TuneReason::Skip ) {
       
   271             mCarousel->setFrequency( frequency, reason, Scroll::Shortest );
       
   272             mFrequencyStrip->setFrequency( frequency, reason, Scroll::Shortest );
       
   273         }
       
   274     }
       
   275 }
       
   276 
       
   277 /*!
       
   278  * Private slot
       
   279  */
       
   280 void RadioMainView::skip( int skipMode )
       
   281 {
       
   282 //    if ( !mAlternateSkipping && ( skipMode == StationSkip::PreviousFavorite || skipMode == StationSkip::NextFavorite ) &&
       
   283 //        mUiEngine->stationModel().favoriteCount() == 0 ) {
       
   284 //        mCarousel->setInfoText( CarouselInfoText::NoFavorites );
       
   285 //    } else {
       
   286         const uint currentFrequency = mFrequencyStrip->frequency();
       
   287         RadioStation station;
       
   288         mUiEngine->stationModel().findFrequency( currentFrequency, station );
       
   289 
       
   290         if ( mAlternateSkipping ) { //TODO: Remove. Temporary test code
       
   291             if ( sender() == mFrequencyStrip ) {
       
   292                 if ( skipMode == StationSkip::NextFavorite ) {
       
   293                     skipMode = StationSkip::Next;
       
   294                 } else if ( skipMode == StationSkip::PreviousFavorite ) {
       
   295                     skipMode = StationSkip::Previous;
       
   296                 }
       
   297             } else if ( sender() == mCarousel ) {
       
   298                 if ( skipMode == StationSkip::Next ) {
       
   299                     skipMode = StationSkip::NextFavorite;
       
   300                 } else if ( skipMode == StationSkip::Previous ) {
       
   301                     skipMode = StationSkip::PreviousFavorite;
       
   302                 }
       
   303             }
       
   304         }
       
   305 
       
   306         const uint frequency = mUiEngine->skipStation( static_cast<StationSkip::Mode>( skipMode ),
       
   307                                                                 currentFrequency);
       
   308 
       
   309         if ( currentFrequency != frequency || station.isFavorite() ) {
       
   310             const Scroll::Direction direction = RadioUtil::scrollDirectionFromSkipMode( skipMode );
       
   311             mCarousel->setFrequency( frequency, TuneReason::Skip, direction );
       
   312             mFrequencyStrip->setFrequency( frequency, TuneReason::Skip, direction );
       
   313         }
       
   314 //    }
       
   315 }
       
   316 
       
   317 /*!
       
   318  * Private slot
       
   319  */
       
   320 void RadioMainView::openStationsView()
       
   321 {
       
   322     mFrequencyStrip->cancelManualSeek();
       
   323     mMainWindow->activateStationsView();
       
   324 }
       
   325 
       
   326 /*!
       
   327  * Private slot
       
   328  */
       
   329 void RadioMainView::startFirstTimeScanning()
       
   330 {
       
   331     if ( mUiEngine->isAntennaAttached() ){
       
   332         QTimer::singleShot( 100, this, SLOT(toggleScanning()) );
       
   333     }
       
   334 }
       
   335 
       
   336 /*!
       
   337  * Private slot
       
   338  */
       
   339 void RadioMainView::toggleScanning()
       
   340 {
       
   341     mFrequencyStrip->cancelManualSeek();
       
   342     if ( mFrequencyScanner ) {
       
   343         mFrequencyScanner->cancelScanning();
       
   344     } else {
       
   345         const int rowCount =  mUiEngine->stationModel().rowCount();
       
   346         if ( rowCount > 0 ) {
       
   347             askQuestion( hbTrId( "txt_rad_info_all_stations_in_stations_list_will_be" ) );
       
   348         } else {
       
   349             userAccepted();
       
   350         }
       
   351     }
       
   352 }
       
   353 
       
   354 /*!
       
   355  * Private slot
       
   356  */
       
   357 void RadioMainView::toggleFavorite()
       
   358 {
       
   359     if ( RadioUtil::isScannerAlive() ) {
       
   360         toggleScanning();
       
   361     } else {
       
   362         mUiEngine->stationModel().setData( QModelIndex(), mFrequencyStrip->frequency(), RadioRole::ToggleFavoriteRole );
       
   363     }
       
   364 }
       
   365 
       
   366 /*!
       
   367  * Private slot
       
   368  */
       
   369 void RadioMainView::seekingStarted()
       
   370 {
       
   371     if ( !RadioUtil::isScannerAlive() ) {
       
   372         mCarousel->setInfoText( CarouselInfoText::Seeking );
       
   373     }
       
   374 }
       
   375 
       
   376 /*!
       
   377  * Private slot
       
   378  */
       
   379 void RadioMainView::updateAntennaStatus( bool connected )
       
   380 {
       
   381     if ( !connected ) {
       
   382         mFrequencyStrip->cancelManualSeek();
       
   383     }
       
   384 
       
   385     HbAction* scanAction = mUiLoader->findObject<HbAction>( DOCML::MV_NAME_SCAN_ACTION );
       
   386     scanAction->setEnabled( connected );
       
   387     mCarousel->updateAntennaStatus( connected );
       
   388     const bool firsTimeStart = mUiEngine->isFirstTimeStart();
       
   389     const int rowCount = mUiEngine->stationModel().rowCount();
       
   390     if ( firsTimeStart && rowCount == 0 && connected ){
       
   391             QTimer::singleShot( 100, this, SLOT(toggleScanning()) );
       
   392         }
       
   393 }
       
   394 
       
   395 /*!
       
   396  * Private slot
       
   397  */
       
   398 void RadioMainView::updateAudioRoute( bool loudspeaker )
       
   399 {
       
   400     HbPushButton* loudspeakerButton = mUiLoader->findWidget<HbPushButton>( DOCML::MV_NAME_SPEAKER_BUTTON );
       
   401     if ( loudspeaker ) {
       
   402         loudspeakerButton->setIcon( HbIcon( "qtg_mono_speaker_off" ) );
       
   403         loudspeakerButton->setText( hbTrId( "txt_rad_button_deactivate_loudspeaker" ) );
       
   404     } else {
       
   405         loudspeakerButton->setIcon( HbIcon( "qtg_mono_speaker" ) );
       
   406         loudspeakerButton->setText( hbTrId( "txt_rad_button_activate_loudspeaker" ) );
       
   407     }
       
   408 }
       
   409 
       
   410 /*!
       
   411  * Private slot
       
   412  */
       
   413 void RadioMainView::setManualSeekMode( bool manualSeekActive )
       
   414 {
       
   415     if ( manualSeekActive ) {
       
   416         qApp->installEventFilter( this );
       
   417 
       
   418         mUiLoader->findWidget<HbPushButton>( DOCML::MV_NAME_FAVORITE_BUTTON )->setText( hbTrId( "txt_rad_button_add_to_favourites" ) );
       
   419     } else {
       
   420         qApp->removeEventFilter( this );
       
   421 
       
   422         updateFavoriteButton();
       
   423     }
       
   424 
       
   425     mUiEngine->setManualSeekMode( manualSeekActive );
       
   426 
       
   427     mCarousel->setManualSeekMode( manualSeekActive );
       
   428 }
       
   429 
       
   430 /*!
       
   431  * Private slot
       
   432  */
       
   433 void RadioMainView::handleFavoriteChange( const RadioStation& station )
       
   434 {
       
   435     mFrequencyStrip->updateFavorite( station.frequency() );
       
   436     updateFavoriteButton();
       
   437 }
       
   438 
       
   439 /*!
       
   440  * Private slot
       
   441  */
       
   442 void RadioMainView::saveActivity()
       
   443 {
       
   444     HbActivityManager* activityManager = qobject_cast<HbApplication*>(qApp)->activityManager();
       
   445 
       
   446     // Get a screenshot for saving to the activity manager
       
   447     QSize screenShotSize = mCarousel->size().toSize();
       
   448     QPixmap screenShot( screenShotSize );
       
   449     QPainter painter( &screenShot );
       
   450 
       
   451     // Draw the background and overlay
       
   452     HbLabel* backgroundLabel = mUiLoader->findWidget<HbLabel>( DOCML::MV_NAME_CAROUSEL_BACKGROUND );
       
   453     painter.drawPixmap( 0, 0, backgroundLabel->icon().pixmap().scaled( screenShotSize ) );
       
   454     backgroundLabel = mUiLoader->findWidget<HbLabel>( DOCML::MV_NAME_CAROUSEL_OVERLAY );
       
   455     painter.drawPixmap( 0, 0, backgroundLabel->icon().pixmap().scaled( screenShotSize ) );
       
   456 
       
   457     mCarousel->drawOffScreen( painter );
       
   458 
       
   459     QVariantHash metadata;
       
   460     metadata.insert( "screenshot", screenShot );
       
   461 
       
   462     #ifdef __WINS__
       
   463         screenShot.save( "c:\\radio.bmp" );
       
   464     #elif defined BUILD_WIN32
       
   465         screenShot.save( "radio.bmp" );
       
   466     #endif
       
   467 
       
   468     // Update the activity to the activity manager
       
   469     bool ok = activityManager->removeActivity( RADIO_MAINVIEW_ACTIVITY_ID );
       
   470     LOG_ASSERT( ok, LOG( "Failed to remove old activity from Activity Manager!" ) );
       
   471     ok = activityManager->addActivity( RADIO_MAINVIEW_ACTIVITY_ID, QVariant(), metadata );
       
   472     LOG_ASSERT( ok, LOG( "Failed to update activity to Activity Manager!" ) );
       
   473 }
       
   474 
       
   475 /*!
       
   476  *
       
   477  */
       
   478 void RadioMainView::updateFavoriteButton()
       
   479 {
       
   480     RadioStation station;
       
   481     RadioStationModel& model = mUiEngine->stationModel();
       
   482     model.findFrequency( mUiEngine->currentFrequency(), station );
       
   483     HbPushButton* favoriteButton = mUiLoader->findWidget<HbPushButton>( DOCML::MV_NAME_FAVORITE_BUTTON );
       
   484     if ( station.isFavorite() ) {
       
   485         favoriteButton->setText( hbTrId( "txt_fmradio_button_remove_from_favourites" ) );
       
   486         favoriteButton->setIcon( HbIcon( "qtg_mono_favourites_remove" ) );
       
   487     } else {
       
   488         favoriteButton->setText( hbTrId( "txt_rad_button_add_to_favourites" ) );
       
   489         favoriteButton->setIcon( HbIcon( "qtg_mono_add_to_favourites" ) );
       
   490     }
       
   491 }