Skip to content

Commit

Permalink
Merge "Set Property" undo commands if they affect the same property
Browse files Browse the repository at this point in the history
Closes #3103
  • Loading branch information
bjorn committed Jan 26, 2022
1 parent 821e849 commit 316ceb4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/tiled/changeproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ void SetProperty::redo()
mDocument->setProperty(obj, mName, mValue);
}

bool SetProperty::mergeWith(const QUndoCommand *other)
{
// If the same property is changed of the same layer, the commands can
// be trivially merged. The value is already changed on the layer, and
// the old value already remembered on this undo command.
auto o = static_cast<const SetProperty*>(other);
if (mDocument == o->mDocument && mName == o->mName && mObjects == o->mObjects) {
mValue = o->mValue;

#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
setObsolete(std::all_of(mProperties.cbegin(), mProperties.cend(),
[this] (const ObjectProperty &p) {
return p.existed && p.previousValue == mValue;
}));
#endif

return true;
}
return false;
}


RemoveProperty::RemoveProperty(Document *document,
const QList<Object*> &objects,
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/changeproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include "object.h"
#include "undocommands.h"

#include <QString>
#include <QUndoCommand>
Expand Down Expand Up @@ -79,6 +80,10 @@ class SetProperty : public QUndoCommand
void undo() override;
void redo() override;

int id() const override { return Cmd_SetProperty; }

bool mergeWith(const QUndoCommand *other) final;

private:
struct ObjectProperty {
QVariant previousValue;
Expand Down
1 change: 1 addition & 0 deletions src/tiled/undocommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ enum UndoCommands {
Cmd_ChangeWangSetName,
Cmd_EraseTiles,
Cmd_PaintTileLayer,
Cmd_SetProperty,
};

/**
Expand Down

0 comments on commit 316ceb4

Please sign in to comment.