GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textcommandeditor.cpp Lines: 117 166 70.5 %
Date: 2017-11-29 Branches: 150 330 45.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  Andrei Karas
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/windows/textcommandeditor.h"
24
25
#include "spellmanager.h"
26
#include "textcommand.h"
27
28
#include "input/keyboardconfig.h"
29
30
#include "gui/models/iconsmodel.h"
31
#include "gui/models/targettypemodel.h"
32
33
#include "gui/widgets/button.h"
34
#include "gui/widgets/containerplacer.h"
35
#include "gui/widgets/dropdown.h"
36
#include "gui/widgets/inttextfield.h"
37
#include "gui/widgets/label.h"
38
#include "gui/widgets/layoutcell.h"
39
40
#ifdef TMWA_SUPPORT
41
#include "gui/models/magicschoolmodel.h"
42
43
#include "gui/widgets/radiobutton.h"
44
#endif  // TMWA_SUPPORT
45
46
#include "utils/delete2.h"
47
48
#include "debug.h"
49
50
#ifdef TMWA_SUPPORT
51
const unsigned int MAGIC_START_ID = 340;
52
#endif  // TMWA_SUPPORT
53
54
2
TextCommandEditor::TextCommandEditor(TextCommand *const command) :
55
    // TRANSLATORS: command editor name
56
2
    Window(_("Command Editor"), Modal_false, nullptr, "commandeditor.xml"),
57
    ActionListener(),
58
#ifdef TMWA_SUPPORT
59

4
    mIsMagicCommand(command != nullptr ?
60
2
        (command->getCommandType() == TextCommandType::Magic) : false),
61
#endif  // TMWA_SUPPORT
62
    mCommand(command),
63
#ifdef TMWA_SUPPORT
64
    // TRANSLATORS: command editor button
65

4
    mIsMagic(new RadioButton(this, _("magic"), "magic", mIsMagicCommand)),
66
    // TRANSLATORS: command editor button
67

4
    mIsOther(new RadioButton(this, _("other"), "magic", !mIsMagicCommand)),
68
#endif  // TMWA_SUPPORT
69
    // TRANSLATORS: command editor label
70

4
    mSymbolLabel(new Label(this, _("Symbol:"))),
71

2
    mSymbolTextField(new TextField(this)),
72
    // TRANSLATORS: command editor label
73

4
    mCommandLabel(new Label(this, _("Command:"))),
74

2
    mCommandTextField(new TextField(this)),
75
    // TRANSLATORS: command editor label
76

4
    mCommentLabel(new Label(this, _("Comment:"))),
77

2
    mCommentTextField(new TextField(this)),
78
4
    mTargetTypeModel(new TargetTypeModel),
79
    // TRANSLATORS: command editor label
80

4
    mTypeLabel(new Label(this, _("Target Type:"))),
81

2
    mTypeDropDown(new DropDown(this, mTargetTypeModel)),
82

2
    mIconsModel(new IconsModel),
83
    // TRANSLATORS: command editor label
84

4
    mIconLabel(new Label(this, _("Icon:"))),
85

2
    mIconDropDown(new DropDown(this, mIconsModel)),
86
#ifdef TMWA_SUPPORT
87
    // TRANSLATORS: command editor label
88

4
    mManaLabel(new Label(this, _("Mana:"))),
89

2
    mManaField(new IntTextField(this, 0)),
90
    // TRANSLATORS: command editor label
91

4
    mMagicLvlLabel(new Label(this, _("Magic level:"))),
92

2
    mMagicLvlField(new IntTextField(this, 0)),
93
4
    mMagicSchoolModel(new MagicSchoolModel),
94
    // TRANSLATORS: command editor label
95

4
    mSchoolLabel(new Label(this, _("Magic School:"))),
96

2
    mSchoolDropDown(new DropDown(this, mMagicSchoolModel)),
97
    // TRANSLATORS: command editor label
98

4
    mSchoolLvlLabel(new Label(this, _("School level:"))),
99

2
    mSchoolLvlField(new IntTextField(this, 0)),
100
#endif  // TMWA_SUPPORT
101
    // TRANSLATORS: command editor button
102

4
    mCancelButton(new Button(this, _("Cancel"), "cancel", this)),
103
    // TRANSLATORS: command editor button
104

4
    mSaveButton(new Button(this, _("Save"), "save", this)),
105
    // TRANSLATORS: command editor button
106

4
    mDeleteButton(new Button(this, _("Delete"), "delete", this)),
107















240
    mEnabledKeyboard(keyboard.isEnabled())
