Debian Codegraph @

src/editor/objectmodelloader.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2011 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include <QDomDocument>
17 #include <QDomElement>
18 #include <QFile>
19 #include <QTextStream>
20
21 #include "../common/config.hpp"
22 #include "mainwindow.hpp"
23 #include "objectmodelloader.hpp"
24
25 ObjectModelLoader::ObjectModelLoader()
26 {
27 }
28
29 bool ObjectModelLoader::load(QString path)
30 {
31 QDomDocument doc;
32
33 QFile file(path);
34 if (!file.open(QIODevice::ReadOnly))
35 {
36 return false;
37 }
38
39 if (!doc.setContent(&file))
40 {
41 file.close();
42 return false;
43 }
44
45 file.close();
46
47 const auto root = doc.documentElement();
48 const auto version = root.attribute("version", Config::Editor::EDITOR_VERSION);
49
50 m_objects.clear();
51
52 auto node = root.firstChild();
53 while (!node.isNull())
54 {
55 const auto tag = node.toElement();
56 if (!tag.isNull())
57 {
58 // Parse an object tag
59 if (tag.nodeName() == "object")
60 {
61 ObjectModel newData;
62 newData.category = tag.attribute("category", "undefined");
63 newData.role = tag.attribute("role", "undefined");
64 newData.width = tag.attribute("width", "0").toUInt();
65 newData.height = tag.attribute("height", "0").toUInt();
66 auto imagePath = tag.attribute("imagePath", "undefined");
67
68 // The corresponding image is loaded
69 // from Config::DATA_PATH/model.imagePath.
70 // Check that it's available and load it.
71 imagePath = QString(Config::General::dataPath) + QDir::separator() + imagePath;
72
73 if (QFile::exists(imagePath))
74 {
75 newData.pixmap = QPixmap(imagePath);
76 m_objects << newData;
77 }
78 else
79 {
80 MainWindow::instance()->console("WARNING!!: " + imagePath + " cannot be read.");
81 }
82 }
83
84 node = node.nextSibling();
85 }
86 }
87
88 return true;
89 }
90
91 ObjectModelLoader::ObjectDataVector ObjectModelLoader::getObjectModelsByCategory(QString category) const
92 {
93 ObjectDataVector result;
94
95 for (auto && object : m_objects)
96 {
97 if (object.category == category)
98 {
99 result << object;
100 }
101 }
102
103 return result;
104 }
105
106 ObjectModel ObjectModelLoader::getObjectModelByRole(QString role) const
107 {
108 for (auto && object : m_objects)
109 {
110 if (object.role == role)
111 {
112 return object;
113 }
114 }
115
116 return {};
117 }
118
119 QString ObjectModelLoader::getCategoryByRole(QString role) const
120 {
121 for (auto && object : m_objects)
122 {
123 if (object.role == role)
124 {
125 return object.category;
126 }
127 }
128
129 return {};
130 }
131
132 ObjectModelLoader::ObjectDataVector ObjectModelLoader::objects() const
133 {
134 return m_objects;
135 }
136
137 QPixmap ObjectModelLoader::getPixmapByRole(QString role) const
138 {
139 for (auto && object : m_objects)
140 {
141 if (object.role == role)
142 {
143 return object.pixmap;
144 }
145 }
146
147 return {};
148 }