radioapp/radiouiengine/inc/radiostationmodel.h
changeset 13 46974bebc798
child 14 63aabac4416d
equal deleted inserted replaced
0:f3d95d9c00ab 13:46974bebc798
       
     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 #ifndef RADIOSTATIONMODEL_H_
       
    19 #define RADIOSTATIONMODEL_H_
       
    20 
       
    21 // System includes
       
    22 #include <qabstractlistmodel>
       
    23 #include <qmap>
       
    24 
       
    25 // User includes
       
    26 #include "radiouiengineexport.h"
       
    27 #include "radiostation.h"
       
    28 
       
    29 // Forward declarations
       
    30 class RadioStationModelPrivate;
       
    31 class RadioPresetStorage;
       
    32 class RadioStationHandlerIf;
       
    33 class RadioEngineWrapper;
       
    34 class RadioUiEngine;
       
    35 class RadioStation;
       
    36 class QIcon;
       
    37 
       
    38 // Constants
       
    39 typedef QMap<uint,RadioStation> Stations;
       
    40 
       
    41 // Class declaration
       
    42 class UI_ENGINE_DLL_EXPORT RadioStationModel : public QAbstractListModel
       
    43 {
       
    44     Q_OBJECT
       
    45     Q_DECLARE_PRIVATE_D( d_ptr, RadioStationModel )
       
    46     Q_DISABLE_COPY( RadioStationModel )
       
    47 
       
    48 public:
       
    49 
       
    50     enum RadioRole
       
    51     {
       
    52         RadioStationRole = Qt::UserRole + 1,
       
    53         ToggleFavoriteRole
       
    54     };
       
    55 
       
    56     enum DetailFlag
       
    57     {
       
    58         Minimal     = 1 << 0,
       
    59         ShowIcons   = 1 << 1,
       
    60         ShowGenre   = 1 << 2
       
    61     };
       
    62     Q_DECLARE_FLAGS( Detail, DetailFlag )
       
    63 
       
    64     explicit RadioStationModel( RadioUiEngine& uiEngine );
       
    65 
       
    66     ~RadioStationModel();
       
    67 
       
    68 // from base class QAbstractListModel
       
    69 
       
    70     Qt::ItemFlags flags ( const QModelIndex& index ) const;
       
    71     int rowCount( const QModelIndex& parent = QModelIndex() ) const;
       
    72     QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const;
       
    73     bool setData( const QModelIndex& index, const QVariant& value, int role = Qt::EditRole );
       
    74 
       
    75 // New functions
       
    76 
       
    77     /*!
       
    78      * Called by the wrapper to initialize the list with given amount of presets
       
    79      */
       
    80     void initialize( RadioPresetStorage* storage, RadioEngineWrapper* engine );
       
    81 
       
    82     /*!
       
    83      * Sets the icons to be used in the lists
       
    84      */
       
    85     void setIcons( const QIcon& favoriteIcon, const QIcon& nowPlayingIcon );
       
    86 
       
    87     /*!
       
    88      * Returns a reference to the station handler interface
       
    89      */
       
    90     RadioStationHandlerIf& stationHandlerIf();
       
    91 
       
    92     /*!
       
    93      * Returns a reference to the underlying QList so that it can be easily looped
       
    94      */
       
    95     const Stations& list() const;
       
    96 
       
    97     /*!
       
    98      * Returns the station at the given index.
       
    99      * The station is returned by value, but it is cheap because it is implicitly shared
       
   100      */
       
   101     RadioStation stationAt( int index ) const;
       
   102 
       
   103     /*!
       
   104      * Functions to find stations by frequency
       
   105      */
       
   106     bool findFrequency( uint frequency, RadioStation& station );
       
   107 
       
   108     /*!
       
   109      * Functions to find stations by preset index
       
   110      */
       
   111     int findPresetIndex( int presetIndex );
       
   112     int findPresetIndex( int presetIndex, RadioStation& station );
       
   113 
       
   114     /*!
       
   115      * Functions to remove stations
       
   116      */
       
   117     void removeByFrequency( uint frequency );
       
   118     void removeByPresetIndex( int presetIndex );
       
   119     void removeStation( const RadioStation& station );
       
   120 
       
   121     /*!
       
   122      * Functions to add and save stations
       
   123      */
       
   124     void addStation( const RadioStation& station );
       
   125     void saveStation( RadioStation& station );
       
   126 
       
   127     /*!
       
   128      * Convenience functions to change common settings
       
   129      */
       
   130     void setFavoriteByFrequency( uint frequency, bool favorite );
       
   131     void setFavoriteByPreset( int presetIndex, bool favorite );
       
   132     void renameStation( int presetIndex, const QString& name );
       
   133     void setFavorites( const QModelIndexList& favorites );
       
   134 
       
   135     /*!
       
   136      * Functions to init and access the currently tuned station
       
   137      */
       
   138     RadioStation& currentStation();
       
   139     const RadioStation& currentStation() const;
       
   140 
       
   141     /*!
       
   142      * Sets the model detail level
       
   143      */
       
   144     void setDetail( Detail level );
       
   145 
       
   146     /*!
       
   147      * Returns a list of radio stations in the given frequency range
       
   148      */
       
   149     QList<RadioStation> stationsInRange( uint minFrequency, uint maxFrequency );
       
   150 
       
   151     /*!
       
   152      * Returns the model index corresponding to the given frequency
       
   153      */
       
   154     QModelIndex modelIndexFromFrequency( uint frequency );
       
   155 
       
   156 signals:
       
   157 
       
   158     void stationDataChanged( const RadioStation& station );
       
   159     void radioTextReceived( const RadioStation& station );
       
   160     void dynamicPsChanged( const RadioStation& station );
       
   161     void favoriteChanged( const RadioStation& station );
       
   162     void stationAdded( const RadioStation& station );
       
   163 
       
   164 public slots:
       
   165 
       
   166     /**
       
   167      * Removes all stations
       
   168      */
       
   169     void removeAll();
       
   170 
       
   171 private slots:
       
   172 
       
   173     /*!
       
   174      * Timer timeout slot to indicate that the dynamic PS check has ended
       
   175      */
       
   176     void dynamicPsCheckEnded();
       
   177 
       
   178 private:
       
   179 
       
   180 // New functions
       
   181 
       
   182     /*!
       
   183      * Checks the given station and emits signals based on what member variables had been changed
       
   184      * since the last save or reset.
       
   185      */
       
   186     void emitChangeSignals( const RadioStation& station, RadioStation::Change flags );
       
   187 
       
   188     void emitDataChanged( const RadioStation& station );
       
   189 
       
   190     /*!
       
   191      * Finds an unused preset index
       
   192      */
       
   193     int findUnusedPresetIndex();
       
   194 
       
   195     /**
       
   196      * Used by the RDS data setters to find the correct station where the data is set
       
   197      * First tries the currentStation variable and if the frequency doesn't match, finds the right one
       
   198      */
       
   199     RadioStation findCurrentStation( uint frequency );
       
   200 
       
   201 private: // data
       
   202 
       
   203     /**
       
   204      * Unmodifiable pointer to the private implementation
       
   205      */
       
   206     RadioStationModelPrivate* const d_ptr;
       
   207 
       
   208 };
       
   209 
       
   210 Q_DECLARE_OPERATORS_FOR_FLAGS( RadioStationModel::Detail )
       
   211 
       
   212 #endif // RADIOSTATIONMODEL_H_