GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/textcommandeditor.cpp Lines: 127 176 72.2 %
Date: 2018-09-20 Branches: 144 310 46.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-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
1
TextCommandEditor::TextCommandEditor(TextCommand *const command) :
55
    // TRANSLATORS: command editor name
56
1
    Window(_("Command Editor"), Modal_false, nullptr, "commandeditor.xml"),
57
    ActionListener(),
58
#ifdef TMWA_SUPPORT
59

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

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

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

2
    mSymbolLabel(new Label(this, _("Symbol:"))),
71
2
    mSymbolTextField(new TextField(this, std::string(),
72

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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










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

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

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

2
    placer(2, 8, mSchoolDropDown, 3, 1).setPadding(3);
204
2
    placer(0, 9, mSchoolLvlLabel, 2, 1).setPadding(3);
205
2
    placer(2, 9, mSchoolLvlField, 3, 1).setPadding(3);
206
207
2
    placer(0, 10, mSaveButton, 2, 1).setPadding(3);
208
2
    placer(2, 10, mCancelButton, 2, 1).setPadding(3);
209
2
    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
1
    setWidth(w);
232
1
    setHeight(h);
233
234
1
    reflowLayout(w, 0);
235
236
1
    center();
237
1
}
238
239
1
void TextCommandEditor::postInit()
240
{
241
1
    Window::postInit();
242
2
    enableVisibleSound(true);
243
1
    setVisible(Visible_true);
244
1
}
245
246
4
TextCommandEditor::~TextCommandEditor()
247
{
248
2
    delete2(mIconsModel);
249
2
    delete2(mTargetTypeModel);
250
#ifdef TMWA_SUPPORT
251
2
    delete2(mMagicSchoolModel);
252
#endif  // TMWA_SUPPORT
253
2
}
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
1
void TextCommandEditor::showControls(const Visible show)
288
{
289
1
    mManaField->setVisible(show);
290
1
    mManaLabel->setVisible(show);
291
1
    mMagicLvlLabel->setVisible(show);
292
1
    mMagicLvlField->setVisible(show);
293
1
    mSchoolLabel->setVisible(show);
294
1
    mSchoolDropDown->setVisible(show);
295
1
    mSchoolLvlLabel->setVisible(show);
296
1
    mSchoolLvlField->setVisible(show);
297
1
}
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

3
}