-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathswitchbutton.hpp
More file actions
80 lines (65 loc) · 2.85 KB
/
Copy pathswitchbutton.hpp
File metadata and controls
80 lines (65 loc) · 2.85 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
#pragma once
#include <QAbstractButton>
#include <memory>
class SwitchButton : public QAbstractButton
{
Q_OBJECT
Q_PROPERTY(double offset READ offset WRITE setOffset NOTIFY offsetChanged)
Q_PROPERTY(
QColor checkedColor READ checkedColor WRITE setCheckedColor NOTIFY checkedColorChanged)
Q_PROPERTY(QColor uncheckedColor READ uncheckedColor WRITE setUncheckedColor NOTIFY
uncheckedColorChanged)
Q_PROPERTY(QColor thumbColor READ thumbColor WRITE setThumbColor NOTIFY thumbColorChanged)
Q_PROPERTY(QColor thumbBorderColor READ thumbBorderColor WRITE setThumbBorderColor NOTIFY
thumbBorderColorChanged)
Q_PROPERTY(int animationDuration READ animationDuration WRITE setAnimationDuration NOTIFY
animationDurationChanged)
public:
explicit SwitchButton(QWidget *parent = nullptr);
~SwitchButton() override;
[[nodiscard]] auto minimumSizeHint() const -> QSize override;
[[nodiscard]] auto sizeHint() const -> QSize override;
// Color properties
void setCheckedColor(const QColor &color);
[[nodiscard]] auto checkedColor() const -> QColor;
void setUncheckedColor(const QColor &color);
[[nodiscard]] auto uncheckedColor() const -> QColor;
void setThumbColor(const QColor &color);
[[nodiscard]] auto thumbColor() const -> QColor;
void setThumbBorderColor(const QColor &color);
[[nodiscard]] auto thumbBorderColor() const -> QColor;
// Animation
void setAnimationDuration(int duration);
[[nodiscard]] auto animationDuration() const -> int;
[[nodiscard]] bool isAnimating() const;
signals:
void offsetChanged(double offset);
void checkedColorChanged(const QColor &color);
void uncheckedColorChanged(const QColor &color);
void thumbColorChanged(const QColor &color);
void thumbBorderColorChanged(const QColor &color);
void animationDurationChanged(int duration);
void animationStarted(bool checked);
void animationFinished(bool checked);
protected:
void paintEvent(QPaintEvent *event) override;
void enterEvent(QEnterEvent *event) override;
void leaveEvent(QEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
private slots:
void onToggled(bool checked);
private:
[[nodiscard]] auto offset() const -> double;
void setOffset(double offset);
void updateThumbPosition();
void startAnimation(bool checked);
void drawBackground(QPainter &painter, const QRectF &rect);
void drawThumb(QPainter &painter, const QRectF &rect);
[[nodiscard]] auto widthMargin() const -> double;
[[nodiscard]] auto heightMargin() const -> double;
[[nodiscard]] auto thumbSize() const -> double;
[[nodiscard]] auto slotRect() const -> QRectF;
[[nodiscard]] auto thumbRect() const -> QRectF;
class SwitchButtonPrivate;
std::unique_ptr<SwitchButtonPrivate> d_ptr;
};