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/get_animation_name_dialog.h"
18 #include "widgets/gui_tools.h"
19 #include "sprite_model.h"
20
21 namespace SolarusEditor {
22
23 /**
24 * @brief Creates a new animation name dialog.
25 * @param model The sprite model.
26 * @param parent Parent object or nullptr.
27 */
GetAnimationNameDialog(const SpriteModel & model,QWidget * parent)28 GetAnimationNameDialog::GetAnimationNameDialog(
29 const SpriteModel& model, QWidget* parent) :
30 QInputDialog(parent),
31 model(model),
32 animation_name("") {
33
34 setWindowTitle(tr("New animation"));
35 setup_ui();
36 }
37
38 /**
39 * @brief Creates a change animation name dialog.
40 * @param model The sprite model.
41 * @param animation_name The animation name.
42 * @param parent Parent object or nullptr.
43 */
GetAnimationNameDialog(const SpriteModel & model,const QString & animation_name,QWidget * parent)44 GetAnimationNameDialog::GetAnimationNameDialog(
45 const SpriteModel& model, const QString& animation_name, QWidget* parent) :
46 QInputDialog(parent),
47 model(model),
48 animation_name(animation_name) {
49
50 setWindowTitle(tr("Change animation name"));
51 setup_ui();
52 set_animation_name(animation_name);
53 }
54
55 /**
56 * @brief Returns animation name entered by the user.
57 * @return The animation name.
58 */
get_animation_name() const59 QString GetAnimationNameDialog::get_animation_name() const {
60
61 return textValue();
62 }
63
64 /**
65 * @brief Sets the animation name displayed in the text edit.
66 * @param value The value to set.
67 */
set_animation_name(const QString & animation_name)68 void GetAnimationNameDialog::set_animation_name(const QString& animation_name) {
69
70 setTextValue(animation_name);
71 }
72
73 /**
74 * @brief Closes the dialog unless the user tries to set invalid data.
75 * @param result Result code of the dialog.
76 */
done(int result)77 void GetAnimationNameDialog::done(int result) {
78
79 if (result == QDialog::Accepted) {
80
81 QString new_animation_name = textValue();
82
83 if (new_animation_name.isEmpty()) {
84 GuiTools::error_dialog(tr("Empty animation name"));
85 return;
86 }
87
88 if (animation_name != new_animation_name &&
89 model.animation_exists(new_animation_name)) {
90 GuiTools::error_dialog(
91 tr("Animation '%1' already exists").arg(new_animation_name));
92 return;
93 }
94 }
95
96 QDialog::done(result);
97 }
98
99 /**
100 * @brief Setups the ui.
101 */
setup_ui()102 void GetAnimationNameDialog::setup_ui () {
103
104 setInputMode(InputMode::TextInput);
105 setLabelText(tr("Animation name:"));
106 }
107
108 }
109