GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_theme.cpp Lines: 148 244 60.7 %
Date: 2021-03-17 Branches: 219 802 27.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2011-2019  The ManaPlus Developers
5
 *  Copyright (C) 2009-2021  Andrei Karas
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/widgets/tabs/setup_theme.h"
24
25
#include "gui/gui.h"
26
#include "gui/themeinfo.h"
27
28
#include "gui/windows/okdialog.h"
29
30
#include "gui/models/fontsizechoicelistmodel.h"
31
#include "gui/models/fontsmodel.h"
32
#include "gui/models/langlistmodel.h"
33
#include "gui/models/themesmodel.h"
34
35
#include "gui/widgets/button.h"
36
#include "gui/widgets/containerplacer.h"
37
#include "gui/widgets/createwidget.h"
38
#include "gui/widgets/dropdown.h"
39
#include "gui/widgets/label.h"
40
#include "gui/widgets/layouthelper.h"
41
42
#include "configuration.h"
43
44
#include "utils/delete2.h"
45
46
#include "debug.h"
47
48
const char* ACTION_THEME = "theme";
49
const char* ACTION_FONT = "font";
50
const char* ACTION_LANG = "lang";
51
const char* ACTION_BOLD_FONT = "bold font";
52
const char* ACTION_PARTICLE_FONT = "particle font";
53
const char* ACTION_HELP_FONT = "help font";
54
const char* ACTION_SECURE_FONT = "secure font";
55
const char* ACTION_NPC_FONT = "npc font";
56
const char* ACTION_JAPAN_FONT = "japanese font";
57
const char* ACTION_CHINA_FONT = "chinese font";
58
const char* ACTION_INFO = "info";
59
60
2
Setup_Theme::Setup_Theme(const Widget2 *const widget) :
61
    SetupTab(widget),
62
    // TRANSLATORS: theme settings label
63

4
    mThemeLabel(new Label(this, _("Gui theme"))),
64

2
    mThemesModel(new ThemesModel),
65
2
    mThemeDropDown(new DropDown(this, mThemesModel,
66

6
        false, Modal_false, nullptr, std::string())),
67
    mTheme(config.getStringValue("theme")),
68
2
    mInfo(Theme::loadInfo(mTheme)),
69

2
    mFontsModel(new FontsModel),
70
    // TRANSLATORS: theme settings label
71

4
    mFontLabel(new Label(this, _("Main Font"))),
72
2
    mFontDropDown(new DropDown(this, mFontsModel,
73

6
        false, Modal_false, nullptr, std::string())),
74
    mFont(config.getStringValue("font")),
75

2
    mLangListModel(new LangListModel),
76
    // TRANSLATORS: theme settings label
77

4
    mLangLabel(new Label(this, _("Language"))),
78
2
    mLangDropDown(new DropDown(this, mLangListModel,
79

6
        true, Modal_false, nullptr, std::string())),
80
    mLang(config.getStringValue("lang")),
81
    // TRANSLATORS: theme settings label
82

4
    mBoldFontLabel(new Label(this, _("Bold font"))),
83
2
    mBoldFontDropDown(new DropDown(this, mFontsModel,
84

6
        false, Modal_false, nullptr, std::string())),
85
    mBoldFont(config.getStringValue("boldFont")),
86
    // TRANSLATORS: theme settings label
87

4
    mParticleFontLabel(new Label(this, _("Particle font"))),
88
2
    mParticleFontDropDown(new DropDown(this, mFontsModel,
89

6
        false, Modal_false, nullptr, std::string())),
90
    mParticleFont(config.getStringValue("particleFont")),
91
    // TRANSLATORS: theme settings label
92

4
    mHelpFontLabel(new Label(this, _("Help font"))),
93
2
    mHelpFontDropDown(new DropDown(this, mFontsModel,
94

6
        false, Modal_false, nullptr, std::string())),
95
    mHelpFont(config.getStringValue("helpFont")),
96
    // TRANSLATORS: theme settings label
97

4
    mSecureFontLabel(new Label(this, _("Secure font"))),
