diff --git a/src/playlistundocommands.cpp b/src/playlistundocommands.cpp index 2e50f6d2b..effcad965 100644 --- a/src/playlistundocommands.cpp +++ b/src/playlistundocommands.cpp @@ -46,19 +46,34 @@ void InsertItems::undo() { RemoveItems::RemoveItems(Playlist *playlist, int pos, int count) - : Base(playlist), - pos_(pos), - count_(count) + : Base(playlist) { - setText(tr("remove %n songs", "", count_)); + setText(tr("remove %n songs", "", count)); + + ranges_ << Range(pos, count); } void RemoveItems::redo() { - items_ = playlist_->RemoveItemsWithoutUndo(pos_, count_); + for (int i=0 ; iRemoveItemsWithoutUndo( + ranges_[i].pos_, ranges_[i].count_); } 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(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; } diff --git a/src/playlistundocommands.h b/src/playlistundocommands.h index ff671d3a7..c3741b105 100644 --- a/src/playlistundocommands.h +++ b/src/playlistundocommands.h @@ -25,6 +25,10 @@ class Playlist; namespace PlaylistUndoCommands { + enum Types { + Type_RemoveItems = 0, + }; + class Base : public QUndoCommand { Q_DECLARE_TR_FUNCTIONS(PlaylistUndoCommands); @@ -51,13 +55,21 @@ namespace PlaylistUndoCommands { public: RemoveItems(Playlist* playlist, int pos, int count); + int id() const { return Type_RemoveItems; } + void undo(); void redo(); + bool mergeWith(const QUndoCommand *other); private: - int pos_; - int count_; - PlaylistItemList items_; + struct Range { + Range(int pos, int count) : pos_(pos), count_(count) {} + int pos_; + int count_; + PlaylistItemList items_; + }; + + QList ranges_; }; class MoveItems : public Base { diff --git a/tests/playlist_test.cpp b/tests/playlist_test.cpp index e7ddc4e50..16780a4cd 100644 --- a/tests/playlist_test.cpp +++ b/tests/playlist_test.cpp @@ -344,15 +344,9 @@ TEST_F(PlaylistTest, UndoMultiRemove) { ASSERT_EQ(0, playlist_.rowCount(QModelIndex())); - // Undo removing 2 items + // Undo removing all 3 items ASSERT_TRUE(playlist_.undo_stack()->canUndo()); - EXPECT_EQ("remove 2 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()); + EXPECT_EQ("remove 3 songs", playlist_.undo_stack()->undoText()); playlist_.undo_stack()->undo(); ASSERT_EQ(3, playlist_.rowCount(QModelIndex())); }