~aleteoryx/muditaos

ref: 40cddc1760077062db361b283c90faed8061a3b7 muditaos/module-apps/apps-common/widgets/DateWidget.cpp -rw-r--r-- 8.1 KiB
40cddc17 — Paweł Joński [MOS-288] Fix upgrade packages build 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "DateWidget.hpp"
#include "DateAndTimeStyle.hpp"
#include <ListView.hpp>
#include <Style.hpp>
#include <time/time_conversion.hpp>
#include <time/time_date_validation.hpp>
#include <module-gui/gui/input/InputEvent.hpp>
#include <time/dateCommon.hpp>

namespace date_and_time = style::window::date_and_time;

namespace gui
{
    DateWidget::DateWidget(Item *parent) : VBox(parent)
    {
        setMinimumSize(style::window::default_body_width, date_and_time::height);
        setEdges(RectangleEdge::None);

        buildInterface();
        applyCallbacks();
    }

    void DateWidget::buildInterface()
    {
        labelsHBox = new HBox(this, 0, 0, 0, 0);
        labelsHBox->setMinimumSize(style::window::default_body_width, date_and_time::topMargin);
        labelsHBox->setMargins(Margins(0, 0, 0, date_and_time::topMargin / 4));
        labelsHBox->setEdges(RectangleEdge::None);
        labelsHBox->activeItem = false;

        dayLabel = new Label(labelsHBox, 0, 0, 0, 0);
        applyLabelSpecificProperties(dayLabel);
        dayLabel->setText(utils::translate("app_settings_title_day"));

        monthLabel = new Label(labelsHBox, 0, 0, 0, 0);
        applyLabelSpecificProperties(monthLabel);
        monthLabel->setMargins(Margins(date_and_time::separator, 0, 0, 0));
        monthLabel->setText(utils::translate("app_settings_title_month"));

        yearLabel = new Label(labelsHBox, 0, 0, 0, 0);
        applyLabelSpecificProperties(yearLabel);
        yearLabel->setMargins(Margins(date_and_time::separator, 0, 0, 0));
        yearLabel->setText(utils::translate("app_settings_title_year"));

        dateHBox = new HBox(this, 0, 0, 0, 0);
        dateHBox->setMinimumSize(style::window::default_body_width, date_and_time::hBox_h);
        dateHBox->setEdges(RectangleEdge::None);
        dateHBox->activeItem = false;

        dayInput = new Label(dateHBox, 0, 0, 0, 0);

        monthInput = new Label(dateHBox, 0, 0, 0, 0);
        monthInput->setMargins(Margins(date_and_time::separator, 0, 0, 0));

        yearInput = new Label(dateHBox, 0, 0, 0, 0);
        yearInput->setMargins(Margins(date_and_time::separator, 0, 0, 0));

        applyItemSpecificProperties(dayInput);
        applyItemSpecificProperties(monthInput);
        applyItemSpecificProperties(yearInput);

        resizeItems();
    }

    void DateWidget::applyItemSpecificProperties(Label *item)
    {
        item->setMinimumSize(date_and_time::time_input_12h_w, date_and_time::hBox_h);
        item->setEdges(RectangleEdge::Bottom);
        item->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
        item->setFont(style::window::font::largelight);
        item->setPenFocusWidth(style::window::default_border_focus_w);
        item->setPenWidth(style::window::default_border_rect_no_focus);
    }

    void DateWidget::applyLabelSpecificProperties(Label *label)
    {
        label->setMinimumSize(date_and_time::time_input_12h_w, date_and_time::topMargin);
        label->setEdges(RectangleEdge::None);
        label->setAlignment(Alignment(Alignment::Horizontal::Left, Alignment::Vertical::Center));
        label->setFont(style::window::font::small);
        label->activeItem = false;
    }

