~aleteoryx/muditaos

ref: 028229a9177e91be0798edeac6a0f12fa27d96fc muditaos/module-apps/application-settings/windows/display-keypad/QuotesAddWindow.cpp -rw-r--r-- 7.8 KiB
028229a9 — Maciej-Mudita [MOS-924] Fix redundant logs about CSQ reporting mode 2 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "QuotesAddWindow.hpp"

#include <application-settings/windows/WindowNames.hpp>

#include <widgets/text/Text.hpp>

namespace style
{
    constexpr auto counterWidth = 70;
    constexpr auto headerWidth  = style::window::default_body_width - counterWidth;
} // namespace style

namespace gui
{
    namespace
    {
        constexpr auto maxQuoteCharactersCount  = 170U;
        constexpr auto maxQuoteLinesCount       = 8U;
        constexpr auto maxAuthorCharactersCount = 30U;

        auto formatCounterText(uint32_t counter, uint32_t maxValue) -> std::string
        {
            std::ostringstream counterText;
            counterText << counter << '/' << maxValue;
            return counterText.str();
        }
    } // namespace

    QuoteAddEditWindow::QuoteAddEditWindow(app::ApplicationCommon *app)
        : AppWindow(app, gui::window::name::new_quote), quoteModel(std::make_shared<Quotes::QuotesModel>(app))
    {
        buildInterface();
    }

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

        navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::save));
        navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));

        auto vBox = new VBox(this,
                             style::window::default_left_margin,
                             style::window::default_vertical_pos + style::margins::very_big,
                             style::window::default_body_width,
                             style::window::default_body_height);

        vBox->setEdges(RectangleEdge::None);
        vBox->setPenFocusWidth(::style::window::default_border_focus_w);
        vBox->setPenWidth(::style::window::default_border_rect_no_focus);

        auto authorHeader = new HBox(vBox, 0, 0, 0, 0);
        authorHeader->setMinimumSize(style::window::default_body_width, style::window::label::small_h);
        authorHeader->setEdges(gui::RectangleEdge::None);
        authorHeader->activeItem = false;

        auto authorLabel = new Label(authorHeader, 0, 0, 0, 0);
        authorLabel->setMinimumSize(style::headerWidth, style::window::label::small_h);
        authorLabel->setEdges(RectangleEdge::None);
        authorLabel->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Left});
        authorLabel->setText(utils::translate("app_settings_display_wallpaper_quotes_author"));
        authorLabel->setPenFocusWidth(::style::window::default_border_focus_w);
        authorLabel->setFont(::style::window::font::small);
        authorLabel->setPadding(gui::Padding(0, 0, 0, 0));
        authorLabel->activeItem = false;

        authorText = new gui::Text(vBox, 0, 0, 0, 0);
        authorText->setMinimumSize(style::window::default_body_width, style::window::label::small_h);
        authorText->setAlignment(gui::Alignment{gui::Alignment::Vertical::Top});
        authorText->setPenFocusWidth(::style::window::default_border_focus_w);
        authorText->setPenWidth(::style::window::default_border_rect_no_focus);
        authorText->setEdges(gui::RectangleEdge::Bottom);
        authorText->setFont(::style::window::font::mediumbold);
        authorText->setInputMode(new InputMode(
            {InputMode::Abc, InputMode::ABC, InputMode::abc, InputMode::digit},
            [=](const UTF8 &text) { navBarTemporaryMode(text); },
            [=]() { navBarRestoreFromTemporaryMode(); },
            [=]() { selectSpecialCharacter(); }));
        authorText->setTextLimitType(gui::TextLimitType::MaxSignsCount, maxAuthorCharactersCount);

        auto quoteHeader = new HBox(vBox, 0, 0, 0, 0);
        quoteHeader->setMinimumSize(style::window::default_body_width, style::window::label::small_h);
        quoteHeader->activeItem = false;
        quoteHeader->setEdges(gui::RectangleEdge::None);
        quoteHeader->setMargins({0, ::style::margins::huge, 0, 0});

        auto quoteLabel = new Label(quoteHeader, 0, 0, 0, 0);
        quoteLabel->setMinimumSize(style::headerWidth, style::window::label::small_h);
        quoteLabel->setEdges(RectangleEdge::None);
        quoteLabel->setPenFocusWidth(::style::window::default_border_focus_w);
        quoteLabel->setText(utils::translate("app_settings_display_wallpaper_quotes_note"));
        quoteLabel->setFont(::style::window::font::small);
        quoteLabel->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Left});
        quoteLabel->activeItem = false;

        quoteText = new gui::Text(vBox, 0, 0, 0, 0);
        quoteText->setAlignment(gui::Alignment{gui::Alignment::Vertical::Top});
        quoteText->setMaximumSize(vBox->getWidth(), style::window::label::small_h * maxQuoteLinesCount);
        quoteText->setPenFocusWidth(::style::window::default_border_focus_w);
        quoteText->setPenWidth(::style::window::default_border_rect_no_focus);
        quoteText->setEdges(gui::RectangleEdge::Bottom);
        quoteText->setFont(::style::window::font::medium);
        quoteText->setInputMode(new InputMode(
            {InputMode::Abc, InputMode::ABC, InputMode::abc, InputMode::digit},
            [=](const UTF8 &text) { navBarTemporaryMode(text); },
            [=]() { navBarRestoreFromTemporaryMode(); },
            [=]() { selectSpecialCharacter(); }));
        quoteText->setTextLimitType(gui::TextLimitType::MaxSignsCount, maxQuoteCharactersCount);
        quoteText->setTextChangedCallback([this](Item &, const UTF8 &text) { setQuoteCharactersCount(text.length()); });

        quoteCharCounter = new gui::Label(vBox, 0, 0, 0, 0);
        quoteCharCounter->setMinimumSize(style::counterWidth, style::window::label::default_h);
        quoteCharCounter->setEdges(gui::RectangleEdge::None);
        quoteCharCounter->setFont(::style::window::font::verysmall);
        quoteCharCounter->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Right});

        setTitle(utils::translate("app_settings_display_wallpaper_quotes_new"));
        vBox->resizeItems();
        setFocusItem(authorText);
    }

    void QuoteAddEditWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        auto *quotedata = dynamic_cast<QuoteSwitchData *>(data);
        if (quotedata == nullptr) {
            return;
        }

        quoteAction = quotedata->getAction();
        quoteData   = quotedata->getQuote();

        if (quoteAction == QuoteAction::Edit) {
            setTitle(utils::translate("app_settings_display_wallpaper_quotes_edit"));
            quoteText->setText(quoteData.quote);
            authorText->setText(quoteData.author);
        }
        else {
            setTitle(utils::translate("app_settings_display_wallpaper_quotes_new"));
        }

        setQuoteCharactersCount(quoteText->getText().length());
    }

    bool QuoteAddEditWindow::onInput(const gui::InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease(gui::KeyCode::KEY_ENTER)) {
            LOG_DEBUG("Save Quote requested");
            quoteData.quote  = quoteText->getText();
            quoteData.author = authorText->getText();

            if (quoteData.quote.empty()) {
                return true;
            }

            if (quoteData.author.empty()) {
                quoteData.author = "Anonymous";
            }

            if (quoteAction == QuoteAction::Add) {
                quoteModel->add(quoteData);
            }
            else {
                quoteModel->edit(quoteData);
            }

            if (quoteAction == QuoteAction::Edit) {
                application->popCurrentWindow();
            }
            application->returnToPreviousWindow();
        }

        return AppWindow::onInput(inputEvent);
    }

    void QuoteAddEditWindow::setQuoteCharactersCount(std::uint32_t count)
    {
        quoteCharCounter->setText(formatCounterText(count, maxQuoteCharactersCount));
    }
} // namespace gui