~aleteoryx/muditaos

muditaos/module-gui/gui/widgets/ProgressBar.cpp -rw-r--r-- 10.0 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <Math.hpp>

#include "DrawCommand.hpp"
#include "ProgressBar.hpp"

namespace gui
{
    ProgressBar::ProgressBar(Item *parent, std::uint32_t x, std::uint32_t y, std::uint32_t w, std::uint32_t h)
        : Rect{parent, x, y, w, h}
    {
        setFillColor(ColorFullBlack);
        setPenWidth(2);
        createWidgets();
        updateDrawArea();
    }

    void ProgressBar::createWidgets()
    {
        // fillRect is smaller, to avoid border overlapping
        fillRect = new gui::Rect(this, 0, 1, widgetArea.w, widgetArea.h - 2);
        fillRect->setRadius(widgetArea.h / 2 - 1);
        fillRect->setFilled(true);
        fillRect->setFillColor(Color{0, 0});
        Rect::setRadius(widgetArea.h / 2);
    }

    void ProgressBar::setMaximum(unsigned int value) noexcept
    {
        maxValue = value;
        if (currentValue > maxValue) {
            currentValue = maxValue;
        }
    }

    bool ProgressBar::setValue(unsigned int value) noexcept
    {
        currentValue = std::clamp(value, 0U, maxValue);
        return currentValue == value;
    }

    void ProgressBar::setPercentageValue(unsigned int value) noexcept
    {
        const auto percent       = static_cast<float>(value) / 100.0f;
        const auto absoluteValue = std::lround(static_cast<float>(maxValue) * percent);
        setValue(absoluteValue);
    }
    int ProgressBar::getMaximum() const noexcept
    {
        return maxValue;
    }

    void ProgressBar::buildDrawListImplementation(std::list<Command> &commands)
    {
        uint32_t progressSize = maxValue == 0U ? 0 : (currentValue * widgetArea.w) / maxValue;
        drawArea.w            = progressSize;

        gui::Rect::buildDrawListImplementation(commands);
    }

    bool ProgressBar::onDimensionChanged([[maybe_unused]] const BoundingBox &oldDim, const BoundingBox &newDim)
    {
        fillRect->setSize(newDim.w, newDim.h);
        return true;
    }

    CircularProgressBar::CircularProgressBar(Item *parent, const Circle::ShapeParams &shape) : Circle{parent, shape}
    {
        createWidgets();
        updateDrawArea();
    }

    void CircularProgressBar::createWidgets()
    {
        const auto progressArcRadius = radius + 1;
        const auto progressArcWidth  = penWidth + 2;
        Arc::ShapeParams arcParams;
        arcParams.setCenterPoint(center)
            .setRadius(progressArcRadius)
            .setStartAngle(-90) // Start drawing the circle from top.
            .setSweepAngle(0)
            .setPenWidth(progressArcWidth)
            .setBorderColor(ColorFullBlack);
        progressArc = new Arc(this, arcParams);

        Circle::ShapeParams indicatorParams;
        indicatorParams.setCenterPoint(calculateProgressIndicatorCenter())
            .setRadius(progressArcWidth)
            .setPenWidth(penWidth + 1)
            .setBorderColor(ColorFullBlack)
            .setFillColor(ColorFullBlack);
        progressIndicator = new Circle(this, indicatorParams);
    }

    Point CircularProgressBar::calculateProgressIndicatorCenter() const
    {
        using namespace trigonometry;
        const auto sweepAngleRadians = toRadians(progressArc->getSweepAngle() + progressArc->getStartAngle());
        return Point(center.x + AdjacentSide::fromAngle(sweepAngleRadians, radius - (penWidth / 2)),
                     center.y + OppositeSide::fromAngle(sweepAngleRadians, radius - (penWidth / 2)));
    }

    void CircularProgressBar::setMaximum(unsigned int value) noexcept
    {
        maxValue = value;
        if (currentValue > maxValue) {
            currentValue = maxValue;
        }
    }

    bool CircularProgressBar::setValue(unsigned int value) noexcept
    {
        currentValue = std::clamp(value, 0U, maxValue);
        return value == currentValue;
    }

    void CircularProgressBar::setPercentageValue(unsigned int value) noexcept
    {
        const auto percent       = static_cast<float>(value) / 100.0f;
        const auto absoluteValue = std::lround(static_cast<float>(maxValue) * percent);
        setValue(absoluteValue);
    }

    int CircularProgressBar::getMaximum() const noexcept
    {
        return maxValue;
    }

    float CircularProgressBar::getPercentageValue() const
    {
        if (maxValue == 0) {
            return .0f;
        }
        return static_cast<float>(currentValue) / maxValue;
    }

    void CircularProgressBar::buildDrawListImplementation(std::list<Command> &commands)
    {
        using namespace trigonometry;

        progressArc->setSweepAngle(std::ceil(getPercentageValue() * FullAngle));
        progressIndicator->setCenter(calculateProgressIndicatorCenter());

        Circle::buildDrawListImplementation(commands);
    }

    bool CircularProgressBar::onDimensionChanged([[maybe_unused]] const BoundingBox &oldDim,
                                                 [[maybe_unused]] const BoundingBox &newDim)
    {
        return true;
    }

