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 "widgets/border_set_selector.h"
18 #include "editor_exception.h"
19 #include "quest.h"
20 #include "tileset_model.h"
21 
22 namespace SolarusEditor {
23 
24 /**
25  * @brief Creates an empty border set selector.
26  *
27  * Call the setter functions and then build() to fill the selector.
28  *
29  * @param parent The parent widget or nullptr.
30  */
BorderSetSelector(QWidget * parent)31 BorderSetSelector::BorderSetSelector(QWidget* parent) :
32   QComboBox(parent),
33   quest(nullptr),
34   tileset_id() {
35 
36 }
37 
38 /**
39  * @brief Returns the id of the tileset where border sets come from.
40  * @return The tileset id or an empty string if it is not set yet.
41  */
get_tileset_id() const42 const QString& BorderSetSelector::get_tileset_id() const {
43   return tileset_id;
44 }
45 
46 /**
47  * @brief Sets the tileset where entities should come from.
48  * @param quest The quest the tileset belongs to.
49  * @param tileset_id Id of the tileset.
50  */
set_tileset_id(Quest & quest,const QString & tileset_id)51 void BorderSetSelector::set_tileset_id(Quest& quest, const QString& tileset_id) {
52 
53   this->quest = &quest;
54   this->tileset_id = tileset_id;
55   QString old_border_set_id = get_selected_border_set_id();
56   build();
57   if (!old_border_set_id.isEmpty()) {
58     set_selected_border_set_id(old_border_set_id);
59   }
60 }
61 
62 /**
63  * @brief Returns the border set id in the selected item.
64  * @return The selected border set id.
65  */
get_selected_border_set_id() const66 QString BorderSetSelector::get_selected_border_set_id() const {
67 
68   return currentData().toString();
69 }
70 
71 /**
72  * @brief Selects the specified border set.
73  * @return Id of the border set to make selected.
74  * Nothing happens if such a border set does not exist.
75  */
set_selected_border_set_id(const QString & border_set_id)76 void BorderSetSelector::set_selected_border_set_id(const QString& border_set_id) {
77 
78   int index = findData(border_set_id, Qt::UserRole);
79   if (index == -1) {
80     return;
81   }
82 
83   setCurrentIndex(index);
84 }
85 
86 /**
87  * @brief Builds or rebuilds the combobox using the parameters previously set.
88  */
build()89 void BorderSetSelector::build() {
90 
91   clear();
92 
93   if (quest == nullptr || tileset_id.isEmpty()) {
94     return;
95   }
96 
97   try {
98     TilesetModel tileset(*quest, tileset_id);
99 
100     // Add border sets.
101     const QStringList& border_set_ids = tileset.get_border_set_ids();
102     for (const QString& border_set_id : border_set_ids) {
103       addItem(tileset.get_border_set_icon(border_set_id), border_set_id, border_set_id);
104     }
105 
106     if (!border_set_ids.isEmpty()) {
107       set_selected_border_set_id(border_set_ids.first());
108     }
109     setIconSize(QSize(24, 24));
110   }
111   catch (const EditorException& ex) {
112     // The tileset file could not be opened: the tileset is probably
113     // unset or incorrect.
114     Q_UNUSED(ex);
115   }
116   }
117 
118 }
119