GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/statspagebasic.cpp Lines: 21 63 33.3 %
Date: 2021-03-17 Branches: 20 93 21.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/widgets/statspagebasic.h"
23
24
#include "being/playerinfo.h"
25
26
#include "gui/widgets/scrollarea.h"
27
#include "gui/widgets/vertcontainer.h"
28
29
#include "gui/widgets/attrs/changedisplay.h"
30
31
#include "utils/gettext.h"
32
#include "utils/foreach.h"
33
#include "utils/stringutils.h"
34
35
#include "resources/db/statdb.h"
36
37
#include "debug.h"
38
39
1
StatsPageBasic::StatsPageBasic(const Widget2 *const widget) :
40
    Container(widget),
41
    WidgetListener(),
42
    AttributeListener(),
43
    StatListener(),
44
    mAttrs(),
45

1
    mAttrCont(new VertContainer(this, 32, true, 0)),
46

2
    mAttrScroll(new ScrollArea(this, mAttrCont, Opaque_false, std::string())),
47


9
    mCharacterPointsLabel(new Label(this, "C"))
48
{
49
1
    addWidgetListener(this);
50
2
    setSelectable(false);
51
52
1
    mAttrScroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
53
1
    mAttrScroll->setVerticalScrollPolicy(ScrollArea::SHOW_AUTO);
54
2
    mAttrScroll->setSelectable(false);
55
2
    mAttrCont->setSelectable(false);
56
57
1
    add(mAttrScroll);
58
1
    const STD_VECTOR<BasicStat> &basicStats = StatDb::getBasicStats();
59
4
    FOR_EACH (STD_VECTOR<BasicStat>::const_iterator, it, basicStats)
60
    {
61
        const BasicStat &stat = *it;
62
        AttrDisplay *disp = new ChangeDisplay(this,
63
            stat.attr,
64
            stat.name,
65
            stat.tag);
66
        disp->update();
67
        mAttrCont->add2(disp, true, -1);
68
        mAttrs[stat.attr] = disp;
69
    }
70
71
    // TRANSLATORS: status window label
72

2
    mCharacterPointsLabel->setCaption(strprintf(_("Character points: %d"),
73
1
        PlayerInfo::getAttribute(Attributes::PLAYER_CHAR_POINTS)));
74
1
    mCharacterPointsLabel->adjustSize();
75
1
    mAttrCont->add1(mCharacterPointsLabel, -1);
76
1
}
77
78
1
void StatsPageBasic::widgetResized(const Event &event A_UNUSED)
79
{
80
3
    mAttrScroll->setSize(getWidth(), getHeight());
81
1
}
82
83
void StatsPageBasic::statChanged(const AttributesT id,
84
                                 const int oldVal1 A_UNUSED,
85
                                 const int oldVal2 A_UNUSED)
86
{
87
    const Attrs::const_iterator it = mAttrs.find(id);
88
    if (it != mAttrs.end() && (it->second != nullptr))
89
        it->second->update();
90
}
91
92
void StatsPageBasic::attributeChanged(const AttributesT id,
93
                                      const int64_t oldVal A_UNUSED,
94
                                      const int64_t newVal)
95
{
96
    PRAGMA45(GCC diagnostic push)
97
    PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
98
    switch (id)
99
    {
100
        case Attributes::PLAYER_CHAR_POINTS:
101
            mCharacterPointsLabel->setCaption(strprintf(
102
                // TRANSLATORS: status window label
103
                _("Character points: %d"), CAST_S32(newVal)));
104
105
            mCharacterPointsLabel->adjustSize();
106
            // Update all attributes
107
            for (Attrs::const_iterator it = mAttrs.begin();
108
                 it != mAttrs.end(); ++it)
109
            {
110
                if (it->second != nullptr)
111
                    it->second->update();
112
            }
113
            return;
114
115
        case Attributes::PLAYER_CORR_POINTS:
116
            // Update all attributes
117
            for (Attrs::const_iterator it = mAttrs.begin();
118
                 it != mAttrs.end(); ++it)
119
            {
120
                if (it->second != nullptr)
121
                    it->second->update();
122
            }
123
            return;
124
125
        default:
126
            break;
127
    }
128
    PRAGMA45(GCC diagnostic pop)
129
    const Attrs::const_iterator it = mAttrs.find(id);
130
    if (it != mAttrs.end() && (it->second != nullptr))
131
        it->second->update();
132
}
133
134
void StatsPageBasic::setPointsNeeded(const AttributesT id,
135
                                     const int needed)
136
{
137
    const Attrs::const_iterator it = mAttrs.find(id);
138
139
    if (it != mAttrs.end())
140
    {
141
        AttrDisplay *const disp = it->second;
142
        if ((disp != nullptr) && disp->getType() == AttrDisplay::CHANGEABLE)
143
            static_cast<ChangeDisplay*>(disp)->setPointsNeeded(needed);
144
    }
145
}
146
147
std::string StatsPageBasic::getStatsStr() const
148
{
149
    Attrs::const_iterator it = mAttrs.begin();
150
    const Attrs::const_iterator it_end = mAttrs.end();
151
    std::string str;
152
    while (it != it_end)
153
    {
154
        const ChangeDisplay *const attr = dynamic_cast<ChangeDisplay*>(
155
            (*it).second);
156
        if (attr != nullptr)
157
        {
158
            str.append(strprintf("%s:%s ", attr->getShortName().c_str(),
159
                attr->getValue().c_str()));
160
        }
161
        ++ it;
162
    }
163
    return str;
164
}