1 /*
2 * objecttemplate.cpp
3 * Copyright 2017, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4 * Copyright 2017, Mohamed Thabet <thabetx@gmail.com>
5 *
6 * This file is part of libtiled.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21 * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "objecttemplate.h"
31
32 #include "objecttemplateformat.h"
33
34 #include <QFileInfo>
35
36 namespace Tiled {
37
ObjectTemplate()38 ObjectTemplate::ObjectTemplate()
39 : ObjectTemplate(QString())
40 {
41 }
42
ObjectTemplate(const QString & fileName)43 ObjectTemplate::ObjectTemplate(const QString &fileName)
44 : Object(ObjectTemplateType)
45 , mFileName(fileName)
46 {
47 }
48
~ObjectTemplate()49 ObjectTemplate::~ObjectTemplate()
50 {
51 }
52
setObject(const MapObject * object)53 void ObjectTemplate::setObject(const MapObject *object)
54 {
55 Tileset *tileset = nullptr;
56
57 if (object) {
58 tileset = object->cell().tileset();
59 mObject.reset(object->clone());
60 mObject->markAsTemplateBase();
61 } else {
62 mObject.reset();
63 }
64
65 if (tileset)
66 mTileset = tileset->sharedPointer();
67 else
68 mTileset.reset();
69 }
70
setObject(std::unique_ptr<MapObject> object)71 void ObjectTemplate::setObject(std::unique_ptr<MapObject> object)
72 {
73 Q_ASSERT(object);
74 mObject = std::move(object);
75
76 Tileset *tileset = mObject->cell().tileset();
77 if (tileset)
78 mTileset = tileset->sharedPointer();
79 else
80 mTileset.reset();
81 }
82
save()83 bool ObjectTemplate::save()
84 {
85 auto format = findFileFormat<ObjectTemplateFormat>(mFormat);
86 if (!format)
87 return false;
88 if (mFileName.isEmpty())
89 return false;
90
91 const bool result = format->write(this, mFileName);
92
93 mLastSaved = QFileInfo(mFileName).lastModified();
94
95 return result;
96 }
97
98 } // namespace Tiled
99