Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/tiled/addremovemapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "map.h"
#include "mapobject.h"
#include "objectgroup.h"
#include "undocommands.h"

#include <QCoreApplication>

Expand Down Expand Up @@ -143,6 +144,37 @@ void AddMapObjects::redo()
mOwnsObjects = false;
}

QUndoCommand *AddMapObjects::clone(QUndoCommand *parent) const
{
auto *clone = new AddMapObjects(mDocument, mEntries, parent);
clone->setText(text());

// Transfer ownership of objects from original to clone
clone->mEntries = mEntries;
const_cast<AddMapObjects*>(this)->mOwnsObjects = false;

return clone;
}

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

if (!cloneChildren(other, this))
return false;

// Append entries from other command
for (const Entry &entry : o->mEntries)
mEntries.append(entry);

// Transfer ownership from other to this
const_cast<AddMapObjects*>(o)->mOwnsObjects = false;

return true;
}


RemoveMapObjects::RemoveMapObjects(Document *document,
MapObject *mapObject,
Expand Down Expand Up @@ -200,3 +232,34 @@ void RemoveMapObjects::redo()

mOwnsObjects = true;
}

QUndoCommand *RemoveMapObjects::clone(QUndoCommand *parent) const
{
auto *clone = new RemoveMapObjects(mDocument, objects(mEntries), parent);
clone->setText(text());

// Transfer ownership of objects from original to clone
clone->mEntries = mEntries;
const_cast<RemoveMapObjects*>(this)->mOwnsObjects = false;

return clone;
}

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

if (!cloneChildren(other, this))
return false;

// Append entries from other command
for (const Entry &entry : o->mEntries)
mEntries.append(entry);

// Transfer ownership from other to this
const_cast<RemoveMapObjects*>(o)->mOwnsObjects = false;

return true;
}
12 changes: 10 additions & 2 deletions src/tiled/addremovemapobject.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 <QVector>

Expand Down Expand Up @@ -69,7 +71,7 @@ class AddRemoveMapObjects : public QUndoCommand
/**
* Undo command that adds an object to a map.
*/
class AddMapObjects : public AddRemoveMapObjects
class AddMapObjects : public AddRemoveMapObjects, public ClonableUndoCommand
{
public:
AddMapObjects(Document *document,
Expand All @@ -83,12 +85,15 @@ class AddMapObjects : public AddRemoveMapObjects

void undo() override;
void redo() override;

QUndoCommand *clone(QUndoCommand *parent = nullptr) const override;
bool mergeWith(const QUndoCommand *other) override;
};

/**
* Undo command that removes one or more objects from a map.
*/
class RemoveMapObjects : public AddRemoveMapObjects
class RemoveMapObjects : public AddRemoveMapObjects, public ClonableUndoCommand
{
public:
RemoveMapObjects(Document *document,
Expand All @@ -101,6 +106,9 @@ class RemoveMapObjects : public AddRemoveMapObjects

void undo() override;
void redo() override;

QUndoCommand *clone(QUndoCommand *parent = nullptr) const override;
bool mergeWith(const QUndoCommand *other) override;
};

} // namespace Tiled
Loading