GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/didyouknowwindow.cpp Lines: 48 86 55.8 %
Date: 2021-03-17 Branches: 53 150 35.3 %

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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/windows/didyouknowwindow.h"
25
26
#include "configuration.h"
27
28
#include "enums/gui/layouttype.h"
29
30
#include "gui/gui.h"
31
32
#include "gui/windows/setupwindow.h"
33
34
#include "gui/widgets/button.h"
35
#include "gui/widgets/checkbox.h"
36
#include "gui/widgets/itemlinkhandler.h"
37
#include "gui/widgets/layout.h"
38
#include "gui/widgets/scrollarea.h"
39
#include "gui/widgets/staticbrowserbox.h"
40
41
#include "utils/delete2.h"
42
#include "utils/gettext.h"
43
44
#include "utils/translation/podict.h"
45
#include "utils/translation/translationmanager.h"
46
47
#include "debug.h"
48
49
DidYouKnowWindow *didYouKnowWindow = nullptr;
50
51
static const int minTip = 1;
52
static const int maxTip = 18;
53
54
2
DidYouKnowWindow::DidYouKnowWindow() :
55
    // TRANSLATORS: did you know window name
56
2
    Window(_("Did You Know?"), Modal_false, nullptr, "didyouknow.xml"),
57
    ActionListener(),
58

2
    mItemLinkHandler(new ItemLinkHandler),
59
    mBrowserBox(new StaticBrowserBox(this, Opaque_true,
60

2
        "browserbox.xml")),
61
2
    mScrollArea(new ScrollArea(this, mBrowserBox,
62

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

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

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







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


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

3
}