Move queuemanager to fancytabbar
This commit is contained in:
454
src/queue/queue.cpp
Normal file
454
src/queue/queue.cpp
Normal file
@@ -0,0 +1,454 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <QObject>
|
||||
#include <QIODevice>
|
||||
#include <QDataStream>
|
||||
#include <QBuffer>
|
||||
#include <QFlags>
|
||||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QStringBuilder>
|
||||
#include <QMimeData>
|
||||
#include <QtAlgorithms>
|
||||
#include <QAbstractItemModel>
|
||||
#include <QAbstractProxyModel>
|
||||
#include <QPersistentModelIndex>
|
||||
|
||||
#include "core/utilities.h"
|
||||
#include "playlist/playlist.h"
|
||||
#include "queue.h"
|
||||
|
||||
using std::stable_sort;
|
||||
|
||||
const char *Queue::kRowsMimetype = "application/x-strawberry-queue-rows";
|
||||
|
||||
Queue::Queue(Playlist *parent) : QAbstractProxyModel(parent), playlist_(parent), total_length_ns_(0) {
|
||||
|
||||
connect(this, SIGNAL(ItemCountChanged(int)), SLOT(UpdateTotalLength()));
|
||||
connect(this, SIGNAL(TotalLengthChanged(quint64)), SLOT(UpdateSummaryText()));
|
||||
|
||||
UpdateSummaryText();
|
||||
|
||||
}
|
||||
|
||||
QModelIndex Queue::mapFromSource(const QModelIndex &source_index) const {
|
||||
|
||||
if (!source_index.isValid()) return QModelIndex();
|
||||
|
||||
const int source_row = source_index.row();
|
||||
for (int i = 0; i < source_indexes_.count(); ++i) {
|
||||
if (source_indexes_[i].row() == source_row)
|
||||
return index(i, source_index.column());
|
||||
}
|
||||
return QModelIndex();
|
||||
|
||||
}
|
||||
|
||||
bool Queue::ContainsSourceRow(int source_row) const {
|
||||
|
||||
for (int i = 0; i < source_indexes_.count(); ++i) {
|
||||
if (source_indexes_[i].row() == source_row) return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
QModelIndex Queue::mapToSource(const QModelIndex &proxy_index) const {
|
||||
|
||||
if (!proxy_index.isValid()) return QModelIndex();
|
||||
|
||||
return source_indexes_[proxy_index.row()];
|
||||
|
||||
}
|
||||
|
||||
void Queue::setSourceModel(QAbstractItemModel *source_model) {
|
||||
|
||||
if (sourceModel()) {
|
||||
disconnect(sourceModel(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(SourceDataChanged(QModelIndex, QModelIndex)));
|
||||
disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(SourceLayoutChanged()));
|
||||
disconnect(sourceModel(), SIGNAL(layoutChanged()), this, SLOT(SourceLayoutChanged()));
|
||||
}
|
||||
|
||||
QAbstractProxyModel::setSourceModel(source_model);
|
||||
|
||||
connect(sourceModel(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(SourceDataChanged(QModelIndex, QModelIndex)));
|
||||
connect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(SourceLayoutChanged()));
|
||||
connect(sourceModel(), SIGNAL(layoutChanged()), this, SLOT(SourceLayoutChanged()));
|
||||
|
||||
}
|
||||
|
||||
void Queue::SourceDataChanged(const QModelIndex &top_left, const QModelIndex &bottom_right) {
|
||||
|
||||
for (int row = top_left.row(); row <= bottom_right.row(); ++row) {
|
||||
QModelIndex proxy_index = mapFromSource(sourceModel()->index(row, 0));
|
||||
if (!proxy_index.isValid()) continue;
|
||||
|
||||
emit dataChanged(proxy_index, proxy_index);
|
||||
}
|
||||
emit ItemCountChanged(this->ItemCount());
|
||||
|
||||
}
|
||||
|
||||
void Queue::SourceLayoutChanged() {
|
||||
|
||||
for (int i = 0; i < source_indexes_.count(); ++i) {
|
||||
if (!source_indexes_[i].isValid()) {
|
||||
beginRemoveRows(QModelIndex(), i, i);
|
||||
source_indexes_.removeAt(i);
|
||||
endRemoveRows();
|
||||
--i;
|
||||
}
|
||||
}
|
||||
emit ItemCountChanged(this->ItemCount());
|
||||
|
||||
}
|
||||
|
||||
QModelIndex Queue::index(int row, int column, const QModelIndex &parent) const {
|
||||
return createIndex(row, column);
|
||||
}
|
||||
|
||||
QModelIndex Queue::parent(const QModelIndex &child) const {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
int Queue::rowCount(const QModelIndex &parent) const {
|
||||
if (parent.isValid()) return 0;
|
||||
return source_indexes_.count();
|
||||
}
|
||||
|
||||
int Queue::columnCount(const QModelIndex&) const { return 1; }
|
||||
|
||||
QVariant Queue::data(const QModelIndex &proxy_index, int role) const {
|
||||
|
||||
QModelIndex source_index = source_indexes_[proxy_index.row()];
|
||||
|
||||
switch (role) {
|
||||
case Playlist::Role_QueuePosition:
|
||||
return proxy_index.row();
|
||||
|
||||
case Qt::DisplayRole: {
|
||||
const QString artist = source_index.sibling(source_index.row(), Playlist::Column_Artist).data().toString();
|
||||
const QString title = source_index.sibling(source_index.row(), Playlist::Column_Title).data().toString();
|
||||
|
||||
if (artist.isEmpty()) return title;
|
||||
return QString(artist + " - " + title);
|
||||
}
|
||||
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Queue::ToggleTracks(const QModelIndexList &source_indexes) {
|
||||
|
||||
for (const QModelIndex &source_index : source_indexes) {
|
||||
QModelIndex proxy_index = mapFromSource(source_index);
|
||||
if (proxy_index.isValid()) {
|
||||
// Dequeue the track
|
||||
const int row = proxy_index.row();
|
||||
beginRemoveRows(QModelIndex(), row, row);
|
||||
source_indexes_.removeAt(row);
|
||||
endRemoveRows();
|
||||
}
|
||||
else {
|
||||
// Enqueue the track
|
||||
const int row = source_indexes_.count();
|
||||
beginInsertRows(QModelIndex(), row, row);
|
||||
source_indexes_ << QPersistentModelIndex(source_index);
|
||||
endInsertRows();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Queue::InsertFirst(const QModelIndexList &source_indexes) {
|
||||
|
||||
for (const QModelIndex &source_index : source_indexes) {
|
||||
QModelIndex proxy_index = mapFromSource(source_index);
|
||||
if (proxy_index.isValid()) {
|
||||
// Already in the queue, so remove it to be reinserted later
|
||||
const int row = proxy_index.row();
|
||||
beginRemoveRows(QModelIndex(), row, row);
|
||||
source_indexes_.removeAt(row);
|
||||
endRemoveRows();
|
||||
}
|
||||
}
|
||||
|
||||
const int rows = source_indexes.count();
|
||||
// Enqueue the tracks at the beginning
|
||||
beginInsertRows(QModelIndex(), 0, rows - 1);
|
||||
int offset = 0;
|
||||
for (const QModelIndex& source_index : source_indexes) {
|
||||
source_indexes_.insert(offset, QPersistentModelIndex(source_index));
|
||||
offset++;
|
||||
}
|
||||
endInsertRows();
|
||||
|
||||
}
|
||||
|
||||
int Queue::PositionOf(const QModelIndex &source_index) const {
|
||||
return mapFromSource(source_index).row();
|
||||
}
|
||||
|
||||
bool Queue::is_empty() const { return source_indexes_.isEmpty(); }
|
||||
|
||||
int Queue::ItemCount() const { return source_indexes_.length(); }
|
||||
|
||||
quint64 Queue::GetTotalLength() const { return total_length_ns_; }
|
||||
|
||||
void Queue::UpdateTotalLength() {
|
||||
|
||||
quint64 total = 0;
|
||||
|
||||
for (QPersistentModelIndex row : source_indexes_) {
|
||||
int id = row.row();
|
||||
|
||||
Q_ASSERT(playlist_->has_item_at(id));
|
||||
|
||||
quint64 length = playlist_->item_at(id)->Metadata().length_nanosec();
|
||||
if (length > 0) total += length;
|
||||
}
|
||||
|
||||
total_length_ns_ = total;
|
||||
|
||||
emit TotalLengthChanged(total);
|
||||
|
||||
}
|
||||
|
||||
void Queue::UpdateSummaryText() {
|
||||
|
||||
QString summary;
|
||||
int tracks = this->ItemCount();
|
||||
quint64 nanoseconds = this->GetTotalLength();
|
||||
|
||||
summary += tracks == 1 ? tr("1 track") : tr("%1 tracks").arg(tracks);
|
||||
|
||||
if (nanoseconds)
|
||||
summary += " - [ " + Utilities::WordyTimeNanosec(nanoseconds) + " ]";
|
||||
|
||||
emit SummaryTextChanged(summary);
|
||||
|
||||
}
|
||||
|
||||
void Queue::Clear() {
|
||||
|
||||
if (source_indexes_.isEmpty()) return;
|
||||
|
||||
beginRemoveRows(QModelIndex(), 0, source_indexes_.count() - 1);
|
||||
source_indexes_.clear();
|
||||
endRemoveRows();
|
||||
|
||||
}
|
||||
|
||||
void Queue::Move(const QList<int> &proxy_rows, int pos) {
|
||||
|
||||
layoutAboutToBeChanged();
|
||||
QList<QPersistentModelIndex> moved_items;
|
||||
|
||||
// Take the items out of the list first, keeping track of whether the insertion point changes
|
||||
int offset = 0;
|
||||
for (int row : proxy_rows) {
|
||||
moved_items << source_indexes_.takeAt(row - offset);
|
||||
if (pos != -1 && pos >= row) pos--;
|
||||
offset++;
|
||||
}
|
||||
|
||||
// Put the items back in
|
||||
const int start = pos == -1 ? source_indexes_.count() : pos;
|
||||
for (int i = start; i < start + moved_items.count(); ++i) {
|
||||
source_indexes_.insert(i, moved_items[i - start]);
|
||||
}
|
||||
|
||||
// Update persistent indexes
|
||||
for (const QModelIndex &pidx : persistentIndexList()) {
|
||||
const int dest_offset = proxy_rows.indexOf(pidx.row());
|
||||
if (dest_offset != -1) {
|
||||
// This index was moved
|
||||
changePersistentIndex(pidx, index(start + dest_offset, pidx.column(), QModelIndex()));
|
||||
}
|
||||
else {
|
||||
int d = 0;
|
||||
for (int row : proxy_rows) {
|
||||
if (pidx.row() > row) d--;
|
||||
}
|
||||
if (pidx.row() + d >= start) d += proxy_rows.count();
|
||||
|
||||
changePersistentIndex(pidx, index(pidx.row() + d, pidx.column(), QModelIndex()));
|
||||
}
|
||||
}
|
||||
|
||||
layoutChanged();
|
||||
|
||||
}
|
||||
|
||||
void Queue::MoveUp(int row) {
|
||||
Move(QList<int>() << row, row - 1);
|
||||
}
|
||||
|
||||
void Queue::MoveDown(int row) {
|
||||
Move(QList<int>() << row, row + 2);
|
||||
}
|
||||
|
||||
QStringList Queue::mimeTypes() const {
|
||||
return QStringList() << kRowsMimetype << Playlist::kRowsMimetype;
|
||||
}
|
||||
|
||||
Qt::DropActions Queue::supportedDropActions() const {
|
||||
return Qt::MoveAction | Qt::CopyAction | Qt::LinkAction;
|
||||
}
|
||||
|
||||
QMimeData *Queue::mimeData(const QModelIndexList &indexes) const {
|
||||
|
||||
QMimeData *data = new QMimeData;
|
||||
|
||||
QList<int> rows;
|
||||
for (const QModelIndex &index : indexes) {
|
||||
if (index.column() != 0) continue;
|
||||
|
||||
rows << index.row();
|
||||
}
|
||||
|
||||
QBuffer buf;
|
||||
buf.open(QIODevice::WriteOnly);
|
||||
QDataStream stream(&buf);
|
||||
stream << rows;
|
||||
buf.close();
|
||||
|
||||
data->setData(kRowsMimetype, buf.data());
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
bool Queue::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int, const QModelIndex&) {
|
||||
|
||||
if (action == Qt::IgnoreAction)
|
||||
return false;
|
||||
|
||||
if (data->hasFormat(kRowsMimetype)) {
|
||||
// Dragged from the queue
|
||||
|
||||
QList<int> proxy_rows;
|
||||
QDataStream stream(data->data(kRowsMimetype));
|
||||
stream >> proxy_rows;
|
||||
|
||||
// Make sure we take them in order
|
||||
std::stable_sort(proxy_rows.begin(), proxy_rows.end());
|
||||
|
||||
Move(proxy_rows, row);
|
||||
}
|
||||
else if (data->hasFormat(Playlist::kRowsMimetype)) {
|
||||
// Dragged from the playlist
|
||||
|
||||
Playlist *playlist = nullptr;
|
||||
QList<int> source_rows;
|
||||
QDataStream stream(data->data(Playlist::kRowsMimetype));
|
||||
stream.readRawData(reinterpret_cast<char*>(&playlist), sizeof(playlist));
|
||||
stream >> source_rows;
|
||||
|
||||
QModelIndexList source_indexes;
|
||||
for (int source_row : source_rows) {
|
||||
const QModelIndex source_index = sourceModel()->index(source_row, 0);
|
||||
const QModelIndex proxy_index = mapFromSource(source_index);
|
||||
if (proxy_index.isValid()) {
|
||||
// This row was already in the queue, so no need to add it again
|
||||
continue;
|
||||
}
|
||||
|
||||
source_indexes << source_index;
|
||||
}
|
||||
|
||||
if (!source_indexes.isEmpty()) {
|
||||
const int insert_point = row == -1 ? source_indexes_.count() : row;
|
||||
beginInsertRows(QModelIndex(), insert_point, insert_point + source_indexes.count() - 1);
|
||||
for (int i = 0 ; i < source_indexes.count() ; ++i) {
|
||||
source_indexes_.insert(insert_point + i, source_indexes[i]);
|
||||
}
|
||||
endInsertRows();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
Qt::ItemFlags Queue::flags(const QModelIndex &index) const {
|
||||
|
||||
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
||||
|
||||
if (index.isValid())
|
||||
flags |= Qt::ItemIsDragEnabled;
|
||||
else
|
||||
flags |= Qt::ItemIsDropEnabled;
|
||||
|
||||
return flags;
|
||||
|
||||
}
|
||||
|
||||
int Queue::PeekNext() const {
|
||||
if (source_indexes_.isEmpty()) return -1;
|
||||
return source_indexes_.first().row();
|
||||
}
|
||||
|
||||
int Queue::TakeNext() {
|
||||
|
||||
if (source_indexes_.isEmpty()) return -1;
|
||||
|
||||
beginRemoveRows(QModelIndex(), 0, 0);
|
||||
int ret = source_indexes_.takeFirst().row();
|
||||
endRemoveRows();
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
QVariant Queue::headerData(int section, Qt::Orientation orientation, int role) const {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
void Queue::Remove(QList<int> &proxy_rows) {
|
||||
|
||||
// Order the rows
|
||||
std::stable_sort(proxy_rows.begin(), proxy_rows.end());
|
||||
|
||||
// Reflects immediately changes in the playlist
|
||||
layoutAboutToBeChanged();
|
||||
|
||||
int removed_rows = 0;
|
||||
for (int row : proxy_rows) {
|
||||
// After the first row, the row number needs to be updated
|
||||
const int real_row = row - removed_rows;
|
||||
beginRemoveRows(QModelIndex(), real_row, real_row);
|
||||
source_indexes_.removeAt(real_row);
|
||||
endRemoveRows();
|
||||
removed_rows++;
|
||||
}
|
||||
|
||||
layoutChanged();
|
||||
|
||||
}
|
||||
103
src/queue/queue.h
Normal file
103
src/queue/queue.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QUEUE_H
|
||||
#define QUEUE_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractItemModel>
|
||||
#include <QAbstractProxyModel>
|
||||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QMimeData>
|
||||
|
||||
#include "playlist/playlist.h"
|
||||
|
||||
class Queue : public QAbstractProxyModel {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Queue(Playlist *parent = nullptr);
|
||||
|
||||
static const char *kRowsMimetype;
|
||||
|
||||
// Query the queue
|
||||
bool is_empty() const;
|
||||
int PositionOf(const QModelIndex &source_index) const;
|
||||
bool ContainsSourceRow(int source_row) const;
|
||||
int PeekNext() const;
|
||||
int ItemCount() const;
|
||||
quint64 GetTotalLength() const;
|
||||
|
||||
// Modify the queue
|
||||
int TakeNext();
|
||||
void ToggleTracks(const QModelIndexList &source_indexes);
|
||||
void InsertFirst(const QModelIndexList &source_indexes);
|
||||
void Clear();
|
||||
void Move(const QList<int> &proxy_rows, int pos);
|
||||
void MoveUp(int row);
|
||||
void MoveDown(int row);
|
||||
void Remove(QList<int> &proxy_rows);
|
||||
|
||||
// QAbstractProxyModel
|
||||
void setSourceModel(QAbstractItemModel *source_model);
|
||||
QModelIndex mapFromSource(const QModelIndex &source_index) const;
|
||||
QModelIndex mapToSource(const QModelIndex &proxy_index) const;
|
||||
|
||||
// QAbstractItemModel
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||
QModelIndex parent(const QModelIndex &child) const;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
||||
QVariant data(const QModelIndex &proxy_index, int role) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
||||
QStringList mimeTypes() const;
|
||||
Qt::DropActions supportedDropActions() const;
|
||||
QMimeData *mimeData(const QModelIndexList &indexes) const;
|
||||
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const;
|
||||
|
||||
public slots:
|
||||
void UpdateSummaryText();
|
||||
|
||||
signals:
|
||||
void TotalLengthChanged(const quint64 length);
|
||||
void ItemCountChanged(const int count);
|
||||
void SummaryTextChanged(const QString& message);
|
||||
|
||||
private slots:
|
||||
void SourceDataChanged(const QModelIndex &top_left, const QModelIndex &bottom_right);
|
||||
void SourceLayoutChanged();
|
||||
void UpdateTotalLength();
|
||||
|
||||
private:
|
||||
QList<QPersistentModelIndex> source_indexes_;
|
||||
const Playlist *playlist_;
|
||||
quint64 total_length_ns_;
|
||||
|
||||
};
|
||||
|
||||
#endif // QUEUE_H
|
||||
176
src/queue/queueview.cpp
Normal file
176
src/queue/queueview.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include <QWidget>
|
||||
#include <QAbstractItemModel>
|
||||
#include <QItemSelectionModel>
|
||||
#include <QKeySequence>
|
||||
#include <QList>
|
||||
#include <QPushButton>
|
||||
#include <QShortcut>
|
||||
#include <QTreeView>
|
||||
#include <QtAlgorithms>
|
||||
#include <QTimer>
|
||||
|
||||
#include "core/iconloader.h"
|
||||
#include "playlist/playlist.h"
|
||||
#include "playlist/playlistdelegates.h"
|
||||
#include "playlist/playlistmanager.h"
|
||||
#include "queue.h"
|
||||
#include "queueview.h"
|
||||
#include "ui_queueview.h"
|
||||
|
||||
using std::stable_sort;
|
||||
|
||||
QueueView::QueueView(QWidget *parent)
|
||||
: QWidget(parent),
|
||||
ui_(new Ui_QueueView),
|
||||
playlists_(nullptr),
|
||||
current_playlist_(nullptr) {
|
||||
|
||||
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()));
|
||||
|
||||
}
|
||||
|
||||
QueueView::~QueueView() {
|
||||
delete ui_;
|
||||
}
|
||||
|
||||
void QueueView::SetPlaylistManager(PlaylistManager *manager) {
|
||||
|
||||
playlists_ = manager;
|
||||
|
||||
connect(playlists_, SIGNAL(CurrentChanged(Playlist*)), SLOT(CurrentPlaylistChanged(Playlist*)));
|
||||
CurrentPlaylistChanged(playlists_->current());
|
||||
|
||||
}
|
||||
|
||||
void QueueView::CurrentPlaylistChanged(Playlist *playlist) {
|
||||
|
||||
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()));
|
||||
disconnect(current_playlist_->queue(), SIGNAL(layoutChanged()), this, SLOT(UpdateButtonState()));
|
||||
disconnect(current_playlist_->queue(), SIGNAL(SummaryTextChanged(QString)), ui_->summary, SLOT(setText(QString)));
|
||||
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()));
|
||||
connect(current_playlist_->queue(), SIGNAL(SummaryTextChanged(QString)), ui_->summary, SLOT(setText(QString)));
|
||||
connect(current_playlist_, SIGNAL(destroyed()), this, SLOT(PlaylistDestroyed()));
|
||||
|
||||
ui_->list->setModel(current_playlist_->queue());
|
||||
|
||||
connect(ui_->list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(UpdateButtonState()));
|
||||
|
||||
QTimer::singleShot(0, current_playlist_->queue(), SLOT(UpdateSummaryText()));
|
||||
|
||||
}
|
||||
|
||||
void QueueView::MoveUp() {
|
||||
|
||||
QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
|
||||
std::stable_sort(indexes.begin(), indexes.end());
|
||||
|
||||
if (indexes.isEmpty() || indexes.first().row() == 0) return;
|
||||
|
||||
for (const QModelIndex &index : indexes) {
|
||||
current_playlist_->queue()->MoveUp(index.row());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void QueueView::MoveDown() {
|
||||
|
||||
QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
|
||||
std::stable_sort(indexes.begin(), indexes.end());
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void QueueView::Clear() {
|
||||
current_playlist_->queue()->Clear();
|
||||
}
|
||||
|
||||
void QueueView::Remove() {
|
||||
|
||||
// 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);
|
||||
|
||||
}
|
||||
|
||||
void QueueView::UpdateButtonState() {
|
||||
|
||||
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());
|
||||
|
||||
}
|
||||
|
||||
void QueueView::PlaylistDestroyed() {
|
||||
current_playlist_ = nullptr;
|
||||
// We'll get another CurrentPlaylistChanged() soon
|
||||
}
|
||||
|
||||
61
src/queue/queueview.h
Normal file
61
src/queue/queueview.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QUEUEVIEW_H
|
||||
#define QUEUEVIEW_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
#include <QDialog>
|
||||
#include <QString>
|
||||
|
||||
class Playlist;
|
||||
class PlaylistManager;
|
||||
class Ui_QueueView;
|
||||
|
||||
class QueueView : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QueueView(QWidget *parent = nullptr);
|
||||
~QueueView();
|
||||
|
||||
void SetPlaylistManager(PlaylistManager *manager);
|
||||
|
||||
private slots:
|
||||
void CurrentPlaylistChanged(Playlist *playlist);
|
||||
void PlaylistDestroyed();
|
||||
void UpdateButtonState();
|
||||
|
||||
void MoveUp();
|
||||
void MoveDown();
|
||||
void Remove();
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
Ui_QueueView *ui_;
|
||||
|
||||
PlaylistManager *playlists_;
|
||||
Playlist *current_playlist_;
|
||||
};
|
||||
|
||||
#endif // QUEUEVIEW_H
|
||||
175
src/queue/queueview.ui
Normal file
175
src/queue/queueview.ui
Normal file
@@ -0,0 +1,175 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>QueueView</class>
|
||||
<widget class="QWidget" name="QueueView">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>582</width>
|
||||
<height>363</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>QueueView</string>
|
||||
</property>
|
||||
<property name="windowIcon">
|
||||
<iconset>
|
||||
<normaloff>:/icons/64x64/strawberry.png</normaloff>:/icons/64x64/strawberry.png</iconset>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="layout_queueview">
|
||||
<property name="spacing">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<property name="spacing">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QToolButton" name="move_down">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Move down</string>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
<width>16</width>
|
||||
<height>16</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+↑</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="move_up">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Move up</string>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
<width>16</width>
|
||||
<height>16</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+↓</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="remove">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Remove</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="clear">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Clear</string>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
<width>16</width>
|
||||
<height>16</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+K</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="spacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="summary">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTreeView" name="list">
|
||||
<property name="acceptDrops">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="showDropIndicator" stdset="0">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="dragEnabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="dragDropMode">
|
||||
<enum>QAbstractItemView::DragDrop</enum>
|
||||
</property>
|
||||
<property name="defaultDropAction">
|
||||
<enum>Qt::MoveAction</enum>
|
||||
</property>
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="selectionMode">
|
||||
<enum>QAbstractItemView::ExtendedSelection</enum>
|
||||
</property>
|
||||
<property name="selectionBehavior">
|
||||
<enum>QAbstractItemView::SelectRows</enum>
|
||||
</property>
|
||||
<property name="rootIsDecorated">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<attribute name="headerVisible">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user