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/pattern_chooser.h"
18 #include "widgets/pattern_picker_dialog.h"
19 #include "tileset_model.h"
20 #include <QInputDialog>
21
22 namespace SolarusEditor {
23
24 namespace {
25
26 const QString style_sheet =
27 "SolarusEditor--PatternChooser {\n"
28 " background-color: white;\n"
29 " border-style: inset;\n"
30 " border-width: 2px;\n"
31 " border-color: gray;\n"
32 " padding: 5px;\n"
33 " color: %1;\n"
34 "}"
35 ;
36
37 } // Anonymous namespace.
38
39 /**
40 * @brief Creates a pattern picker.
41 * @param parent The parent widget.
42 */
PatternChooser(QWidget * parent)43 PatternChooser::PatternChooser(QWidget *parent) :
44 QPushButton(parent),
45 tileset() {
46
47 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
48 setIconSize(QSize(32, 32));
49 update_icon();
50 update_style_sheet();
51
52 connect(this, SIGNAL(clicked()), this, SLOT(pick_pattern_requested()));
53 }
54
55 /**
56 * @brief Sets the tileset where patterns should come from in this chooser.
57 * @param tileset The tileset or nullptr.
58 */
set_tileset(TilesetModel * tileset)59 void PatternChooser::set_tileset(TilesetModel* tileset) {
60
61 this->tileset = tileset;
62 update_icon();
63 update_style_sheet();
64 }
65
66 /**
67 * @brief Returns the current pattern of the picker.
68 * @return Id of the current pattern.
69 */
get_pattern_id() const70 QString PatternChooser::get_pattern_id() const {
71
72 return text();
73 }
74
75 /**
76 * @brief Sets the pattern of the picker.
77 *
78 * Emits pattern_id_changed() if there is a change.
79 *
80 * @param pattern_id The pattern id to set.
81 */
set_pattern_id(const QString & pattern_id)82 void PatternChooser::set_pattern_id(const QString& pattern_id) {
83
84 if (pattern_id == get_pattern_id()) {
85 return;
86 }
87
88 setText(pattern_id);
89 update_icon();
90 update_style_sheet();
91
92 emit pattern_id_changed(pattern_id);
93 }
94
95 /**
96 * @brief Makes the button icon show the current pattern.
97 */
update_icon()98 void PatternChooser::update_icon() {
99
100 if (tileset == nullptr) {
101 // No tileset: use the generic tile icon.
102 setIcon(QIcon(":/images/entity_tile.png"));
103 return;
104 }
105
106 const int pattern_index = tileset->id_to_index(get_pattern_id());
107 if (pattern_index == -1) {
108 // Unknown pattern: show an error icon.
109 setIcon(QIcon(":/images/entity_tile_missing.png"));
110 return;
111 }
112
113 setIcon(tileset->get_pattern_icon(pattern_index));
114 }
115
116 /**
117 * @brief Updates the stylesheet of the chooser.
118 */
update_style_sheet()119 void PatternChooser::update_style_sheet() {
120
121 const bool valid = (tileset != nullptr && tileset->pattern_exists(get_pattern_id()));
122 QString text_color = valid ? "black" : "red";
123 setStyleSheet(style_sheet.arg(text_color));
124 }
125
126 /**
127 * @brief Slot called when the user want to pick another pattern.
128 */
pick_pattern_requested()129 void PatternChooser::pick_pattern_requested() {
130
131 tileset->set_selected_index(tileset->id_to_index(get_pattern_id()));
132 PatternPickerDialog dialog(*tileset);
133 int result = dialog.exec();
134
135 if (result != QDialog::Accepted) {
136 return;
137 }
138
139 QString pattern_id = dialog.get_pattern_id();
140 if (!pattern_id.isEmpty()) {
141 set_pattern_id(pattern_id);
142 }
143 }
144
145 }
146