~aleteoryx/muditaos

ref: 18fdf152bd106c6282a7ebe016f6f496735ffc1c muditaos/module-apps/apps-common/widgets/BarGraph.cpp -rw-r--r-- 6.0 KiB
18fdf152 — Mateusz Grzegorzek [BH-742] Handle different products in run script 4 years 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BarGraph.hpp"

namespace gui
{
    static inline auto rectAxisLengthFrom(uint32_t numberOfRectangles) -> uint32_t
    {
        return numberOfRectangles * (style::bargraph::spacing + style::bargraph::rect_axis_length_short_medium) -
               style::bargraph::spacing;
    }

    void BarGraph::setPercentageValue(unsigned int value)
    {
        const auto percent       = static_cast<float>(value) / 100.0f;
        const auto absoluteValue = numberOfRectangles * percent;
        setValue(absoluteValue);
    }

    auto BarGraph::createRectangle() const -> Rect *
    {
        auto rectangle = new Rect(nullptr, 0, 0, 0, 0);
        rectangle->setMinimumSize(barStyle.width, barStyle.height);
        rectangle->setFillColor(ColorFullBlack);
        rectangle->setBorderColor(ColorFullBlack);
        rectangle->setFilled(false);
        rectangle->setRadius(barStyle.radius);
        rectangle->setPenWidth(style::window::default_border_focus_w);
        return rectangle;
    }

    void BarGraph::createRectangles()
    {
        for (std::uint32_t i = 0; i < numberOfRectangles; i++) {

            auto rectangle = createRectangle();

            if ((i + 1) != numberOfRectangles) {
                rectangle->setMargins(barStyle.margin);
            }

            rectangles.push_back(rectangle);
        }
    }

    bool BarGraph::setValue(unsigned int value)
    {
        if (value > numberOfRectangles) {
            LOG_ERROR("Provider value greater than graph scale");
            return false;
        }

        currentLevel = value;
        for (std::uint32_t i = 0; i < currentLevel; i++) {
            barStyle.fillRender(rectangles[i]);
        }
        for (std::uint32_t i = currentLevel; i < numberOfRectangles; i++) {
            barStyle.emptyRender(rectangles[i]);
        }

        return true;
    }

    void BarGraph::setMaximum(unsigned int value)
    {
        numberOfRectangles = value;

        if (currentLevel > numberOfRectangles) {
            currentLevel = numberOfRectangles;
        }
    }

    void BarGraph::applyBarStyle(BarGraphStyle graphStyle)
    {
        switch (graphStyle) {
        case BarGraphStyle::Heavy:
            barStyle.radius      = style::bargraph::radius_medium;
            barStyle.fillRender  = [](gui::Rect *bar) { bar->setFillColor(ColorFullBlack); };
            barStyle.emptyRender = [](gui::Rect *bar) { bar->setFillColor(ColorFullWhite); };
            break;
        case BarGraphStyle::Light:
            barStyle.radius      = style::bargraph::radius_small;
            barStyle.fillRender  = [](gui::Rect *bar) { bar->setBorderColor(ColorFullBlack); };
            barStyle.emptyRender = [](gui::Rect *bar) { bar->setBorderColor(ColorGrey); };
            break;
        }
    }

    VBarGraph::VBarGraph(Item *parent, Position x, Position y, uint32_t numberOfRectangles, BarGraphStyle graphStyle)
        : VBox(parent, x, y, style::bargraph::rect_axis_length_long_medium, rectAxisLengthFrom(numberOfRectangles))
    {
        applyBarStyle(graphStyle);
        setMinimumSize(style::bargraph::rect_axis_length_long_medium, rectAxisLengthFrom(numberOfRectangles));
        setEdges(RectangleEdge::None);

        setMaximum(numberOfRectangles);
        createGraph();

        std::reverse(std::begin(rectangles), std::end(rectangles));
    }

    void VBarGraph::createGraph()
    {
        if (!rectangles.empty()) {
            erase();
            rectangles.clear();
        }

        setMinimumHeight(rectAxisLengthFrom(numberOfRectangles));
        createRectangles();

        for (auto rect : rectangles) {
            addWidget(rect);
        }

        resizeItems();
    }

    void VBarGraph::applyBarStyle(BarGraphStyle graphStyle)
    {
        BarGraph::applyBarStyle(graphStyle);

        switch (graphStyle) {
        case BarGraphStyle::Heavy:
            barStyle.width  = style::bargraph::rect_axis_length_long_medium;
            barStyle.height = style::bargraph::rect_axis_length_short_medium;
            barStyle.margin = Margins(0, 0, 0, style::bargraph::spacing);
            break;
        case BarGraphStyle::Light:
            barStyle.width  = style::bargraph::rect_axis_length_long_small;
            barStyle.height = style::bargraph::rect_axis_length_short_small;
            barStyle.margin = Margins(0, 0, 0, style::bargraph::spacing);
            break;
        }
    }

    HBarGraph::HBarGraph(Item *parent, Position x, Position y, uint32_t numberOfRectangles, BarGraphStyle graphStyle)
        : HBox(parent, x, y, rectAxisLengthFrom(numberOfRectangles), style::bargraph::rect_axis_length_long_medium)
    {
        applyBarStyle(graphStyle);
        setMinimumSize(rectAxisLengthFrom(numberOfRectangles), style::bargraph::rect_axis_length_long_medium);
        setEdges(RectangleEdge::None);

        setMaximum(numberOfRectangles);
        createGraph();
    }

    void HBarGraph::createGraph()
    {
        if (!rectangles.empty()) {
            erase();
            rectangles.clear();
        }

        setMinimumWidth(rectAxisLengthFrom(numberOfRectangles));
        createRectangles();

        for (auto rect : rectangles) {
            addWidget(rect);
        }

        resizeItems();
    }

    void HBarGraph::applyBarStyle(BarGraphStyle graphStyle)
    {
        BarGraph::applyBarStyle(graphStyle);

        switch (graphStyle) {
        case BarGraphStyle::Heavy:
            barStyle.width  = style::bargraph::rect_axis_length_short_medium;
            barStyle.height = style::bargraph::rect_axis_length_long_medium;
            barStyle.margin = Margins(0, 0, style::bargraph::spacing, 0);
            break;
        case BarGraphStyle::Light:
            barStyle.width  = style::bargraph::rect_axis_length_short_small;
            barStyle.height = style::bargraph::rect_axis_length_long_small;
            barStyle.margin = Margins(0, 0, style::bargraph::spacing, 0);
            break;
        }
    }
} /* namespace gui */