Skip to content

Commit

Permalink
Perfecting reorder of playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodmg committed Nov 3, 2023
1 parent 96f3824 commit 103fed3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main(int argc, char *argv[])
}

#ifdef IS_EMBEDDED
window.setWindowState(Qt::WindowFullScreen);
//window.setWindowState(Qt::WindowFullScreen);
#endif
window.show();

Expand Down
2 changes: 1 addition & 1 deletion player.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 11.0.2, 2023-11-03T00:25:11. -->
<!-- Written by QtCreator 11.0.2, 2023-11-03T11:46:25. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
38 changes: 19 additions & 19 deletions playlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <QFileInfo>
#include <QUrl>
#include <QMimeData>
#include <QDebug>

PlaylistModel::PlaylistModel(QObject *parent) : QAbstractItemModel(parent)
{
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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;
}


Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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);
Expand All @@ -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++;
}
Expand Down
1 change: 0 additions & 1 deletion playlistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private slots:

private:
QScopedPointer<QMediaPlaylist> m_playlist;
QMap<QModelIndex, QVariant> m_data;
};

#endif // PLAYLISTMODEL_H
5 changes: 3 additions & 2 deletions playlistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions qmediaplaylist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 103fed3

Please sign in to comment.