radioapp/radiowidgets/src/radiostationsview.cpp
branchRCL_3
changeset 20 93c594350b9a
parent 19 cce62ebc198e
equal deleted inserted replaced
19:cce62ebc198e 20:93c594350b9a
     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 <HbListView>
       
    20 #include <HbAbstractViewItem>
       
    21 #include <HbPushButton>
       
    22 #include <HbEffect>
       
    23 #include <HbAction>
       
    24 #include <HbInputDialog>
       
    25 #include <HbMenu>
       
    26 #include <QSortFilterProxyModel>
       
    27 
       
    28 // User includes
       
    29 #include "radiostationsview.h"
       
    30 #include "radiologger.h"
       
    31 #include "radiowindow.h"
       
    32 #include "radiouiengine.h"
       
    33 #include "radiofrequencyscanner.h"
       
    34 #include "radiouiloader.h"
       
    35 #include "radiostationmodel.h"
       
    36 #include "radiostation.h"
       
    37 
       
    38 // Constants
       
    39 const char* REGEX_SHOW_FAVORITES = "true";
       
    40 const char* REGEX_SHOW_ALL = ".+";
       
    41 
       
    42 /*!
       
    43  *
       
    44  */
       
    45 RadioStationsView::RadioStationsView() :
       
    46     RadioViewBase( false ),
       
    47     mModel( 0 ),
       
    48     mScanStationsAction( 0 ),
       
    49     mClearListAction( 0 ),
       
    50     mStationsList( 0 ),
       
    51     mFavoritesButton( 0 ),
       
    52     mLocalStationsButton( 0 ),
       
    53     mSelectedStation( new RadioStation ),
       
    54     mCurrentQuestion( NoQuestion )
       
    55 {
       
    56 }
       
    57 
       
    58 /*!
       
    59  *
       
    60  */
       
    61 RadioStationsView::~RadioStationsView()
       
    62 {
       
    63 }
       
    64 
       
    65 /*!
       
    66  *
       
    67  */
       
    68 void RadioStationsView::setNowPlayingIcon( const HbIcon& nowPlayingIcon )
       
    69 {
       
    70     mNowPlayingIcon = nowPlayingIcon;
       
    71 }
       
    72 
       
    73 /*!
       
    74  *
       
    75  */
       
    76 HbIcon RadioStationsView::nowPlayingIcon() const
       
    77 {
       
    78     return mNowPlayingIcon;
       
    79 }
       
    80 
       
    81 /*!
       
    82  *
       
    83  */
       
    84 void RadioStationsView::setFavoriteIcon( const HbIcon& favoriteIcon )
       
    85 {
       
    86     mFavoriteIcon = favoriteIcon;
       
    87 }
       
    88 
       
    89 /*!
       
    90  *
       
    91  */
       
    92 HbIcon RadioStationsView::favoriteIcon() const
       
    93 {
       
    94     return mFavoriteIcon;
       
    95 }
       
    96 
       
    97 /*!
       
    98  * Private slot
       
    99  *
       
   100  */
       
   101 void RadioStationsView::handleClick( const QModelIndex& index )
       
   102 {
       
   103     LOG_TIMESTAMP( "Channel change started" );
       
   104     QModelIndex sourceIndex = mFilterModel->mapToSource( index );
       
   105     *mSelectedStation = mModel->stationAt( sourceIndex.row() );
       
   106     mUiEngine->setFrequency( mSelectedStation->frequency(), TuneReason::StationsList );
       
   107 }
       
   108 
       
   109 /*!
       
   110  * Private slot
       
   111  *
       
   112  */
       
   113 void RadioStationsView::handleLongPress( HbAbstractViewItem* item, const QPointF& coords )
       
   114 {
       
   115     Q_UNUSED( item );
       
   116 
       
   117     HbMenu* menu = mUiLoader->findObject<HbMenu>( DOCML::NAME_CONTEXT_MENU );
       
   118 
       
   119     *mSelectedStation = mFilterModel->data( item->modelIndex(), RadioRole::RadioStationRole ).value<RadioStation>();
       
   120 
       
   121     HbAction* favoriteAction = mUiLoader->findObject<HbAction>( DOCML::NAME_CONTEXT_FAVORITE );
       
   122 
       
   123     if ( mSelectedStation->isFavorite() ) {
       
   124         favoriteAction->setText( hbTrId( "txt_rad_menu_remove_favourite" ) );
       
   125     } else {
       
   126         favoriteAction->setText( hbTrId( "txt_rad_menu_add_to_favourites" ) );
       
   127     }
       
   128 
       
   129     HbAction* playAction = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_PLAY_ACTION );
       
   130 
       
   131     if ( mSelectedStation->frequency() == mUiEngine->currentFrequency() ) {
       
   132         playAction->setVisible( false );
       
   133     } else {
       
   134         playAction->setVisible( true );
       
   135     }
       
   136 
       
   137     menu->setPreferredPos( QPointF( size().width() / 2 - menu->size().width() / 2, coords.y() - menu->size().height() / 2 ) );
       
   138     menu->show();
       
   139 }
       
   140 
       
   141 /*!
       
   142  * Private slot
       
   143  *
       
   144  */
       
   145 void RadioStationsView::updateAntennaStatus( bool connected )
       
   146 {
       
   147     Q_UNUSED( connected );
       
   148     updateVisibilities();
       
   149 }
       
   150 
       
   151 /*!
       
   152  * Private slot
       
   153  *
       
   154  */
       
   155 void RadioStationsView::updateViewMode()
       
   156 {
       
   157     QString section = DOCML::SV_SECTION_SHOW_ALL_STATIONS;
       
   158     QString filter = REGEX_SHOW_ALL;
       
   159 
       
   160     if ( sender() == mFavoritesButton ) {
       
   161         section = DOCML::SV_SECTION_SHOW_FAVORITES;
       
   162         filter = REGEX_SHOW_FAVORITES;
       
   163     }
       
   164 
       
   165     loadSection( DOCML::FILE_STATIONSVIEW, section );
       
   166     mFilterModel->setFilterRegExp( filter );
       
   167 
       
   168     updateVisibilities();
       
   169 }
       
   170 
       
   171 /*!
       
   172  * Private slot
       
   173  *
       
   174  */
       
   175 void RadioStationsView::startScanning()
       
   176 {
       
   177     const int rowCount =  mUiEngine->stationModel().rowCount();
       
   178     mCurrentQuestion = StartScanning;
       
   179     if ( rowCount > 0 ) {
       
   180         askQuestion( hbTrId( "txt_rad_info_all_stations_in_stations_list_will_be" ) );
       
   181     } else {
       
   182         userAccepted();
       
   183     }
       
   184 }
       
   185 
       
   186 /*!
       
   187  * Private slot
       
   188  *
       
   189  */
       
   190 void RadioStationsView::finishScanning()
       
   191 {
       
   192     updateVisibilities();
       
   193     mFrequencyScanner.take();
       
   194 }
       
   195 
       
   196 /*!
       
   197  * Private slot
       
   198  *
       
   199  */
       
   200 void RadioStationsView::updateVisibilities()
       
   201 {
       
   202     LOG_SLOT_CALLER;
       
   203     bool listEmpty = mModel->rowCount() == 0;
       
   204     const bool localStationsMode = !mFavoritesButton->isChecked();
       
   205 
       
   206     if ( !localStationsMode ) {
       
   207         listEmpty = mModel->favoriteCount() == 0;
       
   208     }
       
   209 
       
   210     mClearListAction->setEnabled( !listEmpty );
       
   211 
       
   212     const bool scanAvailable = mUiEngine->isAntennaAttached() && localStationsMode;
       
   213     mScanStationsAction->setEnabled( scanAvailable );
       
   214     HbPushButton* scanButton = mUiLoader->findWidget<HbPushButton>( DOCML::SV_NAME_SCAN_BUTTON );
       
   215     scanButton->setEnabled( scanAvailable );
       
   216 
       
   217     loadSection( DOCML::FILE_STATIONSVIEW, listEmpty ? DOCML::SV_SECTION_SHOW_SCAN_TEXT : DOCML::SV_SECTION_HIDE_SCAN_TEXT );
       
   218 }
       
   219 
       
   220 /*!
       
   221  * Private slot
       
   222  *
       
   223  */
       
   224 void RadioStationsView::clearList()
       
   225 {
       
   226     const bool favoriteMode = mFavoritesButton->isChecked();
       
   227     mCurrentQuestion = ClearList;
       
   228     askQuestion( hbTrId( favoriteMode ? "txt_rad_info_clear_favourite_stations_list"
       
   229                                       : "txt_rad_info_clear_all_stations_list" ) );
       
   230 }
       
   231 
       
   232 /*!
       
   233  * Private slot
       
   234  *
       
   235  */
       
   236 void RadioStationsView::play()
       
   237 {
       
   238     LOG("Play from context menu");
       
   239     mUiEngine->setFrequency( mSelectedStation->frequency(), TuneReason::StationsList );
       
   240 }
       
   241 
       
   242 /*!
       
   243  * Private slot
       
   244  *
       
   245  */
       
   246 void RadioStationsView::rename()
       
   247 {
       
   248     HbInputDialog* nameQuery = new HbInputDialog();
       
   249     nameQuery->setAttribute( Qt::WA_DeleteOnClose, true );
       
   250     nameQuery->setDismissPolicy( HbDialog::TapOutside );
       
   251     nameQuery->setPromptText( hbTrId( "txt_rad_dialog_new_name" ) );
       
   252     nameQuery->setInputMode( HbInputDialog::TextInput );
       
   253     nameQuery->setValue( mSelectedStation->name() );
       
   254     nameQuery->setObjectName( DOCML::NAME_INPUT_QUERY );
       
   255     nameQuery->open( this, SLOT(renameDone(HbAction*)) );
       
   256 }
       
   257 
       
   258 /*!
       
   259  * Private slot
       
   260  *
       
   261  */
       
   262 void RadioStationsView::toggleFavorite()
       
   263 {
       
   264     mModel->setFavoriteByPreset( mSelectedStation->presetIndex(), !mSelectedStation->isFavorite() );
       
   265 }
       
   266 
       
   267 /*!
       
   268  * Private slot
       
   269  *
       
   270  */
       
   271 void RadioStationsView::deleteStation()
       
   272 {
       
   273     mCurrentQuestion = DeleteStation;
       
   274     askQuestion( hbTrId( "txt_rad_info_delete_station" ) );
       
   275 }
       
   276 
       
   277 /*!
       
   278  * Private slot
       
   279  *
       
   280  */
       
   281 void RadioStationsView::renameDone( HbAction* action )
       
   282 {
       
   283     HbInputDialog* dlg = static_cast<HbInputDialog*>( sender() );
       
   284 
       
   285     if( action ) {
       
   286         mModel->renameStation( mSelectedStation->presetIndex(), dlg->value().toString() );
       
   287     }
       
   288 }
       
   289 
       
   290 /*!
       
   291  * From RadioViewBase
       
   292  *
       
   293  */
       
   294 void RadioStationsView::init()
       
   295 {
       
   296     LOG_METHOD;
       
   297     mModel = &mUiEngine->stationModel();
       
   298 
       
   299     mFilterModel = new QSortFilterProxyModel( this );
       
   300     mFilterModel->setDynamicSortFilter( true );
       
   301     mFilterModel->setFilterRole( RadioRole::IsFavoriteRole );
       
   302     mFilterModel->setSourceModel( &mUiEngine->stationModel() );
       
   303 
       
   304     loadSection( DOCML::FILE_STATIONSVIEW, DOCML::SV_SECTION_SHOW_ALL_STATIONS );
       
   305 
       
   306     if ( !mFavoriteIcon.isNull() && !mNowPlayingIcon.isNull() ) {
       
   307         mModel->setIcons( mFavoriteIcon.qicon(), mNowPlayingIcon.qicon() );
       
   308     }
       
   309     mModel->setDetail( RadioStationModel::ShowIcons | RadioStationModel::ShowGenre );
       
   310 
       
   311     mStationsList           = mUiLoader->findObject<HbListView>( DOCML::SV_NAME_STATIONS_LIST );
       
   312     mFavoritesButton        = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_FAVORITES_BUTTON );
       
   313     mLocalStationsButton    = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_LOCALS_BUTTON );
       
   314 
       
   315     Radio::connect( mUiEngine.data(),       SIGNAL(antennaStatusChanged(bool)),
       
   316                     this,                   SLOT(updateAntennaStatus(bool)) );
       
   317     Radio::connect( mModel,                 SIGNAL(rowsInserted(QModelIndex,int,int)),
       
   318                     this,                   SLOT(updateVisibilities()) );
       
   319     Radio::connect( mModel,                 SIGNAL(rowsRemoved(QModelIndex,int,int)),
       
   320                     this,                   SLOT(updateVisibilities()) );
       
   321     Radio::connect( mModel,                 SIGNAL(modelReset()),
       
   322                     this,                   SLOT(updateVisibilities()) );
       
   323     Radio::connect( mFavoritesButton,       SIGNAL(triggered()),
       
   324                     this,                   SLOT(updateViewMode()) );
       
   325     Radio::connect( mLocalStationsButton,   SIGNAL(triggered() ),
       
   326                     this,                   SLOT(updateViewMode()) );
       
   327     Radio::connect( mStationsList,          SIGNAL(activated(QModelIndex)),
       
   328                     this,                   SLOT(handleClick(QModelIndex)) );
       
   329     Radio::connect( mStationsList,          SIGNAL(longPressed(HbAbstractViewItem*,QPointF)),
       
   330                     this,                   SLOT(handleLongPress(HbAbstractViewItem*,QPointF)) );
       
   331 
       
   332     connectCommonMenuItem( MenuItem::Exit );
       
   333     connectXmlElement( DOCML::SV_NAME_SCAN_BUTTON,      SIGNAL(clicked()),
       
   334                        this,                            SLOT(startScanning()) );
       
   335 
       
   336     // Context menu actions
       
   337     connectXmlElement( DOCML::SV_NAME_PLAY_ACTION,      SIGNAL(triggered()),
       
   338                        this,                            SLOT(play()) );
       
   339     connectXmlElement( DOCML::SV_NAME_RENAME_ACTION,    SIGNAL(triggered()),
       
   340                        this,                            SLOT(rename()) );
       
   341     connectXmlElement( DOCML::SV_NAME_FAVORITE_ACTION,  SIGNAL(triggered()),
       
   342                        this,                            SLOT(toggleFavorite()) );
       
   343     connectXmlElement( DOCML::SV_NAME_DELETE_ACTION,    SIGNAL(triggered()),
       
   344                        this,                            SLOT(deleteStation()) );
       
   345 
       
   346     // "Scan local stations" menu item
       
   347     mScanStationsAction = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_SCAN_ACTION );
       
   348     Radio::connect( mScanStationsAction,   SIGNAL(triggered() ),
       
   349                     this,                   SLOT(startScanning() ) );
       
   350 
       
   351     // "Remove all presets" menu item
       
   352     mClearListAction = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_CLEAR_LIST_ACTION );
       
   353     Radio::connect( mClearListAction,   SIGNAL(triggered() ),
       
   354                     this,               SLOT(clearList() ) );
       
   355 
       
   356     connectCommonMenuItem( MenuItem::UseLoudspeaker );
       
   357 
       
   358     initListView();
       
   359 
       
   360     initBackAction();
       
   361 
       
   362     updateViewMode();
       
   363 }
       
   364 
       
   365 /*!
       
   366  * \reimp
       
   367  */
       
   368 void RadioStationsView::userAccepted()
       
   369 {
       
   370     if ( mCurrentQuestion == StartScanning ) {
       
   371         mFrequencyScanner.reset( new RadioFrequencyScanner( *mUiEngine, this ) );
       
   372 
       
   373         Radio::connect( mFrequencyScanner.data(),   SIGNAL(frequencyScannerFinished()),
       
   374                         this,                       SLOT(finishScanning()) );
       
   375 
       
   376         mFrequencyScanner->startScanning();
       
   377     } else if ( mCurrentQuestion == ClearList ){
       
   378         const bool favoriteMode = mFavoritesButton->isChecked();
       
   379         mModel->removeAll( favoriteMode ? RadioStationModel::RemoveFavorites : RadioStationModel::RemoveAll );
       
   380         updateVisibilities();
       
   381     } else if ( mCurrentQuestion == DeleteStation ) {
       
   382         mModel->removeStation( *mSelectedStation );
       
   383     }
       
   384 
       
   385     mCurrentQuestion = NoQuestion;
       
   386 }
       
   387 
       
   388 /*!
       
   389  *
       
   390  */
       
   391 void RadioStationsView::initListView()
       
   392 {
       
   393     mStationsList->setScrollingStyle( HbListView::PanOrFlick );
       
   394     mStationsList->setModel( mFilterModel );
       
   395     mStationsList->setSelectionMode( HbListView::NoSelection );
       
   396     mStationsList->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
       
   397     //TODO: Uncomment after MCL wk28 release to improve scrolling FPS rate
       
   398     //mStationsList->setItemPixmapCacheEnabled( true );
       
   399 }