Skip to content

Commit

Permalink
Implement merging of undo commands for more string properties
Browse files Browse the repository at this point in the history
* MapObject::name (and other MapObject properties)
* WangColor::name
* WangSet::name
* Tileset::name

Part of issue #3103
  • Loading branch information
bjorn committed Jan 25, 2022
1 parent dd46296 commit 63c8af0
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/tiled/changemapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ ChangeMapObject::ChangeMapObject(Document *document,
}
}

bool ChangeMapObject::mergeWith(const QUndoCommand *other)
{
auto o = static_cast<const ChangeMapObject*>(other);
if (mDocument == o->mDocument && mMapObject == o->mMapObject && mProperty == o->mProperty) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
setObsolete(mMapObject->mapObjectProperty(mProperty) == mValue);
#endif
return true;
}
return false;
}

void ChangeMapObject::swap()
{
const auto value = std::exchange(mValue, mMapObject->mapObjectProperty(mProperty));
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/changemapobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "mapobject.h"
#include "tilelayer.h"
#include "undocommands.h"

#include <QUndoCommand>
#include <QVector>
Expand All @@ -48,6 +49,10 @@ class ChangeMapObject : public QUndoCommand
void undo() override { swap(); }
void redo() override { swap(); }

int id() const override { return Cmd_ChangeMapObject; }

bool mergeWith(const QUndoCommand *other) final;

private:
void swap();

Expand Down
10 changes: 10 additions & 0 deletions src/tiled/changewangcolordata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ void ChangeWangColorName::redo()
wangColorModel->setName(mWangColor, mNewName);
}

bool ChangeWangColorName::mergeWith(const QUndoCommand *other)
{
auto o = static_cast<const ChangeWangColorName*>(other);
if (mWangColor != o->mWangColor)
return false;

mNewName = o->mNewName;
return true;
}


ChangeWangColorImage::ChangeWangColorImage(TilesetDocument *tilesetDocument,
WangColor *wangColor,
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/changewangcolordata.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#pragma once

#include "undocommands.h"

#include <QUndoCommand>

#include <QColor>
Expand All @@ -40,6 +42,9 @@ class ChangeWangColorName : public QUndoCommand
void undo() override;
void redo() override;

int id() const override { return Cmd_ChangeWangColorName; }
bool mergeWith(const QUndoCommand *other) override;

private:
TilesetDocument *mTilesetDocument;
WangColor *mWangColor;
Expand Down
10 changes: 10 additions & 0 deletions src/tiled/changewangsetdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ void RenameWangSet::redo()
mTilesetDocument->wangSetModel()->setWangSetName(mWangSet, mNewName);
}

bool RenameWangSet::mergeWith(const QUndoCommand *other)
{
auto o = static_cast<const RenameWangSet*>(other);
if (mWangSet != o->mWangSet)
return false;

mNewName = o->mNewName;
return true;
}


ChangeWangSetType::ChangeWangSetType(TilesetDocument *tilesetDocument,
WangSet *wangSet,
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/changewangsetdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#pragma once

#include "undocommands.h"
#include "wangset.h"

#include <QUndoCommand>
Expand All @@ -41,6 +42,9 @@ class RenameWangSet : public QUndoCommand
void undo() override;
void redo() override;

int id() const override { return Cmd_ChangeWangSetName; }
bool mergeWith(const QUndoCommand *other) override;

private:
TilesetDocument *mTilesetDocument;
WangSet *mWangSet;
Expand Down
12 changes: 11 additions & 1 deletion src/tiled/tilesetchanges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ void RenameTileset::redo()
mTilesetDocument->setTilesetName(mNewName);
}

bool RenameTileset::mergeWith(const QUndoCommand *other)
{
auto o = static_cast<const RenameTileset*>(other);
if (mTilesetDocument != o->mTilesetDocument)
return false;

mNewName = o->mNewName;
return true;
}


ChangeTilesetTileOffset::ChangeTilesetTileOffset(TilesetDocument *tilesetDocument,
QPoint tileOffset)
Expand All @@ -69,7 +79,7 @@ void ChangeTilesetTileOffset::redo()

bool ChangeTilesetTileOffset::mergeWith(const QUndoCommand *other)
{
const ChangeTilesetTileOffset *o = static_cast<const ChangeTilesetTileOffset*>(other);
auto o = static_cast<const ChangeTilesetTileOffset*>(other);
if (mTilesetDocument != o->mTilesetDocument)
return false;

Expand Down
3 changes: 3 additions & 0 deletions src/tiled/tilesetchanges.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class RenameTileset : public QUndoCommand
void undo() override;
void redo() override;

int id() const override { return Cmd_ChangeTilesetName; }
bool mergeWith(const QUndoCommand *other) override;

private:
TilesetDocument *mTilesetDocument;
QString mOldName;
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/undocommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ enum UndoCommands {
Cmd_ChangeLayerParallaxFactor,
Cmd_ChangeLayerTintColor,
Cmd_ChangeLayerVisible,
Cmd_ChangeMapObject,
Cmd_ChangeSelectedArea,
Cmd_ChangeTileTerrain,
Cmd_ChangeTileWangId,
Cmd_ChangeTilesetName,
Cmd_ChangeTilesetTileOffset,
Cmd_ChangeWangColorName,
Cmd_ChangeWangSetName,
Cmd_EraseTiles,
Cmd_PaintTileLayer,
};
Expand Down

0 comments on commit 63c8af0

Please sign in to comment.