GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/didyouknowwindow.cpp Lines: 47 84 56.0 %
Date: 2017-11-29 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-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/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", this)),
64
    // TRANSLATORS: did you know window button
65

8
    mButtonNext(new Button(this, _("Next >"), "next", 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);
81
82
8
    mBrowserBox->setOpaque(Opaque_false);
83
    // TRANSLATORS: did you know window button
84


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

6
}