GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_theme.cpp Lines: 137 231 59.3 %
Date: 2017-11-29 Branches: 233 868 26.8 %

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/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
4
Setup_Theme::Setup_Theme(const Widget2 *const widget) :
61
    SetupTab(widget),
62
    // TRANSLATORS: theme settings label
63

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

4
    mThemesModel(new ThemesModel),
65

4
    mThemeDropDown(new DropDown(this, mThemesModel)),
66
    mTheme(config.getStringValue("theme")),
67
4
    mInfo(Theme::loadInfo(mTheme)),
68

4
    mFontsModel(new FontsModel),
69
    // TRANSLATORS: theme settings label
70

8
    mFontLabel(new Label(this, _("Main Font"))),
71

4
    mFontDropDown(new DropDown(this, mFontsModel)),
72
    mFont(config.getStringValue("font")),
73

4
    mLangListModel(new LangListModel),
74
    // TRANSLATORS: theme settings label
75

8
    mLangLabel(new Label(this, _("Language"))),
76

4
    mLangDropDown(new DropDown(this, mLangListModel, true)),
77
    mLang(config.getStringValue("lang")),
78
    // TRANSLATORS: theme settings label
79

8
    mBoldFontLabel(new Label(this, _("Bold font"))),
80

4
    mBoldFontDropDown(new DropDown(this, mFontsModel)),
81
    mBoldFont(config.getStringValue("boldFont")),
82
    // TRANSLATORS: theme settings label
83

8
    mParticleFontLabel(new Label(this, _("Particle font"))),
84

4
    mParticleFontDropDown(new DropDown(this, mFontsModel)),
85
    mParticleFont(config.getStringValue("particleFont")),
86
    // TRANSLATORS: theme settings label
87

8
    mHelpFontLabel(new Label(this, _("Help font"))),
88

4
    mHelpFontDropDown(new DropDown(this, mFontsModel)),
89
    mHelpFont(config.getStringValue("helpFont")),
90
    // TRANSLATORS: theme settings label
91

8
    mSecureFontLabel(new Label(this, _("Secure font"))),
92

4
    mSecureFontDropDown(new DropDown(this, mFontsModel)),
93
    mSecureFont(config.getStringValue("secureFont")),
94
    // TRANSLATORS: theme settings label
95

8
    mNpcFontLabel(new Label(this, _("Npc font"))),
96

4
    mNpcFontDropDown(new DropDown(this, mFontsModel)),
97
    mNpcFont(config.getStringValue("npcFont")),
98
    // TRANSLATORS: theme settings label
99

8
    mJapanFontLabel(new Label(this, _("Japanese font"))),
100

4
    mJapanFontDropDown(new DropDown(this, mFontsModel)),
101
    mJapanFont(config.getStringValue("japanFont")),
102
    // TRANSLATORS: theme settings label
103

8
    mChinaFontLabel(new Label(this, _("Chinese font"))),
104

4
    mChinaFontDropDown(new DropDown(this, mFontsModel)),
105
    mChinaFont(config.getStringValue("chinaFont")),
106
8
    mFontSizeListModel(new FontSizeChoiceListModel),
107
    // TRANSLATORS: theme settings label
108

8
    mFontSizeLabel(new Label(this, _("Font size"))),
109

16
    mFontSize(config.getIntValue("fontSize")),
110

4
    mFontSizeDropDown(new DropDown(this, mFontSizeListModel)),
111
8
    mNpcFontSizeListModel(new FontSizeChoiceListModel),
112
    // TRANSLATORS: theme settings label
113

8
    mNpcFontSizeLabel(new Label(this, _("Npc font size"))),
114

16
    mNpcFontSize(config.getIntValue("npcfontSize")),
115

4
    mNpcFontSizeDropDown(new DropDown(this, mNpcFontSizeListModel)),
116
    // TRANSLATORS: button name with information about selected theme
117

8
    mInfoButton(new Button(this, _("i"), ACTION_INFO, this)),
118























580
    mThemeInfo()
