radioapp/radiowidgets/src/radiomainwindow.cpp
changeset 19 afea38384506
parent 16 f54ebcfc1b80
child 21 6bac020dcc51
child 23 a2b50a479edf
equal deleted inserted replaced
16:f54ebcfc1b80 19:afea38384506
     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 #include <HbInstance>
       
    19 #include <HbAction>
       
    20 #include <HbMessageBox>
       
    21 #include <HbVolumeSliderPopup>
       
    22 
       
    23 #include "radiomainwindow.h"
       
    24 #include "radiotuningview.h"
       
    25 #include "radiostationsview.h"
       
    26 #include "radiouiengine.h"
       
    27 #include "radiostationmodel.h"
       
    28 #include "radiologger.h"
       
    29 #include "radioxmluiloader.h"
       
    30 
       
    31 // Constants
       
    32 
       
    33 /**
       
    34  * Desired amount of delay of volumesliderpopup
       
    35  */
       
    36 const int KVolumeSliderDelay = 5000;
       
    37 
       
    38 /*!
       
    39  *
       
    40  */
       
    41 RadioMainWindow::RadioMainWindow( QWidget* parent ) :
       
    42     HbMainWindow( parent ),
       
    43     mUiEngine( 0 )
       
    44 {
       
    45 }
       
    46 
       
    47 /*!
       
    48  *
       
    49  */
       
    50 RadioMainWindow::~RadioMainWindow()
       
    51 {
       
    52     // Destructor needs to be defined. See explanation from RadioEngineWrapperPrivate destructor.
       
    53 }
       
    54 
       
    55 /*!
       
    56  *
       
    57  */
       
    58 bool RadioMainWindow::isOfflineUsageAllowed()
       
    59 {
       
    60     DummyViewPtr dummyView = prepareToShowDialog();
       
    61 
       
    62     HbMessageBox box( HbMessageBox::MessageTypeQuestion );
       
    63     box.setText( hbTrId( "txt_rad_info_activate_radio_in_offline_mode" ) );
       
    64     box.setTimeout( HbPopup::NoTimeout );
       
    65     box.setDismissPolicy( HbPopup::NoDismiss );
       
    66 
       
    67     HbAction* primaryAction = new HbAction( hbTrId( "txt_common_button_yes" ) );
       
    68     box.setPrimaryAction( primaryAction );
       
    69     HbAction* secondaryAction = new HbAction( hbTrId( "txt_common_button_no" ) );
       
    70     box.setSecondaryAction( secondaryAction );
       
    71 
       
    72     const bool answer = box.exec() == box.primaryAction();
       
    73 
       
    74     dialogShown( dummyView );
       
    75 
       
    76     return answer;
       
    77 }
       
    78 
       
    79 /*!
       
    80  *
       
    81  */
       
    82 void RadioMainWindow::showErrorMessage( const QString& text )
       
    83 {
       
    84     DummyViewPtr dummyView = prepareToShowDialog();
       
    85 
       
    86     HbMessageBox::warning( text );
       
    87 
       
    88     dialogShown( dummyView );
       
    89 }
       
    90 
       
    91 /*!
       
    92  *
       
    93  */
       
    94 void RadioMainWindow::init( RadioUiEngine* uiEngine )
       
    95 {
       
    96     LOG_METHOD;
       
    97     mUiEngine = uiEngine;
       
    98 
       
    99     // MainWindow is the one that always listens for orientation changes and then delegates
       
   100     // the updates to the views
       
   101     connectAndTest( hbInstance->allMainWindows().first(),   SIGNAL(orientationChanged(Qt::Orientation)),
       
   102                     this,                                   SLOT(updateOrientation(Qt::Orientation)) );
       
   103 
       
   104     connectAndTest( mUiEngine,  SIGNAL(volumeChanged(int)),
       
   105                     this,       SLOT(showVolumeLevel(int)) );
       
   106     connectAndTest( mUiEngine,  SIGNAL(antennaStatusChanged(bool)),
       
   107                     this,       SLOT(updateAntennaStatus(bool)) );
       
   108 
       
   109     activateTuningView();
       
   110 }
       
   111 
       
   112 /*!
       
   113  *
       
   114  */
       
   115 RadioUiEngine& RadioMainWindow::uiEngine()
       
   116 {
       
   117     return *mUiEngine;
       
   118 }
       
   119 
       
   120 /*!
       
   121  * Returns the XML layout section that corresponds to the view orientation
       
   122  */
       
   123 QString RadioMainWindow::orientationSection()
       
   124 {
       
   125     return orientation() == Qt::Vertical ? DOCML::SECTION_PORTRAIT : DOCML::SECTION_LANDSCAPE;
       
   126 }
       
   127 
       
   128 /*!
       
   129  *
       
   130  */
       
   131 void RadioMainWindow::activateTuningView()
       
   132 {
       
   133     activateView( mTuningView, DOCML::FILE_TUNINGVIEW, Hb::ViewSwitchUseBackAnim );
       
   134 }
       
   135 
       
   136 /*!
       
   137  *
       
   138  */
       
   139 void RadioMainWindow::activateStationsView()
       
   140 {
       
   141     activateView( mStationsView, DOCML::FILE_STATIONSVIEW );
       
   142 }
       
   143 
       
   144 /*!
       
   145  *
       
   146  */
       
   147 void RadioMainWindow::activateHistoryView()
       
   148 {
       
   149     activateView( mHistoryView, DOCML::FILE_HISTORYVIEW );
       
   150 }
       
   151 
       
   152 /*!
       
   153  * Private slot
       
   154  *
       
   155  */
       
   156 void RadioMainWindow::updateOrientation( Qt::Orientation orientation )
       
   157 {
       
   158     HbView* view = currentView();
       
   159     RADIO_ASSERT( view, "RadioMainWindow::updateOrientation", "Current view not found!" );
       
   160     if ( view ) {
       
   161         static_cast<RadioViewBase*>( view )->updateOrientation( orientation );
       
   162     }
       
   163 }
       
   164 
       
   165 /*!
       
   166  * Private slot
       
   167  *
       
   168  */
       
   169 void RadioMainWindow::showVolumeLevel( int volume )
       
   170 {
       
   171     if ( !mVolSlider ) {
       
   172         mVolSlider.reset( new HbVolumeSliderPopup() );
       
   173         mVolSlider->setRange( 0, KMaximumVolumeLevel );
       
   174         mVolSlider->setSingleStep( 1 );
       
   175         mVolSlider->setTimeout( KVolumeSliderDelay );
       
   176         connectAndTest( mVolSlider.data(),  SIGNAL(valueChanged(int)),
       
   177                         mUiEngine,          SLOT(setVolume(int)) );
       
   178     }
       
   179 
       
   180     mVolSlider->setValue( volume );
       
   181     mVolSlider->setText( QString( "%L1%" ).arg( volume * 100 / KMaximumVolumeLevel ) );
       
   182     mVolSlider->show();
       
   183 }
       
   184 
       
   185 /*!
       
   186  * Private slot
       
   187  *
       
   188  */
       
   189 void RadioMainWindow::updateAntennaStatus( bool connected )
       
   190 {
       
   191     if ( !connected ) {
       
   192         HbMessageBox infoBox( hbTrId( "txt_rad_dpophead_connect_wired_headset" ) );
       
   193         infoBox.exec();
       
   194     }
       
   195 }
       
   196 
       
   197 /*!
       
   198  *
       
   199  */
       
   200 void RadioMainWindow::activateView( ViewPtr& aMember, const QString& docmlFile, Hb::ViewSwitchFlags flags )
       
   201 {
       
   202     LOG_METHOD;
       
   203     if ( aMember && aMember == currentView() ) {
       
   204         return;
       
   205     }
       
   206 
       
   207     RadioViewBase* previousView = static_cast<RadioViewBase*>( currentView() );
       
   208     if ( previousView && previousView->isTransient() ) {
       
   209         removeView( previousView );
       
   210         previousView->deleteLater();
       
   211     }
       
   212 
       
   213     bool viewCreated = false;
       
   214     if ( !aMember ) {
       
   215         viewCreated = true;
       
   216 
       
   217         RadioXmlUiLoader* uiLoader = new RadioXmlUiLoader();
       
   218         bool ok = false;
       
   219 
       
   220         // View takes ownership of the ui loader when it is created inside the load function
       
   221         QObjectList objectList = uiLoader->load( docmlFile, &ok );
       
   222 
       
   223         RADIO_ASSERT( ok , "FMRadio", "invalid DocML file" );
       
   224         if ( !ok ) {
       
   225             delete uiLoader;
       
   226             uiLoader = 0;
       
   227             return;
       
   228         }
       
   229 
       
   230         aMember = ViewPtr( uiLoader->findObject<RadioViewBase>( DOCML::NAME_VIEW ) );
       
   231 
       
   232         foreach( QObject* object, objectList ) {
       
   233             const QString className = object->metaObject()->className();
       
   234             if ( !object->parent() && object != aMember.data() ) {
       
   235                 object->setParent( aMember.data() );
       
   236             }
       
   237         }
       
   238 
       
   239         aMember->init( uiLoader, this );
       
   240 
       
   241         addView( aMember );
       
   242     }
       
   243 
       
   244     aMember->updateOrientation( orientation(), viewCreated );
       
   245 
       
   246     setCurrentView( aMember, true, flags );
       
   247 }
       
   248 
       
   249 /*!
       
   250  *
       
   251  */
       
   252 DummyViewPtr RadioMainWindow::prepareToShowDialog()
       
   253 {
       
   254     // To be able to draw a dialog on screen we need a HbMainWindow instance and a HbView to get a graphics scene
       
   255     // so we create a dummy view and set it active
       
   256     DummyViewPtr dummyView( new HbView() );
       
   257     addView( dummyView.data() );
       
   258     setCurrentView( dummyView.data() );
       
   259     show();
       
   260     return dummyView;
       
   261 }
       
   262 
       
   263 /*!
       
   264  *
       
   265  */
       
   266 void RadioMainWindow::dialogShown( DummyViewPtr pointer )
       
   267 {
       
   268     // Clean up the dummy view
       
   269     hide();
       
   270     removeView( pointer.data() );
       
   271     pointer.clear();
       
   272 }