~aleteoryx/muditaos

ref: 6d344fe6cb6df6fc0ce4d83d2a6c88f078b20677 muditaos/module-gui/gui/widgets/SideListView.cpp -rw-r--r-- 5.0 KiB
6d344fe6 — Paweł Joński [BH-655] Add SideListView 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Image.hpp"
#include "SideListView.hpp"
#include "Style.hpp"

namespace gui
{
    SideListView::SideListView(Item *parent,
                               unsigned int x,
                               unsigned int y,
                               unsigned int w,
                               unsigned int h,
                               std::shared_ptr<SideListItemProvider> prov)
        : Rect{parent, x, y, w, h}
    {

        setEdges(RectangleEdge::None);

        auto body = new VBox{this, x, y, w, h};
        body->setEdges(RectangleEdge::None);

        progressbar = new HBarGraph(body, 0U, 0U, 0U);
        progressbar->setMargins(Margins(style::sidelistview::progress_bar::margin_left,
                                        style::sidelistview::progress_bar::margin_top,
                                        style::sidelistview::progress_bar::margin_right,
                                        style::sidelistview::progress_bar::margin_bottom));
        progressbar->activeItem = false;
        progressbar->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));

        listItemBox = new gui::HBox(body, 0U, 0U, style::sidelistview::list_item::w, style::sidelistview::list_item::h);
        listItemBox->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        listItemBox->setEdges(gui::RectangleEdge::None);

        auto arrowsHBox = new HBox{this,
                                   style::sidelistview::arrows_hbox::x,
                                   style::sidelistview::arrows_hbox::y,
                                   style::sidelistview::arrows_hbox::w,
                                   style::sidelistview::arrows_hbox::h};
        arrowsHBox->setEdges(gui::RectangleEdge::None);
        auto arrowLeft = new gui::Image(style::sidelistview::arrow_left_image);
        arrowLeft->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center));
        arrowLeft->activeItem = false;
        arrowLeft->setEdges(RectangleEdge::None);
        arrowsHBox->addWidget(arrowLeft);

        auto arrowRight = new gui::Image(style::sidelistview::arrow_right_image);
        arrowRight->setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center));
        arrowRight->activeItem = false;
        arrowRight->setEdges(RectangleEdge::None);

        auto rectFiller = new gui::Rect(arrowsHBox,
                                        0U,
                                        0U,
                                        arrowsHBox->getWidth() - arrowLeft->getWidth() - arrowRight->getWidth(),
                                        arrowsHBox->getHeight());

        rectFiller->setMaximumSize(arrowsHBox->getWidth(), arrowsHBox->getHeight());
        rectFiller->setEdges(RectangleEdge::None);

        arrowsHBox->addWidget(arrowRight);

        if (prov != nullptr) {
            provider       = std::move(prov);
            provider->list = this;
        }

        type = gui::ItemType::LIST;

        progressbar->setMaximum(provider->requestRecordsCount());
        progressbar->createGraph();
        progressbar->setValue(progressbarStartValue);

        if (auto firstItem = provider->getItem(Order::Next)) {
            listItemBox->addWidget(firstItem);
            listItemBox->setFocusItem(firstItem);
        }
    }

    bool SideListView::onInput(const gui::InputEvent &inputEvent)
    {
        if (auto ret = listItemBox->onInput(inputEvent)) {
            return ret;
        }

        if (inputEvent.isShortRelease(KeyCode::KEY_ENTER)) {
            if (slide(Order::Next)) {
                progressbar->setValue(progressbar->getValue() + 1);
                return true;
            }
        }
        else if (inputEvent.isShortRelease(KeyCode::KEY_RF)) {
            if (slide(Order::Previous)) {
                progressbar->setValue(progressbar->getValue() - 1);
                return true;
            }
        }

        return false;
    }

    auto SideListView::slide(gui::Order order) -> bool
    {
        auto newItem = provider->getItem(order);

        clear();
        if (newItem != nullptr) {
            listItemBox->addWidget(newItem);
            listItemBox->setFocusItem(newItem);
            return true;
        }
        return false;
    }

    SideListView::~SideListView()
    {
        clear();
    }

    void SideListView::clear()
    {
        listItemBox->setFocusItem(nullptr);

        while (auto el = listItemBox->children.back()) {
            if (el->type == ItemType::LIST_ITEM) {
                if (!dynamic_cast<ListItem *>(el)->deleteByList) {
                    listItemBox->removeWidget(el);
                }
                else {
                    listItemBox->erase(el);
                }
            }
            else {
                listItemBox->erase(el);
            }
        }
    }

} /* namespace gui */