98
2
    mSecureFontDropDown(new DropDown(this, mFontsModel,
99

6
        false, Modal_false, nullptr, std::string())),
100
    mSecureFont(config.getStringValue("secureFont")),
101
    // TRANSLATORS: theme settings label
102

4
    mNpcFontLabel(new Label(this, _("Npc font"))),
103
2
    mNpcFontDropDown(new DropDown(this, mFontsModel,
104

6
        false, Modal_false, nullptr, std::string())),
105
    mNpcFont(config.getStringValue("npcFont")),
106
    // TRANSLATORS: theme settings label
107

4
    mJapanFontLabel(new Label(this, _("Japanese font"))),
108
2
    mJapanFontDropDown(new DropDown(this, mFontsModel,
109

6
        false, Modal_false, nullptr, std::string())),
110
    mJapanFont(config.getStringValue("japanFont")),
111
    // TRANSLATORS: theme settings label
112

4
    mChinaFontLabel(new Label(this, _("Chinese font"))),
113
2
    mChinaFontDropDown(new DropDown(this, mFontsModel,
114

6
        false, Modal_false, nullptr, std::string())),
115
    mChinaFont(config.getStringValue("chinaFont")),
116
4
    mFontSizeListModel(new FontSizeChoiceListModel),
117
    // TRANSLATORS: theme settings label
118

4
    mFontSizeLabel(new Label(this, _("Font size"))),
119

8
    mFontSize(config.getIntValue("fontSize")),
120
2
    mFontSizeDropDown(new DropDown(this, mFontSizeListModel,
121

6
        false, Modal_false, nullptr, std::string())),
122
4
    mNpcFontSizeListModel(new FontSizeChoiceListModel),
123
    // TRANSLATORS: theme settings label
124

4
    mNpcFontSizeLabel(new Label(this, _("Npc font size"))),
125

8
    mNpcFontSize(config.getIntValue("npcfontSize")),
126
2
    mNpcFontSizeDropDown(new DropDown(this, mNpcFontSizeListModel,
127

6
        false, Modal_false, nullptr, std::string())),
128
    // TRANSLATORS: button name with information about selected theme
129

4
    mInfoButton(new Button(this, _("i"), ACTION_INFO, BUTTON_SKIN, this)),
130

















236
    mThemeInfo()
