strawberry-audio-player-win.../src/playlist/playlistundocommands.cpp

148 lines
3.9 KiB
C++
Raw Permalink 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
*/
#include "config.h"
2024-04-23 17:15:42 +02:00
#include <utility>
2020-02-08 15:03:11 +01:00
#include <QtGlobal>
#include <QList>
#include <QUndoStack>
2018-02-27 18:06:05 +01:00
#include "playlist.h"
#include "playlistitem.h"
#include "playlistundocommands.h"
2018-02-27 18:06:05 +01:00
namespace PlaylistUndoCommands {
2021-06-12 20:53:23 +02:00
Base::Base(Playlist *playlist) : QUndoCommand(nullptr), playlist_(playlist) {}
2018-02-27 18:06:05 +01:00
2023-02-18 14:09:27 +01:00
InsertItems::InsertItems(Playlist *playlist, const PlaylistItemPtrList &items, int pos, bool enqueue, bool enqueue_next)
2021-07-11 07:40:57 +02:00
: Base(playlist),
items_(items),
pos_(pos),
enqueue_(enqueue),
enqueue_next_(enqueue_next) {
2021-10-30 02:21:29 +02:00
setText(tr("add %n songs", "", static_cast<int>(items_.count())));
2021-07-11 07:40:57 +02:00
2018-02-27 18:06:05 +01:00
}
void InsertItems::redo() {
playlist_->InsertItemsWithoutUndo(items_, pos_, enqueue_, enqueue_next_);
2018-02-27 18:06:05 +01:00
}
void InsertItems::undo() {
2021-03-21 18:53:02 +01:00
const int start = pos_ == -1 ? static_cast<int>(playlist_->rowCount() - items_.count()) : pos_;
2021-10-30 02:21:29 +02:00
playlist_->RemoveItemsWithoutUndo(start, static_cast<int>(items_.count()));
2018-02-27 18:06:05 +01:00
}
bool InsertItems::UpdateItem(const PlaylistItemPtr &updated_item) {
for (int i = 0; i < items_.size(); i++) {
PlaylistItemPtr item = items_[i];
if (item->Metadata().url() == updated_item->Metadata().url()) {
items_[i] = updated_item;
return true;
}
}
return false;
}
RemoveItems::RemoveItems(Playlist *playlist, int pos, int count) : Base(playlist) {
setText(tr("remove %n songs", "", count));
ranges_ << Range(pos, count);
}
void RemoveItems::redo() {
2021-08-23 21:21:08 +02:00
for (int i = 0; i < ranges_.count(); ++i) {
ranges_[i].items_ = playlist_->RemoveItemsWithoutUndo(ranges_[i].pos_, ranges_[i].count_);
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
}
void RemoveItems::undo() {
2021-08-23 21:21:08 +02:00
for (int i = static_cast<int>(ranges_.count() - 1); i >= 0; --i) {
2018-02-27 18:06:05 +01:00
playlist_->InsertItemsWithoutUndo(ranges_[i].items_, ranges_[i].pos_);
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
}
bool RemoveItems::mergeWith(const QUndoCommand *other) {
2021-08-23 21:21:08 +02:00
2021-06-12 20:53:23 +02:00
const RemoveItems *remove_command = static_cast<const RemoveItems*>(other);
2018-02-27 18:06:05 +01:00
ranges_.append(remove_command->ranges_);
int sum = 0;
2024-04-23 17:15:42 +02:00
for (const Range &range : std::as_const(ranges_)) sum += range.count_;
2018-02-27 18:06:05 +01:00
setText(tr("remove %n songs", "", sum));
return true;
2021-08-23 21:21:08 +02:00
2018-02-27 18:06:05 +01:00
}
MoveItems::MoveItems(Playlist *playlist, const QList<int> &source_rows, int pos)
2021-07-11 07:40:57 +02:00
: Base(playlist),
source_rows_(source_rows),
pos_(pos) {
2021-10-30 02:21:29 +02:00
setText(tr("move %n songs", "", static_cast<int>(source_rows.count())));
2021-07-11 07:40:57 +02:00
2018-02-27 18:06:05 +01:00
}
void MoveItems::redo() {
playlist_->MoveItemsWithoutUndo(source_rows_, pos_);
}
void MoveItems::undo() {
playlist_->MoveItemsWithoutUndo(pos_, source_rows_);
}
2023-02-18 14:09:27 +01:00
ReOrderItems::ReOrderItems(Playlist *playlist, const PlaylistItemPtrList &new_items)
2018-02-27 18:06:05 +01:00
: Base(playlist), old_items_(playlist->items_), new_items_(new_items) {}
void ReOrderItems::undo() { playlist_->ReOrderWithoutUndo(old_items_); }
void ReOrderItems::redo() { playlist_->ReOrderWithoutUndo(new_items_); }
2023-02-18 14:09:27 +01:00
SortItems::SortItems(Playlist *playlist, int column, Qt::SortOrder order, const PlaylistItemPtrList &new_items)
2021-07-11 07:40:57 +02:00
: ReOrderItems(playlist, new_items) {
2019-09-15 20:27:32 +02:00
Q_UNUSED(column);
Q_UNUSED(order);
2018-02-27 18:06:05 +01:00
setText(tr("sort songs"));
2019-09-15 20:27:32 +02:00
2018-02-27 18:06:05 +01:00
}
2023-02-18 14:09:27 +01:00
ShuffleItems::ShuffleItems(Playlist *playlist, const PlaylistItemPtrList &new_items)
2021-07-11 07:40:57 +02:00
: ReOrderItems(playlist, new_items) {
2018-02-27 18:06:05 +01:00
setText(tr("shuffle songs"));
2021-07-11 07:40:57 +02:00
2018-02-27 18:06:05 +01:00
}
2021-06-20 23:55:02 +02:00
} // namespace PlaylistUndoCommands