    void DateWidget::applyCallbacks()
    {
        focusChangedCallback = [&]([[maybe_unused]] Item &item) {
            setFocusItem(focus ? dateHBox : nullptr);
            return true;
        };

        inputCallback = [&](Item &item, const InputEvent &event) {
            auto focusedItem = getFocusItem();
            if (!event.isShortRelease()) {
                return false;
            }
            if (event.is(KeyCode::KEY_ENTER) || event.is(KeyCode::KEY_RF)) {
                return false;
            }
            if (focusedItem->onInput(event)) {
                validateDate();
                return true;
            }
            else if (dateHBox->onInput(event)) {
                return true;
            }
            return false;
        };

        yearInput->focusChangedCallback = [&](Item &item) {
            yearInput->setText(std::to_string(static_cast<int>(validateDate().year())));
            return true;
        };
        monthInput->focusChangedCallback = [&](Item &item) {
            monthInput->setText(std::to_string(static_cast<unsigned>(validateDate().month())));
            return true;
        };
        dayInput->focusChangedCallback = [&](Item &item) {
            dayInput->setText(std::to_string(static_cast<unsigned>(validateDate().day())));
            return true;
        };

        setOnInputCallback(*dayInput);
        setOnInputCallback(*monthInput);
        setOnInputCallback(*yearInput);
    }

    date::year_month_day DateWidget::validateDate()
    {
        auto actualDate = TimePointToYearMonthDay(TimePointNow());
        uint32_t day;
        try {
            day = std::stoi(dayInput->getText().c_str());
        }
        catch (std::exception &) {
            day = static_cast<unsigned>(actualDate.day());
        }
        uint32_t month;
        try {
            month = std::stoi(monthInput->getText().c_str());
        }
        catch (std::exception &) {
            month = static_cast<unsigned>(actualDate.month());
        }
        int year;
        try {
            year = std::stoi(yearInput->getText().c_str());
        }
        catch (std::exception &) {
            year = static_cast<int>(actualDate.year());
        }

        if (year > utils::time::Locale::max_years) {
            yearInput->setText(std::to_string(utils::time::Locale::max_years));
        }

        year = std::clamp(year, utils::time::Locale::min_years, utils::time::Locale::max_years);

        if (month > static_cast<unsigned>(date::dec)) {
            monthInput->setText(std::to_string(static_cast<unsigned>(date::dec)));
        }
        month = std::clamp(static_cast<unsigned>(month), 1u, static_cast<unsigned>(date::dec));

        date::year_month_day_last max_date = date::year(year) / date::month(month) / date::last;
        if (day > static_cast<unsigned>(max_date.day())) {
            dayInput->setText(std::to_string(static_cast<unsigned>(max_date.day())));
        }
        day = std::clamp(static_cast<unsigned>(day), 1u, static_cast<unsigned>(max_date.day()));

        return date::year(year) / date::month(month) / date::day(day);
    }

    const date::year_month_day DateWidget::getChosenDate()
    {
        return validateDate();
    }

    void DateWidget::setDate(int keyValue, Label &item)
    {
        auto itemValue = item.getText();
        auto key       = std::to_string(keyValue);
        if (itemValue == "0") {
            itemValue = key;
        }
        else {
            itemValue += key;
        }
        item.setText(itemValue);

        if (!utils::time::validateDate(dayInput->getText(), monthInput->getText(), yearInput->getText())) {
            item.setText(key);
        }
    }

    void DateWidget::setOnInputCallback(Label &dateInput)
    {
        dateInput.inputCallback = [&](Item &item, const InputEvent &event) {
            if (!event.isShortRelease()) {
                return false;
            }
            if (event.isDigit()) {
                setDate(event.numericValue(), dateInput);
                return true;
            }
            else if (event.is(KeyCode::KEY_PND)) {
                clearInput(dateInput);
                return true;
            }
            return false;
        };
    }

    void DateWidget::clearInput(Label &dateInput)
    {
        auto value = dateInput.getText();
        if (auto length = value.length(); length > 0) {
            value.removeChar(length - 1);
            dateInput.setText(value);
        }
    }

    void DateWidget::loadData(const date::year_month_day &yearMonthDay)
    {
        dayInput->setText(std::to_string(static_cast<unsigned>(yearMonthDay.day())));
        monthInput->setText(std::to_string(static_cast<unsigned>(yearMonthDay.month())));
        yearInput->setText(std::to_string(static_cast<int>(yearMonthDay.year())));
    }

} /* namespace gui */