~aleteoryx/muditaos

ref: 6665a43d2b901b89809c4f18338d8ea6c0b08df7 muditaos/module-apps/application-phonebook/widgets/ContactFlagsWidget.cpp -rw-r--r-- 3.6 KiB
6665a43d — Lefucjusz [BH-1780] Fix uncaught std::filesystem::file_size exception 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ContactFlagsWidget.hpp"

#include "ContactFlagIconWidget.hpp"
#include "ContactFlagsStyle.hpp"

#include <Alignment.hpp>
#include <BoxLayout.hpp>
#include <i18n/i18n.hpp>
#include <tools/Common.hpp>
#include <Style.hpp>

namespace gui
{

    const unsigned char ContactFlagsWidget::iconsCount = 4;
    ContactFlagsWidget::ContactFlagsWidget(Item *parent)
        : Rect(parent, 0, style::window::default_vertical_pos, style::window_width, style::widget::ContactFlag::height)
    {
        favourites = speedDial = ice = true;
        blocked                      = false;
        setEdges(RectangleEdge::Bottom);
        initGUIIcons();
        buildWidget();
        repositionIcons();
    }

    void ContactFlagsWidget::setFavourites(bool isEnabled)
    {
        if (favourites != isEnabled) {
            favourites = isEnabled;
            repositionIcons();
        }
    }

    void ContactFlagsWidget::setSpeedDial(bool isEnabled, unsigned char position)
    {
        if (speedDial != isEnabled) {
            speedDial = isEnabled;
            repositionIcons();
        }
        if (speedDialPos != position) {
            speedDialPos = position;
            speedDialIcon->setIconNumber(speedDialPos);
        }
    }

    void ContactFlagsWidget::setICE(bool isEnabled)
    {
        if (ice != isEnabled) {
            ice = isEnabled;
            repositionIcons();
        }
    }

    void ContactFlagsWidget::setBlocked(bool isBlocked)
    {
        if (blocked != isBlocked) {
            blocked = isBlocked;
            repositionIcons();
        }
    }

    void ContactFlagsWidget::initGUIIcons()
    {
        favouritesIcon = new ContactFlagIconWidget(
            "phonebook_heart_32px_W_M", utils::translate("app_phonebook_contact_flag_fav"), this);
        speedDialIcon =
            new ContactFlagIconWidget(speedDialPos, utils::translate("app_phonebook_contact_flag_speed_dial"), this);
        iceIcon = new ContactFlagIconWidget("ice_32px_W_G", utils::translate("app_phonebook_contact_flag_ice"), this);
        blockedIcon = new ContactFlagIconWidget(
            "phonebook_blocked_32px_W_G", utils::translate("app_phonebook_contact_flag_blocked"), this);
    }

    void ContactFlagsWidget::buildWidget()
    {
        mainBox = new HBox(this, 0, 0, style::window_width, style::widget::ContactFlag::itemHeight);
        mainBox->setAlignment(Alignment(Alignment::Horizontal::Center));
        mainBox->setEdges(RectangleEdge::None);
        mainBox->addWidget(favouritesIcon);
        mainBox->addWidget(iceIcon);
        mainBox->addWidget(speedDialIcon);
        mainBox->addWidget(blockedIcon);
    }

    void ContactFlagsWidget::repositionIcons()
    {
        favouritesIcon->setVisible(false);
        speedDialIcon->setVisible(false);
        iceIcon->setVisible(false);
        blockedIcon->setVisible(false);

        if (blocked) {
            blockedIcon->setVisible(true);
        }
        else {
            if (favourites) {
                favouritesIcon->setVisible(true);
            }

            if (ice) {
                iceIcon->setVisible(true);
            }

            if (speedDial) {
                speedDialIcon->setVisible(true);
            }
        }
        hideIfNoFlags();
        mainBox->resizeItems();
    }

    void ContactFlagsWidget::hideIfNoFlags()
    {
        if (blocked || favourites || ice || speedDial) {
            setVisible(true);
        }
        else {
            setVisible(false);
        }
    }

} // namespace gui