131
{
132
    // TRANSLATORS: theme settings tab name
133
10
    setName(_("Theme"));
134
135
10
    mThemeDropDown->setActionEventId(ACTION_THEME);
136
2
    mThemeDropDown->addActionListener(this);
137
10
    mFontDropDown->setActionEventId(ACTION_FONT);
138
2
    mFontDropDown->addActionListener(this);
139
8
    mLangDropDown->setActionEventId(ACTION_LANG);
140
2
    mLangDropDown->addActionListener(this);
141
10
    mBoldFontDropDown->setActionEventId(ACTION_BOLD_FONT);
142
2
    mBoldFontDropDown->addActionListener(this);
143
10
    mParticleFontDropDown->setActionEventId(ACTION_PARTICLE_FONT);
144
2
    mParticleFontDropDown->addActionListener(this);
145
10
    mHelpFontDropDown->setActionEventId(ACTION_HELP_FONT);
146
2
    mHelpFontDropDown->addActionListener(this);
147
10
    mSecureFontDropDown->setActionEventId(ACTION_SECURE_FONT);
148
2
    mSecureFontDropDown->addActionListener(this);
149
10
    mNpcFontDropDown->setActionEventId(ACTION_NPC_FONT);
150
2
    mNpcFontDropDown->addActionListener(this);
151
10
    mJapanFontDropDown->setActionEventId(ACTION_JAPAN_FONT);
152
2
    mJapanFontDropDown->addActionListener(this);
153
10
    mChinaFontDropDown->setActionEventId(ACTION_CHINA_FONT);
154
2
    mChinaFontDropDown->addActionListener(this);
155
2
    mFontSizeDropDown->setSelected(mFontSize - 9);
156
2
    mFontSizeDropDown->adjustHeight();
157
2
    mNpcFontSizeDropDown->setSelected(mNpcFontSize - 9);
158
2
    mNpcFontSizeDropDown->adjustHeight();
159
160
2
    const std::string skin = Theme::getThemeName();
161
2
    if (!skin.empty())
162
2
        mThemeDropDown->setSelectedString(skin);
163
    else
164
        mThemeDropDown->setSelected(0);
165
166

10
    const std::string str = config.getStringValue("lang");
167
2
    for (int f = 0; f < langs_count; f ++)
168
    {
169
2
        if (LANG_NAME[f].value == str)
170
        {
171
2
            mLangDropDown->setSelected(f);
172
            break;
173
        }
174
    }
175
176
4
    mFontDropDown->setSelectedString(getFileName(
177

12
        config.getStringValue("font")));
178
4
    mBoldFontDropDown->setSelectedString(getFileName(
179

12
        config.getStringValue("boldFont")));
180
4
    mParticleFontDropDown->setSelectedString(getFileName(
181

12
        config.getStringValue("particleFont")));
182
4
    mHelpFontDropDown->setSelectedString(getFileName(
183

12
        config.getStringValue("helpFont")));
184
4
    mSecureFontDropDown->setSelectedString(getFileName(
185

12
        config.getStringValue("secureFont")));
186
4
    mNpcFontDropDown->setSelectedString(getFileName(
187

12
        config.getStringValue("npcFont")));
188
4
    mJapanFontDropDown->setSelectedString(getFileName(
189

12
        config.getStringValue("japanFont")));
190
4
    mChinaFontDropDown->setSelectedString(getFileName(
191

12
        config.getStringValue("chinaFont")));
192
193
2
    updateInfo();
194
195
    // Do the layout
196
4
    LayoutHelper h(this);
197
2
    ContainerPlacer place = h.getPlacer(0, 0);
198
199
2
    place(0, 0, mThemeLabel, 5, 1);
200
2
    place(0, 1, mLangLabel, 5, 1);
201
2
    place(0, 2, mFontSizeLabel, 5, 1);
202
2
    place(0, 3, mNpcFontSizeLabel, 5, 1);
203
2
    place(0, 4, mFontLabel, 5, 1);
204
2
    place(0, 5, mBoldFontLabel, 5, 1);
205
2
    place(0, 6, mParticleFontLabel, 5, 1);
206
2
    place(0, 7, mHelpFontLabel, 5, 1);
207
2
    place(0, 8, mSecureFontLabel, 5, 1);
208
2
    place(0, 9, mNpcFontLabel, 5, 1);
209
2
    place(0, 10, mJapanFontLabel, 5, 1);
210
2
    place(0, 11, mChinaFontLabel, 5, 1);
211
212

2
    place(6, 0, mThemeDropDown, 10, 1);
213

2
    place(6, 1, mLangDropDown, 10, 1);
214

2
    place(6, 2, mFontSizeDropDown, 10, 1);
215

2
    place(6, 3, mNpcFontSizeDropDown, 10, 1);
216

2
    place(6, 4, mFontDropDown, 10, 1);
217

2
    place(6, 5, mBoldFontDropDown, 10, 1);
218

2
    place(6, 6, mParticleFontDropDown, 10, 1);
219

2
    place(6, 7, mHelpFontDropDown, 10, 1);
220

2
    place(6, 8, mSecureFontDropDown, 10, 1);
221

2
    place(6, 9, mNpcFontDropDown, 10, 1);
222

2
    place(6, 10, mJapanFontDropDown, 10, 1);
223

2
    place(6, 11, mChinaFontDropDown, 10, 1);
224
225
2
    place(17, 0, mInfoButton, 1, 1);
226
227
2
    int size = mainGraphics->mWidth - 10;
228
2
    const int maxWidth = mFontSize * 30 + 290;
229
2
    if (size < 465)
230
        size = 465;
231
2
    else if (size > maxWidth)
232
        size = maxWidth;
233
234
2
    setDimension(Rect(0, 0, size, 500));
235
2
}
236
237
28
Setup_Theme::~Setup_Theme()
238
{
239
2
    delete2(mInfo)
240
4
    delete2(mThemesModel)
241
4
    delete2(mFontsModel)
242
4
    delete2(mFontSizeListModel)
243
4
    delete2(mNpcFontSizeListModel)
244
2
    delete2(mLangListModel)
245
4
}
246
247
2
void Setup_Theme::updateInfo()
248
{
249
2
    delete mInfo;
250
2
    mInfo = Theme::loadInfo(mTheme);
251
2
    if (mInfo != nullptr)
252
    {
253
        // TRANSLATORS: theme name
254
10
        mThemeInfo = std::string(_("Name: ")).append(mInfo->name)
255
            // TRANSLATORS: theme copyright
256

4
            .append("\n").append(_("Copyright:")).append("\n")
257
6
            .append(mInfo->copyright);
258
    }
259
    else
260
    {
261
        mThemeInfo.clear();
262
    }
263

14
    replaceAll(mThemeInfo, "\\n", "\n");
264
6
    mInfoButton->setEnabled(!mThemeInfo.empty());
265
2
}
266
267
void Setup_Theme::action(const ActionEvent &event)
268
{
269
    const std::string &eventId = event.getId();
270
    if (eventId == ACTION_THEME)
271
    {
272
        if (mThemeDropDown->getSelected() == 0)
273
            mTheme.clear();
274
        else
275
            mTheme = mThemeDropDown->getSelectedString();
276
        updateInfo();
277
    }
278
    else if (eventId == ACTION_FONT)
279
    {
280
        mFont = mFontDropDown->getSelectedString();
281
    }
282
    else if (eventId == ACTION_LANG)
283
    {
284
        const int id = mLangDropDown->getSelected();
285
        if (id < 0 || id >= langs_count)
286
            mLang.clear();
287
        else
288
            mLang = LANG_NAME[id].value;
289
    }
290
    else if (eventId == ACTION_BOLD_FONT)
291
    {
292
        mBoldFont = mBoldFontDropDown->getSelectedString();
293
    }
294
    else if (eventId == ACTION_PARTICLE_FONT)
295
    {
296
        mParticleFont = mParticleFontDropDown->getSelectedString();
297
    }
298
    else if (eventId == ACTION_HELP_FONT)
299
    {
300
        mHelpFont = mHelpFontDropDown->getSelectedString();
301
    }
302
    else if (eventId == ACTION_SECURE_FONT)
303
    {
304
        mSecureFont = mSecureFontDropDown->getSelectedString();
305
    }
306
    else if (eventId == ACTION_NPC_FONT)
307
    {
308
        mNpcFont = mNpcFontDropDown->getSelectedString();
309
    }
310
    else if (eventId == ACTION_JAPAN_FONT)
311
    {
312
        mJapanFont = mJapanFontDropDown->getSelectedString();
313
    }
314
    else if (eventId == ACTION_CHINA_FONT)
315
    {
316
        mChinaFont = mChinaFontDropDown->getSelectedString();
317
    }
318
    else if (eventId == ACTION_INFO)
319
    {
320
        CREATEWIDGET(OkDialog,
321
            // TRANSLATORS: theme info dialog header
322
            _("Theme info"),
323
            mThemeInfo,
324
            // TRANSLATORS: ok dialog button
325
            _("OK"),
326
            DialogType::OK,
327
            Modal_true,
328
            ShowCenter_true,
329
            nullptr,
330
            600);
331
    }
332
}
333
334
void Setup_Theme::cancel()
335
{
336
    mTheme = config.getStringValue("theme");
337
    mLang = config.getStringValue("lang");
338
    mFont = getFileName(config.getStringValue("font"));
339
    mBoldFont = getFileName(config.getStringValue("boldFont"));
340
    mParticleFont = getFileName(config.getStringValue("particleFont"));
341
    mHelpFont = getFileName(config.getStringValue("helpFont"));
342
    mSecureFont = getFileName(config.getStringValue("secureFont"));
343
    mNpcFont = getFileName(config.getStringValue("npcFont"));
344
    mJapanFont = getFileName(config.getStringValue("japanFont"));
345
    mChinaFont = getFileName(config.getStringValue("chinaFont"));
346
}
347
348
#define updateField(name1, name2) if (!mInfo->name1.empty()) \
349
    name2 = mInfo->name1
