GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/sliderlist.cpp Lines: 63 109 57.8 %
Date: 2017-11-29 Branches: 37 74 50.0 %

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/sliderlist.h"
22
23
#include "gui/gui.h"
24
25
#include "gui/fonts/font.h"
26
27
#include "gui/models/listmodel.h"
28
29
#include "gui/widgets/button.h"
30
#include "gui/widgets/label.h"
31
32
#include "debug.h"
33
34
static const int buttonWidth = 27;
35
static const int buttonSpace = 30;
36
static const int sliderHeight = 30;
37
38
44
SliderList::SliderList(const Widget2 *const widget,
39
44
                       ListModel *const listModel) :
40
    Container(widget),
41
    ActionListener(),
42
    MouseListener(),
43
    mButtons(),
44

44
    mLabel(new Label(this)),
45
    mListModel(listModel),
46
    mPrevEventId(),
47
    mNextEventId(),
48
    mOldWidth(0),
49
264
    mSelectedIndex(0)
50
{
51
44
    mAllowLogic = false;
52
44
    setHeight(sliderHeight);
53
44
}
54
55
44
void SliderList::postInit2(ActionListener *const listener,
56
                           const std::string &eventId)
57
{
58
88
    mPrevEventId = eventId + "_prev";
59
88
    mNextEventId = eventId + "_next";
60
61

176
    mButtons[0] = new Button(this, "<", mPrevEventId, this);
62

176
    mButtons[1] = new Button(this, ">", mNextEventId, this);
63
64
44
    add(mButtons[0]);
65
44
    add(mLabel);
66
44
    add(mButtons[1]);
67
68
44
    if (!eventId.empty())
69
44
        setActionEventId(eventId);
70
71
44
    if (listener != nullptr)
72
44
        addActionListener(listener);
73
74
44
    updateLabel();
75
44
    addMouseListener(this);
76
44
}
77
78
264
SliderList::~SliderList()
79
{
80
88
}
81
82
void SliderList::updateAlpha()
83
{
84
    Button::updateAlpha();
85
}
86
87
void SliderList::mouseWheelMovedUp(MouseEvent& event)
88
{
89
    setSelected(mSelectedIndex - 1);
90
    event.consume();
91
}
92
93
void SliderList::mouseWheelMovedDown(MouseEvent& event)
94
{
95
    setSelected(mSelectedIndex + 1);
96
    event.consume();
97
}
98
99
void SliderList::resize()
100
{
101
    const int width = getWidth();
102
103
    mButtons[0]->setWidth(buttonWidth);
104
    mLabel->setWidth(width - buttonSpace * 2);
105
    mButtons[1]->setPosition(width - buttonSpace + 3, 0);
106
    mButtons[1]->setWidth(buttonWidth);
107
    updateLabel();
108
}
109
110
void SliderList::draw(Graphics *const graphics)
111
{
112
    BLOCK_START("SliderList::draw")
113
    const int width = mDimension.width;
114
    if (mOldWidth != width)
115
    {
116
        resize();
117
        mOldWidth = width;
118
    }
119
    Container::draw(graphics);
120
    BLOCK_END("SliderList::draw")
121
}
122
123
void SliderList::safeDraw(Graphics *const graphics)
124
{
125
    BLOCK_START("SliderList::draw")
126
    const int width = mDimension.width;
127
    if (mOldWidth != width)
128
    {
129
        resize();
130
        mOldWidth = width;
131
    }
132
    Container::draw(graphics);
133
    BLOCK_END("SliderList::draw")
134
}
135
136
132
void SliderList::updateLabel()
137
{
138
264
    if ((mListModel == nullptr) || mSelectedIndex < 0
139

264
        || mSelectedIndex >= mListModel->getNumberOfElements())
140
    {
141
        return;
142
    }
143
144
264
    mLabel->setCaption(mListModel->getElementAt(mSelectedIndex));
145
132
    mLabel->adjustSize();
146
132
    const int space = mDimension.width - buttonSpace * 2;
147
264
    const int labelWidth = mLabel->getWidth();
148
264
    int labelY = (mDimension.height - mLabel->getHeight()) / 2;
149
132
    if (labelY < 0)
150
        labelY = 0;
151
152
132
    if (space < 0 || space < labelWidth)
153
84
        mLabel->setPosition(buttonSpace, labelY);
154
    else
155
48
        mLabel->setPosition(buttonSpace + (space - labelWidth) / 2, labelY);
156
}
157
158
void SliderList::action(const ActionEvent &event)
159
{
160
    if (mListModel == nullptr)
161
        return;
162
163
    const std::string &eventId = event.getId();
164
    if (eventId == mPrevEventId)
165
    {
166
        mSelectedIndex --;
167
        if (mSelectedIndex < 0)
168
            mSelectedIndex = mListModel->getNumberOfElements() - 1;
169
    }
170
    else if (eventId == mNextEventId)
171
    {
172
        mSelectedIndex ++;
173
        if (mSelectedIndex >= mListModel->getNumberOfElements())
174
            mSelectedIndex = 0;
175
    }
176
    updateLabel();
177
    distributeActionEvent();
178
}
179
180
44
void SliderList::setSelectedString(const std::string &str)
181
{
182
44
    if (mListModel == nullptr)
183
        return;
184
185
892
    for (int f = 0; f < mListModel->getNumberOfElements(); f ++)
186
    {
187
1392
        if (mListModel->getElementAt(f) == str)
188
        {
189
40
            setSelected(f);
190
40
            break;
191
        }
192
    }
193
}
194
195
std::string SliderList::getSelectedString() const
196
{
197
    if (mListModel == nullptr)
198
        return std::string();
199
200
    return mListModel->getElementAt(mSelectedIndex);
201
}
202
203
44
void SliderList::setSelected(const int idx)
204
{
205
44
    if (mListModel == nullptr)
206
        return;
207
208
44
    mSelectedIndex = idx;
209
44
    const int num = mListModel->getNumberOfElements();
210
44
    if (mSelectedIndex >= num)
211
        mSelectedIndex = 0;
212
44
    if (mSelectedIndex < 0)
213
        mSelectedIndex = num - 1;
214
44
    updateLabel();
215
}
216
217
44
void SliderList::adjustSize()
218
{
219
44
    setWidth(getMaxLabelWidth() + 60);
220
44
    updateLabel();
221
44
}
222
223
44
int SliderList::getMaxLabelWidth() const
224
{
225

44
    if ((mListModel == nullptr) || (gui == nullptr))
226
        return 1;
227
228
44
    int maxWidth = 0;
229
44
    const Font *const font = getFont();
230
231
44
    const int num = mListModel->getNumberOfElements();
232
872
    for (int f = 0; f < num; f ++)
233
    {
234
1656
        const int w = font->getWidth(mListModel->getElementAt(f));
235
828
        if (w > maxWidth)
236
84
            maxWidth = w;
237
    }
238
239
    return maxWidth;
240

6
}