GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/statspagebasic.cpp Lines: 20 64 31.3 %
Date: 2017-11-29 Branches: 21 89 23.6 %

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

2
    mAttrCont(new VertContainer(this, 32)),
45

2
    mAttrScroll(new ScrollArea(this, mAttrCont, Opaque_false)),
46



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

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