GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/sliderlist.cpp Lines: 65 112 58.0 %
Date: 2018-05-19 03:07:18 Branches: 37 74 50.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2018  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
52
SliderList::SliderList(const Widget2 *const widget,
39
52
                       ListModel *const listModel) :
40
    Container(widget),
41
    ActionListener(),
42
    MouseListener(),
43
    mButtons(),
44

52
    mLabel(new Label(this)),
45
    mListModel(listModel),
46
    mPrevEventId(),
47
    mNextEventId(),
48
    mOldWidth(0),
49
312
    mSelectedIndex(0)
50
{
51
52
    mAllowLogic = false;
52
52
    setHeight(sliderHeight);
53
52
}
54
55
52
void SliderList::postInit2(ActionListener *const listener,
56
                           const std::string &eventId)
57
{
58
104
    mPrevEventId = eventId + "_prev";
59
104
    mNextEventId = eventId + "_next";
60
61
260
    mButtons[0] = new Button(this,
62
        "<",
63
        mPrevEventId,
64
        BUTTON_SKIN,
65

52
        this);
66
260
    mButtons[1] = new Button(this,
67
        ">",
68
        mNextEventId,
69
        BUTTON_SKIN,
70

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

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

52
    if ((mListModel == nullptr) || (gui == nullptr))
235
        return 1;
236
237
52
    int maxWidth = 0;
238
52
    const Font *const font = getFont();
239
240
52
    const int num = mListModel->getNumberOfElements();
241
1040
    for (int f = 0; f < num; f ++)
242
    {
243
1976
        const int w = font->getWidth(mListModel->getElementAt(f));
244
988
        if (w > maxWidth)
245
100
            maxWidth = w;
246
    }
247
248
    return maxWidth;
249

6
}