~aleteoryx/muditaos

ref: 07cc59d97869e83b319f930403112d2cbd321248 muditaos/module-apps/application-bell-main/windows/BellMainWindow.cpp -rw-r--r-- 5.1 KiB
07cc59d9 — Mateusz Piesta [BH-708] Time and units 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BellMainWindow.hpp"

#include <application-bell-main/ApplicationBellMain.hpp>
#include <data/BellMainStyle.hpp>
#include <gui/input/InputEvent.hpp>
#include <i18n/i18n.hpp>
#include <log/log.hpp>
#include <service-time/api/TimeSettingsApi.hpp>
#include <widgets/AlarmSetSpinner.hpp>
#include <widgets/TimeSetFmtSpinner.hpp>
#include <widgets/TimeSetSpinner.hpp>

namespace
{
    constexpr uint32_t mockHour   = 10;
    constexpr uint32_t mockMinute = 15;
} // namespace

namespace gui
{
    BellMainWindow::BellMainWindow(app::Application *app) : AppWindow(app, gui::name::window::main_window)
    {
        buildInterface();

        preBuildDrawListHook = [this](std::list<Command> &cmd) { updateTime(); };
    }

    void BellMainWindow::buildInterface()
    {
        AppWindow::buildInterface();

        statusBar->setVisible(false);
        header->setTitleVisibility(false);
        bottomBar->setVisible(false);

        auto vBox = new gui::VBox(this,
                                  bellMainStyle::mainWindow::body::posX,
                                  bellMainStyle::mainWindow::body::posY,
                                  style::window::default_body_width,
                                  style::window::default_body_height);
        vBox->setEdges(gui::RectangleEdge::None);

        alarmSetSpinner = new AlarmSetSpinner(this, 0, 0, style::alarm_set_spinner::w, style::alarm_set_spinner::h);
        alarmSetSpinner->setMinimumSize(style::window::default_body_width, style::alarm_set_spinner::h);
        // for test purposes only
        alarmSetSpinner->setHour(mockHour);
        alarmSetSpinner->setMinute(mockMinute);

        alarmSetSpinner->setFont(style::window::font::largelight);
        alarmSetSpinner->setEditMode(EditMode::Browse);
        alarmStatus = BellAlarm::Status::DEACTIVATED;
        alarmSetSpinner->setAlarmStatus(alarmStatus);
        vBox->addWidget(alarmSetSpinner);

        namespace timeLabel = bellMainStyle::mainWindow::timeLabel;
        time                = new TimeSetFmtSpinner(
            this, timeLabel::posX, timeLabel::posY, timeLabel::width, timeLabel::height, stm::api::timeFormat());
        time->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        time->setEditMode(EditMode::Browse);

        namespace temperatureLabel = bellMainStyle::mainWindow::temperatureLabel;
        temperature                = new gui::Label(
            this, temperatureLabel::posX, temperatureLabel::posY, temperatureLabel::width, temperatureLabel::height);
        temperature->setFilled(false);
        temperature->setBorderColor(gui::ColorNoColor);
        temperature->setFont(temperatureLabel::font);
        temperature->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        std::string degree_sign(temperatureLabel::degree);
        temperature->setText("12 " + degree_sign + "C");

        setFocusItem(time);
    }

    bool BellMainWindow::onInput(const InputEvent &inputEvent)
    {
        LOG_INFO("Bell Main Window Input");
        if (inputEvent.isShortRelease()) {
            switch (inputEvent.getKeyCode()) {
            case KeyCode::KEY_ENTER:
                return handleEnterKey(inputEvent);
            case KeyCode::KEY_UP:
            case KeyCode::KEY_DOWN:
                handleEditModeEntry();
                break;

            default:
                break;
            }
        }
        // check if any of the lower inheritance onInput methods catch the event
        return AppWindow::onInput(inputEvent);
    }

    bool BellMainWindow::handleEnterKey(const InputEvent &inputEvent)
    {
        if (alarmEditMode) {
            auto ret = AppWindow::onInput(inputEvent);
            if (not ret) {
                // alarm setting finished
                alarmEditMode = false;
                setFocusItem(nullptr);
                alarmSetSpinner->setEditMode(EditMode::Browse);
                alarmStatus = BellAlarm::Status::ACTIVATED;
                alarmSetSpinner->setAlarmStatus(alarmStatus);
                return true;
            }
            return ret;
        }
        else {
            LOG_INFO("Open MainMenu");
            application->switchWindow(gui::window::name::bell_main_menu, nullptr);
            return true;
        }
    }

    void BellMainWindow::handleEditModeEntry()
    {
        if (!alarmEditMode) {
            alarmEditMode = true;
            setFocusItem(alarmSetSpinner);
            alarmSetSpinner->setEditMode(EditMode::Edit);
        }
    }

    bool BellMainWindow::updateTime()
    {
        if (time != nullptr) {
            const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
            time->setHour(std::localtime(&now)->tm_hour);
            time->setMinute(std::localtime(&now)->tm_min);
            time->setTimeFormat(stm::api::timeFormat());
            return true;
        }

        return false;
    }

} // namespace gui