16 */ |
16 */ |
17 |
17 |
18 // System includes |
18 // System includes |
19 #include <QStringList> |
19 #include <QStringList> |
20 |
20 |
|
21 // User includes |
21 #include "radiostationmodel.h" |
22 #include "radiostationmodel.h" |
22 #include "radiostationmodel_p.h" |
23 #include "radiostationmodel_p.h" |
23 #include "radiopresetstorage.h" |
24 #include "radiopresetstorage.h" |
24 #include "radioenginewrapper.h" |
25 #include "radioenginewrapper.h" |
25 #include "radiouiengine.h" |
26 #include "radiouiengine.h" |
139 */ |
144 */ |
140 bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role ) |
145 bool RadioStationModel::setData( const QModelIndex& index, const QVariant& value, int role ) |
141 { |
146 { |
142 Q_UNUSED( index ); |
147 Q_UNUSED( index ); |
143 |
148 |
144 if ( role == RadioStationModel::ToggleFavoriteRole ) { |
149 if ( role == RadioRole::ToggleFavoriteRole ) { |
145 const uint frequency = value.toUInt(); |
150 const uint frequency = value.toUInt(); |
146 RadioStation station; |
151 RadioStation station; |
147 if ( findFrequency( frequency, station ) ) { |
152 if ( findFrequency( frequency, station ) ) { |
148 setFavoriteByPreset( station.presetIndex(), !station.isFavorite() ); |
153 setFavoriteByPreset( station.presetIndex(), !station.isFavorite() ); |
149 } else { |
154 } else { |
162 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper ) |
167 void RadioStationModel::initialize( RadioPresetStorage* storage, RadioEngineWrapper* wrapper ) |
163 { |
168 { |
164 Q_D( RadioStationModel ); |
169 Q_D( RadioStationModel ); |
165 d->mPresetStorage = storage; |
170 d->mPresetStorage = storage; |
166 d->mWrapper = wrapper; |
171 d->mWrapper = wrapper; |
167 const int presetCount = d->mPresetStorage->presetCount(); |
172 |
168 int index = d->mPresetStorage->firstPreset(); |
173 int index = d->mPresetStorage->firstPreset(); |
169 LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d", presetCount, index ); |
174 LOG_FORMAT( "RadioStationModelPrivate::initialize: presetCount: %d, firstIndex: %d", |
170 |
175 d->mPresetStorage->presetCount(), index ); |
171 #ifdef COMPILE_WITH_NEW_PRESET_UTILITY |
176 |
172 while ( index >= 0 ) { |
177 while ( index >= 0 ) { |
173 #else |
|
174 index = 0; |
|
175 while ( index < presetCount ) { |
|
176 #endif // COMPILE_WITH_NEW_PRESET_UTILITY |
|
177 |
|
178 RadioStation station; |
178 RadioStation station; |
179 station.detach(); |
179 station.detach(); |
180 |
180 |
181 RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() ); |
181 RadioStationIf* preset = static_cast<RadioStationIf*>( station.data_ptr() ); |
182 if ( d->mPresetStorage->readPreset( index, *preset ) ) { |
182 if ( d->mPresetStorage->readPreset( index, *preset ) ) { |
231 RadioStation RadioStationModel::stationAt( int index ) const |
229 RadioStation RadioStationModel::stationAt( int index ) const |
232 { |
230 { |
233 // Get the value from the keys list instead of directly accessing the values list |
231 // Get the value from the keys list instead of directly accessing the values list |
234 // because QMap may have added a default-constructed value to the values list |
232 // because QMap may have added a default-constructed value to the values list |
235 Q_D( const RadioStationModel ); |
233 Q_D( const RadioStationModel ); |
236 if ( index < d->mStations.keys().count() ) { |
234 if ( index >= 0 && index < d->mStations.keys().count() ) { |
237 uint frequency = d->mStations.keys().at( index ); |
235 uint frequency = d->mStations.keys().at( index ); |
238 return d->mStations.value( frequency ); |
236 return d->mStations.value( frequency ); |
239 } |
237 } |
240 return RadioStation(); |
238 return RadioStation(); |
241 } |
239 } |
242 |
240 |
243 /*! |
241 /*! |
244 * Finds a station by frequency |
242 * Finds a station by frequency |
245 */ |
243 */ |
246 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station ) |
244 bool RadioStationModel::findFrequency( uint frequency, RadioStation& station ) const |
247 { |
245 { |
248 Q_D( RadioStationModel ); |
246 Q_D( const RadioStationModel ); |
249 if ( d->mStations.contains( frequency ) ) { |
247 if ( d->mStations.contains( frequency ) ) { |
250 station = d->mStations.value( frequency ); |
248 station = d->mStations.value( frequency ); |
251 return true; |
249 return true; |
252 } |
250 } |
253 return false; |
251 return false; |
290 { |
288 { |
291 Q_D( RadioStationModel ); |
289 Q_D( RadioStationModel ); |
292 const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite; |
290 const bool findFavorite = mode == StationSkip::PreviousFavorite || mode == StationSkip::NextFavorite; |
293 const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite; |
291 const bool findNext = mode == StationSkip::Next || mode == StationSkip::NextFavorite; |
294 QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values(); |
292 QList<RadioStation> list = findFavorite ? d->favorites() : d->mStations.values(); |
|
293 |
|
294 if ( list.isEmpty() ) { |
|
295 return RadioStation(); |
|
296 } |
295 |
297 |
296 // Find the previous and next station from current frequency |
298 // Find the previous and next station from current frequency |
297 RadioStation previous; |
299 RadioStation previous; |
298 RadioStation next; |
300 RadioStation next; |
299 foreach( const RadioStation& station, list ) { |
301 foreach( const RadioStation& station, list ) { |
318 |
320 |
319 return findNext ? next : previous; |
321 return findNext ? next : previous; |
320 } |
322 } |
321 |
323 |
322 /*! |
324 /*! |
|
325 * Checks if the model contains the given frequency |
|
326 */ |
|
327 bool RadioStationModel::contains( const uint frequency ) const |
|
328 { |
|
329 RadioStation unused; |
|
330 return findFrequency( frequency, unused ); |
|
331 } |
|
332 |
|
333 /*! |
323 * Removes a station by frequency |
334 * Removes a station by frequency |
324 */ |
335 */ |
325 void RadioStationModel::removeByFrequency( uint frequency ) |
336 void RadioStationModel::removeByFrequency( uint frequency ) |
326 { |
337 { |
327 RadioStation station; |
338 RadioStation station; |
359 } |
370 } |
360 |
371 |
361 // Copy the station to a temporary variable that can be used as signal parameter |
372 // Copy the station to a temporary variable that can be used as signal parameter |
362 RadioStation tempStation = station; |
373 RadioStation tempStation = station; |
363 |
374 |
364 const int row = modelIndexFromFrequency( tempStation.frequency() ).row(); |
375 const int row = indexFromFrequency( tempStation.frequency() ); |
365 beginRemoveRows( QModelIndex(), row, row ); |
376 beginRemoveRows( QModelIndex(), row, row ); |
366 |
377 |
367 d->mPresetStorage->deletePreset( tempStation.presetIndex() ); |
378 d->mPresetStorage->deletePreset( tempStation.presetIndex() ); |
368 d->mStations.remove( frequency ); |
379 d->mStations.remove( frequency ); |
369 |
380 |
613 } |
622 } |
614 |
623 |
615 /*! |
624 /*! |
616 * Returns the model index corresponding to the given frequency |
625 * Returns the model index corresponding to the given frequency |
617 */ |
626 */ |
618 QModelIndex RadioStationModel::modelIndexFromFrequency( uint frequency ) |
627 int RadioStationModel::indexFromFrequency( uint frequency ) |
619 { |
628 { |
620 RadioStation station; |
629 RadioStation station; |
621 if ( findFrequency( frequency, station ) ) { |
630 if ( findFrequency( frequency, station ) ) { |
622 return index( findPresetIndex( station.presetIndex() ), 0 ); |
631 return findPresetIndex( station.presetIndex() ); |
623 } |
632 } |
624 return QModelIndex(); |
633 return -1; |
625 } |
634 } |
626 |
635 |
627 /*! |
636 /*! |
628 * Private slot |
637 * Private slot |
629 * Timer timeout slot to indicate that the dynamic PS check has ended |
638 * Timer timeout slot to indicate that the dynamic PS check has ended |