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

200 lines
6.7 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>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* 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
*/
2020-02-12 00:07:05 +01:00
#include "config.h"
2018-10-19 20:18:46 +02:00
#include <algorithm>
2024-04-23 17:15:42 +02:00
#include <utility>
2018-02-27 18:06:05 +01:00
#include <QWidget>
#include <QAbstractItemModel>
#include <QItemSelectionModel>
2020-02-08 15:03:11 +01:00
#include <QList>
#include <QMetaObject>
#include <QSettings>
2020-02-08 15:03:11 +01:00
#include <QKeySequence>
#include <QLabel>
#include <QToolButton>
#include "core/shared_ptr.h"
#include "core/iconloader.h"
#include "core/settings.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"
#include "settings/appearancesettingspage.h"
2018-02-27 18:06:05 +01:00
2018-10-21 15:13:48 +02:00
QueueView::QueueView(QWidget *parent)
: QWidget(parent),
ui_(new Ui_QueueView),
playlist_manager_(nullptr),
2018-02-27 18:06:05 +01:00
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
2024-04-09 23:20:26 +02:00
ui_->move_down->setIcon(IconLoader::Load(QStringLiteral("go-down")));
ui_->move_up->setIcon(IconLoader::Load(QStringLiteral("go-up")));
ui_->remove->setIcon(IconLoader::Load(QStringLiteral("edit-delete")));
ui_->clear->setIcon(IconLoader::Load(QStringLiteral("edit-clear-list")));
2018-02-27 18:06:05 +01:00
// Set a standard shortcut
ui_->remove->setShortcut(QKeySequence::Delete);
// Button connections
2021-01-26 16:48:04 +01:00
QObject::connect(ui_->move_down, &QToolButton::clicked, this, &QueueView::MoveDown);
QObject::connect(ui_->move_up, &QToolButton::clicked, this, &QueueView::MoveUp);
QObject::connect(ui_->remove, &QToolButton::clicked, this, &QueueView::Remove);
QObject::connect(ui_->clear, &QToolButton::clicked, this, &QueueView::Clear);
2018-02-27 18:06:05 +01:00
ReloadSettings();
2018-02-27 18:06:05 +01:00
}
2018-10-21 15:13:48 +02:00
QueueView::~QueueView() {
2018-02-27 18:06:05 +01:00
delete ui_;
}
void QueueView::SetPlaylistManager(SharedPtr<PlaylistManager> playlist_manager) {
2018-02-27 18:06:05 +01:00
playlist_manager_ = playlist_manager;
2018-02-27 18:06:05 +01:00
QObject::connect(&*playlist_manager, &PlaylistManager::CurrentChanged, this, &QueueView::CurrentPlaylistChanged);
CurrentPlaylistChanged(playlist_manager_->current());
2018-02-27 18:06:05 +01:00
}
void QueueView::ReloadSettings() {
Settings s;
s.beginGroup(AppearanceSettingsPage::kSettingsGroup);
int iconsize = s.value(AppearanceSettingsPage::kIconSizeLeftPanelButtons, 22).toInt();
s.endGroup();
ui_->move_down->setIconSize(QSize(iconsize, iconsize));
ui_->move_up->setIconSize(QSize(iconsize, iconsize));
ui_->remove->setIconSize(QSize(iconsize, iconsize));
ui_->clear->setIconSize(QSize(iconsize, iconsize));
}
2018-10-21 15:13:48 +02:00
void QueueView::CurrentPlaylistChanged(Playlist *playlist) {
2018-02-27 18:06:05 +01:00
if (current_playlist_) {
2021-01-26 16:48:04 +01:00
QObject::disconnect(current_playlist_->queue(), &Queue::rowsInserted, this, &QueueView::UpdateButtonState);
QObject::disconnect(current_playlist_->queue(), &Queue::rowsRemoved, this, &QueueView::UpdateButtonState);
QObject::disconnect(current_playlist_->queue(), &Queue::layoutChanged, this, &QueueView::UpdateButtonState);
QObject::disconnect(current_playlist_->queue(), &Queue::SummaryTextChanged, ui_->summary, &QLabel::setText);
QObject::disconnect(current_playlist_, &Playlist::destroyed, this, &QueueView::PlaylistDestroyed);
2018-02-27 18:06:05 +01:00
}
current_playlist_ = playlist;
2021-01-26 16:48:04 +01:00
QObject::connect(current_playlist_->queue(), &Queue::rowsInserted, this, &QueueView::UpdateButtonState);
QObject::connect(current_playlist_->queue(), &Queue::rowsRemoved, this, &QueueView::UpdateButtonState);
QObject::connect(current_playlist_->queue(), &Queue::layoutChanged, this, &QueueView::UpdateButtonState);
QObject::connect(current_playlist_->queue(), &Queue::SummaryTextChanged, ui_->summary, &QLabel::setText);
QObject::connect(current_playlist_, &Playlist::destroyed, this, &QueueView::PlaylistDestroyed);
2018-02-27 18:06:05 +01:00
ui_->list->setModel(current_playlist_->queue());
2021-01-26 16:48:04 +01:00
QObject::connect(ui_->list->selectionModel(), &QItemSelectionModel::currentChanged, this, &QueueView::UpdateButtonState);
QObject::connect(ui_->list->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QueueView::UpdateButtonState);
2018-02-27 18:06:05 +01:00
QMetaObject::invokeMethod(current_playlist_->queue(), &Queue::UpdateSummaryText);
2018-10-21 15:13:48 +02:00
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;
2024-04-23 17:15:42 +02:00
for (const QModelIndex &idx : std::as_const(indexes)) {
2021-01-26 16:48:04 +01:00
current_playlist_->queue()->MoveUp(idx.row());
2018-02-27 18:06:05 +01:00
}
}
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
2021-08-23 21:21:08 +02:00
if (indexes.isEmpty() || indexes.last().row() == current_playlist_->queue()->rowCount() - 1) {
2018-02-27 18:06:05 +01:00
return;
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
2021-03-21 18:53:02 +01:00
for (int i = static_cast<int>(indexes.count() - 1); i >= 0; --i) {
2018-02-27 18:06:05 +01:00
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;
2024-04-23 17:15:42 +02:00
const QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
for (const QModelIndex &idx : indexes) {
2021-01-26 16:48:04 +01:00
if (idx.isValid()) row_list << idx.row();
2018-02-27 18:06:05 +01:00
}
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
if (ui_->list->selectionModel()->selectedRows().count() > 0) {
2018-02-27 18:06:05 +01:00
ui_->remove->setEnabled(true);
QModelIndex index_top = ui_->list->model()->index(0, 0);
QModelIndex index_bottom = ui_->list->model()->index(ui_->list->model()->rowCount() - 1, 0);
const QModelIndexList selected = ui_->list->selectionModel()->selectedIndexes();
bool all_selected = ui_->list->selectionModel()->selectedRows().count() == ui_->list->model()->rowCount();
ui_->move_up->setEnabled(!all_selected && !selected.contains(index_top));
ui_->move_down->setEnabled(!all_selected && !selected.contains(index_bottom));
2018-02-27 18:06:05 +01:00
}
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
}