radioapp/radiowidgets/src/radiostationsview.cpp
changeset 28 075425b8d9a4
parent 24 6df133bd92e1
child 32 189d20c34778
child 34 bc10a61bd7d3
equal deleted inserted replaced
24:6df133bd92e1 28:075425b8d9a4
    21 #include <HbPushButton>
    21 #include <HbPushButton>
    22 #include <HbEffect>
    22 #include <HbEffect>
    23 #include <HbAction>
    23 #include <HbAction>
    24 #include <HbInputDialog>
    24 #include <HbInputDialog>
    25 #include <HbMenu>
    25 #include <HbMenu>
       
    26 #include <QSortFilterProxyModel>
    26 
    27 
    27 // User includes
    28 // User includes
    28 #include "radiostationsview.h"
    29 #include "radiostationsview.h"
    29 #include "radiologger.h"
    30 #include "radiologger.h"
    30 #include "radiowindow.h"
    31 #include "radiowindow.h"
    31 #include "radiouiengine.h"
    32 #include "radiouiengine.h"
    32 #include "radiobannerlabel.h"
       
    33 #include "radiofrequencyscanner.h"
    33 #include "radiofrequencyscanner.h"
    34 #include "radiouiloader.h"
    34 #include "radiouiloader.h"
    35 #include "radiostationmodel.h"
    35 #include "radiostationmodel.h"
    36 #include "radiostation.h"
    36 #include "radiostation.h"
    37 #include "radiostationfiltermodel.h"
    37 
       
    38 // Constants
       
    39 const char* REGEX_SHOW_FAVORITES = "true";
       
    40 const char* REGEX_SHOW_ALL = ".+";
    38 
    41 
    39 /*!
    42 /*!
    40  *
    43  *
    41  */
    44  */
    42 RadioStationsView::RadioStationsView() :
    45 RadioStationsView::RadioStationsView() :
    43     RadioViewBase( false ),
    46     RadioViewBase( false ),
    44     mModel( 0 ),
    47     mModel( 0 ),
    45     mFilterModel( 0 ),
       
    46     mScanStationsAction( 0 ),
    48     mScanStationsAction( 0 ),
    47     mClearListAction( 0 ),
    49     mClearListAction( 0 ),
    48     mStationsList( 0 ),
    50     mStationsList( 0 ),
    49     mHeadingBanner( 0 ),
       
    50     mFavoritesButton( 0 ),
    51     mFavoritesButton( 0 ),
    51     mLocalStationsButton( 0 ),
    52     mLocalStationsButton( 0 ),
    52     mSelectedStation( new RadioStation ),
    53     mSelectedStation( new RadioStation ),
    53     mCurrentQuestion( NoQuestion )
    54     mCurrentQuestion( NoQuestion )
    54 {
    55 {
    95 
    96 
    96 /*!
    97 /*!
    97  * Private slot
    98  * Private slot
    98  *
    99  *
    99  */
   100  */
   100 void RadioStationsView::listItemClicked( const QModelIndex& index )
   101 void RadioStationsView::handleClick( const QModelIndex& index )
   101 {
   102 {
   102     LOG_TIMESTAMP( "Channel change started" );
   103     LOG_TIMESTAMP( "Channel change started" );
   103     QModelIndex sourceIndex = mFilterModel->mapToSource( index );
   104     QModelIndex sourceIndex = mFilterModel->mapToSource( index );
   104     *mSelectedStation = mModel->stationAt( sourceIndex.row() );
   105     *mSelectedStation = mModel->stationAt( sourceIndex.row() );
   105     mMainWindow->uiEngine().tuneFrequency( mSelectedStation->frequency(), TuneReason::StationsList );
   106     mUiEngine->setFrequency( mSelectedStation->frequency(), TuneReason::StationsList );
   106 }
   107 }
   107 
   108 
   108 /*!
   109 /*!
   109  * Private slot
   110  * Private slot
   110  *
   111  *
   111  */
   112  */
   112 void RadioStationsView::listItemLongPressed( HbAbstractViewItem* item, const QPointF& coords )
   113 void RadioStationsView::handleLongPress( HbAbstractViewItem* item, const QPointF& coords )
   113 {
   114 {
   114     Q_UNUSED( item );
   115     Q_UNUSED( item );
   115 
   116 
   116     HbMenu* menu = mUiLoader->findObject<HbMenu>( DOCML::NAME_CONTEXT_MENU );
   117     HbMenu* menu = mUiLoader->findObject<HbMenu>( DOCML::NAME_CONTEXT_MENU );
   117 
   118 
   118     *mSelectedStation = mFilterModel->data( item->modelIndex(), RadioStationModel::RadioStationRole ).value<RadioStation>();
   119     *mSelectedStation = mFilterModel->data( item->modelIndex(), RadioRole::RadioStationRole ).value<RadioStation>();
   119 
   120 
   120     HbAction* favoriteAction = mUiLoader->findObject<HbAction>( DOCML::NAME_CONTEXT_FAVORITE );
   121     HbAction* favoriteAction = mUiLoader->findObject<HbAction>( DOCML::NAME_CONTEXT_FAVORITE );
   121     if ( mSelectedStation->isFavorite() ) {
   122     if ( mSelectedStation->isFavorite() ) {
   122         favoriteAction->setText( hbTrId( "txt_rad_menu_remove_favourite" ) );
   123         favoriteAction->setText( hbTrId( "txt_rad_menu_remove_favourite" ) );
   123     } else {
   124     } else {
   133  *
   134  *
   134  */
   135  */
   135 void RadioStationsView::updateAntennaStatus( bool connected )
   136 void RadioStationsView::updateAntennaStatus( bool connected )
   136 {
   137 {
   137     Q_UNUSED( connected );
   138     Q_UNUSED( connected );
   138     updateControlVisibilities();
   139     updateVisibilities();
   139 }
   140 }
   140 
   141 
   141 /*!
   142 /*!
   142  * Private slot
   143  * Private slot
   143  *
   144  *
   144  */
   145  */
   145 void RadioStationsView::updateCurrentStation()
   146 void RadioStationsView::updateViewMode()
   146 {
   147 {
   147     mFilterModel->invalidate();
   148     QString section = DOCML::SV_SECTION_SHOW_ALL_STATIONS;
   148 }
   149     QString filter = REGEX_SHOW_ALL;
   149 
   150 
   150 /*!
   151     if ( sender() == mFavoritesButton ) {
   151  * Private slot
   152         section = DOCML::SV_SECTION_SHOW_FAVORITES;
   152  *
   153         filter = REGEX_SHOW_FAVORITES;
   153  */
   154     }
   154 void RadioStationsView::deckButtonPressed()
   155 
   155 {
   156     loadSection( DOCML::FILE_STATIONSVIEW, section );
   156     const bool showFavorites = mFavoritesButton->isChecked();
   157     mFilterModel->setFilterRegExp( filter );
   157     if ( showFavorites ) {
   158 
   158         loadSection( DOCML::FILE_STATIONSVIEW, DOCML::SV_SECTION_SHOW_FAVORITES );
   159     updateVisibilities();
   159     } else {
       
   160         loadSection( DOCML::FILE_STATIONSVIEW, DOCML::SV_SECTION_SHOW_ALL_STATIONS );
       
   161     }
       
   162 
       
   163     mFilterModel->setTypeFilter( showFavorites ? RadioStation::Favorite
       
   164                                                : RadioStation::LocalStation );
       
   165 
       
   166     updateControlVisibilities();
       
   167 }
   160 }
   168 
   161 
   169 /*!
   162 /*!
   170  * Private slot
   163  * Private slot
   171  *
   164  *
   172  */
   165  */
   173 void RadioStationsView::startScanning()
   166 void RadioStationsView::startScanning()
   174 {
   167 {
   175     const int rowCount =  mMainWindow->uiEngine().stationModel().rowCount();
   168     const int rowCount =  mUiEngine->stationModel().rowCount();
   176     mCurrentQuestion = StartScanning;
   169     mCurrentQuestion = StartScanning;
   177     if ( rowCount > 0 ) {
   170     if ( rowCount > 0 ) {
   178         askQuestion( hbTrId( "txt_rad_info_all_stations_in_stations_list_will_be" ) );
   171         askQuestion( hbTrId( "txt_rad_info_all_stations_in_stations_list_will_be" ) );
   179     } else {
   172     } else {
   180         userAccepted();
   173         userAccepted();
   185  * Private slot
   178  * Private slot
   186  *
   179  *
   187  */
   180  */
   188 void RadioStationsView::finishScanning()
   181 void RadioStationsView::finishScanning()
   189 {
   182 {
   190     updateControlVisibilities();
   183     updateVisibilities();
   191     mFrequencyScanner.take();
   184     mFrequencyScanner.take();
   192 }
   185 }
   193 
   186 
   194 /*!
   187 /*!
   195  * Private slot
   188  * Private slot
   196  *
   189  *
   197  */
   190  */
   198 void RadioStationsView::updateControlVisibilities()
   191 void RadioStationsView::updateVisibilities()
   199 {
   192 {
   200     LOG_SLOT_CALLER;
   193     LOG_SLOT_CALLER;
   201     bool listEmpty = mModel->rowCount() == 0;
   194     bool listEmpty = mModel->rowCount() == 0;
   202     const bool localStationsMode = !mFavoritesButton->isChecked();
   195     const bool localStationsMode = !mFavoritesButton->isChecked();
   203 
   196 
   204     if ( !localStationsMode ) {
   197     if ( !localStationsMode ) {
   205         listEmpty = mModel->favoriteCount() == 0;
   198         listEmpty = mModel->favoriteCount() == 0;
   206     }
   199     }
   207 
   200 
   208     mClearListAction->setVisible( !listEmpty );
   201     mClearListAction->setEnabled( !listEmpty );
   209 
   202 
   210     const bool scanAvailable = mMainWindow->uiEngine().isAntennaAttached() && localStationsMode;
   203     const bool scanAvailable = mUiEngine->isAntennaAttached() && localStationsMode;
   211     mScanStationsAction->setVisible( scanAvailable );
   204     mScanStationsAction->setEnabled( scanAvailable );
   212     HbPushButton* scanButton = mUiLoader->findWidget<HbPushButton>( DOCML::SV_NAME_SCAN_BUTTON );
   205     HbPushButton* scanButton = mUiLoader->findWidget<HbPushButton>( DOCML::SV_NAME_SCAN_BUTTON );
   213     scanButton->setEnabled( scanAvailable );
   206     scanButton->setEnabled( scanAvailable );
   214 
   207 
   215     loadSection( DOCML::FILE_STATIONSVIEW, listEmpty ? DOCML::SV_SECTION_SHOW_SCAN_TEXT : DOCML::SV_SECTION_HIDE_SCAN_TEXT );
   208     loadSection( DOCML::FILE_STATIONSVIEW, listEmpty ? DOCML::SV_SECTION_SHOW_SCAN_TEXT : DOCML::SV_SECTION_HIDE_SCAN_TEXT );
   216 }
   209 }
   233  */
   226  */
   234 void RadioStationsView::rename()
   227 void RadioStationsView::rename()
   235 {
   228 {
   236     HbInputDialog* nameQuery = new HbInputDialog();
   229     HbInputDialog* nameQuery = new HbInputDialog();
   237     nameQuery->setAttribute( Qt::WA_DeleteOnClose, true );
   230     nameQuery->setAttribute( Qt::WA_DeleteOnClose, true );
   238     nameQuery->setDismissPolicy( HbDialog::NoDismiss );
   231     nameQuery->setDismissPolicy( HbDialog::TapOutside );
   239     nameQuery->setPromptText( hbTrId( "txt_rad_dialog_new_name" ) );
   232     nameQuery->setPromptText( hbTrId( "txt_rad_dialog_new_name" ) );
   240     nameQuery->setInputMode( HbInputDialog::TextInput );
   233     nameQuery->setInputMode( HbInputDialog::TextInput );
   241     nameQuery->setValue( mSelectedStation->name() );
   234     nameQuery->setValue( mSelectedStation->name() );
   242     nameQuery->setObjectName( DOCML::NAME_INPUT_QUERY );
   235     nameQuery->setObjectName( DOCML::NAME_INPUT_QUERY );
   243     nameQuery->open( this, SLOT(renameDone(HbAction* )) );
   236     nameQuery->open( this, SLOT(renameDone(HbAction*)) );
   244 }
   237 }
   245 
   238 
   246 /*!
   239 /*!
   247  * Private slot
   240  * Private slot
   248  *
   241  *
   268  */
   261  */
   269 void RadioStationsView::renameDone( HbAction* action )
   262 void RadioStationsView::renameDone( HbAction* action )
   270 {
   263 {
   271     HbInputDialog* dlg = static_cast<HbInputDialog*>( sender() );
   264     HbInputDialog* dlg = static_cast<HbInputDialog*>( sender() );
   272 
   265 
   273     if( action == dlg->primaryAction() ) {
   266     if( action ) {
   274         mModel->renameStation( mSelectedStation->presetIndex(), dlg->value().toString() );
   267         mModel->renameStation( mSelectedStation->presetIndex(), dlg->value().toString() );
   275     }
   268     }
   276 }
   269 }
   277 
   270 
   278 /*!
   271 /*!
   280  *
   273  *
   281  */
   274  */
   282 void RadioStationsView::init()
   275 void RadioStationsView::init()
   283 {
   276 {
   284     LOG_METHOD;
   277     LOG_METHOD;
   285     mInitialized = true;
   278     mModel = &mUiEngine->stationModel();
   286     mModel = &mMainWindow->uiEngine().stationModel();
   279 
   287 
   280     mFilterModel = new QSortFilterProxyModel( this );
   288     RadioUiEngine* engine = &mMainWindow->uiEngine();
   281     mFilterModel->setDynamicSortFilter( true );
   289 
   282     mFilterModel->setFilterRole( RadioRole::IsFavoriteRole );
   290     mFilterModel = engine->createNewFilterModel( this );
   283     mFilterModel->setSourceModel( &mUiEngine->stationModel() );
   291     mFilterModel->setTypeFilter( RadioStation::LocalStation );
       
   292 
   284 
   293     loadSection( DOCML::FILE_STATIONSVIEW, DOCML::SV_SECTION_SHOW_ALL_STATIONS );
   285     loadSection( DOCML::FILE_STATIONSVIEW, DOCML::SV_SECTION_SHOW_ALL_STATIONS );
   294 
   286 
   295     if ( !mFavoriteIcon.isNull() && !mNowPlayingIcon.isNull() ) {
   287     if ( !mFavoriteIcon.isNull() && !mNowPlayingIcon.isNull() ) {
   296         mModel->setIcons( mFavoriteIcon.qicon(), mNowPlayingIcon.qicon() );
   288         mModel->setIcons( mFavoriteIcon.qicon(), mNowPlayingIcon.qicon() );
   297     }
   289     }
   298     mModel->setDetail( RadioStationModel::ShowIcons | RadioStationModel::ShowGenre );
   290     mModel->setDetail( RadioStationModel::ShowIcons | RadioStationModel::ShowGenre );
   299 
   291 
   300     mStationsList           = mUiLoader->findObject<HbListView>( DOCML::SV_NAME_STATIONS_LIST );
   292     mStationsList           = mUiLoader->findObject<HbListView>( DOCML::SV_NAME_STATIONS_LIST );
   301     mHeadingBanner          = mUiLoader->findWidget<RadioBannerLabel>( DOCML::SV_NAME_HEADING_BANNER );
       
   302     mFavoritesButton        = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_FAVORITES_BUTTON );
   293     mFavoritesButton        = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_FAVORITES_BUTTON );
   303     mLocalStationsButton    = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_LOCALS_BUTTON );
   294     mLocalStationsButton    = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_LOCALS_BUTTON );
   304 
   295 
   305     connectAndTest( engine,                 SIGNAL(antennaStatusChanged(bool)),
   296     Radio::connect( mUiEngine.data(),       SIGNAL(antennaStatusChanged(bool)),
   306                     this,                   SLOT(updateAntennaStatus(bool)) );
   297                     this,                   SLOT(updateAntennaStatus(bool)) );
   307     connectAndTest( mModel,                 SIGNAL(rowsInserted(QModelIndex,int,int)),
   298     Radio::connect( mModel,                 SIGNAL(rowsInserted(QModelIndex,int,int)),
   308                     this,                   SLOT(updateControlVisibilities() ) );
   299                     this,                   SLOT(updateVisibilities()) );
   309     connectAndTest( mModel,                 SIGNAL(rowsRemoved(QModelIndex,int,int)),
   300     Radio::connect( mModel,                 SIGNAL(rowsRemoved(QModelIndex,int,int)),
   310                     this,                   SLOT(updateControlVisibilities()) );
   301                     this,                   SLOT(updateVisibilities()) );
   311     connectAndTest( mModel,                 SIGNAL(modelReset() ),
   302     Radio::connect( mModel,                 SIGNAL(modelReset()),
   312                     this,                   SLOT(updateControlVisibilities() ) );
   303                     this,                   SLOT(updateVisibilities()) );
   313     connectAndTest( mFavoritesButton,       SIGNAL(triggered() ),
   304     Radio::connect( mFavoritesButton,       SIGNAL(triggered()),
   314                     this,                   SLOT(deckButtonPressed() ) );
   305                     this,                   SLOT(updateViewMode()) );
   315     connectAndTest( mLocalStationsButton,   SIGNAL(triggered() ),
   306     Radio::connect( mLocalStationsButton,   SIGNAL(triggered() ),
   316                     this,                   SLOT(deckButtonPressed() ) );
   307                     this,                   SLOT(updateViewMode()) );
   317 
   308     Radio::connect( mStationsList,          SIGNAL(activated(QModelIndex)),
   318     connectAndTest( engine, SIGNAL(tunedToFrequency(uint,int)), this, SLOT(updateCurrentStation()) );
   309                     this,                   SLOT(handleClick(QModelIndex)) );
       
   310     Radio::connect( mStationsList,          SIGNAL(longPressed(HbAbstractViewItem*,QPointF)),
       
   311                     this,                   SLOT(handleLongPress(HbAbstractViewItem*,QPointF)) );
       
   312 
       
   313     connectCommonMenuItem( MenuItem::Exit );
       
   314     connectXmlElement( DOCML::SV_NAME_SCAN_BUTTON,      SIGNAL(clicked()),
       
   315                        this,                            SLOT(startScanning()) );
       
   316 
       
   317     // Context menu actions
       
   318     connectXmlElement( DOCML::SV_NAME_RENAME_ACTION,    SIGNAL(triggered()),
       
   319                        this,                            SLOT(rename()) );
       
   320     connectXmlElement( DOCML::SV_NAME_FAVORITE_ACTION,  SIGNAL(triggered()),
       
   321                        this,                            SLOT(toggleFavorite()) );
       
   322     connectXmlElement( DOCML::SV_NAME_DELETE_ACTION,    SIGNAL(triggered()),
       
   323                        this,                            SLOT(deleteStation()) );
   319 
   324 
   320     // "Scan local stations" menu item
   325     // "Scan local stations" menu item
   321     mScanStationsAction = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_SCAN_ACTION );
   326     mScanStationsAction = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_SCAN_ACTION );
   322     
   327     Radio::connect( mScanStationsAction,   SIGNAL(triggered() ),
       
   328                     this,                   SLOT(startScanning() ) );
       
   329 
   323     // "Remove all presets" menu item
   330     // "Remove all presets" menu item
   324     mClearListAction = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_CLEAR_LIST_ACTION );
   331     mClearListAction = mUiLoader->findObject<HbAction>( DOCML::SV_NAME_CLEAR_LIST_ACTION );
   325     connectAndTest( mClearListAction,   SIGNAL(triggered() ),
   332     Radio::connect( mClearListAction,   SIGNAL(triggered() ),
   326                     this,               SLOT(clearList() ) );
   333                     this,               SLOT(clearList() ) );
   327 
   334 
   328     connectCommonMenuItem( MenuItem::UseLoudspeaker );
   335     connectCommonMenuItem( MenuItem::UseLoudspeaker );
   329 
   336 
   330     initListView();
   337     initListView();
   331     
   338 
   332     initBackAction();
   339     initBackAction();
   333 
   340 
   334     updateControlVisibilities();
   341     updateViewMode();
   335 }
   342 }
   336 
   343 
   337 /*!
   344 /*!
   338  * \reimp
   345  * \reimp
   339  */
   346  */
   340 void RadioStationsView::userAccepted()
   347 void RadioStationsView::userAccepted()
   341 {
   348 {
   342     if ( mCurrentQuestion == StartScanning ) {
   349     if ( mCurrentQuestion == StartScanning ) {
   343         mFrequencyScanner.reset( new RadioFrequencyScanner( mMainWindow->uiEngine(), this ) );
   350         mFrequencyScanner.reset( new RadioFrequencyScanner( *mUiEngine, this ) );
   344 
   351 
   345         connectAndTest( mFrequencyScanner.data(),   SIGNAL(frequencyScannerFinished()),
   352         Radio::connect( mFrequencyScanner.data(),   SIGNAL(frequencyScannerFinished()),
   346                         this,                       SLOT(finishScanning()) );
   353                         this,                       SLOT(finishScanning()) );
   347 
   354 
   348         mFrequencyScanner->startScanning();
   355         mFrequencyScanner->startScanning();
   349     } else if ( mCurrentQuestion == ClearList ){
   356     } else if ( mCurrentQuestion == ClearList ){
   350         const bool favoriteMode = mFavoritesButton->isChecked();
   357         const bool favoriteMode = mFavoritesButton->isChecked();
   351         mModel->removeAll( favoriteMode ? RadioStationModel::RemoveFavorites : RadioStationModel::RemoveAll );
   358         mModel->removeAll( favoriteMode ? RadioStationModel::RemoveFavorites : RadioStationModel::RemoveAll );
   352         updateControlVisibilities();
   359         updateVisibilities();
   353     } else if ( mCurrentQuestion == DeleteStation ) {
   360     } else if ( mCurrentQuestion == DeleteStation ) {
   354         mModel->removeStation( mModel->currentStation() );
   361         mModel->removeStation( mModel->currentStation() );
   355     }
   362     }
   356 
   363 
   357     mCurrentQuestion = NoQuestion;
   364     mCurrentQuestion = NoQuestion;
   358 }
       
   359 
       
   360 /*!
       
   361  * \reimp
       
   362  */
       
   363 void RadioStationsView::showEvent( QShowEvent* event )
       
   364 {
       
   365     RadioViewBase::showEvent( event );
       
   366 }
   365 }
   367 
   366 
   368 /*!
   367 /*!
   369  *
   368  *
   370  */
   369  */