Repository navigation
Expand file tree
/
Copy pathQelSwitch.cpp
More file actions
187 lines (151 loc) · 4.23 KB
/
Copy pathQelSwitch.cpp
File metadata and controls
187 lines (151 loc) · 4.23 KB
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
#include "QelSwitch.h"
#include "../QelStyleHelper/QelStyleHelper.h"
#include "../QelTheme/QelTheme.h"
#include <QMouseEvent>
#include <QPainter>
namespace qel {
namespace {
QelStyleHelper::StateStyleSet checkedTrackStyles()
{
const QelTheme::ColorTokens &c = QelTheme::colors();
return {
{c.primary, c.fillBlank, c.primary},
{c.primaryHover, c.fillBlank, c.primaryHover},
{c.primaryHover, c.fillBlank, c.primaryHover},
{c.primaryHover, c.fillBlank, c.primary},
{c.primaryDisabled, c.fillBlank, c.primaryDisabled},
{c.primaryDisabled, c.fillBlank, c.primaryDisabled}
};
}
QelStyleHelper::StateStyleSet uncheckedTrackStyles()
{
const QelTheme::ColorTokens &c = QelTheme::colors();
return {
{c.borderBase, c.fillBlank, c.borderBase},
{c.textPlaceholder, c.fillBlank, c.textPlaceholder},
{c.textPlaceholder, c.fillBlank, c.textPlaceholder},
{c.textPlaceholder, c.fillBlank, c.primary},
{c.borderLight, c.fillBlank, c.borderLight},
{c.borderLight, c.fillBlank, c.borderLight}
};
}
} // namespace
QelSwitch::QelSwitch(QWidget *parent)
: QWidget(parent),
animation_(new QPropertyAnimation(this, "offset", this))
{
setCursor(Qt::PointingHandCursor);
setFocusPolicy(Qt::StrongFocus);
animation_->setDuration(150);
applySize();
syncOffset(false);
}
bool QelSwitch::checked() const
{
return checked_;
}
void QelSwitch::setChecked(bool checked)
{
if (checked_ == checked) {
return;
}
checked_ = checked;
syncOffset(true);
update();
emit toggled(checked_);
}
void QelSwitch::setDisabled(bool disabled)
{
setEnabled(!disabled);
update();
}
void QelSwitch::setSize(Size size)
{
size_ = size;
applySize();
syncOffset(false);
update();
}
qreal QelSwitch::offset() const
{
return offset_;
}
void QelSwitch::setOffset(qreal offset)
{
offset_ = offset;
update();
}
void QelSwitch::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
if (!isEnabled() || event->button() != Qt::LeftButton) {
return;
}
setChecked(!checked_);
}
void QelSwitch::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QelStateContext context;
context.enabled = isEnabled();
context.hovered = underMouse();
context.focused = hasFocus();
const QelVisualState state = QelStyleHelper::resolveState(context);
const QelStyleHelper::StateStyleSet styles =
checked_ ? checkedTrackStyles() : uncheckedTrackStyles();
const QelStyleHelper::ComponentStyle visual =
QelStyleHelper::styleForState(styles, state);
const QelTheme::ColorTokens &c = QelTheme::colors();
QRectF trackRect(0.5, 0.5, controlWidth_ - 1.0, controlHeight_ - 1.0);
if (hasFocus() && isEnabled()) {
painter.setPen(QPen(QColor(c.primary), 1.0));
} else {
painter.setPen(Qt::NoPen);
}
painter.setBrush(QColor(visual.background));
painter.drawRoundedRect(trackRect, controlHeight_ / 2.0, controlHeight_ / 2.0);
const qreal top = (controlHeight_ - knobDiameter_) / 2.0;
const QRectF knobRect(offset_, top, knobDiameter_, knobDiameter_);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(c.fillBlank));
painter.drawEllipse(knobRect);
}
void QelSwitch::applySize()
{
switch (size_) {
case Size::Large:
controlWidth_ = 50;
controlHeight_ = 24;
knobDiameter_ = 20;
break;
case Size::Default:
controlWidth_ = 40;
controlHeight_ = 20;
knobDiameter_ = 16;
break;
case Size::Small:
controlWidth_ = 32;
controlHeight_ = 16;
knobDiameter_ = 12;
break;
}
setFixedSize(controlWidth_, controlHeight_);
}
void QelSwitch::syncOffset(bool animated)
{
const qreal minX = 2.0;
const qreal maxX = controlWidth_ - knobDiameter_ - 2.0;
const qreal target = checked_ ? maxX : minX;
if (!animated) {
animation_->stop();
offset_ = target;
return;
}
animation_->stop();
animation_->setStartValue(offset_);
animation_->setEndValue(target);
animation_->start();
}
} // namespace qel