Group "remove" undo commands in the playlist. Fixes issue #262

This commit is contained in:
David Sansome 2010-04-29 23:58:31 +00:00
parent 70565565c7
commit 0c9e69ed30
3 changed files with 38 additions and 17 deletions

View File

@ -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 ; i<ranges_.count() ; ++i)
ranges_[i].items_ = playlist_->RemoveItemsWithoutUndo(
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<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;
}

View File

@ -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<Range> ranges_;
};
class MoveItems : public Base {

View File

@ -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()));
}