108
{
109
2
    const int w = 350;
110
2
    const int h = 370;
111
112
2
    keyboard.setEnabled(false);
113
114
10
    setWindowName("TextCommandEditor");
115
2
    setDefaultSize(w, h, ImagePosition::CENTER);
116
117
#ifdef TMWA_SUPPORT
118
10
    mIsMagic->setActionEventId("magic");
119
2
    mIsMagic->addActionListener(this);
120
121
10
    mIsOther->setActionEventId("other");
122
2
    mIsOther->addActionListener(this);
123
124
2
    mManaField->setRange(0, 500);
125
2
    mManaField->setWidth(20);
126
127
2
    mMagicLvlField->setRange(0, 5);
128
2
    mMagicLvlField->setWidth(20);
129
130
10
    mSchoolDropDown->setActionEventId("school");
131
2
    mSchoolDropDown->addActionListener(this);
132
2
    mSchoolDropDown->setSelected(0);
133
134
2
    mSchoolLvlField->setRange(0, 5);
135
2
    mSchoolLvlField->setWidth(20);
136
#endif  // TMWA_SUPPORT
137
138
10
    mTypeDropDown->setActionEventId("type");
139
2
    mTypeDropDown->addActionListener(this);
140
141
10
    mIconDropDown->setActionEventId("icon");
142
2
    mIconDropDown->addActionListener(this);
143
2
    if (mCommand != nullptr)
144
6
        mIconDropDown->setSelectedString(mCommand->getIcon());
145
146
2
    mSaveButton->adjustSize();
147
2
    mCancelButton->adjustSize();
148
2
    mDeleteButton->adjustSize();
149
150
2
    if (command != nullptr)
151
    {
152
#ifdef TMWA_SUPPORT
153
2
        if (command->getCommandType() == TextCommandType::Magic)
154
            showControls(Visible_true);
155
        else
156
2
            showControls(Visible_false);
157
158
4
        mManaField->setValue(command->getMana());
159
4
        mMagicLvlField->setValue(command->getBaseLvl());
160
6
        mSchoolDropDown->setSelected(CAST_S32(command->getSchool())
161
2
            - MAGIC_START_ID);
162
4
        mSchoolLvlField->setValue(command->getSchoolLvl());
163
#endif  // TMWA_SUPPORT
164
    }
165
166
2
    ContainerPlacer placer;
167
2
    placer = getPlacer(0, 0);
168
169
#ifdef TMWA_SUPPORT
170
2
    placer(0, 0, mIsMagic, 1);
171
2
    placer(2, 0, mIsOther, 1);
172
4
    placer(0, 1, mSymbolLabel, 2).setPadding(3);
173
4
    placer(2, 1, mSymbolTextField, 3).setPadding(3);
174
4
    placer(0, 2, mCommandLabel, 2).setPadding(3);
175
4
    placer(2, 2, mCommandTextField, 4).setPadding(3);
176
177
4
    placer(0, 3, mCommentLabel, 2).setPadding(3);
178
4
    placer(2, 3, mCommentTextField, 4).setPadding(3);
179
180
4
    placer(0, 4, mTypeLabel, 2).setPadding(3);
181

4
    placer(2, 4, mTypeDropDown, 3).setPadding(3);
182
183
4
    placer(0, 5, mIconLabel, 2).setPadding(3);
184

4
    placer(2, 5, mIconDropDown, 3).setPadding(3);
185
186
4
    placer(0, 6, mManaLabel, 2).setPadding(3);
187
4
    placer(2, 6, mManaField, 3).setPadding(3);
188
4
    placer(0, 7, mMagicLvlLabel, 2).setPadding(3);
189
4
    placer(2, 7, mMagicLvlField, 3).setPadding(3);
190
191
4
    placer(0, 8, mSchoolLabel, 2).setPadding(3);
192

4
    placer(2, 8, mSchoolDropDown, 3).setPadding(3);
193
4
    placer(0, 9, mSchoolLvlLabel, 2).setPadding(3);
194
4
    placer(2, 9, mSchoolLvlField, 3).setPadding(3);
195
196
4
    placer(0, 10, mSaveButton, 2).setPadding(3);
197
4
    placer(2, 10, mCancelButton, 2).setPadding(3);
198
4
    placer(4, 10, mDeleteButton, 2).setPadding(3);
199
#else  // TMWA_SUPPORT
200
201
    placer(0, 0, mSymbolLabel, 2).setPadding(3);
202
    placer(2, 0, mSymbolTextField, 3).setPadding(3);
203
    placer(0, 1, mCommandLabel, 2).setPadding(3);
204
    placer(2, 1, mCommandTextField, 4).setPadding(3);
205
206
    placer(0, 2, mCommentLabel, 2).setPadding(3);
207
    placer(2, 2, mCommentTextField, 4).setPadding(3);
208
209
    placer(0, 3, mTypeLabel, 2).setPadding(3);
210
    placer(2, 3, mTypeDropDown, 3).setPadding(3);
211
212
    placer(0, 4, mIconLabel, 2).setPadding(3);
213
    placer(2, 4, mIconDropDown, 3).setPadding(3);
214
215
    placer(0, 5, mSaveButton, 2).setPadding(3);
216
    placer(2, 5, mCancelButton, 2).setPadding(3);
217
    placer(4, 5, mDeleteButton, 2).setPadding(3);
218
#endif  // TMWA_SUPPORT
219
220
2
    setWidth(w);
221
2
    setHeight(h);
222
223
2
    reflowLayout(w);
224
225
2
    center();
226
2
}
227
228
2
void TextCommandEditor::postInit()
229
{
230
2
    Window::postInit();
231
4
    enableVisibleSound(true);
232
2
    setVisible(Visible_true);
233
2
}
234
235
8
TextCommandEditor::~TextCommandEditor()
236
{
237
4
    delete2(mIconsModel);
238
4
    delete2(mTargetTypeModel);
239
#ifdef TMWA_SUPPORT
240
4
    delete2(mMagicSchoolModel);
241
#endif  // TMWA_SUPPORT
242
4
}
243
244
void TextCommandEditor::action(const ActionEvent &event)
245
{
246
    const std::string &eventId = event.getId();
247
    if (eventId == "save")
248
    {
249
        save();
250
        scheduleDelete();
251
    }
252
    else if (eventId == "cancel")
253
    {
254
        scheduleDelete();
255
    }
256
    else if (eventId == "delete")
257
    {
258
        deleteCommand();
259
        scheduleDelete();
260
    }
261
#ifdef TMWA_SUPPORT
262
    else if (eventId == "magic")
263
    {
264
        mIsMagicCommand = true;
265
        showControls(Visible_true);
266
    }
267
    else if (eventId == "other")
268
    {
269
        mIsMagicCommand = false;
270
        showControls(Visible_false);
271
    }
272
#endif  // TMWA_SUPPORT
273
}
274
275
#ifdef TMWA_SUPPORT
276
2
void TextCommandEditor::showControls(const Visible show)
277
{
278
2
    mManaField->setVisible(show);
279
2
    mManaLabel->setVisible(show);
280
2
    mMagicLvlLabel->setVisible(show);
281
2
    mMagicLvlField->setVisible(show);
282
2
    mSchoolLabel->setVisible(show);
283
2
    mSchoolDropDown->setVisible(show);
284
2
    mSchoolLvlLabel->setVisible(show);
285
2
    mSchoolLvlField->setVisible(show);
286
2
}
287
#endif  // TMWA_SUPPORT
288
289
void TextCommandEditor::scheduleDelete()
290
{
291
    keyboard.setEnabled(mEnabledKeyboard);
292
    Window::scheduleDelete();
293
}
294
295
void TextCommandEditor::save()
296
{
297
    if (mCommand == nullptr)
298
        return;
299
#ifdef TMWA_SUPPORT
300
    if (mIsMagicCommand)
301
        mCommand->setCommandType(TextCommandType::Magic);
302
    else
303
        mCommand->setCommandType(TextCommandType::Text);
304
#endif  // TMWA_SUPPORT
305
306
    mCommand->setSymbol(mSymbolTextField->getText());
307
    mCommand->setCommand(mCommandTextField->getText());
308
    mCommand->setComment(mCommentTextField->getText());
309
    mCommand->setTargetType(
310
            static_cast<CommandTargetT>(mTypeDropDown->getSelected()));
311
    mCommand->setIcon(mIconDropDown->getSelectedString());
312
313
#ifdef TMWA_SUPPORT
314
    mCommand->setMana(mManaField->getValue());
315
    mCommand->setBaseLvl(mMagicLvlField->getValue());
316
    mCommand->setSchool(static_cast<MagicSchoolT>(
317
            mSchoolDropDown->getSelected() + MAGIC_START_ID));
318
    mCommand->setSchoolLvl(mSchoolLvlField->getValue());
319
#endif  // TMWA_SUPPORT
320
321
    if (spellManager != nullptr)
322
        spellManager->save();
323
}
324
325
void TextCommandEditor::deleteCommand()
326
{
327
    if (mCommand == nullptr)
328
        return;
329
    mCommand->setSymbol("");
330
    mCommand->setCommand("");
331
    mCommand->setComment("");
332
    mCommand->setTargetType(CommandTarget::NoTarget);
333
    mCommand->setIcon("");
334
#ifdef TMWA_SUPPORT
335
    mCommand->setCommandType(TextCommandType::Text);
336
    mCommand->setMana(0);
337
    mCommand->setBaseLvl(0);
338
    mCommand->setSchool(MagicSchool::SkillMagic);
339
    mCommand->setSchoolLvl(0);
340
#endif  // TMWA_SUPPORT
341
342
    if (spellManager != nullptr)
343
        spellManager->save();
344

6
}