350
351
void Setup_Theme::apply()
352
{
353
    if (config.getStringValue("theme") != mTheme)
354
    {
355
        CREATEWIDGET(OkDialog,
356
            // TRANSLATORS: theme message dialog
357
            _("Theme Changed"),
358
            // TRANSLATORS: ok dialog message
359
            _("Restart your client for the change to take effect."),
360
            // TRANSLATORS: ok dialog button
361
            _("OK"),
362
            DialogType::OK,
363
            Modal_true,
364
            ShowCenter_true,
365
            nullptr,
366
            260);
367
    }
368
369
    config.setValue("selectedSkin", "");
370
    if (config.getStringValue("theme") != mTheme && (mInfo != nullptr))
371
    {
372
        updateField(font, mFont);
373
        updateField(boldFont, mBoldFont);
374
        updateField(particleFont, mParticleFont);
375
        updateField(helpFont, mHelpFont);
376
        updateField(secureFont, mSecureFont);
377
        updateField(npcFont, mNpcFont);
378
        updateField(japanFont, mJapanFont);
379
        updateField(chinaFont, mChinaFont);
380
        if (mInfo->fontSize != 0)
381
        {
382
            const int size = mInfo->fontSize - 9;
383
            if (size >= 0)
384
                mFontSizeDropDown->setSelected(size);
385
        }
386
        if (mInfo->npcfontSize != 0)
387
        {
388
            const int size = mInfo->npcfontSize - 9;
389
            if (size >= 0)
390
                mNpcFontSizeDropDown->setSelected(size);
391
        }
392
        if (mInfo->guiAlpha > 0.01F)
393
            config.setValue("guialpha", mInfo->guiAlpha);
394
    }
395
    config.setValue("theme", mTheme);
396
    config.setValue("lang", mLang);
397
    if (config.getValue("font", "dejavusans.ttf") != mFont
398
        || config.getValue("boldFont", "dejavusans-bold.ttf") != mBoldFont
399
        || config.getValue("particleFont", "dejavusans.ttf") != mParticleFont
400
        || config.getValue("helpFont", "dejavusansmono.ttf") != mHelpFont
401
        || config.getValue("secureFont", "dejavusansmono.ttf") != mSecureFont
402
        || config.getValue("npcFont", "dejavusans.ttf") != mNpcFont
403
        || config.getValue("japanFont", "mplus-1p-regular.ttf") != mJapanFont
404
        || config.getValue("chinaFont", "fonts/wqy-microhei.ttf")
405
        != mChinaFont
406
        || config.getIntValue("fontSize")
407
        != CAST_S32(mFontSizeDropDown->getSelected()) + 9
408
        || config.getIntValue("npcfontSize")
409
        != CAST_S32(mNpcFontSizeDropDown->getSelected()) + 9)
410
    {
411
        config.setValue("font", "fonts/" + getFileName(mFont));
412
        config.setValue("boldFont", "fonts/" + getFileName(mBoldFont));
413
        config.setValue("particleFont", "fonts/" + getFileName(mParticleFont));
414
        config.setValue("helpFont", "fonts/" + getFileName(mHelpFont));
415
        config.setValue("secureFont", "fonts/" + getFileName(mSecureFont));
416
        config.setValue("npcFont", "fonts/" + getFileName(mNpcFont));
417
        config.setValue("japanFont", "fonts/" + getFileName(mJapanFont));
418
        config.setValue("chinaFont", "fonts/" + getFileName(mChinaFont));
419
        config.setValue("fontSize", mFontSizeDropDown->getSelected() + 9);
420
        config.setValue("npcfontSize",
421
            mNpcFontSizeDropDown->getSelected() + 9);
422
        gui->updateFonts();
423
    }
424

3
}
425
426
#undef updateField