summary refs log tree commit diff
path: root/src/ui/ToggleButton.cpp
blob: 33bf8f92029b732ce9d20034f029842009ddc1fe (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <QColor>
#include <QCoreApplication>
#include <QEvent>
#include <QPainter>

#include "ToggleButton.h"

void
Toggle::paintEvent(QPaintEvent *event)
{
        Q_UNUSED(event);
}

Toggle::Toggle(QWidget *parent)
  : QAbstractButton{parent}
{
        init();

        connect(this, &QAbstractButton::toggled, this, &Toggle::setState);
}

void
Toggle::setState(bool isEnabled)
{
        setChecked(isEnabled);
        thumb_->setShift(isEnabled ? Position::Left : Position::Right);
        setupProperties();
}

void
Toggle::init()
{
        track_ = new ToggleTrack(this);
        thumb_ = new ToggleThumb(this);

        setCursor(QCursor(Qt::PointingHandCursor));
        setCheckable(true);
        setChecked(false);

        setState(false);
        setupProperties();

        QCoreApplication::processEvents();
}

void
Toggle::setupProperties()
{
        if (isEnabled()) {
                Position position = thumb_->shift();

                thumb_->setThumbColor(trackColor());

                if (position == Position::Left)
                        track_->setTrackColor(activeColor());
                else if (position == Position::Right)
                        track_->setTrackColor(inactiveColor());
        }

        update();
}

void
Toggle::setDisabledColor(const QColor &color)
{
        disabledColor_ = color;
        setupProperties();
}

void
Toggle::setActiveColor(const QColor &color)
{
        activeColor_ = color;
        setupProperties();
}

void
Toggle::setInactiveColor(const QColor &color)
{
        inactiveColor_ = color;
        setupProperties();
}

void
Toggle::setTrackColor(const QColor &color)
{
        trackColor_ = color;
        setupProperties();
}

ToggleThumb::ToggleThumb(Toggle *parent)
  : QWidget{parent}
  , toggle_{parent}
  , position_{Position::Right}
  , offset_{0}
{
        parent->installEventFilter(this);
}

void
ToggleThumb::setShift(Position position)
{
        if (position_ != position) {
                position_ = position;
                updateOffset();
        }
}

bool
ToggleThumb::eventFilter(QObject *obj, QEvent *event)
{
        const QEvent::Type type = event->type();

        if (QEvent::Resize == type || QEvent::Move == type) {
                setGeometry(toggle_->rect().adjusted(8, 8, -8, -8));
                updateOffset();
        }

        return QWidget::eventFilter(obj, event);
}

void
ToggleThumb::paintEvent(QPaintEvent *event)
{
        Q_UNUSED(event)

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        QBrush brush;
        brush.setStyle(Qt::SolidPattern);
        brush.setColor(toggle_->isEnabled() ? thumbColor_ : Qt::white);

        painter.setBrush(brush);
        painter.setPen(Qt::NoPen);

        int s;
        QRectF r;

        s = height() - 10;
        r = QRectF(5 + offset_, 5, s, s);

        painter.drawEllipse(r);

        if (!toggle_->isEnabled()) {
                brush.setColor(toggle_->disabledColor());
                painter.setBrush(brush);
                painter.drawEllipse(r);
        }
}

void
ToggleThumb::updateOffset()
{
        const QSize s(size());
        offset_ = position_ == Position::Left ? static_cast<qreal>(s.width() - s.height()) : 0;
        update();
}

ToggleTrack::ToggleTrack(Toggle *parent)
  : QWidget{parent}
  , toggle_{parent}
{
        Q_ASSERT(parent);

        parent->installEventFilter(this);
}

void
ToggleTrack::setTrackColor(const QColor &color)
{
        trackColor_ = color;
        update();
}

bool
ToggleTrack::eventFilter(QObject *obj, QEvent *event)
{
        const QEvent::Type type = event->type();

        if (QEvent::Resize == type || QEvent::Move == type) {
                setGeometry(toggle_->rect());
        }

        return QWidget::eventFilter(obj, event);
}

void
ToggleTrack::paintEvent(QPaintEvent *event)
{
        Q_UNUSED(event)

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        QBrush brush;
        if (toggle_->isEnabled()) {
                brush.setColor(trackColor_);
                painter.setOpacity(0.8);
        } else {
                brush.setColor(toggle_->disabledColor());
                painter.setOpacity(0.6);
        }

        brush.setStyle(Qt::SolidPattern);
        painter.setBrush(brush);
        painter.setPen(Qt::NoPen);

        const int h = height() / 2;
        const QRect r(0, h / 2, width(), h);
        painter.drawRoundedRect(r.adjusted(14, 4, -14, -4), h / 2 - 4, h / 2 - 4);
}