1 /*
2  * addremovemapobject.h
3  * Copyright 2009, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include <QUndoCommand>
24 #include <QVector>
25 
26 namespace Tiled {
27 
28 class MapObject;
29 class ObjectGroup;
30 
31 class Document;
32 
33 /**
34  * Abstract base class for AddMapObject and RemoveMapObject.
35  */
36 class AddRemoveMapObjects : public QUndoCommand
37 {
38 public:
39     struct Entry {
EntryEntry40         Entry() {}
41 
EntryEntry42         Entry(MapObject *mapObject, ObjectGroup *objectGroup)
43             : mapObject(mapObject)
44             , objectGroup(objectGroup)
45         {}
46 
47         MapObject *mapObject = nullptr;
48         ObjectGroup *objectGroup = nullptr;
49         int index = -1;
50     };
51 
52     AddRemoveMapObjects(Document *document,
53                         const QVector<Entry> &entries,
54                         bool ownObjects,
55                         QUndoCommand *parent = nullptr);
56     ~AddRemoveMapObjects();
57 
58     void releaseObjects();
59 
60     static QVector<Entry> entries(const QList<MapObject *> &objects);
61     static QList<MapObject*> objects(const QVector<Entry> &entries);
62 
63 protected:
64     Document *mDocument;
65     QVector<Entry> mEntries;
66     bool mOwnsObjects;
67 };
68 
69 /**
70  * Undo command that adds an object to a map.
71  */
72 class AddMapObjects : public AddRemoveMapObjects
73 {
74 public:
75     AddMapObjects(Document *document,
76                   ObjectGroup *objectGroup,
77                   MapObject *mapObject,
78                   QUndoCommand *parent = nullptr);
79 
80     AddMapObjects(Document *document,
81                   const QVector<Entry> &entries,
82                   QUndoCommand *parent = nullptr);
83 
84     void undo() override;
85     void redo() override;
86 };
87 
88 /**
89  * Undo command that removes one or more objects from a map.
90  */
91 class RemoveMapObjects : public AddRemoveMapObjects
92 {
93 public:
94     RemoveMapObjects(Document *document,
95                      MapObject *mapObject,
96                      QUndoCommand *parent = nullptr);
97 
98     RemoveMapObjects(Document *document,
99                      const QList<MapObject *> &mapObjects,
100                      QUndoCommand *parent = nullptr);
101 
102     void undo() override;
103     void redo() override;
104 };
105 
106 } // namespace Tiled
107