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/pair_spin_box.h"
18 #include <QHBoxLayout>
19 
20 namespace SolarusEditor {
21 
22 /**
23  * @brief Creates a pair spin box.
24  * @param parent The parent widget.
25  */
PairSpinBox(QWidget * parent)26 PairSpinBox::PairSpinBox(QWidget *parent) :
27   QWidget(parent) {
28 
29   QHBoxLayout* layout = new QHBoxLayout(this);
30   layout->setMargin(0);
31 
32   first_spin_box.setMinimumSize(70, 0);
33   first_spin_box.setMaximumSize(70, 100);
34   second_spin_box.setMinimumSize(70, 0);
35   second_spin_box.setMaximumSize(70, 100);
36   layout->addWidget(&first_spin_box);
37   layout->addWidget(&separator_label);
38   layout->addWidget(&second_spin_box);
39   layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));
40 
41   connect(&first_spin_box, SIGNAL(valueChanged(int)),
42           this, SLOT(on_first_value_changed()));
43   connect(&second_spin_box, SIGNAL(valueChanged(int)),
44           this, SLOT(on_second_value_changed()));
45 
46   connect(&first_spin_box, SIGNAL(editingFinished()),
47           this, SIGNAL(editing_finished()));
48   connect(&second_spin_box, SIGNAL(editingFinished()),
49           this, SIGNAL(editing_finished()));
50 }
51 
52 /**
53  * @brief Returns the first value.
54  * @return The first value.
55  */
get_first_value() const56 int PairSpinBox::get_first_value() const {
57 
58   return first_spin_box.value();
59 }
60 
61 /**
62  * @brief Returns the second value.
63  * @return The second value.
64  */
get_second_value() const65 int PairSpinBox::get_second_value() const {
66 
67   return second_spin_box.value();
68 }
69 
70 /**
71  * @brief Returns the first maximum value.
72  * @return The first maximum value.
73  */
get_first_max() const74 int PairSpinBox::get_first_max() const {
75 
76   return first_spin_box.maximum();
77 }
78 
79 /**
80  * @brief Returns the second maximum value.
81  * @return The second maximum value.
82  */
get_second_max() const83 int PairSpinBox::get_second_max() const {
84 
85   return second_spin_box.maximum();
86 }
87 
88 /**
89  * @brief Returns the first minimum value.
90  * @return The first minimum value.
91  */
get_first_min() const92 int PairSpinBox::get_first_min() const {
93 
94   return first_spin_box.minimum();
95 }
96 
97 /**
98  * @brief Returns the second minimum value.
99  * @return The second minimum value.
100  */
get_second_min() const101 int PairSpinBox::get_second_min() const {
102 
103   return second_spin_box.minimum();
104 }
105 
106 /**
107  * @brief Returns the first step value.
108  * @return The first step value.
109  */
get_first_step() const110 int PairSpinBox::get_first_step() const {
111 
112   return first_spin_box.singleStep();
113 }
114 
115 /**
116  * @brief Returns the second step value.
117  * @return The second step value.
118  */
get_second_step() const119 int PairSpinBox::get_second_step() const {
120 
121   return second_spin_box.singleStep();
122 }
123 
124 /**
125  * @brief Returns the values.
126  * @return The values.
127  */
get_values() const128 QPair<int, int> PairSpinBox::get_values() const {
129 
130   return QPair<int, int>(get_first_value(), get_second_value());
131 }
132 
133 /**
134  * @brief Returns the point value.
135  * @return The point value.
136  */
get_point() const137 QPoint PairSpinBox::get_point() const {
138 
139   return QPoint(get_first_value(), get_second_value());
140 }
141 
142 /**
143  * @brief Returns the size value.
144  * @return The size value.
145  */
get_size() const146 QSize PairSpinBox::get_size() const {
147 
148   return QSize(get_first_value(), get_second_value());
149 }
150 
151 /**
152  * @brief Returns the separator text.
153  * @return The separator text.
154  */
get_separator_text() const155 QString PairSpinBox::get_separator_text() const {
156 
157   return separator_label.text();
158 }
159 
160 /**
161  * @brief Returns whether the first spin box is enabled.
162  * @return @c true if the first spin box is enabled.
163  */
get_first_enabled() const164 bool PairSpinBox::get_first_enabled() const {
165 
166   return first_spin_box.isEnabled();
167 }
168 
169 /**
170  * @brief Returns whether the second spin box is enabled.
171  * @return @c true if the second spin box is enabled.
172  */
get_second_enabled() const173 bool PairSpinBox::get_second_enabled() const {
174 
175   return second_spin_box.isEnabled();
176 }
177 
178 /**
179  * @brief Changes the first value.
180  * @param value The new value.
181  */
set_first_value(int value)182 void PairSpinBox::set_first_value(int value) {
183 
184   first_spin_box.setValue(value);
185 }
186 
187 /**
188  * @brief Changes the second value.
189  * @param value The new value.
190  */
set_second_value(int value)191 void PairSpinBox::set_second_value(int value) {
192 
193   second_spin_box.setValue(value);
194 }
195 
196 /**
197  * @brief Changes the first maximum value.
198  * @param max The new maximum value.
199  */
set_first_max(int max)200 void PairSpinBox::set_first_max(int max) {
201 
202   first_spin_box.setMaximum(max);
203 }
204 
205 /**
206  * @brief Changes the second maximum value.
207  * @param max The new maximum value.
208  */
set_second_max(int max)209 void PairSpinBox::set_second_max(int max) {
210 
211   second_spin_box.setMaximum(max);
212 }
213 
214 /**
215  * @brief Changes the first minimum value.
216  * @param min The new minimum value.
217  */
set_first_min(int min)218 void PairSpinBox::set_first_min(int min) {
219 
220   first_spin_box.setMinimum(min);
221 }
222 
223 /**
224  * @brief Changes the second minimum value.
225  * @param min The new minimum value.
226  */
set_second_min(int min)227 void PairSpinBox::set_second_min(int min) {
228 
229   second_spin_box.setMinimum(min);
230 }
231 
232 /**
233  * @brief Changes the first step value.
234  * @param step The new step value.
235  */
set_first_step(int step)236 void PairSpinBox::set_first_step(int step) {
237 
238   first_spin_box.setSingleStep(step);
239 }
240 
241 /**
242  * @brief Changes the second step value.
243  * @param step The new step value.
244  */
set_second_step(int step)245 void PairSpinBox::set_second_step(int step) {
246 
247   second_spin_box.setSingleStep(step);
248 }
249 
250 /**
251  * @brief Changes the first tooltip.
252  * @param text The new tooltip.
253  */
set_first_tooltip(const QString & text)254 void PairSpinBox::set_first_tooltip(const QString &text) {
255 
256   first_spin_box.setToolTip(text);
257 }
258 
259 /**
260  * @brief Changes the second tooltip.
261  * @param text The new tooltip.
262  */
set_second_tooltip(const QString & text)263 void PairSpinBox::set_second_tooltip(const QString &text) {
264 
265   second_spin_box.setToolTip(text);
266 }
267 
268 /**
269  * @brief Changes values.
270  * @param first_value The new first value.
271  * @param second_value The new second value.
272  */
set_values(int first_value,int second_value)273 void PairSpinBox::set_values(int first_value, int second_value) {
274 
275   set_first_value(first_value);
276   set_second_value(second_value);
277 }
278 
279 /**
280  * @brief Changes values.
281  * @param values The new values.
282  */
set_values(const QPair<int,int> & values)283 void PairSpinBox::set_values(const QPair<int, int> &values) {
284 
285   set_values(values.first, values.second);
286 }
287 
288 /**
289  * @brief Changes values.
290  * @param values The new values.
291  */
set_point(const QPoint & values)292 void PairSpinBox::set_point(const QPoint& values) {
293 
294   set_first_value(values.x());
295   set_second_value(values.y());
296 }
297 
298 /**
299  * @brief Changes values.
300  * @param values The new values.
301  */
set_size(const QSize & values)302 void PairSpinBox::set_size(const QSize& values) {
303 
304   set_first_value(values.width());
305   set_second_value(values.height());
306 }
307 
308 /**
309  * @brief Changes maximum values.
310  * @param max The new maximum values.
311  */
set_max(int max)312 void PairSpinBox::set_max(int max) {
313 
314   set_first_max(max);
315   set_second_max(max);
316 }
317 
318 /**
319  * @brief Changes minimum values.
320  * @param min The new minimum values.
321  */
set_min(int min)322 void PairSpinBox::set_min(int min) {
323 
324   set_first_min(min);
325   set_second_min(min);
326 }
327 
328 /**
329  * @brief Changes step values.
330  * @param step The new step values.
331  */
set_step(int step)332 void PairSpinBox::set_step(int step) {
333 
334   set_first_step(step);
335   set_second_step(step);
336 }
337 
338 /**
339  * @brief Changes tooltips.
340  * @param first_text The new first tooltip.
341  * @param second_text The new second tooltip.
342  */
set_tooltips(const QString & first_text,const QString & second_text)343 void PairSpinBox::set_tooltips(
344   const QString &first_text, const QString &second_text) {
345 
346   set_first_tooltip(first_text);
347   set_second_tooltip(second_text);
348 }
349 
350 /**
351  * @brief Configures the widget.
352  * @param separator_text The new separator text.
353  * @param min The new minimum values.
354  * @param max The new maximum values.
355  * @param step The new step values.
356  */
config(const QString & separator_text,int min,int max,int step)357 void PairSpinBox::config(
358   const QString &separator_text, int min, int max, int step) {
359 
360   separator_label.setText(separator_text);
361   set_min(min);
362   set_max(max);
363   set_step(step);
364 }
365 
366 /**
367  * @brief Changes the enable state of the first spin box.
368  * @param enabled @c true to enable the spin box.
369  */
set_first_enabled(bool enabled)370 void PairSpinBox::set_first_enabled(bool enabled) {
371 
372   first_spin_box.setEnabled(enabled);
373 }
374 
375 /**
376  * @brief Changes the enable state of the second spin box.
377  * @param enabled @c true to enable the spin box.
378  */
set_second_enabled(bool enabled)379 void PairSpinBox::set_second_enabled(bool enabled) {
380 
381   second_spin_box.setEnabled(enabled);
382 }
383 
384 /**
385  * @brief Slot called when the user has just change the first value.
386  */
on_first_value_changed()387 void PairSpinBox::on_first_value_changed() {
388 
389   emit first_value_changed(get_first_value());
390   emit value_changed(get_first_value(), get_second_value());
391 }
392 
393 /**
394  * @brief Slot called when the user has just change the second value.
395  */
on_second_value_changed()396 void PairSpinBox::on_second_value_changed() {
397 
398   emit second_value_changed(get_second_value());
399   emit value_changed(get_first_value(), get_second_value());
400 }
401 
402 }
403