diff options
author | Yoann Lopes <yoann.lopes@theqtcompany.com> | 2015-10-08 18:26:39 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-12-11 14:53:16 +0000 |
commit | 82e135167a5d24f600f006480b78a59511ae5cb3 (patch) | |
tree | 0e27d0b2520e42d4b7bfb1db32dd5446df49502f /src/multimedia/playback/qmediaplaylist.cpp | |
parent | 94c33684fe3ba3c08af91d5d465c35f5dc706721 (diff) |
Change the way a playlist is bound to a media object.v5.6.0-beta1
The previous behavior was to simply switch from the internal
control to the service's control, discarding anything that was
added to the playlist before binding.
We now carry over the changes made to the playlist when switching
controls. This means the switch is now transparent to the user.
When the service's control is read-only, we cannot transfer the
items, which means the user must be notified of the items that
might have been "lost" during the switch.
Auto-test modified to reflect this change.
Change-Id: Ibf80b650b06425ddbaeb320b72ac5d3082a25960
Reviewed-by: Jim Hodapp <jim.hodapp@canonical.com>
Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
Diffstat (limited to 'src/multimedia/playback/qmediaplaylist.cpp')
-rw-r--r-- | src/multimedia/playback/qmediaplaylist.cpp | 72 |
1 files changed, 64 insertions, 8 deletions
diff --git a/src/multimedia/playback/qmediaplaylist.cpp b/src/multimedia/playback/qmediaplaylist.cpp index 06813592e..c63340637 100644 --- a/src/multimedia/playback/qmediaplaylist.cpp +++ b/src/multimedia/playback/qmediaplaylist.cpp @@ -168,10 +168,13 @@ bool QMediaPlaylist::setMediaObject(QMediaObject *mediaObject) newControl = d->networkPlaylistControl; if (d->control != newControl) { - int oldSize = 0; + int removedStart = -1; + int removedEnd = -1; + int insertedStart = -1; + int insertedEnd = -1; + if (d->control) { QMediaPlaylistProvider *playlist = d->control->playlistProvider(); - oldSize = playlist->mediaCount(); disconnect(playlist, SIGNAL(loadFailed(QMediaPlaylist::Error,QString)), this, SLOT(_q_loadFailed(QMediaPlaylist::Error,QString))); @@ -190,6 +193,12 @@ bool QMediaPlaylist::setMediaObject(QMediaObject *mediaObject) disconnect(d->control, SIGNAL(currentMediaChanged(QMediaContent)), this, SIGNAL(currentMediaChanged(QMediaContent))); + // Copy playlist items, sync playback mode and sync current index between + // old control and new control + d->syncControls(d->control, newControl, + &removedStart, &removedEnd, + &insertedStart, &insertedEnd); + if (d->mediaObject) d->mediaObject->service()->releaseControl(d->control); } @@ -214,14 +223,14 @@ bool QMediaPlaylist::setMediaObject(QMediaObject *mediaObject) connect(d->control, SIGNAL(currentMediaChanged(QMediaContent)), this, SIGNAL(currentMediaChanged(QMediaContent))); - if (oldSize) { - emit mediaAboutToBeRemoved(0, oldSize-1); - emit mediaRemoved(0, oldSize-1); + if (removedStart != -1 && removedEnd != -1) { + emit mediaAboutToBeRemoved(removedStart, removedEnd); + emit mediaRemoved(removedStart, removedEnd); } - if (playlist->mediaCount()) { - emit mediaAboutToBeInserted(0,playlist->mediaCount()-1); - emit mediaInserted(0,playlist->mediaCount()-1); + if (insertedStart != -1 && insertedEnd != -1) { + emit mediaAboutToBeInserted(insertedStart, insertedEnd); + emit mediaInserted(insertedStart, insertedEnd); } } @@ -436,6 +445,53 @@ bool QMediaPlaylistPrivate::writeItems(QMediaPlaylistWriter *writer) } /*! + * \internal + * Copy playlist items, sync playback mode and sync current index between old control and new control +*/ +void QMediaPlaylistPrivate::syncControls(QMediaPlaylistControl *oldControl, QMediaPlaylistControl *newControl, + int *removedStart, int *removedEnd, + int *insertedStart, int *insertedEnd) +{ + Q_ASSERT(oldControl != NULL && newControl != NULL); + Q_ASSERT(removedStart != NULL && removedEnd != NULL + && insertedStart != NULL && insertedEnd != NULL); + + QMediaPlaylistProvider *oldPlaylist = oldControl->playlistProvider(); + QMediaPlaylistProvider *newPlaylist = newControl->playlistProvider(); + + Q_ASSERT(oldPlaylist != NULL && newPlaylist != NULL); + + *removedStart = -1; + *removedEnd = -1; + *insertedStart = -1; + *insertedEnd = -1; + + if (newPlaylist->isReadOnly()) { + // we can't transfer the items from the old control. + // Report these items as removed. + if (oldPlaylist->mediaCount() > 0) { + *removedStart = 0; + *removedEnd = oldPlaylist->mediaCount() - 1; + } + // The new control might have some items that can't be cleared. + // Report these as inserted. + if (newPlaylist->mediaCount() > 0) { + *insertedStart = 0; + *insertedEnd = newPlaylist->mediaCount() - 1; + } + } else { + const int oldPlaylistSize = oldPlaylist->mediaCount(); + + newPlaylist->clear(); + for (int i = 0; i < oldPlaylistSize; ++i) + newPlaylist->addMedia(oldPlaylist->media(i)); + } + + newControl->setPlaybackMode(oldControl->playbackMode()); + newControl->setCurrentIndex(oldControl->currentIndex()); +} + +/*! Load playlist using network \a request. If \a format is specified, it is used, otherwise format is guessed from playlist name and data. |