~aleteoryx/muditaos

ref: 588925f30a1a0e862dc074c15b6fa1711443c6e8 muditaos/module-apps/application-antenna/windows/ScanModesWindow.cpp -rw-r--r-- 6.2 KiB
588925f3 — Wojtek Rzepecki [EGD-7297] Fix callog icons 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
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ScanModesWindow.hpp"
#include <application-antenna/AntennaAppStyle.hpp>
#include <application-antenna/ApplicationAntenna.hpp>
#include <i18n/i18n.hpp>
#include <module-gui/gui/widgets/BoxLayout.hpp>
#include <module-gui/gui/widgets/Item.hpp>
#include <module-gui/gui/widgets/Label.hpp>
#include <module-gui/gui/widgets/Style.hpp>
#include <module-gui/gui/widgets/Window.hpp>
#include <service-cellular/CellularServiceAPI.hpp>

namespace gui
{

    ScanModesWindow::ScanModesWindow(app::ApplicationCommon *app) : AppWindow(app, gui::name::window::scan_window)
    {
        buildInterface();
    }

    void ScanModesWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }
    void ScanModesWindow::buildInterface()
    {
        AppWindow::buildInterface();

        bottomBar->setActive(BottomBar::Side::LEFT, true);
        bottomBar->setActive(BottomBar::Side::CENTER, true);
        bottomBar->setActive(BottomBar::Side::RIGHT, true);
        bottomBar->setText(BottomBar::Side::CENTER, utils::translate(style::strings::common::open));
        bottomBar->setText(BottomBar::Side::RIGHT, utils::translate(style::strings::common::back));

        setTitle(utils::translate("app_desktop_tools_antenna"));

        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::Auto, "AUTO"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::GSM_only, "GSM only"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::WCDMA_only, "WCDMA only"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::LTE_only, "LTE only"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::TD_SCDMA_only, "TD-SCDMA only"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::UTMS_only, "UTMS only"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::CDMA_only, "CDMA only"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::HDR_only, "HDR only"));
        modeButtonParams.insert(std::pair<uint32_t, std::string>(scanModes::CDMA_and_HDR_only, "CDMA and HDR only"));

        uint32_t w = this->getWidth() - style::window::default_left_margin * 2;
        uint32_t h = this->getHeight() - style::window::default_vertical_pos - bottomBar->getHeight();

        modesBox = new gui::VBox(this, style::window::default_left_margin, style::window::default_vertical_pos, w, h);
        modesBox->setPenWidth(0);
        modesBox->setPenFocusWidth(0);
        modesBox->setEdges(gui::RectangleEdge::None);

        for (auto el : modeButtonParams) {
            gui::Label *modeLabel = addMode(nullptr, el.second);

            modeLabel->activatedCallback = [=](gui::Item &) {
                std::string mode;
                try {
                    mode = std::to_string(el.first);
                }
                catch (const std::exception &e) {
                    LOG_ERROR("ServiceCellular::std::to_string exception %s", e.what());
                }
                auto result = CellularServiceAPI::SetScanMode(this->application, mode);
                if (result) {
                    commandResult->setText("Success!");
                }
                else {
                    commandResult->setText("Failure!");
                }
                application->render(RefreshModes::GUI_REFRESH_FAST);
                return true;
            };
            modesBox->addWidget(modeLabel);
            modesBox->addWidget(addSpacer());
        }

        commandResult = addMode(this, "");
        commandResult->setPosition(style::window::default_left_margin, antenna::scan_mode_window::reusultLabelPosY);
        setFocusItem(modesBox);

        CellularServiceAPI::GetScanMode(this->application);
    }

    void ScanModesWindow::destroyInterface()
    {
        erase();
    }

    void ScanModesWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {}

    bool ScanModesWindow::onInput(const InputEvent &inputEvent)
    {
        bool ret = AppWindow::onInput(inputEvent);
        if (ret) {
            application->render(RefreshModes::GUI_REFRESH_FAST);
            return true;
        }
        return false;
    }

    gui::Label *ScanModesWindow::addMode(gui::Item *parent, const UTF8 &text)
    {
        gui::Label *label = new gui::Label(parent,
                                           antenna::scan_mode_window::commonDefaultPos,
                                           antenna::scan_mode_window::commonDefaultPos,
                                           antenna::scan_mode_window::buttonW,
                                           antenna::scan_mode_window::buttonH,
                                           text);
        label->setFilled(false);
        label->setPenFocusWidth(antenna::scan_mode_window::commonFocusPen);
        label->setPenWidth(antenna::scan_mode_window::commonNoFocusPen);

        label->setFont(style::window::font::verysmall);
        label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));

        return label;
    }

    gui::Label *ScanModesWindow::addSpacer(void)
    {
        gui::Label *label = new gui::Label(nullptr,
                                           antenna::scan_mode_window::commonDefaultPos,
                                           antenna::scan_mode_window::commonDefaultPos,
                                           antenna::scan_mode_window::buttonW,
                                           antenna::scan_mode_window::commonSpacerH);
        label->setEdges(gui::RectangleEdge::None);
        label->setFilled(false);
        label->activeItem = false;
        label->visible    = false;

        return label;
    }
    void ScanModesWindow::updateCurrentMode(std::string &data)
    {
        uint32_t mode = 0;
        try {
            mode = std::stoi(data);
        }
        catch (const std::exception &e) {
            LOG_ERROR("ScanModesWindow::updateCurrentMode exception %s", e.what());
            return;
        }
        if (mode < modeButtonParams.size()) {
            modesBox->setFocusOnElement(mode);
        }
    }
} // namespace gui