mirror of
https://github.com/clementine-player/Clementine
synced 2025-02-01 11:56:45 +01:00
Group "remove" undo commands in the playlist. Fixes issue #262
This commit is contained in:
parent
70565565c7
commit
0c9e69ed30
@ -46,19 +46,34 @@ void InsertItems::undo() {
|
|||||||
|
|
||||||
|
|
||||||
RemoveItems::RemoveItems(Playlist *playlist, int pos, int count)
|
RemoveItems::RemoveItems(Playlist *playlist, int pos, int count)
|
||||||
: Base(playlist),
|
: Base(playlist)
|
||||||
pos_(pos),
|
|
||||||
count_(count)
|
|
||||||
{
|
{
|
||||||
setText(tr("remove %n songs", "", count_));
|
setText(tr("remove %n songs", "", count));
|
||||||
|
|
||||||
|
ranges_ << Range(pos, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoveItems::redo() {
|
void RemoveItems::redo() {
|
||||||
items_ = playlist_->RemoveItemsWithoutUndo(pos_, count_);
|
for (int i=0 ; i<ranges_.count() ; ++i)
|
||||||
|
ranges_[i].items_ = playlist_->RemoveItemsWithoutUndo(
|
||||||
|
ranges_[i].pos_, ranges_[i].count_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoveItems::undo() {
|
void RemoveItems::undo() {
|
||||||
playlist_->InsertItemsWithoutUndo(items_, pos_);
|
for (int i=ranges_.count()-1 ; i>=0 ; --i)
|
||||||
|
playlist_->InsertItemsWithoutUndo(ranges_[i].items_, ranges_[i].pos_);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RemoveItems::mergeWith(const QUndoCommand *other) {
|
||||||
|
const RemoveItems* remove_command = static_cast<const RemoveItems*>(other);
|
||||||
|
ranges_.append(remove_command->ranges_);
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
foreach (const Range& range, ranges_)
|
||||||
|
sum += range.count_;
|
||||||
|
setText(tr("remove %n songs", "", sum));
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,6 +25,10 @@
|
|||||||
class Playlist;
|
class Playlist;
|
||||||
|
|
||||||
namespace PlaylistUndoCommands {
|
namespace PlaylistUndoCommands {
|
||||||
|
enum Types {
|
||||||
|
Type_RemoveItems = 0,
|
||||||
|
};
|
||||||
|
|
||||||
class Base : public QUndoCommand {
|
class Base : public QUndoCommand {
|
||||||
Q_DECLARE_TR_FUNCTIONS(PlaylistUndoCommands);
|
Q_DECLARE_TR_FUNCTIONS(PlaylistUndoCommands);
|
||||||
|
|
||||||
@ -51,15 +55,23 @@ namespace PlaylistUndoCommands {
|
|||||||
public:
|
public:
|
||||||
RemoveItems(Playlist* playlist, int pos, int count);
|
RemoveItems(Playlist* playlist, int pos, int count);
|
||||||
|
|
||||||
|
int id() const { return Type_RemoveItems; }
|
||||||
|
|
||||||
void undo();
|
void undo();
|
||||||
void redo();
|
void redo();
|
||||||
|
bool mergeWith(const QUndoCommand *other);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct Range {
|
||||||
|
Range(int pos, int count) : pos_(pos), count_(count) {}
|
||||||
int pos_;
|
int pos_;
|
||||||
int count_;
|
int count_;
|
||||||
PlaylistItemList items_;
|
PlaylistItemList items_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
QList<Range> ranges_;
|
||||||
|
};
|
||||||
|
|
||||||
class MoveItems : public Base {
|
class MoveItems : public Base {
|
||||||
public:
|
public:
|
||||||
MoveItems(Playlist* playlist, const QList<int>& source_rows, int pos);
|
MoveItems(Playlist* playlist, const QList<int>& source_rows, int pos);
|
||||||
|
@ -344,15 +344,9 @@ TEST_F(PlaylistTest, UndoMultiRemove) {
|
|||||||
|
|
||||||
ASSERT_EQ(0, playlist_.rowCount(QModelIndex()));
|
ASSERT_EQ(0, playlist_.rowCount(QModelIndex()));
|
||||||
|
|
||||||
// Undo removing 2 items
|
// Undo removing all 3 items
|
||||||
ASSERT_TRUE(playlist_.undo_stack()->canUndo());
|
ASSERT_TRUE(playlist_.undo_stack()->canUndo());
|
||||||
EXPECT_EQ("remove 2 songs", playlist_.undo_stack()->undoText());
|
EXPECT_EQ("remove 3 songs", playlist_.undo_stack()->undoText());
|
||||||
playlist_.undo_stack()->undo();
|
|
||||||
ASSERT_EQ(2, playlist_.rowCount(QModelIndex()));
|
|
||||||
|
|
||||||
// Undo removing 1 item
|
|
||||||
ASSERT_TRUE(playlist_.undo_stack()->canUndo());
|
|
||||||
EXPECT_EQ("remove 1 songs", playlist_.undo_stack()->undoText());
|
|
||||||
playlist_.undo_stack()->undo();
|
playlist_.undo_stack()->undo();
|
||||||
ASSERT_EQ(3, playlist_.rowCount(QModelIndex()));
|
ASSERT_EQ(3, playlist_.rowCount(QModelIndex()));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user