~aleteoryx/muditaos

ref: 8e6490863a74d30442b434f91b90a2f07095e2d3 muditaos/module-apps/application-bell-main/windows/BellMainWindow.cpp -rw-r--r-- 4.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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 <time/time_conversion.hpp>

static constexpr uint32_t mockHour   = 10;
static constexpr uint32_t mockMinute = 15;

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

    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 gui::Label(this, timeLabel::posX, timeLabel::posY, timeLabel::width, timeLabel::height);
        time->setFilled(false);
        time->setBorderColor(gui::ColorNoColor);
        time->setFont(timeLabel::font);
        time->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));

        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 (ret == false) {
                // 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) {
            utils::time::Timestamp timestamp{std::time(nullptr)};
            time->setText(timestamp.str("%H:%M"));
            return true;
        }

        return false;
    }

} // namespace gui