radioapp/radiouiengine/src/radiohistorymodel.cpp
changeset 19 afea38384506
parent 16 f54ebcfc1b80
child 37 451b2e1545b2
equal deleted inserted replaced
16:f54ebcfc1b80 19:afea38384506
    37 {
    37 {
    38     connectAndTest( &uiEngine,  SIGNAL(tunedToFrequency(uint,int)),
    38     connectAndTest( &uiEngine,  SIGNAL(tunedToFrequency(uint,int)),
    39                     this,       SLOT(resetCurrentSong()) );
    39                     this,       SLOT(resetCurrentSong()) );
    40     connectAndTest( &uiEngine,  SIGNAL(seekingStarted(int)),
    40     connectAndTest( &uiEngine,  SIGNAL(seekingStarted(int)),
    41                     this,       SLOT(resetCurrentSong()) );
    41                     this,       SLOT(resetCurrentSong()) );
       
    42 
       
    43     Q_D( RadioHistoryModel );
       
    44     d->connectToDatabase();
    42 }
    45 }
    43 
    46 
    44 /*!
    47 /*!
    45  *
    48  *
    46  */
    49  */
    47 RadioHistoryModel::~RadioHistoryModel()
    50 RadioHistoryModel::~RadioHistoryModel()
    48 {
    51 {
    49     Q_D( RadioHistoryModel );
    52     Q_D( RadioHistoryModel );
    50     d->mItems.clear();
       
    51     delete d_ptr;
    53     delete d_ptr;
    52 }
    54 }
    53 
    55 
    54 /*!
    56 /*!
    55  * \reimp
    57  * \reimp
    56  */
    58  */
    57 Qt::ItemFlags RadioHistoryModel::flags ( const QModelIndex& index ) const
       
    58 {
       
    59     Qt::ItemFlags flags = QAbstractListModel::flags( index );
       
    60     flags |= Qt::ItemIsEditable;
       
    61     return flags;
       
    62 }
       
    63 
       
    64 /*!
       
    65  * \reimp
       
    66  */
       
    67 int RadioHistoryModel::rowCount( const QModelIndex& parent ) const
    59 int RadioHistoryModel::rowCount( const QModelIndex& parent ) const
    68 {
    60 {
    69     Q_UNUSED( parent );
    61     Q_UNUSED( parent );
    70     Q_D( const RadioHistoryModel );
    62     Q_D( const RadioHistoryModel );
    71     return d->mItems.count();
    63     return d->rowCount();
    72 }
    64 }
    73 
    65 
    74 /*!
    66 /*!
    75  * \reimp
    67  * \reimp
    76  */
    68  */
    79     if ( !index.isValid() ) {
    71     if ( !index.isValid() ) {
    80         return QVariant();
    72         return QVariant();
    81     }
    73     }
    82 
    74 
    83     Q_D( const RadioHistoryModel );
    75     Q_D( const RadioHistoryModel );
    84     if ( role == Qt::DisplayRole ) {
    76     return d->data( index.row(), role );
    85         RadioHistoryItem item = d->mItems.at( index.row() );
       
    86 
       
    87         QStringList list;
       
    88         if ( d->mShowDetails ) {
       
    89             list.append( item.artist() + " - " + item.title() );
       
    90             list.append( item.time() + " " + item.station() + " " /*+ RadioUiEngine::parseFrequency( item.frequency() ) */ );
       
    91         } else {
       
    92             list.append( item.artist() );
       
    93             list.append( item.title() );
       
    94         }
       
    95 
       
    96         return list;
       
    97     }
       
    98 
       
    99     return QVariant();
       
   100 }
       
   101 
       
   102 /*!
       
   103  * \reimp
       
   104  */
       
   105 bool RadioHistoryModel::setData( const QModelIndex& index, const QVariant& value, int role )
       
   106 {
       
   107     Q_UNUSED( value );
       
   108     if ( !index.isValid() ) {
       
   109         return false;
       
   110     }
       
   111 
       
   112     if ( role == RadioHistoryModel::SetFavoriteRole ) {
       
   113         Q_D( RadioHistoryModel );
       
   114         RadioHistoryItem item = d->mItems.at( index.row() );
       
   115         item.setFavorite();
       
   116         updateItem( index.row(), item );
       
   117         return true;
       
   118     }
       
   119 
       
   120     return false;
       
   121 }
    77 }
   122 
    78 
   123 /*!
    79 /*!
   124  * Public slot
    80  * Public slot
   125  */
    81  */
   131 }
    87 }
   132 
    88 
   133 /*!
    89 /*!
   134  * Public slot
    90  * Public slot
   135  */
    91  */
   136 void RadioHistoryModel::setFavorite()
       
   137 {
       
   138     Q_D( RadioHistoryModel );
       
   139     RadioHistoryItem item = d->mItems.first();
       
   140     item.setFavorite();
       
   141     updateItem( 0, item );
       
   142 }
       
   143 
       
   144 /*!
       
   145  * Public slot
       
   146  */
       
   147 void RadioHistoryModel::removeAll()
    92 void RadioHistoryModel::removeAll()
   148 {
    93 {
   149     Q_D( RadioHistoryModel );
    94     Q_D( RadioHistoryModel );
   150 
    95     d->removeAll();
   151     beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
    96 }
   152 
    97 
   153     d->mItems.clear();
    98 /*!
   154 
    99  * Sets the icons to be used in the list
   155     endRemoveRows();
   100  */
       
   101 void RadioHistoryModel::setIcons( const QIcon& nonTaggedIcon, const QIcon& taggedIcon )
       
   102 {
       
   103     Q_D( RadioHistoryModel );
       
   104     d->mNonTaggedIcon = nonTaggedIcon;
       
   105     d->mTaggedIcon = taggedIcon;
   156 }
   106 }
   157 
   107 
   158 /*!
   108 /*!
   159  *
   109  *
   160  */
   110  */
   175 }
   125 }
   176 
   126 
   177 /*!
   127 /*!
   178  *
   128  *
   179  */
   129  */
       
   130 void RadioHistoryModel::setShowTagged( bool showTagged )
       
   131 {
       
   132     Q_D( RadioHistoryModel );
       
   133     d->setViewMode( showTagged ? RadioHistoryModelPrivate::ShowTagged : RadioHistoryModelPrivate::ShowAll );
       
   134 }
       
   135 
       
   136 /*!
       
   137  *
       
   138  */
       
   139 void RadioHistoryModel::toggleTagging( const RadioHistoryItem& item, const int row )
       
   140 {
       
   141     Q_D( RadioHistoryModel );
       
   142     d->toggleTagging( item, row );
       
   143 }
       
   144 
       
   145 /*!
       
   146  *
       
   147  */
       
   148 RadioHistoryItem RadioHistoryModel::itemAtIndex( const QModelIndex& index ) const
       
   149 {
       
   150     Q_D( const RadioHistoryModel );
       
   151     return d->itemAtIndex( index );
       
   152 }
       
   153 
       
   154 /*!
       
   155  *
       
   156  */
   180 void RadioHistoryModel::addItem( const QString& artist, const QString& title, const RadioStation& station )
   157 void RadioHistoryModel::addItem( const QString& artist, const QString& title, const RadioStation& station )
   181 {
   158 {
   182     Q_D( RadioHistoryModel );
   159     Q_D( RadioHistoryModel );
   183 
   160     d->addItem( artist, title, station );
   184     RadioHistoryItem item;
       
   185     const int itemIndex = findItem( artist, title, item );
       
   186     if ( itemIndex != -1 ) {
       
   187         item.increasePlayCount();
       
   188         updateItem( itemIndex, item, true );
       
   189     } else {
       
   190         item.setArtist( artist );
       
   191         item.setTitle( title );
       
   192         item.setStation( station.name() );
       
   193         item.setFrequency( station.frequency() );
       
   194         item.setCurrentTime();
       
   195 
       
   196         beginInsertRows( QModelIndex(), 0, 0 );
       
   197 
       
   198         d->mItems.prepend( item );
       
   199 
       
   200         endInsertRows();
       
   201     }
       
   202 
       
   203     d->mTopItemIsPlaying = true;
       
   204     emit itemAdded();
       
   205 }
   161 }
   206 
   162 
   207 /*!
   163 /*!
   208  *
   164  *
   209  */
   165  */
   217 /*!
   173 /*!
   218  *
   174  *
   219  */
   175  */
   220 void RadioHistoryModel::addRadioTextPlus( int rtClass, const QString& rtItem, const RadioStation& station )
   176 void RadioHistoryModel::addRadioTextPlus( int rtClass, const QString& rtItem, const RadioStation& station )
   221 {
   177 {
   222     if ( rtClass == RtPlus::Artist || rtClass == RtPlus::Title ) {
   178     if ( rtClass == RtPlus::Dummy || rtClass == RtPlus::Artist || rtClass == RtPlus::Title ) {
   223         Q_D( RadioHistoryModel );
   179         Q_D( RadioHistoryModel );
   224         if ( d->mRtItemHolder.isEmpty() ) {
   180         if ( d->mRtItemClass == -1 ) {
       
   181             d->mRtItemClass = rtClass;
   225             d->mRtItemHolder = rtItem;
   182             d->mRtItemHolder = rtItem;
   226         } else {
   183         } else {
   227             if ( rtClass == RtPlus::Title ) {
   184             // Received: Artist - Title
       
   185             if ( d->mRtItemClass == RtPlus::Artist && rtClass == RtPlus::Title ) {
   228                 addItem( d->mRtItemHolder, rtItem, station );
   186                 addItem( d->mRtItemHolder, rtItem, station );
   229             } else {
   187 
       
   188             // Received: Title - Artist
       
   189             } else if ( rtClass == RtPlus::Artist && d->mRtItemClass == RtPlus::Title ) {
   230                 addItem( rtItem, d->mRtItemHolder, station );
   190                 addItem( rtItem, d->mRtItemHolder, station );
       
   191 
       
   192             // Received: Dummy - Title
       
   193             } else if ( d->mRtItemClass == RtPlus::Dummy && rtClass == RtPlus::Title ) {
       
   194                 addItem( "", rtItem, station );
       
   195 
       
   196             // Received: Title - Dummy
       
   197             } else if ( rtClass == RtPlus::Dummy && d->mRtItemClass == RtPlus::Title ) {
       
   198                 addItem( "", d->mRtItemHolder, station );
   231             }
   199             }
       
   200 
   232             d->mRtItemHolder = "";
   201             d->mRtItemHolder = "";
       
   202             d->mRtItemClass = -1;
   233         }
   203         }
   234     }
   204     }
   235 }
   205 }
   236 
   206 
   237 /*!
   207 /*!
   238  *
   208  *
   239  */
   209  */
   240 int RadioHistoryModel::findItem( const QString& artist, const QString& title, RadioHistoryItem& item )
   210 void RadioHistoryModel::reportChangedData( int start, int end )
   241 {
   211 {
   242     Q_D( RadioHistoryModel );
   212     if ( end == -1 ) {
   243     const int itemCount = d->mItems.count();
   213         end = start;
   244     for ( int i = 0; i < itemCount; ++i ) {
       
   245         RadioHistoryItem existingItem = d->mItems.at( i );
       
   246         if ( existingItem.artist().compare( artist ) == 0
       
   247              && existingItem.title().compare( title ) == 0 ) {
       
   248             item = existingItem;
       
   249             return i;
       
   250         }
       
   251     }
   214     }
   252 
   215     const QModelIndex startIndex = index( start, 0, QModelIndex() );
   253     return -1;
   216     const QModelIndex endIndex = index( end, 0, QModelIndex() );
   254 }
   217     emit dataChanged( startIndex, endIndex );
   255 
   218 }
   256 /*!
   219 
   257  *
   220 /*!
   258  */
   221  *
   259 void RadioHistoryModel::updateItem( int index, const RadioHistoryItem& item, bool prepend )
   222  */
   260 {
   223 void RadioHistoryModel::emitItemAdded()
   261     Q_D( RadioHistoryModel );
   224 {
   262     d->mItems.removeAt( index );
   225     emit itemAdded();
   263 
   226 }
   264     if ( prepend ) {
       
   265         d->mItems.prepend( item );
       
   266     } else {
       
   267         d->mItems.insert( index, item );
       
   268     }
       
   269 }