Changeset View
Changeset View
Standalone View
Standalone View
src/models/datamodel.cpp
Show All 28 Lines | |||||
29 | #include <algorithm> | 29 | #include <algorithm> | ||
30 | 30 | | |||
31 | class DataModelPrivate | 31 | class DataModelPrivate | ||
32 | { | 32 | { | ||
33 | public: | 33 | public: | ||
34 | 34 | | |||
35 | DataModel::ListTrackDataType mAllTrackData; | 35 | DataModel::ListTrackDataType mAllTrackData; | ||
36 | 36 | | |||
37 | DataModel::ListRadioDataType mAllRadiosData; | ||||
mgallien: It would better (safer) if you add one more type like DataModel::ListRadioDataType. Online… | |||||
38 | | ||||
37 | DataModel::ListAlbumDataType mAllAlbumData; | 39 | DataModel::ListAlbumDataType mAllAlbumData; | ||
38 | 40 | | |||
39 | DataModel::ListArtistDataType mAllArtistData; | 41 | DataModel::ListArtistDataType mAllArtistData; | ||
40 | 42 | | |||
41 | DataModel::ListGenreDataType mAllGenreData; | 43 | DataModel::ListGenreDataType mAllGenreData; | ||
42 | 44 | | |||
43 | ModelDataLoader mDataLoader; | 45 | ModelDataLoader mDataLoader; | ||
44 | 46 | | |||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Line(s) | |||||
118 | QVariant DataModel::data(const QModelIndex &index, int role) const | 120 | QVariant DataModel::data(const QModelIndex &index, int role) const | ||
119 | { | 121 | { | ||
120 | auto result = QVariant(); | 122 | auto result = QVariant(); | ||
121 | 123 | | |||
122 | if (!index.isValid()) { | 124 | if (!index.isValid()) { | ||
123 | return result; | 125 | return result; | ||
124 | } | 126 | } | ||
125 | 127 | | |||
126 | const auto dataCount = d->mAllTrackData.size() + d->mAllAlbumData.size() + d->mAllArtistData.size() + d->mAllGenreData.size(); | 128 | const auto dataCount = d->mModelType == ElisaUtils::Radio ? d->mAllRadiosData.size() : d->mAllTrackData.size() + d->mAllAlbumData.size() + d->mAllArtistData.size() + d->mAllGenreData.size(); | ||
127 | 129 | | |||
128 | Q_ASSERT(index.isValid()); | 130 | Q_ASSERT(index.isValid()); | ||
129 | Q_ASSERT(index.column() == 0); | 131 | Q_ASSERT(index.column() == 0); | ||
130 | Q_ASSERT(index.row() >= 0 && index.row() < dataCount); | | |||
131 | Q_ASSERT(!index.parent().isValid()); | 132 | Q_ASSERT(!index.parent().isValid()); | ||
132 | Q_ASSERT(index.model() == this); | 133 | Q_ASSERT(index.model() == this); | ||
133 | Q_ASSERT(index.internalId() == 0); | 134 | Q_ASSERT(index.internalId() == 0); | ||
135 | Q_ASSERT(index.row() >= 0 && index.row() < dataCount); | ||||
134 | 136 | | |||
135 | switch(role) | 137 | switch(role) | ||
136 | { | 138 | { | ||
137 | case Qt::DisplayRole: | 139 | case Qt::DisplayRole: | ||
138 | switch(d->mModelType) | 140 | switch(d->mModelType) | ||
mgallien: What is the reason for that ?
It does not look good. | |||||
The dataCount did not make sense for radios, I reintegrated the check and added a different dataCount on line 128. jguidon: The dataCount did not make sense for radios, I reintegrated the check and added a different… | |||||
139 | { | 141 | { | ||
140 | case ElisaUtils::Track: | 142 | case ElisaUtils::Track: | ||
141 | result = d->mAllTrackData[index.row()][TrackDataType::key_type::TitleRole]; | 143 | result = d->mAllTrackData[index.row()][TrackDataType::key_type::TitleRole]; | ||
142 | break; | 144 | break; | ||
143 | case ElisaUtils::Album: | 145 | case ElisaUtils::Album: | ||
144 | result = d->mAllAlbumData[index.row()][AlbumDataType::key_type::TitleRole]; | 146 | result = d->mAllAlbumData[index.row()][AlbumDataType::key_type::TitleRole]; | ||
145 | break; | 147 | break; | ||
146 | case ElisaUtils::Artist: | 148 | case ElisaUtils::Artist: | ||
147 | result = d->mAllArtistData[index.row()][ArtistDataType::key_type::TitleRole]; | 149 | result = d->mAllArtistData[index.row()][ArtistDataType::key_type::TitleRole]; | ||
148 | break; | 150 | break; | ||
149 | case ElisaUtils::Genre: | 151 | case ElisaUtils::Genre: | ||
150 | result = d->mAllGenreData[index.row()][GenreDataType::key_type::TitleRole]; | 152 | result = d->mAllGenreData[index.row()][GenreDataType::key_type::TitleRole]; | ||
151 | break; | 153 | break; | ||
154 | case ElisaUtils::Radio: | ||||
155 | result = d->mAllRadiosData[index.row()][GenreDataType::key_type::TitleRole]; | ||||
156 | break; | ||||
152 | case ElisaUtils::Lyricist: | 157 | case ElisaUtils::Lyricist: | ||
153 | case ElisaUtils::Composer: | 158 | case ElisaUtils::Composer: | ||
154 | case ElisaUtils::FileName: | 159 | case ElisaUtils::FileName: | ||
155 | case ElisaUtils::Unknown: | 160 | case ElisaUtils::Unknown: | ||
156 | break; | 161 | break; | ||
157 | } | 162 | } | ||
158 | break; | 163 | break; | ||
159 | case DatabaseInterface::ColumnsRoles::DurationRole: | 164 | case DatabaseInterface::ColumnsRoles::DurationRole: | ||
160 | { | 165 | { | ||
161 | if (d->mModelType == ElisaUtils::Track) { | 166 | switch (d->mModelType) | ||
167 | { | ||||
168 | case ElisaUtils::Track: | ||||
169 | { | ||||
162 | auto trackDuration = d->mAllTrackData[index.row()][TrackDataType::key_type::DurationRole].toTime(); | 170 | auto trackDuration = d->mAllTrackData[index.row()][TrackDataType::key_type::DurationRole].toTime(); | ||
163 | if (trackDuration.hour() == 0) { | 171 | if (trackDuration.hour() == 0) { | ||
164 | result = trackDuration.toString(QStringLiteral("mm:ss")); | 172 | result = trackDuration.toString(QStringLiteral("mm:ss")); | ||
165 | } else { | 173 | } else { | ||
166 | result = trackDuration.toString(); | 174 | result = trackDuration.toString(); | ||
167 | } | 175 | } | ||
176 | break; | ||||
177 | } | ||||
178 | case ElisaUtils::Album: | ||||
179 | case ElisaUtils::Artist: | ||||
astippich: use QString instead of QStringLiteral(""), it is a little bit cheaper | |||||
No need for that. If the data is missing, the view should handle that (i.e. an empty/invalid QVariant). This is the same in most places where fallback is an empty/invalid QVariant when the data is missing or unavailable. mgallien: No need for that. If the data is missing, the view should handle that (i.e. an empty/invalid… | |||||
180 | case ElisaUtils::Genre: | ||||
181 | case ElisaUtils::Lyricist: | ||||
182 | case ElisaUtils::Composer: | ||||
183 | case ElisaUtils::FileName: | ||||
184 | case ElisaUtils::Radio: | ||||
185 | case ElisaUtils::Unknown: | ||||
186 | break; | ||||
187 | } | ||||
188 | break; | ||||
189 | } | ||||
190 | case DatabaseInterface::ColumnsRoles::IsSingleDiscAlbumRole: | ||||
191 | { | ||||
192 | switch (d->mModelType) | ||||
193 | { | ||||
194 | case ElisaUtils::Track: | ||||
195 | case ElisaUtils::Radio: | ||||
196 | result = false; | ||||
197 | break; | ||||
198 | case ElisaUtils::Album: | ||||
199 | case ElisaUtils::Artist: | ||||
200 | case ElisaUtils::Genre: | ||||
201 | case ElisaUtils::Lyricist: | ||||
202 | case ElisaUtils::Composer: | ||||
203 | case ElisaUtils::FileName: | ||||
204 | case ElisaUtils::Unknown: | ||||
205 | break; | ||||
168 | } | 206 | } | ||
169 | break; | 207 | break; | ||
170 | } | 208 | } | ||
171 | default: | 209 | default: | ||
172 | switch(d->mModelType) | 210 | switch(d->mModelType) | ||
173 | { | 211 | { | ||
174 | case ElisaUtils::Track: | 212 | case ElisaUtils::Track: | ||
175 | result = d->mAllTrackData[index.row()][static_cast<TrackDataType::key_type>(role)]; | 213 | result = d->mAllTrackData[index.row()][static_cast<TrackDataType::key_type>(role)]; | ||
176 | break; | 214 | break; | ||
177 | case ElisaUtils::Album: | 215 | case ElisaUtils::Album: | ||
178 | result = d->mAllAlbumData[index.row()][static_cast<AlbumDataType::key_type>(role)]; | 216 | result = d->mAllAlbumData[index.row()][static_cast<AlbumDataType::key_type>(role)]; | ||
179 | break; | 217 | break; | ||
180 | case ElisaUtils::Artist: | 218 | case ElisaUtils::Artist: | ||
181 | result = d->mAllArtistData[index.row()][static_cast<ArtistDataType::key_type>(role)]; | 219 | result = d->mAllArtistData[index.row()][static_cast<ArtistDataType::key_type>(role)]; | ||
182 | break; | 220 | break; | ||
183 | case ElisaUtils::Genre: | 221 | case ElisaUtils::Genre: | ||
184 | result = d->mAllGenreData[index.row()][static_cast<GenreDataType::key_type>(role)]; | 222 | result = d->mAllGenreData[index.row()][static_cast<GenreDataType::key_type>(role)]; | ||
185 | break; | 223 | break; | ||
224 | case ElisaUtils::Radio: | ||||
225 | result = d->mAllRadiosData[index.row()][static_cast<TrackDataType::key_type>(role)]; | ||||
226 | break; | ||||
186 | case ElisaUtils::Lyricist: | 227 | case ElisaUtils::Lyricist: | ||
187 | case ElisaUtils::Composer: | 228 | case ElisaUtils::Composer: | ||
188 | case ElisaUtils::FileName: | 229 | case ElisaUtils::FileName: | ||
189 | case ElisaUtils::Unknown: | 230 | case ElisaUtils::Unknown: | ||
190 | break; | 231 | break; | ||
191 | } | 232 | } | ||
192 | } | 233 | } | ||
193 | 234 | | |||
▲ Show 20 Lines • Show All 144 Lines • ▼ Show 20 Line(s) | 358 | { | |||
338 | case ElisaUtils::FilterByFrequentlyPlayed: | 379 | case ElisaUtils::FilterByFrequentlyPlayed: | ||
339 | Q_EMIT needFrequentlyPlayedData(d->mModelType); | 380 | Q_EMIT needFrequentlyPlayedData(d->mModelType); | ||
340 | break; | 381 | break; | ||
341 | case ElisaUtils::UnknownFilter: | 382 | case ElisaUtils::UnknownFilter: | ||
342 | break; | 383 | break; | ||
343 | } | 384 | } | ||
344 | } | 385 | } | ||
345 | 386 | | |||
346 | int DataModel::trackIndexFromId(qulonglong id) const | 387 | int DataModel::indexFromId(qulonglong id) const | ||
347 | { | 388 | { | ||
348 | int result; | 389 | int result; | ||
390 | DataModel::ListTrackDataType mAllData = d->mModelType == ElisaUtils::Radio ? d->mAllRadiosData: d->mAllTrackData; | ||||
349 | 391 | | |||
350 | for (result = 0; result < d->mAllTrackData.size(); ++result) { | 392 | for (result = 0; result < mAllData.size(); ++result) { | ||
351 | if (d->mAllTrackData[result].databaseId() == id) { | 393 | if (mAllData[result].databaseId() == id) { | ||
352 | return result; | 394 | return result; | ||
353 | } | 395 | } | ||
354 | } | 396 | } | ||
355 | 397 | | |||
356 | result = -1; | 398 | result = -1; | ||
357 | 399 | | |||
358 | return result; | 400 | return result; | ||
359 | } | 401 | } | ||
360 | 402 | | |||
361 | void DataModel::connectModel(DatabaseInterface *database) | 403 | void DataModel::connectModel(DatabaseInterface *database) | ||
362 | { | 404 | { | ||
363 | d->mDataLoader.setDatabase(database); | 405 | d->mDataLoader.setDatabase(database); | ||
364 | 406 | | |||
365 | connect(&d->mDataLoader, &ModelDataLoader::allTracksData, | 407 | connect(&d->mDataLoader, &ModelDataLoader::allTracksData, | ||
366 | this, &DataModel::tracksAdded); | 408 | this, &DataModel::tracksAdded); | ||
409 | connect(&d->mDataLoader, &ModelDataLoader::allRadiosData, | ||||
410 | this, &DataModel::radiosAdded); | ||||
367 | connect(&d->mDataLoader, &ModelDataLoader::allAlbumsData, | 411 | connect(&d->mDataLoader, &ModelDataLoader::allAlbumsData, | ||
368 | this, &DataModel::albumsAdded); | 412 | this, &DataModel::albumsAdded); | ||
369 | connect(&d->mDataLoader, &ModelDataLoader::allArtistsData, | 413 | connect(&d->mDataLoader, &ModelDataLoader::allArtistsData, | ||
370 | this, &DataModel::artistsAdded); | 414 | this, &DataModel::artistsAdded); | ||
371 | connect(&d->mDataLoader, &ModelDataLoader::allGenresData, | 415 | connect(&d->mDataLoader, &ModelDataLoader::allGenresData, | ||
372 | this, &DataModel::genresAdded); | 416 | this, &DataModel::genresAdded); | ||
373 | connect(&d->mDataLoader, &ModelDataLoader::genresAdded, | 417 | connect(&d->mDataLoader, &ModelDataLoader::genresAdded, | ||
This is too much specific to one usecase of DataModel. mgallien: This is too much specific to one usecase of DataModel.
Why do you need this when other views do… | |||||
I just copied trackIndexFromId, in the first case we get mAllTrackData[result] and mAllRadiosData[result] in the other. I will integrate both methods. jguidon: I just copied trackIndexFromId, in the first case we get mAllTrackData[result] and… | |||||
374 | this, &DataModel::genresAdded); | 418 | this, &DataModel::genresAdded); | ||
375 | connect(&d->mDataLoader, &ModelDataLoader::albumsAdded, | 419 | connect(&d->mDataLoader, &ModelDataLoader::albumsAdded, | ||
376 | this, &DataModel::albumsAdded); | 420 | this, &DataModel::albumsAdded); | ||
377 | connect(&d->mDataLoader, &ModelDataLoader::albumModified, | 421 | connect(&d->mDataLoader, &ModelDataLoader::albumModified, | ||
378 | this, &DataModel::albumModified); | 422 | this, &DataModel::albumModified); | ||
379 | connect(&d->mDataLoader, &ModelDataLoader::albumRemoved, | 423 | connect(&d->mDataLoader, &ModelDataLoader::albumRemoved, | ||
380 | this, &DataModel::albumRemoved); | 424 | this, &DataModel::albumRemoved); | ||
381 | connect(&d->mDataLoader, &ModelDataLoader::tracksAdded, | 425 | connect(&d->mDataLoader, &ModelDataLoader::tracksAdded, | ||
382 | this, &DataModel::tracksAdded); | 426 | this, &DataModel::tracksAdded); | ||
383 | connect(&d->mDataLoader, &ModelDataLoader::trackModified, | 427 | connect(&d->mDataLoader, &ModelDataLoader::trackModified, | ||
384 | this, &DataModel::trackModified); | 428 | this, &DataModel::trackModified); | ||
385 | connect(&d->mDataLoader, &ModelDataLoader::trackRemoved, | 429 | connect(&d->mDataLoader, &ModelDataLoader::trackRemoved, | ||
386 | this, &DataModel::trackRemoved); | 430 | this, &DataModel::trackRemoved); | ||
387 | connect(&d->mDataLoader, &ModelDataLoader::artistsAdded, | 431 | connect(&d->mDataLoader, &ModelDataLoader::artistsAdded, | ||
388 | this, &DataModel::artistsAdded); | 432 | this, &DataModel::artistsAdded); | ||
389 | connect(&d->mDataLoader, &ModelDataLoader::artistRemoved, | 433 | connect(&d->mDataLoader, &ModelDataLoader::artistRemoved, | ||
390 | this, &DataModel::artistRemoved); | 434 | this, &DataModel::artistRemoved); | ||
435 | connect(&d->mDataLoader, &ModelDataLoader::radioAdded, | ||||
436 | this, &DataModel::radioAdded); | ||||
437 | connect(&d->mDataLoader, &ModelDataLoader::radioModified, | ||||
438 | this, &DataModel::radioModified); | ||||
439 | connect(&d->mDataLoader, &ModelDataLoader::radioRemoved, | ||||
440 | this, &DataModel::radioRemoved); | ||||
391 | } | 441 | } | ||
392 | 442 | | |||
393 | void DataModel::tracksAdded(ListTrackDataType newData) | 443 | void DataModel::tracksAdded(ListTrackDataType newData) | ||
394 | { | 444 | { | ||
395 | if (newData.isEmpty() && d->mModelType == ElisaUtils::Track) { | 445 | if (newData.isEmpty() && d->mModelType == ElisaUtils::Track) { | ||
396 | setBusy(false); | 446 | setBusy(false); | ||
397 | } | 447 | } | ||
398 | 448 | | |||
399 | if (newData.isEmpty() || d->mModelType != ElisaUtils::Track) { | 449 | if (newData.isEmpty() || d->mModelType != ElisaUtils::Track) { | ||
400 | return; | 450 | return; | ||
401 | } | 451 | } | ||
402 | 452 | | |||
403 | if (d->mFilterType == ElisaUtils::FilterById && !d->mAllTrackData.isEmpty()) { | 453 | if (d->mFilterType == ElisaUtils::FilterById && !d->mAllTrackData.isEmpty()) { | ||
404 | for (const auto &newTrack : newData) { | 454 | for (const auto &newTrack : newData) { | ||
405 | auto trackIndex = trackIndexFromId(newTrack.databaseId()); | 455 | auto trackIndex = indexFromId(newTrack.databaseId()); | ||
406 | 456 | | |||
407 | if (trackIndex != -1) { | 457 | if (trackIndex != -1) { | ||
408 | continue; | 458 | continue; | ||
409 | } | 459 | } | ||
410 | 460 | | |||
411 | bool trackInserted = false; | 461 | bool trackInserted = false; | ||
412 | for (int trackIndex = 0; trackIndex < d->mAllTrackData.count(); ++trackIndex) { | 462 | for (int trackIndex = 0; trackIndex < d->mAllTrackData.count(); ++trackIndex) { | ||
413 | const auto &oneTrack = d->mAllTrackData[trackIndex]; | 463 | const auto &oneTrack = d->mAllTrackData[trackIndex]; | ||
Show All 32 Lines | 489 | } else { | |||
446 | } else { | 496 | } else { | ||
447 | beginInsertRows({}, d->mAllTrackData.size(), d->mAllTrackData.size() + newData.size() - 1); | 497 | beginInsertRows({}, d->mAllTrackData.size(), d->mAllTrackData.size() + newData.size() - 1); | ||
448 | d->mAllTrackData.append(newData); | 498 | d->mAllTrackData.append(newData); | ||
449 | endInsertRows(); | 499 | endInsertRows(); | ||
450 | } | 500 | } | ||
451 | } | 501 | } | ||
452 | } | 502 | } | ||
453 | 503 | | |||
504 | void DataModel::radiosAdded(ListRadioDataType newData) | ||||
505 | { | ||||
506 | if (newData.isEmpty() && d->mModelType == ElisaUtils::Radio) { | ||||
507 | setBusy(false); | ||||
508 | } | ||||
509 | | ||||
510 | if (newData.isEmpty() || d->mModelType != ElisaUtils::Radio) { | ||||
511 | return; | ||||
512 | } | ||||
513 | | ||||
514 | if (d->mFilterType == ElisaUtils::FilterById && !d->mAllRadiosData.isEmpty()) { | ||||
515 | for (const auto &newTrack : newData) { | ||||
516 | auto trackIndex = indexFromId(newTrack.databaseId()); | ||||
517 | | ||||
518 | if (trackIndex != -1) { | ||||
519 | continue; | ||||
520 | } | ||||
521 | | ||||
522 | bool trackInserted = false; | ||||
523 | for (int trackIndex = 0; trackIndex < d->mAllRadiosData.count(); ++trackIndex) { | ||||
524 | const auto &oneTrack = d->mAllRadiosData[trackIndex]; | ||||
525 | | ||||
526 | if (oneTrack.trackNumber() > newTrack.trackNumber()) { | ||||
527 | beginInsertRows({}, trackIndex, trackIndex); | ||||
528 | d->mAllRadiosData.insert(trackIndex, newTrack); | ||||
529 | endInsertRows(); | ||||
530 | | ||||
531 | if (d->mAllRadiosData.size() == 1) { | ||||
532 | setBusy(false); | ||||
533 | } | ||||
534 | | ||||
535 | trackInserted = true; | ||||
536 | break; | ||||
537 | } | ||||
538 | } | ||||
539 | | ||||
540 | if (!trackInserted) { | ||||
541 | beginInsertRows({}, d->mAllRadiosData.count(), d->mAllRadiosData.count()); | ||||
542 | d->mAllRadiosData.insert(d->mAllRadiosData.count(), newTrack); | ||||
543 | endInsertRows(); | ||||
544 | | ||||
545 | if (d->mAllRadiosData.size() == 1) { | ||||
546 | setBusy(false); | ||||
547 | } | ||||
548 | } | ||||
549 | } | ||||
550 | } else { | ||||
551 | if (d->mAllRadiosData.isEmpty()) { | ||||
552 | beginInsertRows({}, 0, newData.size() - 1); | ||||
553 | d->mAllRadiosData.swap(newData); | ||||
554 | endInsertRows(); | ||||
555 | | ||||
556 | setBusy(false); | ||||
557 | } else { | ||||
558 | beginInsertRows({}, d->mAllRadiosData.size(), d->mAllRadiosData.size() + newData.size() - 1); | ||||
559 | d->mAllRadiosData.append(newData); | ||||
560 | endInsertRows(); | ||||
561 | } | ||||
562 | } | ||||
563 | } | ||||
564 | | ||||
454 | void DataModel::trackModified(const TrackDataType &modifiedTrack) | 565 | void DataModel::trackModified(const TrackDataType &modifiedTrack) | ||
455 | { | 566 | { | ||
456 | if (d->mModelType != ElisaUtils::Track) { | 567 | if (d->mModelType != ElisaUtils::Track) { | ||
457 | return; | 568 | return; | ||
458 | } | 569 | } | ||
459 | 570 | | |||
460 | if (!d->mAlbumTitle.isEmpty() && !d->mAlbumArtist.isEmpty()) { | 571 | if (!d->mAlbumTitle.isEmpty() && !d->mAlbumArtist.isEmpty()) { | ||
461 | if (modifiedTrack.album() != d->mAlbumTitle) { | 572 | if (modifiedTrack.album() != d->mAlbumTitle) { | ||
462 | return; | 573 | return; | ||
463 | } | 574 | } | ||
464 | 575 | | |||
465 | auto trackIndex = trackIndexFromId(modifiedTrack.databaseId()); | 576 | auto trackIndex = indexFromId(modifiedTrack.databaseId()); | ||
466 | 577 | | |||
467 | if (trackIndex == -1) { | 578 | if (trackIndex == -1) { | ||
468 | return; | 579 | return; | ||
469 | } | 580 | } | ||
470 | 581 | | |||
471 | d->mAllTrackData[trackIndex] = modifiedTrack; | 582 | d->mAllTrackData[trackIndex] = modifiedTrack; | ||
472 | Q_EMIT dataChanged(index(trackIndex, 0), index(trackIndex, 0)); | 583 | Q_EMIT dataChanged(index(trackIndex, 0), index(trackIndex, 0)); | ||
473 | } else { | 584 | } else { | ||
Show All 9 Lines | |||||
483 | auto position = itTrack - d->mAllTrackData.begin(); | 594 | auto position = itTrack - d->mAllTrackData.begin(); | ||
484 | 595 | | |||
485 | d->mAllTrackData[position] = modifiedTrack; | 596 | d->mAllTrackData[position] = modifiedTrack; | ||
486 | 597 | | |||
487 | Q_EMIT dataChanged(index(position, 0), index(position, 0)); | 598 | Q_EMIT dataChanged(index(position, 0), index(position, 0)); | ||
488 | } | 599 | } | ||
489 | } | 600 | } | ||
490 | 601 | | |||
602 | void DataModel::radioModified(const TrackDataType &modifiedRadio) | ||||
603 | { | ||||
604 | if (d->mModelType != ElisaUtils::Radio) { | ||||
605 | return; | ||||
606 | } | ||||
607 | | ||||
608 | auto trackIndex = indexFromId(modifiedRadio.databaseId()); | ||||
609 | | ||||
610 | if (trackIndex == -1) { | ||||
611 | return; | ||||
612 | } | ||||
613 | | ||||
614 | d->mAllRadiosData[trackIndex] = modifiedRadio; | ||||
615 | Q_EMIT dataChanged(index(trackIndex, 0), index(trackIndex, 0)); | ||||
616 | } | ||||
617 | | ||||
491 | void DataModel::trackRemoved(qulonglong removedTrackId) | 618 | void DataModel::trackRemoved(qulonglong removedTrackId) | ||
492 | { | 619 | { | ||
493 | if (d->mModelType != ElisaUtils::Track) { | 620 | if (d->mModelType != ElisaUtils::Track) { | ||
494 | return; | 621 | return; | ||
495 | } | 622 | } | ||
496 | 623 | | |||
497 | if (!d->mAlbumTitle.isEmpty() && !d->mAlbumArtist.isEmpty()) { | 624 | if (!d->mAlbumTitle.isEmpty() && !d->mAlbumArtist.isEmpty()) { | ||
498 | auto trackIndex = trackIndexFromId(removedTrackId); | 625 | auto trackIndex = indexFromId(removedTrackId); | ||
499 | 626 | | |||
500 | if (trackIndex == -1) { | 627 | if (trackIndex == -1) { | ||
501 | return; | 628 | return; | ||
502 | } | 629 | } | ||
503 | 630 | | |||
504 | beginRemoveRows({}, trackIndex, trackIndex); | 631 | beginRemoveRows({}, trackIndex, trackIndex); | ||
505 | d->mAllTrackData.removeAt(trackIndex); | 632 | d->mAllTrackData.removeAt(trackIndex); | ||
506 | endRemoveRows(); | 633 | endRemoveRows(); | ||
507 | } else { | 634 | } else { | ||
508 | auto itTrack = std::find_if(d->mAllTrackData.begin(), d->mAllTrackData.end(), | 635 | auto itTrack = std::find_if(d->mAllTrackData.begin(), d->mAllTrackData.end(), | ||
509 | [removedTrackId](auto track) {return track.databaseId() == removedTrackId;}); | 636 | [removedTrackId](auto track) {return track.databaseId() == removedTrackId;}); | ||
510 | 637 | | |||
511 | if (itTrack == d->mAllTrackData.end()) { | 638 | if (itTrack == d->mAllTrackData.end()) { | ||
512 | return; | 639 | return; | ||
513 | } | 640 | } | ||
514 | 641 | | |||
515 | auto position = itTrack - d->mAllTrackData.begin(); | 642 | auto position = itTrack - d->mAllTrackData.begin(); | ||
516 | 643 | | |||
517 | beginRemoveRows({}, position, position); | 644 | beginRemoveRows({}, position, position); | ||
518 | d->mAllTrackData.erase(itTrack); | 645 | d->mAllTrackData.erase(itTrack); | ||
519 | endRemoveRows(); | 646 | endRemoveRows(); | ||
520 | } | 647 | } | ||
521 | } | 648 | } | ||
522 | 649 | | |||
650 | void DataModel::radioRemoved(qulonglong removedRadioId) | ||||
651 | { | ||||
652 | if (d->mModelType != ElisaUtils::Radio) { | ||||
653 | return; | ||||
654 | } | ||||
655 | | ||||
656 | | ||||
657 | auto itRadio = std::find_if(d->mAllRadiosData.begin(), d->mAllRadiosData.end(), | ||||
658 | [removedRadioId](auto track) {return track.databaseId() == removedRadioId;}); | ||||
659 | | ||||
660 | if (itRadio == d->mAllRadiosData.end()) { | ||||
661 | return; | ||||
662 | } | ||||
663 | | ||||
664 | auto position = itRadio - d->mAllRadiosData.begin(); | ||||
665 | | ||||
666 | beginRemoveRows({}, position, position); | ||||
667 | d->mAllRadiosData.erase(itRadio); | ||||
668 | endRemoveRows(); | ||||
669 | } | ||||
670 | | ||||
671 | void DataModel::radioAdded(const DataModel::TrackDataType radioData) | ||||
672 | { | ||||
673 | if (d->mModelType != ElisaUtils::Radio) { | ||||
674 | return; | ||||
675 | } | ||||
676 | | ||||
677 | ListRadioDataType list; | ||||
678 | list.append(radioData); | ||||
679 | radiosAdded(list); | ||||
680 | } | ||||
681 | | ||||
682 | void DataModel::removeRadios() | ||||
683 | { | ||||
684 | if (d->mModelType != ElisaUtils::Radio) { | ||||
685 | return; | ||||
686 | } | ||||
687 | | ||||
688 | beginRemoveRows({}, 0, d->mAllRadiosData.size()); | ||||
689 | d->mAllRadiosData.clear(); | ||||
690 | endRemoveRows(); | ||||
691 | } | ||||
692 | | ||||
523 | void DataModel::genresAdded(DataModel::ListGenreDataType newData) | 693 | void DataModel::genresAdded(DataModel::ListGenreDataType newData) | ||
524 | { | 694 | { | ||
525 | if (newData.isEmpty() && d->mModelType == ElisaUtils::Genre) { | 695 | if (newData.isEmpty() && d->mModelType == ElisaUtils::Genre) { | ||
526 | setBusy(false); | 696 | setBusy(false); | ||
527 | } | 697 | } | ||
528 | 698 | | |||
529 | if (newData.isEmpty() || d->mModelType != ElisaUtils::Genre) { | 699 | if (newData.isEmpty() || d->mModelType != ElisaUtils::Genre) { | ||
530 | return; | 700 | return; | ||
▲ Show 20 Lines • Show All 140 Lines • Show Last 20 Lines |
It would better (safer) if you add one more type like DataModel::ListRadioDataType. Online radios are similar to tracks but by doing that, the compiler will help avoid mistakes when connecting signals to slots.