~aleteoryx/muditaos

ref: cb3b6cd5dad3b69acbcad189da09242972896bdb muditaos/module-apps/application-calculator/windows/CalculatorMainWindow.cpp -rw-r--r-- 7.4 KiB
cb3b6cd5 — Pawel Olejniczak [EGD-5338] Add APN options window - gui 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CalculatorMainWindow.hpp"
#include "application-calculator/widgets/CalculatorStyle.hpp"
#include "application-calculator/data/CalculatorUtility.hpp"
#include <i18n/i18n.hpp>

namespace gui
{

    CalculatorMainWindow::CalculatorMainWindow(app::Application *app, std::string name) : AppWindow(app, name)
    {
        buildInterface();
    }

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

        setTitle(utils::localize.get("app_calculator_title_main"));

        bottomBar->setActive(gui::BottomBar::Side::CENTER, true);
        bottomBar->setActive(gui::BottomBar::Side::RIGHT, true);
        bottomBar->setActive(gui::BottomBar::Side::LEFT, true);
        bottomBar->setText(gui::BottomBar::Side::RIGHT, utils::localize.get(style::strings::common::back));
        bottomBar->setText(gui::BottomBar::Side::CENTER, utils::localize.get(style::calculator::equals));
        bottomBar->setText(gui::BottomBar::Side::LEFT, utils::localize.get(style::calculator::decimal_separator));
        bottomBar->setFont(BottomBar::Side::LEFT, style::window::font::largelight);

        mathOperationInput = new gui::Text(this,
                                           style::calculator::window::input_margin,
                                           style::calculator::window::input_offset_top,
                                           style::calculator::window::input_width,
                                           style::calculator::window::input_height);
        mathOperationInput->setEdges(gui::RectangleEdge::Bottom);
        mathOperationInput->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center));
        mathOperationInput->setFont(style::window::font::supersizemelight);
        mathOperationInput->setInputMode(new InputMode({InputMode::digit}));
        mathOperationInput->setPenFocusWidth(style::window::default_border_focus_w);
        mathOperationInput->setPenWidth(style::window::default_border_rect_no_focus);
        mathOperationInput->setEditMode(gui::EditMode::Edit);

        mathBox = new gui::MathOperationsBox(this,
                                             style::calculator::window::math_box_offset_top,
                                             style::window::default_body_width,
                                             style::calculator::window::math_box_height,
                                             style::calculator::window::math_box_cell_width,
                                             style::calculator::window::math_box_cell_height);
        addWidget(mathBox);
        setFocusItem(mathOperationInput);
        applyInputCallback();
    }

    void CalculatorMainWindow::applyInputCallback()
    {
        mathOperationInput->inputCallback = [&](Item &item, const InputEvent &event) {
            if (clearInput) {
                mathOperationInput->clear();
                clearInput = false;
            }
            if (!event.isShortPress()) {
                return false;
            }
            auto lastChar         = mathOperationInput->getText()[mathOperationInput->getText().length() - 1];
            bool lastCharIsSymbol = isSymbol(lastChar);
            if (event.keyCode == gui::KeyCode::KEY_UP) {
                writeEquation(lastCharIsSymbol, style::calculator::symbols::strings::plus);
                return true;
            }
            if (event.keyCode == gui::KeyCode::KEY_DOWN) {
                if (lastChar != style::calculator::symbols::codes::minus) {
                    writeEquation(lastCharIsSymbol, style::calculator::symbols::strings::minus);
                }
                return true;
            }
            if (event.keyCode == gui::KeyCode::KEY_LEFT) {
                writeEquation(lastCharIsSymbol, style::calculator::symbols::strings::multiplication);
                return true;
            }
            if (event.keyCode == gui::KeyCode::KEY_RIGHT) {
                writeEquation(lastCharIsSymbol, style::calculator::symbols::strings::division);
                return true;
            }
            if (event.keyCode == gui::KeyCode::KEY_LF) {
                if (!isPreviousNumberDecimal()) {
                    writeEquation(lastCharIsSymbol, utils::localize.get("app_calculator_decimal_separator"));
                }
                return true;
            }
            return false;
        };
    }

    bool CalculatorMainWindow::isSymbol(uint32_t character)
    {
        return character == style::calculator::symbols::codes::plus ||
               character == style::calculator::symbols::codes::minus ||
               character == style::calculator::symbols::codes::division ||
               character == style::calculator::symbols::codes::multiplication ||
               character == style::calculator::symbols::codes::comma ||
               character == style::calculator::symbols::codes::full_stop;
    }

    void CalculatorMainWindow::writeEquation(bool lastCharIsSymbol, const UTF8 &symbol)
    {
        if (!mathOperationInput->getText().empty()) {

            if (lastCharIsSymbol && symbol != style::calculator::symbols::strings::minus) {
                mathOperationInput->setRichText(
                    std::string(mathOperationInput->getText()).erase(mathOperationInput->getText().length() - 1) +
                    symbol.c_str());
            }
            else {
                mathOperationInput->setRichText(mathOperationInput->getText() + symbol);
            }
        }
        else if (symbol == style::calculator::symbols::strings::minus) {
            mathOperationInput->setRichText(mathOperationInput->getText() + symbol);
        }
    }

    bool CalculatorMainWindow::isPreviousNumberDecimal()
    {
        if (!mathOperationInput->getText().empty()) {
            std::vector<int> symbolsIndexes;
            auto input = std::string(mathOperationInput->getText()).erase(mathOperationInput->getText().length() - 1);
            symbolsIndexes.push_back(input.find_last_of(style::calculator::symbols::strings::minus));
            symbolsIndexes.push_back(input.find_last_of(style::calculator::symbols::strings::plus));
            symbolsIndexes.push_back(input.find_last_of(style::calculator::symbols::strings::division));
            symbolsIndexes.push_back(input.find_last_of(style::calculator::symbols::strings::multiplication));
            auto it = std::max_element(symbolsIndexes.begin(), symbolsIndexes.end());
            std::string lastNumber;

            if (*it == -1) {
                lastNumber = input.substr(0, std::string::npos);
            }
            else {
                lastNumber = input.substr(*it, std::string::npos);
            }
            return lastNumber.find(utils::localize.get("app_calculator_decimal_separator")) != std::string::npos;
        }
        return false;
    }

    bool CalculatorMainWindow::onInput(const gui::InputEvent &inputEvent)
    {
        if (AppWindow::onInput(inputEvent)) {
            return true;
        }

        if (!inputEvent.isShortPress()) {
            return false;
        }

        if (inputEvent.keyCode == gui::KeyCode::KEY_ENTER) {
            auto result = Calculator().calculate(std::string(mathOperationInput->getText()));
            mathOperationInput->setRichText(result.value);
            clearInput = result.isError;
            return true;
        }

        return false;
    }

} // namespace gui