1 /*
2  * Copyright (C) 2014-2018 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus Quest Editor 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  *
9  * Solarus Quest Editor is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "editor_exception.h"
18 #include "quest.h"
19 #include "quest_properties.h"
20 #include "size.h"
21 
22 namespace SolarusEditor {
23 
24 /**
25  * @brief Creates quest properties for the specified quest.
26  * @param quest The quest.
27  */
QuestProperties(Quest & quest)28 QuestProperties::QuestProperties(Quest& quest) :
29   quest(quest) {
30 
31   connect(&quest, SIGNAL(root_path_changed(const QString&)),
32           this, SLOT(reload()));
33   reload();
34 }
35 
36 /**
37  * @brief Reads quest.dat into this object.
38  */
reload()39 void QuestProperties::reload() {
40 
41   if (!quest.is_valid() || !quest.exists()) {
42     return;
43   }
44 
45   QString file_name = quest.get_properties_path();
46   if (!properties.import_from_file(file_name.toLocal8Bit().toStdString())) {
47     throw EditorException(tr("Cannot open file '%1'").arg(file_name));
48   }
49 }
50 
51 /**
52  * @brief Saves the properties to the quest.dat file of the quest.
53  * @throws EditorException If the save operation failed.
54  */
save() const55 void QuestProperties::save() const {
56 
57   if (!quest.is_valid() || !quest.exists()) {
58     throw EditorException(tr("No quest"));
59   }
60 
61   QString file_name = quest.get_properties_path();
62   if (!properties.export_to_file(file_name.toLocal8Bit().toStdString())) {
63     throw EditorException(tr("Cannot write file '%1'").arg(file_name));
64   }
65 }
66 
67 /**
68  * @brief Returns the Solarus compatibility version of the quest.
69  * @return The Solarus version.
70  */
get_solarus_version() const71 QString QuestProperties::get_solarus_version() const {
72 
73   return QString::fromStdString(properties.get_solarus_version());
74 }
75 
76 /**
77  * @brief Returns the Solarus compatibility version of the quest, without patch number.
78  * @return The Solarus version without patch number.
79  */
get_solarus_version_without_patch() const80 QString QuestProperties::get_solarus_version_without_patch() const {
81 
82   QString version = get_solarus_version();
83 
84   if (version.isEmpty()) {
85     return version;
86   }
87 
88   int dot_index_1 = version.indexOf('.');
89   int dot_index_2 = version.indexOf('.', dot_index_1 + 1);
90   if (dot_index_2 != -1) {
91     // Remove the patch version (it does not break compatibility).
92     version = version.section('.', 0, -2);
93   }
94   return version;
95 }
96 
97 /**
98  * @brief Returns the write directory of the quest.
99  * @return The write directory.
100  */
get_write_dir() const101 QString QuestProperties::get_write_dir() const {
102 
103   return QString::fromStdString(properties.get_quest_write_dir());
104 }
105 
106 /**
107  * @brief Changes the write directory of the quest.
108  * @param write_dir The new write directory.
109  */
set_write_dir(const QString & write_dir)110 void QuestProperties::set_write_dir(const QString& write_dir) {
111 
112   QString old_write_dir = get_write_dir();
113   if (write_dir == old_write_dir) {
114     return;
115   }
116 
117   properties.set_quest_write_dir(write_dir.toStdString());
118   emit write_dir_changed(write_dir);
119 }
120 
121 /**
122  * @brief Returns the title of the quest.
123  * @return The title.
124  */
get_title() const125 QString QuestProperties::get_title() const {
126 
127   return QString::fromStdString(properties.get_title());
128 }
129 
130 /**
131  * @brief Changes the title of the quest.
132  * @param title The new title.
133  */
set_title(const QString & title)134 void QuestProperties::set_title(const QString& title) {
135 
136   QString old_title = get_title();
137   if (title == old_title) {
138     return;
139   }
140 
141   properties.set_title(title.toStdString());
142   emit title_changed(title);
143 }
144 
145 /**
146  * @brief Returns the one-line description of the quest.
147  * @return The one-line description.
148  */
get_short_description() const149 QString QuestProperties::get_short_description() const {
150 
151   return QString::fromStdString(properties.get_short_description());
152 }
153 
154 /**
155  * @brief Changes the one-line description of the quest.
156  * @param short_description The one-line description.
157  */
set_short_description(const QString & short_description)158 void QuestProperties::set_short_description(const QString& short_description) {
159 
160   QString old_short_description = get_short_description();
161   if (short_description == old_short_description) {
162     return;
163   }
164 
165   properties.set_short_description(short_description.toStdString());
166   emit short_description_changed(short_description);
167 }
168 
169 /**
170  * @brief Returns the one-line description of the quest.
171  * @return The one-line description.
172  */
get_long_description() const173 QString QuestProperties::get_long_description() const {
174 
175   return QString::fromStdString(properties.get_long_description());
176 }
177 
178 /**
179  * @brief Changes the one-line description of the quest.
180  * @param long_description The one-line description.
181  */
set_long_description(const QString & long_description)182 void QuestProperties::set_long_description(const QString& long_description) {
183 
184   QString old_long_description = get_long_description();
185   if (long_description == old_long_description) {
186     return;
187   }
188 
189   properties.set_long_description(long_description.toStdString());
190   emit long_description_changed(long_description);
191 }
192 
193 /**
194  * @brief Returns the author of the quest.
195  * @return The author.
196  */
get_author() const197 QString QuestProperties::get_author() const {
198 
199   return QString::fromStdString(properties.get_author());
200 }
201 
202 /**
203  * @brief Changes the author of the quest.
204  * @param author The author.
205  */
set_author(const QString & author)206 void QuestProperties::set_author(const QString& author) {
207 
208   QString old_author = get_author();
209   if (author == old_author) {
210     return;
211   }
212 
213   properties.set_author(author.toStdString());
214   emit author_changed(author);
215 }
216 
217 /**
218  * @brief Returns the version of the quest.
219  * @return The version.
220  */
get_quest_version() const221 QString QuestProperties::get_quest_version() const {
222 
223   return QString::fromStdString(properties.get_quest_version());
224 }
225 
226 /**
227  * @brief Changes the version of the quest.
228  * @param quest_version The version.
229  */
set_quest_version(const QString & quest_version)230 void QuestProperties::set_quest_version(const QString& quest_version) {
231 
232   QString old_quest_version = get_quest_version();
233   if (quest_version == old_quest_version) {
234     return;
235   }
236 
237   properties.set_quest_version(quest_version.toStdString());
238   emit quest_version_changed(quest_version);
239 }
240 
241 /**
242  * @brief Returns the website of the quest.
243  * @return The website.
244  */
get_website() const245 QString QuestProperties::get_website() const {
246 
247   return QString::fromStdString(properties.get_website());
248 }
249 
250 /**
251  * @brief Changes the website of the quest.
252  * @param website The website.
253  */
set_website(const QString & website)254 void QuestProperties::set_website(const QString& website) {
255 
256   QString old_website = get_website();
257   if (website == old_website) {
258     return;
259   }
260 
261   properties.set_website(website.toStdString());
262   emit website_changed(website);
263 }
264 
265 /**
266  * @brief Returns the quest release date.
267  * @return The release date or an invalid date.
268  */
get_release_date() const269 QDate QuestProperties::get_release_date() const {
270 
271   QString date_string = QString::fromStdString(properties.get_release_date());
272   return QDate::fromString(date_string, "yyyyMMdd");
273 }
274 
275 /**
276  * @brief Changes the quest release date.
277  * @param release_date The release date or an invalid date.
278  */
set_release_date(const QDate & release_date)279 void QuestProperties::set_release_date(const QDate& release_date) {
280 
281   QDate old_release_date = get_release_date();
282   if (release_date == old_release_date) {
283     return;
284   }
285 
286   QString date_string;
287   if (release_date.isValid()) {
288     date_string = release_date.toString("yyyyMMdd");
289   }
290   properties.set_release_date(date_string.toStdString());
291   emit release_date_changed(release_date);
292 }
293 
294 /**
295  * @brief Returns the normal size of the quest.
296  * @return The normal size.
297  */
get_normal_quest_size() const298 QSize QuestProperties::get_normal_quest_size() const {
299 
300   return Size::to_qsize(properties.get_normal_quest_size());
301 }
302 
303 /**
304  * @brief Changes the normal size of the quest.
305  * @param size The new normal size.
306  */
set_normal_quest_size(const QSize & size)307 void QuestProperties::set_normal_quest_size(const QSize& size) {
308 
309   QSize old_size = get_normal_quest_size();
310   if (size == old_size) {
311     return;
312   }
313 
314   properties.set_normal_quest_size(Size::to_solarus_size(size));
315   emit normal_size_changed(size);
316 }
317 
318 /**
319  * @brief Returns the minimum quest size of the quest.
320  * @return The minimum quest size.
321  */
get_min_quest_size() const322 QSize QuestProperties::get_min_quest_size() const {
323 
324   return Size::to_qsize(properties.get_min_quest_size());
325 }
326 
327 /**
328  * @brief Changes the minimum size of the quest.
329  * @param size The new minimum size.
330  */
set_min_quest_size(const QSize & size)331 void QuestProperties::set_min_quest_size(const QSize& size) {
332 
333   QSize old_size = get_min_quest_size();
334   if (size == old_size) {
335     return;
336   }
337 
338   properties.set_min_quest_size(Size::to_solarus_size(size));
339   emit min_size_changed(size);
340 }
341 
342 /**
343  * @brief Returns the maximum quest size of the quest.
344  * @return The maximum quest size.
345  */
get_max_quest_size() const346 QSize QuestProperties::get_max_quest_size() const {
347 
348   return Size::to_qsize(properties.get_max_quest_size());
349 }
350 
351 /**
352  * @brief Changes the maximum size of the quest.
353  * @param size The new maximum size.
354  */
set_max_quest_size(const QSize & size)355 void QuestProperties::set_max_quest_size(const QSize& size) {
356 
357   QSize old_size = get_max_quest_size();
358   if (size == old_size) {
359     return;
360   }
361 
362   properties.set_max_quest_size(Size::to_solarus_size(size));
363   emit max_size_changed(size);
364 }
365 
366 }
367