forked from AngryFender/Remini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtogglebutton.cpp
More file actions
76 lines (62 loc) · 2.66 KB
/
Copy pathtogglebutton.cpp
File metadata and controls
76 lines (62 loc) · 2.66 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
#include "togglebutton.h"
#include <QMouseEvent>
#include <QPainter>
// ---------------------------------------------------------------------------
// Constructor
//
// The button starts unchecked. If you want it to start checked, call
// setChecked(true) after construction instead of relying on a default.
// ---------------------------------------------------------------------------
ToggleButton::ToggleButton(QWidget *parent) : QAbstractButton(parent)
{
setCheckable(true);
trackEdit.setStyle(Qt::SolidPattern);
trackEdit.setColor(QStringLiteral("#b3d9ff"));
trackReadonly.setStyle(Qt::SolidPattern);
trackReadonly.setColor(QStringLiteral("#a0a0a4"));
circleEdit.setStyle(Qt::SolidPattern);
circleEdit.setColor(QStringLiteral("#2492ff"));
circleReadonly.setStyle(Qt::SolidPattern);
circleReadonly.setColor(QStringLiteral("#eaeaf0"));
}
bool ToggleButton::getState() const { return isChecked(); }
// ---------------------------------------------------------------------------
// setChecked:
// * Delegates to QAbstractButton so that isChecked() and the toggled()
// signal stay in sync with the visual state.
// * Repaints.
// ---------------------------------------------------------------------------
void ToggleButton::setChecked(bool checked)
{
QAbstractButton::setChecked(checked);
update();
}
// ---------------------------------------------------------------------------
// paintEvent
//
// All geometry is computed from width() / height() so the button scales
// correctly on HiDPI screens. Hard-coded pixel positions would drift between
// X11 (fractional QT_SCALE_FACTOR) and Wayland (integer compositor scale).
// ---------------------------------------------------------------------------
void ToggleButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
p.setPen(Qt::NoPen);
const bool on = isChecked();
// Reserve some outer margin so the track is never flush with the border.
const qreal margin = qMin(width(), height()) * 0.15;
const qreal trackW = width() - 2 * margin;
const qreal trackH = height() * 0.35;
const qreal trackY = (height() - trackH) / 2.0;
const qreal radius = trackH / 2.0;
const qreal knobR = radius * 0.85;
const qreal knobY = trackY + trackH / 2.0;
const qreal knobOnX = margin + trackW - radius;
const qreal knobOffX = margin + radius;
p.setBrush(on ? trackEdit : trackReadonly);
p.drawRoundedRect(QRectF(margin, trackY, trackW, trackH), radius, radius);
p.setBrush(on ? circleEdit : circleReadonly);
p.drawEllipse(QPointF(on ? knobOnX : knobOffX, knobY), knobR, knobR);
}