From 103fed3810d320ba3296e9f4d158c798eda9a7de Mon Sep 17 00:00:00 2001 From: Rodrigo Mendez Date: Fri, 3 Nov 2023 15:22:30 -0600 Subject: [PATCH] Perfecting reorder of playlist --- main.cpp | 2 +- player.pro.user | 2 +- playlistmodel.cpp | 38 +++++++++++++++++++------------------- playlistmodel.h | 1 - playlistview.cpp | 5 +++-- qmediaplaylist.cpp | 7 +++++++ 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/main.cpp b/main.cpp index c618edb..cea2330 100644 --- a/main.cpp +++ b/main.cpp @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) } #ifdef IS_EMBEDDED - window.setWindowState(Qt::WindowFullScreen); + //window.setWindowState(Qt::WindowFullScreen); #endif window.show(); diff --git a/player.pro.user b/player.pro.user index ecf159f..ad8d7c0 100644 --- a/player.pro.user +++ b/player.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/playlistmodel.cpp b/playlistmodel.cpp index 58c822d..91a85a1 100644 --- a/playlistmodel.cpp +++ b/playlistmodel.cpp @@ -9,7 +9,6 @@ #include #include #include -#include PlaylistModel::PlaylistModel(QObject *parent) : QAbstractItemModel(parent) { @@ -139,16 +138,14 @@ QMediaPlaylist *PlaylistModel::playlist() const bool PlaylistModel::setData(const QModelIndex &index, const QVariant &value, int role) { Q_UNUSED(role); - m_data[index] = value; + Q_UNUSED(value); emit dataChanged(index, index); return true; } void PlaylistModel::beginInsertItems(int start, int end) { - m_data.clear(); beginInsertRows(QModelIndex(), start, end); - qDebug() << ">>>>>beginInsertItems"; } void PlaylistModel::endInsertItems() @@ -158,9 +155,7 @@ void PlaylistModel::endInsertItems() void PlaylistModel::beginRemoveItems(int start, int end) { - m_data.clear(); beginRemoveRows(QModelIndex(), start, end); - qDebug() << ">>>>>beginRemoveItems"; } void PlaylistModel::endRemoveItems() @@ -171,9 +166,7 @@ void PlaylistModel::endRemoveItems() void PlaylistModel::changeItems(int start, int end) { - m_data.clear(); emit dataChanged(index(start, 0), index(end, ColumnCount)); - qDebug() << ">>>>>changeItems"; } Qt::ItemFlags PlaylistModel::flags(const QModelIndex &index) const @@ -186,12 +179,12 @@ Qt::ItemFlags PlaylistModel::flags(const QModelIndex &index) const Qt::DropActions PlaylistModel::supportedDragActions() const { - return Qt::MoveAction; + return Qt::CopyAction; } Qt::DropActions PlaylistModel::supportedDropActions() const { - return Qt::MoveAction; + return Qt::CopyAction; } @@ -210,7 +203,7 @@ bool PlaylistModel::canDropMimeData(const QMimeData *data, Qt::DropAction action Q_UNUSED(column); Q_UNUSED(parent); - if ( action != Qt::MoveAction || !data->hasFormat(PlaylistModel::MimeType)) + if ( action != Qt::CopyAction || !data->hasFormat(PlaylistModel::MimeType) || row == -1) return false; return true; @@ -224,8 +217,9 @@ QMimeData *PlaylistModel::mimeData(const QModelIndexList &indexes) const QDataStream stream(&encodedData, QIODevice::WriteOnly); for (const QModelIndex &index : indexes) { - if (index.isValid()) { - QString location = m_playlist->media(index.row()).toString(); + // Only "pick" first column, avoid multiple insertions + if (index.isValid() && index.column() == 0) { + QString location = QString::number(index.row()); stream << location; } } @@ -241,7 +235,7 @@ bool PlaylistModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i if (action == Qt::IgnoreAction) return true; - else if (action != Qt::MoveAction) + else if (action != Qt::CopyAction) return false; QByteArray encodedData = data->data(PlaylistModel::MimeType); @@ -257,13 +251,19 @@ bool PlaylistModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i } insertRows(row, rows, QModelIndex()); - for (const QString &text : qAsConst(newItems)) + for (const QString &originalIdx : qAsConst(newItems)) { - QModelIndex idx = index(row, 0, QModelIndex()); - setData(idx, text); + int newIndex = row; - QUrl url = QUrl(text); - m_playlist->insertMedia(idx.row(), url); // TODO better way to do this to avoid cuts in playback + // Special case when moving to the bottom of the list + int mediaCount = m_playlist->mediaCount(); + int maxIndex = mediaCount - 1; + if(newIndex > maxIndex) { + newIndex = maxIndex; + } + + m_playlist->moveMedia(originalIdx.toInt(), newIndex); + m_playlist->setCurrentIndex(newIndex); // TODO set selection instead of currentIndex row++; } diff --git a/playlistmodel.h b/playlistmodel.h index d52aa44..fde5cab 100644 --- a/playlistmodel.h +++ b/playlistmodel.h @@ -60,7 +60,6 @@ private slots: private: QScopedPointer m_playlist; - QMap m_data; }; #endif // PLAYLISTMODEL_H diff --git a/playlistview.cpp b/playlistview.cpp index 99c266e..1a439ad 100644 --- a/playlistview.cpp +++ b/playlistview.cpp @@ -100,9 +100,10 @@ void PlaylistView::setupPlayListUi() ui->playList->setDragEnabled(true); ui->playList->setAcceptDrops(true); ui->playList->setDropIndicatorShown(true); - ui->playList->setDragDropMode(QAbstractItemView::InternalMove); - ui->playList->setDefaultDropAction(Qt::MoveAction); + ui->playList->setDragDropMode(QAbstractItemView::DragDrop); + ui->playList->setDefaultDropAction(Qt::CopyAction); ui->playList->setDragDropOverwriteMode(false); + ui->playList->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows); } void PlaylistView::setupFileBrowserUi() diff --git a/qmediaplaylist.cpp b/qmediaplaylist.cpp index 3802894..361d51d 100644 --- a/qmediaplaylist.cpp +++ b/qmediaplaylist.cpp @@ -540,6 +540,13 @@ bool QMediaPlaylist::moveMedia(int from, int to) return false; d->playlist.move(from, to); + + // Do also playqueue + d->playqueue = QList(d->playlist); + if(shuffleEnabled) { + this->shuffle(); + } + emit mediaChanged(from, to); return true; }