diff --git a/main.cpp b/main.cpp index cea2330..c618edb 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/playlistmodel.cpp b/playlistmodel.cpp index 91a85a1..aaa5096 100644 --- a/playlistmodel.cpp +++ b/playlistmodel.cpp @@ -263,7 +263,7 @@ bool PlaylistModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i } m_playlist->moveMedia(originalIdx.toInt(), newIndex); - m_playlist->setCurrentIndex(newIndex); // TODO set selection instead of currentIndex + //m_playlist->setCurrentIndex(newIndex); // TODO set selection instead of currentIndex row++; } diff --git a/playlistview.cpp b/playlistview.cpp index 1a439ad..e8d3854 100644 --- a/playlistview.cpp +++ b/playlistview.cpp @@ -2,7 +2,6 @@ #include "ui_playlistview.h" #include "filebrowsericonprovider.h" #include "util.h" -#include #include #include @@ -22,7 +21,7 @@ PlaylistView::PlaylistView(QWidget *parent, PlaylistModel *playlistModel) : connect(ui->goPlayerButton, &QPushButton::clicked, this, &PlaylistView::showPlayerClicked); connect(m_playlist, &QMediaPlaylist::currentIndexChanged, this, &PlaylistView::playlistPositionChanged); - connect(ui->playList, &QAbstractItemView::clicked, this, &PlaylistView::songSelected); + connect(ui->playList, &QAbstractItemView::clicked, this, &PlaylistView::handleSongSelected); connect(ui->clearButton, &QPushButton::clicked, this, &PlaylistView::clearPlaylist); connect(ui->removeButton, &QPushButton::clicked, this, &PlaylistView::removeItem); @@ -36,6 +35,9 @@ PlaylistView::PlaylistView(QWidget *parent, PlaylistModel *playlistModel) : connect(m_playlist, &QMediaPlaylist::mediaChanged, this, &PlaylistView::updateTotalDuration); connect(m_playlist, &QMediaPlaylist::mediaInserted, this, &PlaylistView::updateTotalDuration); connect(m_playlist, &QMediaPlaylist::mediaRemoved, this, &PlaylistView::updateTotalDuration); + connect(m_playlist, &QMediaPlaylist::currentSelectionChanged, this, &PlaylistView::handleSelectionChanged); + + connect(ui->editButton, &QPushButton::clicked, this, &PlaylistView::toggleEditMode); updateTotalDuration(); } @@ -80,9 +82,9 @@ void PlaylistView::setupPlayListUi() sp.setScrollMetric(QScrollerProperties::MousePressEventDelay, 1.0); sp.setScrollMetric(QScrollerProperties::OvershootDragResistanceFactor, 0.3); sp.setScrollMetric(QScrollerProperties::OvershootScrollDistanceFactor, 0.1); - QScroller* playlistViewScroller = QScroller::scroller(ui->playList); - playlistViewScroller->grabGesture(ui->playList, QScroller::LeftMouseButtonGesture); - playlistViewScroller->setScrollerProperties(sp); + m_playlistViewScroller = QScroller::scroller(ui->playList); + m_playlistViewScroller->grabGesture(ui->playList, QScroller::LeftMouseButtonGesture); + m_playlistViewScroller->setScrollerProperties(sp); ui->playList->setModel(m_playlistModel); ui->playList->setCurrentIndex(m_playlistModel->index(m_playlist->currentIndex(), 0)); @@ -97,13 +99,14 @@ void PlaylistView::setupPlayListUi() ui->playList->header()->setSectionResizeMode(4, QHeaderView::ResizeMode::Fixed); ui->playList->header()->setDefaultSectionSize(80); - ui->playList->setDragEnabled(true); - ui->playList->setAcceptDrops(true); ui->playList->setDropIndicatorShown(true); ui->playList->setDragDropMode(QAbstractItemView::DragDrop); ui->playList->setDefaultDropAction(Qt::CopyAction); ui->playList->setDragDropOverwriteMode(false); ui->playList->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows); + // Disable drag and drop until we enable edit mode + ui->playList->setDragEnabled(false); + ui->playList->setAcceptDrops(false); } void PlaylistView::setupFileBrowserUi() @@ -212,4 +215,35 @@ void PlaylistView::updateTotalDuration() ui->plDuration->setText(formatDuration(total)); } +void PlaylistView::handleSongSelected(const QModelIndex &index) +{ + bool inEditMode = ui->editButton->isChecked(); + if(!inEditMode) { + emit songSelected(index); + } +} + +void PlaylistView::toggleEditMode() +{ + bool isEnabled = ui->editButton->isChecked(); + + if(isEnabled) { + ui->playList->setDragEnabled(true); + ui->playList->setAcceptDrops(true); + m_playlistViewScroller->ungrabGesture(ui->playList); + } else { + ui->playList->setDragEnabled(false); + ui->playList->setAcceptDrops(false); + m_playlistViewScroller->grabGesture(ui->playList, QScroller::LeftMouseButtonGesture); + } +} +void PlaylistView::handleSelectionChanged(int index) +{ + if(index < 0) + return; + auto idx = m_playlistModel->index(index, 0); + ui->playList->selectionModel()->setCurrentIndex(idx, + QItemSelectionModel::SelectionFlag::Rows | + QItemSelectionModel::SelectionFlag::SelectCurrent); +} diff --git a/playlistview.h b/playlistview.h index 32e07e3..0abf07d 100644 --- a/playlistview.h +++ b/playlistview.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "qmediaplaylist.h" #include "playlistmodel.h" @@ -24,6 +25,7 @@ class PlaylistView : public QWidget QMediaPlaylist *m_playlist = nullptr; PlaylistModel *m_playlistModel = nullptr; QFileSystemModel *m_fileSystemModel = nullptr; + QScroller *m_playlistViewScroller = nullptr; void setupPlayListUi(); void setupFileBrowserUi(); @@ -35,6 +37,8 @@ private slots: void playlistPositionChanged(int); void clearPlaylist(); void removeItem(); + void handleSongSelected(const QModelIndex &index); + void handleSelectionChanged(int index); // File browser functions void fbGoHome(); @@ -46,6 +50,8 @@ private slots: void updateTotalDuration(); + void toggleEditMode(); + signals: void showPlayerClicked(); void songSelected(const QModelIndex &index); diff --git a/playlistview.ui b/playlistview.ui index 63b6236..b8d08e5 100644 --- a/playlistview.ui +++ b/playlistview.ui @@ -817,6 +817,56 @@ QHeaderView::section { + + + + + 84 + 54 + + + + + 85 + 54 + + + + + Bitstream Vera Sans Mono + 12 + true + + + + #editButton { + color: #333350; + background-color: #bdced6; + border: 4px solid #4a5a6b; + border-radius: none; +} + +#editButton:pressed { + color: #080810; + background-color: #7b8c9c; + border: 4px solid #080810; +} + +#editButton:checked { + background-color: #00d600; +} + + + EDIT + + + true + + + false + + + diff --git a/qmediaplaylist.cpp b/qmediaplaylist.cpp index 361d51d..0f64ecc 100644 --- a/qmediaplaylist.cpp +++ b/qmediaplaylist.cpp @@ -539,6 +539,30 @@ bool QMediaPlaylist::moveMedia(int from, int to) if (from < 0 || from > d->playlist.count() || to < 0 || to > d->playlist.count()) return false; + // Nothing to do here + if(from == to) + return false; + + // Get the current playing media position and update after change + int currentPlayingPos = d->currentPos(); + int newPlayingPos = currentPlayingPos; + // If currentPlayingPos is between the movement positions, it will change + if(currentPlayingPos == from) { + // Special case, current playing item is the one moving + newPlayingPos = to; + } + // Case when moving down + if(from < to && currentPlayingPos > from && currentPlayingPos <= to) { + // Everything in between moves up + newPlayingPos = currentPlayingPos - 1; + } + // Case when moving up + if(from > to && currentPlayingPos >= to && currentPlayingPos < from) { + // Everything in between moves down + newPlayingPos = currentPlayingPos + 1; + } + + // Do te moves d->playlist.move(from, to); // Do also playqueue @@ -547,7 +571,11 @@ bool QMediaPlaylist::moveMedia(int from, int to) this->shuffle(); } - emit mediaChanged(from, to); + // Set new position + d->setCurrentPos(newPlayingPos); + + emit mediaChanged(0, d->playlist.count()); + emit currentSelectionChanged(to); // highlight destination return true; } @@ -574,6 +602,18 @@ bool QMediaPlaylist::removeMedia(int start, int end) start = qBound(0, start, d->playlist.size() - 1); end = qBound(0, end, d->playlist.size() - 1); + // Get the current playing media position and update after change + int currentPlayingPos = d->currentPos(); + int newPlayingPos = currentPlayingPos; + // If currently playing position is between or at start or end, set at the begining + if(currentPlayingPos >= start && currentPlayingPos <= end) { + newPlayingPos = -1; + } + // If current playing position is after end, it will move up + if(currentPlayingPos > end) { + newPlayingPos = currentPlayingPos - (end - start) - 1; + } + emit mediaAboutToBeRemoved(start, end); d->playlist.remove(start, end - start + 1); @@ -585,7 +625,11 @@ bool QMediaPlaylist::removeMedia(int start, int end) vacuumMetadata(); + // Set new position + d->setCurrentPos(newPlayingPos); + emit mediaRemoved(start, end); + emit mediaChanged(0, d->playlist.count()); return true; } diff --git a/qmediaplaylist.h b/qmediaplaylist.h index f5e79f2..b089ecb 100644 --- a/qmediaplaylist.h +++ b/qmediaplaylist.h @@ -91,6 +91,9 @@ public slots: void playbackModeChanged(QMediaPlaylist::PlaybackMode mode); void currentMediaChanged(const QUrl &); + // Emited when we want to inform the view to select a different index without playinf + void currentSelectionChanged(int index); + void mediaAboutToBeInserted(int start, int end); void mediaInserted(int start, int end); void mediaAboutToBeRemoved(int start, int end); diff --git a/qmediaplaylist_p.cpp b/qmediaplaylist_p.cpp index c9bcab2..2d00f08 100644 --- a/qmediaplaylist_p.cpp +++ b/qmediaplaylist_p.cpp @@ -75,6 +75,12 @@ int QMediaPlaylistPrivate::currentQueuePos() const void QMediaPlaylistPrivate::setCurrentPos(int pos) { m_currentPos = pos; + + if (pos < 0 || pos >= playlist.size()) { + m_currentQueuePos = pos; + return; + } + // Sync currentPlayPos QUrl item = playlist.at(m_currentPos); m_currentQueuePos = playqueue.indexOf(item); @@ -83,6 +89,12 @@ void QMediaPlaylistPrivate::setCurrentPos(int pos) void QMediaPlaylistPrivate::setCurrentQueuePos(int pos) { m_currentQueuePos = pos; + + if (pos < 0 || pos >= playlist.size()) { + m_currentPos = pos; + return; + } + // Sync currentPos QUrl item = playqueue.at(m_currentQueuePos); m_currentPos = playlist.indexOf(item);