~aleteoryx/muditaos

ref: 8e6490863a74d30442b434f91b90a2f07095e2d3 muditaos/module-gui/gui/widgets/Spinner.cpp -rw-r--r-- 2.7 KiB
8e649086 — Mateusz Grzegorzek [BH-395] Librarize application-settings 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Spinner.hpp"

#include <InputEvent.hpp>

#include <sstream>
#include <iomanip>

namespace gui
{

    Spinner::Spinner(int minValue, int maxValue, int step, Boundaries boundaries)
        : minValue(minValue), maxValue(maxValue), step(step), currentValue(minValue), boundaries(boundaries)
    {
        setEditMode(EditMode::Browse);
        updateSpinner();
    }

    void Spinner::setCurrentValue(int newCurrentValue)
    {
        currentValue = newCurrentValue;
        updateSpinner();
    }

    void Spinner::setFixedFieldWidth(unsigned char newFixedFieldWidth)
    {
        fixedFieldWidth = newFixedFieldWidth;
    }

    int Spinner::getCurrentValue() const noexcept
    {
        return currentValue;
    }

    unsigned char Spinner::getFixedFieldWidth() const noexcept
    {
        return fixedFieldWidth;
    }

    void Spinner::stepUp()
    {
        currentValue += step;
        if (currentValue > maxValue) {
            if (boundaries == Boundaries::Continuous) {
                currentValue = minValue;
            }
            else {
                currentValue = maxValue;
            }
        }
        updateSpinner();
    }

    void Spinner::stepDown()
    {
        currentValue -= step;
        if (currentValue < minValue) {
            if (boundaries == Boundaries::Continuous) {
                currentValue = maxValue;
            }
            else {
                currentValue = minValue;
            }
        }
        updateSpinner();
    }

    bool Spinner::onInput(const InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease()) {
            switch (inputEvent.getKeyCode()) {
            case KeyCode::KEY_UP:
                stepUp();
                return true;
            case KeyCode::KEY_DOWN:
                stepDown();
                return true;
            default:
                break;
            }
        }
        return false;
    }

    bool Spinner::onFocus(bool state)
    {
        if (focus) {
            setEdges(RectangleEdge::Bottom);
        }
        else {
            setEdges(RectangleEdge::None);
        }
        showCursor(state);
        return true;
    }

    void Spinner::updateSpinner()
    {
        std::stringstream outStream;
        if (fixedFieldWidth > 0) {
            outStream << std::setw(fixedFieldWidth) << std::setfill('0');
        }
        outStream << currentValue;
        setText(outStream.str());
    }
    void Spinner::setMinValue(int newMinValue)
    {
        minValue = newMinValue;
    }
    void Spinner::setMaxValue(int newMaxValue)
    {
        maxValue = newMaxValue;
    }

} // namespace gui