~aleteoryx/muditaos

ref: 4d2eeeb97d2dcf8587d6ec433fd550687cb87968 muditaos/module-apps/widgets/BarGraph.cpp -rw-r--r-- 3.9 KiB
4d2eeeb9 — Mateusz Grzegorzek [EGD-5932] Add the rest of queries to Quotes agent 5 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
// 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 rectAxisLenghtFrom(uint32_t numberOfRectangles) -> uint32_t
    {
        return numberOfRectangles * (style::bargraph::spacing + style::bargraph::rect_axis_length_sml) -
               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(uint32_t width, uint32_t height) const -> Rect *
    {
        auto rectangle = new Rect(nullptr, 0, 0, 0, 0);
        rectangle->setMinimumSize(width, height);
        rectangle->setFillColor(ColorFullBlack);
        rectangle->setBorderColor(ColorFullBlack);
        rectangle->setFilled(false);
        rectangle->setRadius(style::bargraph::radius);
        rectangle->setPenWidth(style::window::default_border_focus_w);
        return rectangle;
    }

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

        currentLevel = value;
        for (unsigned int i = 0; i < currentLevel; i++) {
            rectangles[i]->setFillColor(ColorFullBlack);
        }
        for (unsigned int i = currentLevel; i < numberOfRectangles; i++) {
            rectangles[i]->setFillColor(ColorFullWhite);
        }

        return true;
    }

    VBarGraph::VBarGraph(Item *parent, Position x, Position y, uint32_t numberOfRectangles)
        : VBox(parent, x, y, style::bargraph::rect_axis_length_lrg, rectAxisLenghtFrom(numberOfRectangles))
    {
        setMinimumSize(style::bargraph::rect_axis_length_lrg, rectAxisLenghtFrom(numberOfRectangles));
        setEdges(RectangleEdge::None);
        setMaximum(numberOfRectangles);
        std::reverse(std::begin(rectangles), std::end(rectangles));
    }

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

        if (currentLevel > numberOfRectangles) {
            currentLevel = numberOfRectangles;
        }
        if (!rectangles.empty()) {
            erase();
            rectangles.clear();
        }

        for (unsigned int i = 0; i < numberOfRectangles; i++) {

            auto rectangle =
                createRectangle(style::bargraph::rect_axis_length_lrg, style::bargraph::rect_axis_length_sml);
            rectangle->setBorderColor(ColorFullBlack);

            if ((i + 1) != numberOfRectangles) {
                rectangle->setMargins(Margins(0, 0, 0, style::bargraph::spacing));
            }

            addWidget(rectangle);
            rectangles.push_back(rectangle);
        }
    }

    HBarGraph::HBarGraph(Item *parent, Position x, Position y, uint32_t numberOfRectangles) : HBox(parent)
    {
        setMinimumSize(rectAxisLenghtFrom(numberOfRectangles), style::bargraph::rect_axis_length_lrg);
        setEdges(RectangleEdge::None);
        setMaximum(numberOfRectangles);
    }

    void HBarGraph::setMaximum(unsigned int value)
    {
        numberOfRectangles = value;
        if (currentLevel > numberOfRectangles) {
            currentLevel = numberOfRectangles;
        }

        if (!rectangles.empty()) {
            erase();
            rectangles.clear();
        }

        for (unsigned int i = 0; i <= numberOfRectangles; i++) {

            auto rectangle =
                createRectangle(style::bargraph::rect_axis_length_sml, style::bargraph::rect_axis_length_lrg);

            if ((i + 1) != numberOfRectangles) {
                rectangle->setMargins(Margins(0, 0, style::bargraph::spacing, 0));
            }

            addWidget(rectangle);
            rectangles.push_back(rectangle);
        }
    }

} /* namespace gui */