GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/didyouknowwindow.cpp Lines: 48 85 56.5 %
Date: 2018-05-19 03:07:18 Branches: 54 158 34.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
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/didyouknowwindow.h"
24
25
#include "configuration.h"
26
27
#include "enums/gui/layouttype.h"
28
29
#include "gui/gui.h"
30
31
#include "gui/windows/setupwindow.h"
32
33
#include "gui/widgets/button.h"
34
#include "gui/widgets/checkbox.h"
35
#include "gui/widgets/itemlinkhandler.h"
36
#include "gui/widgets/layout.h"
37
#include "gui/widgets/scrollarea.h"
38
#include "gui/widgets/staticbrowserbox.h"
39
40
#include "utils/delete2.h"
41
#include "utils/gettext.h"
42
43
#include "utils/translation/podict.h"
44
#include "utils/translation/translationmanager.h"
45
46
#include "debug.h"
47
48
DidYouKnowWindow *didYouKnowWindow = nullptr;
49
50
static const int minTip = 1;
51
static const int maxTip = 18;
52
53
4
DidYouKnowWindow::DidYouKnowWindow() :
54
    // TRANSLATORS: did you know window name
55
4
    Window(_("Did You Know?"), Modal_false, nullptr, "didyouknow.xml"),
56
    ActionListener(),
57

4
    mItemLinkHandler(new ItemLinkHandler),
58
    mBrowserBox(new StaticBrowserBox(this, Opaque_true,
59

4
        "browserbox.xml")),
60
4
    mScrollArea(new ScrollArea(this, mBrowserBox,
61

8
        Opaque_true, "didyouknow_background.xml")),
62
    // TRANSLATORS: did you know window button
63

8
    mButtonPrev(new Button(this, _("< Previous"), "prev", BUTTON_SKIN, this)),
64
    // TRANSLATORS: did you know window button
65

8
    mButtonNext(new Button(this, _("Next >"), "next", BUTTON_SKIN, this)),
66
    // TRANSLATORS: did you know window checkbox
67
4
    mOpenAgainCheckBox(new CheckBox(this, _("Auto open this window"),
68







160
        config.getBoolValue("showDidYouKnow"), this, "openagain"))
69
{
70
4
    setMinWidth(300);
71
4
    setMinHeight(220);
72
4
    setContentSize(455, 350);
73
20
    setWindowName("DidYouKnow");
74
4
    setCloseButton(true);
75
4
    setResizable(true);
76
4
    setStickyButtonLock(true);
77
78
4
    if (setupWindow != nullptr)
79
2
        setupWindow->registerWindowForReset(this);
80
4
    setDefaultSize(500, 400, ImagePosition::CENTER, 0, 0);
81
82
8
    mBrowserBox->setOpaque(Opaque_false);
83
    Button *const okButton = new Button(this,
84
        // TRANSLATORS: did you know window button
85
4
        _("Close"),
86
        "close",
87
        BUTTON_SKIN,
88


28
        this);
89
90
4
    mBrowserBox->setLinkHandler(mItemLinkHandler);
91
4
    if (gui != nullptr)
92
8
        mBrowserBox->setFont(gui->getHelpFont());
93
8
    mBrowserBox->setProcessVars(true);
94
8
    mBrowserBox->setEnableImages(true);
95
8
    mBrowserBox->setEnableKeys(true);
96
8
    mBrowserBox->setEnableTabs(true);
97
98
8
    place(0, 0, mScrollArea, 5, 3).setPadding(3);
99
4
    place(0, 3, mOpenAgainCheckBox, 5, 1);
100
4
    place(1, 4, mButtonPrev, 1, 1);
101
4
    place(2, 4, mButtonNext, 1, 1);
102
4
    place(4, 4, okButton, 1, 1);
103
104
4
    Layout &layout = getLayout();
105
4
    layout.setRowHeight(0, LayoutType::SET);
106
107
4
    loadWindowState();
108
8
    enableVisibleSound(true);
109
4
}
110
111
4
void DidYouKnowWindow::postInit()
112
{
113
4
    Window::postInit();
114
8
    widgetResized(Event(nullptr));
115
4
}
116
117
16
DidYouKnowWindow::~DidYouKnowWindow()
118
{
119
4
    delete2(mItemLinkHandler);
120
8
}
121
122
void DidYouKnowWindow::action(const ActionEvent &event)
123
{
124
    const std::string &eventId = event.getId();
125
    if (eventId == "close")
126
    {
127
        setVisible(Visible_false);
128
    }
129
    else
130
    {
131
        const unsigned num = config.getIntValue("currentTip");
132
        if (eventId == "prev")
133
        {
134
            loadData(num - 1);
135
        }
136
        else if (eventId == "next")
137
        {
138
            loadData(num + 1);
139
        }
140
        else if (eventId == "openagain")
141
        {
142
            config.setValue("showDidYouKnow",
143
                mOpenAgainCheckBox->isSelected());
144
        }
145
    }
146
}
147
148
void DidYouKnowWindow::loadData(int num)
149
{
150
    mBrowserBox->clearRows();
151
    if (num == 0)
152
    {
153
        const int curTip = config.getIntValue("currentTip");
154
        if (curTip == 1)
155
            num = maxTip;
156
        else
157
            num = curTip + 1;
158
    }
159
160
    if (num < minTip || num > maxTip)
161
        num = minTip;
162
163
    config.setValue("currentTip", num);
164
165
    loadFile(num);
166
167
    mBrowserBox->updateHeight();
168
    mScrollArea->setVerticalScrollAmount(0);
169
}
170
171
void DidYouKnowWindow::loadFile(const int num)
172
{
173
    const std::string file = strprintf("tips/%d", num);
174
    std::string helpPath = branding.getStringValue("helpPath");
175
    if (helpPath.empty())
176
        helpPath = paths.getStringValue("help");
177
178
    StringVect lines;
179
    TranslationManager::translateFile(pathJoin(helpPath, file).append(".txt"),
180
        translator, lines);
181
182
    for (size_t i = 0, sz = lines.size(); i < sz; ++i)
183
        mBrowserBox->addRow(lines[i], false);
184
}
185
186
void DidYouKnowWindow::setVisible(Visible visible)
187
{
188
    Window::setVisible(visible);
189
190
    if (visible == Visible_true || isWindowVisible())
191
        loadData(0);
192

6
}