~aleteoryx/muditaos

ref: 0a322b878cb26c450064e5e3d9ec825415de241c muditaos/module-gui/gui/widgets/CheckBoxWithLabel.cpp -rw-r--r-- 1.7 KiB
0a322b87 — Przemyslaw Brudny [EGD-6770] ListView onPageElement rebuild page jump added 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CheckBoxWithLabel.hpp"

namespace gui
{
    CheckBoxWithLabel::CheckBoxWithLabel(
        Item *parent, int x, int y, UTF8 description, std::function<void(CheckBoxWithLabel &)> clickCallback)
    {
        auto body = new gui::HBox(nullptr, x, y, style::window::default_body_width, style::window::label::big_h);
        body->setEdges(gui::RectangleEdge::None);

        label =
            new gui::Label(nullptr, 0, 0, style::checkbox::description_width, style::window::label::big_h, description);
        check = new gui::CheckBox(nullptr,
                                  style::checkbox::check_x,
                                  style::checkbox::check_y,
                                  style::checkbox::check_width,
                                  style::window::label::small_h);
        check->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));

        label->activatedCallback = [=](Item &item) {
            setChecked(!check->isChecked());
            if (clickCallback) {
                clickCallback(*this);
            }
            return true;
        };

        style::window::decorateOption(label);
        style::window::decorate(check);

        /*
         * We need to add widgets after decorate to correctly redraw them
         */
        parent->addWidget(body);
        body->addWidget(label);
        body->addWidget(check);
    }

    bool CheckBoxWithLabel::isChecked() const
    {
        return check->isChecked();
    }

    void CheckBoxWithLabel::setChecked(bool state)
    {
        check->setImageVisible(state);
    }
} // namespace gui