119
{
120
    // TRANSLATORS: theme settings tab name
121
20
    setName(_("Theme"));
122
123
20
    mThemeDropDown->setActionEventId(ACTION_THEME);
124
4
    mThemeDropDown->addActionListener(this);
125
20
    mFontDropDown->setActionEventId(ACTION_FONT);
126
4
    mFontDropDown->addActionListener(this);
127
20
    mLangDropDown->setActionEventId(ACTION_LANG);
128
4
    mLangDropDown->addActionListener(this);
129
20
    mBoldFontDropDown->setActionEventId(ACTION_BOLD_FONT);
130
4
    mBoldFontDropDown->addActionListener(this);
131
16
    mParticleFontDropDown->setActionEventId(ACTION_PARTICLE_FONT);
132
4
    mParticleFontDropDown->addActionListener(this);
133
20
    mHelpFontDropDown->setActionEventId(ACTION_HELP_FONT);
134
4
    mHelpFontDropDown->addActionListener(this);
135
20
    mSecureFontDropDown->setActionEventId(ACTION_SECURE_FONT);
136
4
    mSecureFontDropDown->addActionListener(this);
137
20
    mNpcFontDropDown->setActionEventId(ACTION_NPC_FONT);
138
4
    mNpcFontDropDown->addActionListener(this);
139
16
    mJapanFontDropDown->setActionEventId(ACTION_JAPAN_FONT);
140
4
    mJapanFontDropDown->addActionListener(this);
141
20
    mChinaFontDropDown->setActionEventId(ACTION_CHINA_FONT);
142
4
    mChinaFontDropDown->addActionListener(this);
143
4
    mFontSizeDropDown->setSelected(mFontSize - 9);
144
4
    mFontSizeDropDown->adjustHeight();
145
4
    mNpcFontSizeDropDown->setSelected(mNpcFontSize - 9);
146
4
    mNpcFontSizeDropDown->adjustHeight();
147
148
4
    const std::string skin = Theme::getThemeName();
149
4
    if (!skin.empty())
150
4
        mThemeDropDown->setSelectedString(skin);
151
    else
152
        mThemeDropDown->setSelected(0);
153
154

20
    const std::string str = config.getStringValue("lang");
155
4
    for (int f = 0; f < langs_count; f ++)
156
    {
157
8
        if (LANG_NAME[f].value == str)
158
        {
159
4
            mLangDropDown->setSelected(f);
160
            break;
161
        }
162
    }
163
164

12
    mFontDropDown->setSelectedString(getFileName(
165

20
        config.getStringValue("font")));
166

12
    mBoldFontDropDown->setSelectedString(getFileName(
167

20
        config.getStringValue("boldFont")));
168

12
    mParticleFontDropDown->setSelectedString(getFileName(
169

20
        config.getStringValue("particleFont")));
170

12
    mHelpFontDropDown->setSelectedString(getFileName(
171

20
        config.getStringValue("helpFont")));
172

12
    mSecureFontDropDown->setSelectedString(getFileName(
173

20
        config.getStringValue("secureFont")));
174

12
    mNpcFontDropDown->setSelectedString(getFileName(
175

20
        config.getStringValue("npcFont")));
176

12
    mJapanFontDropDown->setSelectedString(getFileName(
177

20
        config.getStringValue("japanFont")));
178

12
    mChinaFontDropDown->setSelectedString(getFileName(
179

20
        config.getStringValue("chinaFont")));
180
181
4
    updateInfo();
182
183
    // Do the layout
184
8
    LayoutHelper h(this);
185
4
    ContainerPlacer place = h.getPlacer(0, 0);
186
187
4
    place(0, 0, mThemeLabel, 5);
188
4
    place(0, 1, mLangLabel, 5);
189
4
    place(0, 2, mFontSizeLabel, 5);
190
4
    place(0, 3, mNpcFontSizeLabel, 5);
191
4
    place(0, 4, mFontLabel, 5);
192
4
    place(0, 5, mBoldFontLabel, 5);
193
4
    place(0, 6, mParticleFontLabel, 5);
194
4
    place(0, 7, mHelpFontLabel, 5);
195
4
    place(0, 8, mSecureFontLabel, 5);
196
4
    place(0, 9, mNpcFontLabel, 5);
197
4
    place(0, 10, mJapanFontLabel, 5);
198
4
    place(0, 11, mChinaFontLabel, 5);
199
200

4
    place(6, 0, mThemeDropDown, 10);
201

4
    place(6, 1, mLangDropDown, 10);
202

4
    place(6, 2, mFontSizeDropDown, 10);
203

4
    place(6, 3, mNpcFontSizeDropDown, 10);
204

4
    place(6, 4, mFontDropDown, 10);
205

4
    place(6, 5, mBoldFontDropDown, 10);
206

4
    place(6, 6, mParticleFontDropDown, 10);
207

4
    place(6, 7, mHelpFontDropDown, 10);
208

4
    place(6, 8, mSecureFontDropDown, 10);
209

4
    place(6, 9, mNpcFontDropDown, 10);
210

4
    place(6, 10, mJapanFontDropDown, 10);
211

4
    place(6, 11, mChinaFontDropDown, 10);
212
213
4
    place(17, 0, mInfoButton, 1);
214
215
4
    int size = mainGraphics->mWidth - 10;
216
4
    const int maxWidth = mFontSize * 30 + 290;
217
4
    if (size < 465)
218
        size = 465;
219
4
    else if (size > maxWidth)
220
        size = maxWidth;
221
222
8
    setDimension(Rect(0, 0, size, 500));
223
4
}
224
225
56
Setup_Theme::~Setup_Theme()
226
{
227
4
    delete2(mInfo);
228
8
    delete2(mThemesModel);
229
8
    delete2(mFontsModel);
230
8
    delete2(mFontSizeListModel);
231
8
    delete2(mNpcFontSizeListModel);
232
4
    delete2(mLangListModel);
233
4
    delete2(mInfo);
234
8
}
235
236
4
void Setup_Theme::updateInfo()
237
{
238
4
    delete mInfo;
239
4
    mInfo = Theme::loadInfo(mTheme);
240
4
    if (mInfo != nullptr)
241
    {
242
        // TRANSLATORS: theme name
243
24
        mThemeInfo = std::string(_("Name: ")).append(mInfo->name)
244
            // TRANSLATORS: theme copyright
245

4
            .append("\n").append(_("Copyright:")).append("\n")
246
8
            .append(mInfo->copyright);
247
    }
248
    else
249
    {
250
        mThemeInfo.clear();
251
    }
252

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

6
}
414
415
#undef updateField