Skip to content

Commit

Permalink
Edit mode working
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodmg committed Nov 3, 2023
1 parent 103fed3 commit a1150c0
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 10 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 playlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
48 changes: 41 additions & 7 deletions playlistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "ui_playlistview.h"
#include "filebrowsericonprovider.h"
#include "util.h"
#include <QScroller>
#include <QFileIconProvider>
#include <QStandardPaths>

Expand All @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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));
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
6 changes: 6 additions & 0 deletions playlistview.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QWidget>
#include <QUrl>
#include <QFileSystemModel>
#include <QScroller>
#include "qmediaplaylist.h"
#include "playlistmodel.h"

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -46,6 +50,8 @@ private slots:

void updateTotalDuration();

void toggleEditMode();

signals:
void showPlayerClicked();
void songSelected(const QModelIndex &index);
Expand Down
50 changes: 50 additions & 0 deletions playlistview.ui
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,56 @@ QHeaderView::section {
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="editButton">
<property name="minimumSize">
<size>
<width>84</width>
<height>54</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>85</width>
<height>54</height>
</size>
</property>
<property name="font">
<font>
<family>Bitstream Vera Sans Mono</family>
<pointsize>12</pointsize>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="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;
}</string>
</property>
<property name="text">
<string>EDIT</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeButton">
<property name="minimumSize">
Expand Down
46 changes: 45 additions & 1 deletion qmediaplaylist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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);

Expand All @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions qmediaplaylist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions qmediaplaylist_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a1150c0

Please sign in to comment.