~aleteoryx/muditaos

ref: 1a2ea5324960ca9f560d6011dba03b7b14a5ed01 muditaos/module-apps/apps-common/widgets/TimeSetFmtSpinner.cpp -rw-r--r-- 9.3 KiB
1a2ea532 — Marcin Zieliński [MOS-809] Remove dead code around celular channels 3 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
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-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TimeSetFmtSpinner.hpp"

#include <date/date.h>
#include <gui/core/FontManager.hpp>
#include <gui/core/RawFont.hpp>
#include <widgets/spinners/Spinners.hpp>

namespace gui
{

    TimeSetFmtSpinner::TimeSetFmtSpinner(
        Item *parent, uint32_t x, uint32_t y, uint32_t w, uint32_t h, utils::time::Locale::TimeFormat timeFormat)
        : HBox{parent, x, y, w, h}
    {
        using namespace utils;

        setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        setEdges(RectangleEdge::None);

        timeSetSpinner = new TimeSetSpinner(this, 0, 0, 0, 0);
        timeSetSpinner->setFont(focusFontName, noFocusFontName);
        timeSetSpinner->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));

        auto textRange = StringSpinner::range{time::Locale::getAM(), time::Locale::getPM()};
        fmt            = new StringSpinner(textRange, Boundaries::Continuous);
        updateFmtFont(noFocusFontName);
        fmt->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        fmt->setMargins(getFmtMargins(noFocusFontName));
        fmt->setEdges(RectangleEdge::None);
        fmt->setVisible(false);
        fmt->setPenFocusWidth(style::time_set_spinner::focus::size);
        addWidget(fmt);
        fmt->setEdges(RectangleEdge::Bottom);

        focusChangedCallback = [&](Item &) {
            if (focus) {
                setTimeFormat(this->timeFormat);
                if (editMode == EditMode::Edit) {
                    setFocusItem(timeSetSpinner);
                }
            }
            else {
                setFocusItem(nullptr);
            }
            return true;
        };

        fmt->focusChangedCallback = [&](Item &) {
            if (fmt->focus) {
                updateFmtFont(focusFontName);
            }
            else {
                updateFmtFont(noFocusFontName);
            }
            return true;
        };

        setTimeFormat(timeFormat);
    }

    auto TimeSetFmtSpinner::setTimeFormat(utils::time::Locale::TimeFormat newFormat) noexcept -> void
    {
        using namespace utils;

        switch (newFormat) {
        case utils::time::Locale::TimeFormat::FormatTime12H: {
            fmt->setVisible(true);
            auto hours = std::chrono::hours(timeSetSpinner->getHour());
            timeSetSpinner->setHourRange(time::Locale::min_hour_12H_mode, time::Locale::max_hour_12H_mode);

            if (timeFormat != newFormat) {
                timeSetSpinner->setHour(date::make12(hours).count());
                set12HPeriod(date::is_pm(hours) ? Period::PM : Period::AM);
            }

        } break;
        case utils::time::Locale::TimeFormat::FormatTime24H: {
            fmt->setVisible(false);
            auto hours = std::chrono::hours(timeSetSpinner->getHour());
            timeSetSpinner->setHourRange(time::Locale::min_hour_24H_mode, time::Locale::max_hour_24H_mode);

            if (newFormat != timeFormat) {
                timeSetSpinner->setHour(date::make24(hours, isPM()).count());

                if (focusItem == fmt) {
                    setFocusItem(timeSetSpinner);
                }
            }
        } break;
        default:
            break;
        }

        timeFormat = newFormat;
        fmt->handleContentChanged();
    }

    auto TimeSetFmtSpinner::setMinute(int value) noexcept -> void
    {
        timeSetSpinner->setMinute(value);
    }

    auto TimeSetFmtSpinner::setEditMode(EditMode newEditMode) noexcept -> void
    {
        editMode = newEditMode;
        if (editMode == EditMode::Edit) {
            setFocusItem(timeSetSpinner);
            timeSetSpinner->setEditMode(editMode);
        }
        else {
            setFocusItem(nullptr);
        }
    }

    auto TimeSetFmtSpinner::getHour() const noexcept -> int
    {
        return timeSetSpinner->getHour();
    }

    auto TimeSetFmtSpinner::getMinute() const noexcept -> int
    {
        return timeSetSpinner->getMinute();
    }

    auto TimeSetFmtSpinner::getFmtMargins(const std::string &fmtFont) const noexcept -> Margins
    {
        return fmtMarginsMap.find(fmtFont)->second;
    }

    auto TimeSetFmtSpinner::setHour(int value) noexcept -> void
    {
        timeSetSpinner->setHour(value);
    }

    auto TimeSetFmtSpinner::setFont(std::string newFontName) noexcept -> void
    {
        setFont(newFontName, newFontName);
    }

    auto TimeSetFmtSpinner::setFont(std::string newFocusFontName, std::string newNoFocusFontName) noexcept -> void
    {
        focusFontName   = std::move(newFocusFontName);
        noFocusFontName = std::move(newNoFocusFontName);

        timeSetSpinner->setFont(focusFontName, noFocusFontName);
        updateFmtFont(noFocusFontName);

        setMinimumSize(timeSetSpinner->widgetMinimumArea.w + fmt->widgetMinimumArea.w +
                           fmt->margins.getSumInAxis(Axis::X),
                       timeSetSpinner->widgetMinimumArea.h);
        resizeItems();
    }

    auto TimeSetFmtSpinner::updateFmtFont(const std::string &fontName) noexcept -> void
    {
        fmt->setFont(fontName);
        fmt->setMinimumWidthToFitText();
        fmt->setMinimumHeightToFitText();
        fmt->setMargins(getFmtMargins(fontName));
        fmt->setText(fmt->getText());
    }

    auto TimeSetFmtSpinner::onInput(const InputEvent &inputEvent) -> bool
    {
        // Ignore input event when not in edit mode
        if (editMode != EditMode::Edit) {
            return false;
        }

        if (auto ret = this->focusItem->onInput(inputEvent)) {
            return ret;
        }

        if (inputEvent.isShortRelease()) {
            switch (inputEvent.getKeyCode()) {
            case KeyCode::KEY_ENTER:
                return handleEnterKey();
            case KeyCode::KEY_RF:
                return handleRightFunctionKey();

            default:
                break;
            }
        }
        return false;
    }

    auto TimeSetFmtSpinner::handleEnterKey() -> bool
    {
        using namespace utils::time;
        if ((focusItem == timeSetSpinner) && (timeFormat == Locale::TimeFormat::FormatTime12H)) {
            setFocusItem(fmt);
            return true;
        }
        return false;
    }

    auto TimeSetFmtSpinner::handleRightFunctionKey() -> bool
    {
        if (focusItem == fmt) {
            setFocusItem(timeSetSpinner);
            return true;
        }

        return false;
    }

    auto TimeSetFmtSpinner::isPM() const noexcept -> bool
    {
        return fmt->value() == utils::time::Locale::getPM().c_str();
    }

    auto TimeSetFmtSpinner::getTimeFormat() const noexcept -> utils::time::Locale::TimeFormat
    {
        return timeFormat;
    }

    auto TimeSetFmtSpinner::setTime(std::time_t time) noexcept -> void
    {
        using namespace utils::time;
        if (timeFormat == Locale::TimeFormat::FormatTime24H) {
            timeSetSpinner->setTime(time);
        }
        else {
            const auto t       = std::localtime(&time);
            const auto hours   = std::chrono::hours{t->tm_hour};
            const auto time12H = date::make12(hours);
            const auto isPM    = date::is_pm(hours);
            fmt->set_value(isPM ? utils::time::Locale::getPM() : utils::time::Locale::getAM());
            timeSetSpinner->setTime(time12H.count(), t->tm_min);
        }
    }

    auto TimeSetFmtSpinner::setTimeFormatSpinnerVisibility(bool visibility) noexcept -> void
    {
        fmt->setVisible(visibility);
        resizeItems();
    }

    auto TimeSetFmtSpinner::getTime() const noexcept -> std::time_t
    {
        using namespace utils::time;
        const auto now     = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
        const auto newTime = std::localtime(&now);
        if (timeFormat == Locale::TimeFormat::FormatTime24H) {
            newTime->tm_hour = timeSetSpinner->getHour();
        }
        else {
            newTime->tm_hour = date::make24(std::chrono::hours{timeSetSpinner->getHour()}, isPM()).count();
        }
        newTime->tm_min = timeSetSpinner->getMinute();

        return std::mktime(newTime);
    }

    void TimeSetFmtSpinner::handleContentChanged()
    {
        fmt->setMinimumWidthToFitText();
        fmt->setMargins(getFmtMargins(noFocusFontName));

        auto widthToSet =
            timeFormat == utils::time::Locale::TimeFormat::FormatTime12H
                ? timeSetSpinner->widgetMinimumArea.w + fmt->widgetMinimumArea.w + fmt->margins.getSumInAxis(Axis::X)
                : timeSetSpinner->widgetMinimumArea.w;

        setMinimumWidth(widthToSet);
        setMaximumWidth(widgetMinimumArea.w);

        HBox::handleContentChanged();
    }
    auto TimeSetFmtSpinner::set12HPeriod(const Period period) -> void
    {
        period == Period::AM ? fmt->set_value(utils::time::Locale::getAM())
                             : fmt->set_value(utils::time::Locale::getPM());
    }
    auto TimeSetFmtSpinner::getHour24Format() const noexcept -> std::chrono::hours
    {
        using namespace utils::time;
        if (timeFormat == Locale::TimeFormat::FormatTime24H) {
            return std::chrono::hours{timeSetSpinner->getHour()};
        }

        return date::make24(std::chrono::hours{timeSetSpinner->getHour()}, isPM());
    }

} // namespace gui