    ArcProgressBar::ArcProgressBar(Item *parent,
                                   const Arc::ShapeParams &shape,
                                   ProgressDirection direction,
                                   ProgressChange change)
        : Arc{parent, shape}, direction{direction}, change{change}
    {
        if (direction == ProgressDirection::CounterClockwise) {
            start -= sweep;
        }
        createWidgets();
        updateDrawArea();
    }

    void ArcProgressBar::createWidgets()
    {
        // Arc progress indicator (stronger line) must be a bit wider than the base circle
        // Those values were selected to match the design and look good enough on multiple
        // radius and penWidth values
        const auto progressArcRadius       = radius + 3;
        const auto progressArcWidth        = penWidth + 7;
        const auto progressIndicatorRadius = (progressArcWidth - 1) / 2;

        Arc::ShapeParams arcParams;
        arcParams.setCenterPoint(center)
            .setRadius(progressArcRadius)
            .setSweepAngle(0)
            .setPenWidth(progressArcWidth)
            .setBorderColor(ColorFullBlack);
        progressArc = new Arc(this, arcParams);

        Circle::ShapeParams indicatorStartParams;
        indicatorStartParams.setCenterPoint(calculateStartIndicatorCenter())
            .setRadius(progressIndicatorRadius)
            .setPenWidth(2)
            .setBorderColor(ColorFullBlack)
            .setFillColor(ColorFullBlack);
        progressStartIndicator = new Circle(this, indicatorStartParams);

        Circle::ShapeParams indicatorEndParams;
        indicatorEndParams.setCenterPoint(calculateEndIndicatorCenter())
            .setRadius(progressIndicatorRadius)
            .setPenWidth(2)
            .setBorderColor(ColorFullBlack)
            .setFillColor(ColorFullBlack);
        progressEndIndicator = new Circle(this, indicatorEndParams);
    }

    Point ArcProgressBar::calculateStartIndicatorCenter() const
    {
        using namespace trigonometry;
        const auto sweepAngleRadians = toRadians(progressArc->getSweepAngle() + progressArc->getStartAngle());
        return Point(center.x + AdjacentSide::fromAngle(sweepAngleRadians, radius - (penWidth / 2)),
                     center.y + OppositeSide::fromAngle(sweepAngleRadians, radius - (penWidth / 2)));
    }

    Point ArcProgressBar::calculateEndIndicatorCenter() const
    {
        using namespace trigonometry;
        const auto sweepAngleRadians = toRadians(progressArc->getStartAngle());
        return Point(center.x + AdjacentSide::fromAngle(sweepAngleRadians, radius - (penWidth / 2)),
                     center.y + OppositeSide::fromAngle(sweepAngleRadians, radius - (penWidth / 2)));
    }

    void ArcProgressBar::setMaximum(unsigned int value) noexcept
    {
        maxValue = value;
        if (currentValue > maxValue) {
            currentValue = maxValue;
        }
    }

    bool ArcProgressBar::setValue(unsigned int value) noexcept
    {
        switch (change) {
        case ProgressChange::IncrementFromZero:
            currentValue = std::clamp(value, 0U, maxValue);
            break;
        case ProgressChange::DecrementFromFull:
            currentValue = maxValue - std::clamp(value, 0U, maxValue);
            break;
        default:
            break;
        }

        return value == currentValue;
    }

    void ArcProgressBar::setPercentageValue(unsigned int value) noexcept
    {
        const auto percent       = static_cast<float>(value) / 100.0f;
        const auto absoluteValue = std::lround(static_cast<float>(maxValue) * percent);
        setValue(absoluteValue);
    }

    int ArcProgressBar::getMaximum() const noexcept
    {
        return maxValue;
    }

    float ArcProgressBar::getPercentageValue() const
    {
        if (maxValue == 0) {
            return .0f;
        }
        return static_cast<float>(currentValue) / maxValue;
    }

    void ArcProgressBar::buildDrawListImplementation(std::list<Command> &commands)
    {
        const auto dTheta = std::ceil(getPercentageValue() * sweep);

        progressArc->setSweepAngle(dTheta);
        if ((direction == ProgressDirection::Clockwise) != (change == ProgressChange::IncrementFromZero)) {
            progressArc->setStartAngle(start + sweep - dTheta);
        }
        else {
            progressArc->setStartAngle(start);
        }
        progressStartIndicator->setCenter(calculateStartIndicatorCenter());
        progressEndIndicator->setCenter(calculateEndIndicatorCenter());

        const auto progressItemsVisible = ((dTheta != 0) || (change == ProgressChange::IncrementFromZero));
        progressArc->setVisible(progressItemsVisible);
        progressStartIndicator->setVisible(progressItemsVisible);
        progressEndIndicator->setVisible(progressItemsVisible);

        Arc::buildDrawListImplementation(commands);
    }

    bool ArcProgressBar::onDimensionChanged([[maybe_unused]] const BoundingBox &oldDim,
                                            [[maybe_unused]] const BoundingBox &newDim)
    {
        return true;
    }
} /* namespace gui */