GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textcommandeditor.cpp Lines: 127 176 72.2 %
Date: 2018-06-18 21:15:20 Branches: 145 320 45.3 %

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-2018  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
4
    mSymbolTextField(new TextField(this, std::string(),
72

4
        LoseFocusOnTab_true, nullptr, std::string(), false)),
73
    // TRANSLATORS: command editor label
74

4
    mCommandLabel(new Label(this, _("Command:"))),
75
4
    mCommandTextField(new TextField(this, std::string(),
76

4
        LoseFocusOnTab_true, nullptr, std::string(), false)),
77
    // TRANSLATORS: command editor label
78

4
    mCommentLabel(new Label(this, _("Comment:"))),
79
4
    mCommentTextField(new TextField(this, std::string(),
80

4
         LoseFocusOnTab_true, nullptr, std::string(), false)),
81
4
    mTargetTypeModel(new TargetTypeModel),
82
    // TRANSLATORS: command editor label
83

4
    mTypeLabel(new Label(this, _("Target Type:"))),
84
2
    mTypeDropDown(new DropDown(this, mTargetTypeModel,
85

6
        false, Modal_false, nullptr, std::string())),
86

2
    mIconsModel(new IconsModel),
87
    // TRANSLATORS: command editor label
88

4
    mIconLabel(new Label(this, _("Icon:"))),
89
2
    mIconDropDown(new DropDown(this, mIconsModel,
90

6
        false, Modal_false, nullptr, std::string())),
91
#ifdef TMWA_SUPPORT
92
    // TRANSLATORS: command editor label
93

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

2
    mManaField(new IntTextField(this, 0, 0, 0, Enable_true, 0)),
95
    // TRANSLATORS: command editor label
96

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

2
    mMagicLvlField(new IntTextField(this, 0, 0, 0, Enable_true, 0)),
98
4
    mMagicSchoolModel(new MagicSchoolModel),
99
    // TRANSLATORS: command editor label
100

4
    mSchoolLabel(new Label(this, _("Magic School:"))),
101
2
    mSchoolDropDown(new DropDown(this, mMagicSchoolModel,
102

6
        false, Modal_false, nullptr, std::string())),
103
    // TRANSLATORS: command editor label
104

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

2
    mSchoolLvlField(new IntTextField(this, 0, 0, 0, Enable_true, 0)),
106
#endif  // TMWA_SUPPORT
107
    // TRANSLATORS: command editor button
108

4
    mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
109
    // TRANSLATORS: command editor button
110

4
    mSaveButton(new Button(this, _("Save"), "save", BUTTON_SKIN, this)),
111
    // TRANSLATORS: command editor button
112

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











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

4
    placer(2, 4, mTypeDropDown, 3, 1).setPadding(3);
193
194
4
    placer(0, 5, mIconLabel, 2, 1).setPadding(3);
195

4
    placer(2, 5, mIconDropDown, 3, 1).setPadding(3);
196
197
4
    placer(0, 6, mManaLabel, 2, 1).setPadding(3);
198
4
    placer(2, 6, mManaField, 3, 1).setPadding(3);
199
4
    placer(0, 7, mMagicLvlLabel, 2, 1).setPadding(3);
200
4
    placer(2, 7, mMagicLvlField, 3, 1).setPadding(3);
201
202
4
    placer(0, 8, mSchoolLabel, 2, 1).setPadding(3);
203

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

6
}