strawberry-audio-player-win.../src/queue/queueview.cpp

176 lines
5.4 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
2018-10-19 20:18:46 +02:00
#include <algorithm>
2018-02-27 18:06:05 +01:00
#include <QWidget>
#include <QAbstractItemModel>
#include <QItemSelectionModel>
#include <QKeySequence>
#include <QList>
#include <QPushButton>
#include <QShortcut>
#include <QTreeView>
#include <QtAlgorithms>
2018-10-21 15:13:48 +02:00
#include <QTimer>
#include "core/iconloader.h"
2018-10-21 15:13:48 +02:00
#include "playlist/playlist.h"
#include "playlist/playlistdelegates.h"
#include "playlist/playlistmanager.h"
2018-02-27 18:06:05 +01:00
#include "queue.h"
2018-10-21 15:13:48 +02:00
#include "queueview.h"
#include "ui_queueview.h"
2018-02-27 18:06:05 +01:00
2018-10-19 20:18:46 +02:00
using std::stable_sort;
2018-10-21 15:13:48 +02:00
QueueView::QueueView(QWidget *parent)
: QWidget(parent),
ui_(new Ui_QueueView),
2018-02-27 18:06:05 +01:00
playlists_(nullptr),
current_playlist_(nullptr) {
2018-10-21 15:13:48 +02:00
2018-02-27 18:06:05 +01:00
ui_->setupUi(this);
ui_->list->setItemDelegate(new QueuedItemDelegate(this, 0));
// Set icons on buttons
ui_->move_down->setIcon(IconLoader::Load("go-down"));
ui_->move_up->setIcon(IconLoader::Load("go-up"));
ui_->remove->setIcon(IconLoader::Load("edit-delete"));
ui_->clear->setIcon(IconLoader::Load("edit-clear-list"));
// Set a standard shortcut
ui_->remove->setShortcut(QKeySequence::Delete);
// Button connections
connect(ui_->move_down, SIGNAL(clicked()), SLOT(MoveDown()));
connect(ui_->move_up, SIGNAL(clicked()), SLOT(MoveUp()));
connect(ui_->remove, SIGNAL(clicked()), SLOT(Remove()));
connect(ui_->clear, SIGNAL(clicked()), SLOT(Clear()));
}
2018-10-21 15:13:48 +02:00
QueueView::~QueueView() {
2018-02-27 18:06:05 +01:00
delete ui_;
}
2018-10-21 15:13:48 +02:00
void QueueView::SetPlaylistManager(PlaylistManager *manager) {
2018-02-27 18:06:05 +01:00
playlists_ = manager;
connect(playlists_, SIGNAL(CurrentChanged(Playlist*)), SLOT(CurrentPlaylistChanged(Playlist*)));
CurrentPlaylistChanged(playlists_->current());
}
2018-10-21 15:13:48 +02:00
void QueueView::CurrentPlaylistChanged(Playlist *playlist) {
2018-02-27 18:06:05 +01:00
if (current_playlist_) {
disconnect(current_playlist_->queue(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(UpdateButtonState()));
disconnect(current_playlist_->queue(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(UpdateButtonState()));
2018-02-27 18:06:05 +01:00
disconnect(current_playlist_->queue(), SIGNAL(layoutChanged()), this, SLOT(UpdateButtonState()));
2018-10-21 15:13:48 +02:00
disconnect(current_playlist_->queue(), SIGNAL(SummaryTextChanged(QString)), ui_->summary, SLOT(setText(QString)));
2018-02-27 18:06:05 +01:00
disconnect(current_playlist_, SIGNAL(destroyed()), this, SLOT(PlaylistDestroyed()));
}
current_playlist_ = playlist;
connect(current_playlist_->queue(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(UpdateButtonState()));
connect(current_playlist_->queue(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(UpdateButtonState()));
connect(current_playlist_->queue(), SIGNAL(layoutChanged()), this, SLOT(UpdateButtonState()));
2018-10-21 15:13:48 +02:00
connect(current_playlist_->queue(), SIGNAL(SummaryTextChanged(QString)), ui_->summary, SLOT(setText(QString)));
2018-02-27 18:06:05 +01:00
connect(current_playlist_, SIGNAL(destroyed()), this, SLOT(PlaylistDestroyed()));
ui_->list->setModel(current_playlist_->queue());
connect(ui_->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(UpdateButtonState()));
2018-10-21 15:13:48 +02:00
QTimer::singleShot(0, current_playlist_->queue(), SLOT(UpdateSummaryText()));
2018-02-27 18:06:05 +01:00
}
2018-10-21 15:13:48 +02:00
void QueueView::MoveUp() {
2018-02-27 18:06:05 +01:00
QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
2018-10-19 20:18:46 +02:00
std::stable_sort(indexes.begin(), indexes.end());
2018-02-27 18:06:05 +01:00
if (indexes.isEmpty() || indexes.first().row() == 0) return;
for (const QModelIndex &index : indexes) {
current_playlist_->queue()->MoveUp(index.row());
}
}
2018-10-21 15:13:48 +02:00
void QueueView::MoveDown() {
2018-02-27 18:06:05 +01:00
QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
2018-10-19 20:18:46 +02:00
std::stable_sort(indexes.begin(), indexes.end());
2018-02-27 18:06:05 +01:00
if (indexes.isEmpty() || indexes.last().row() == current_playlist_->queue()->rowCount()-1)
return;
for (int i = indexes.count() - 1; i >= 0; --i) {
current_playlist_->queue()->MoveDown(indexes[i].row());
}
}
2018-10-21 15:13:48 +02:00
void QueueView::Clear() {
2018-02-27 18:06:05 +01:00
current_playlist_->queue()->Clear();
}
2018-10-21 15:13:48 +02:00
void QueueView::Remove() {
2018-02-27 18:06:05 +01:00
// collect the rows to be removed
QList<int> row_list;
for (const QModelIndex &index : ui_->list->selectionModel()->selectedRows()) {
if (index.isValid()) row_list << index.row();
}
current_playlist_->queue()->Remove(row_list);
}
2018-10-21 15:13:48 +02:00
void QueueView::UpdateButtonState() {
2018-02-27 18:06:05 +01:00
const QModelIndex current = ui_->list->selectionModel()->currentIndex();
if (current.isValid()) {
ui_->move_up->setEnabled(current.row() != 0);
ui_->move_down->setEnabled(current.row() != current_playlist_->queue()->rowCount()-1);
ui_->remove->setEnabled(true);
}
else {
ui_->move_up->setEnabled(false);
ui_->move_down->setEnabled(false);
ui_->remove->setEnabled(false);
}
ui_->clear->setEnabled(!current_playlist_->queue()->is_empty());
}
2018-10-21 15:13:48 +02:00
void QueueView::PlaylistDestroyed() {
2018-02-27 18:06:05 +01:00
current_playlist_ = nullptr;
// We'll get another CurrentPlaylistChanged() soon
}