raDIYo 0.4
swswitch.h
1/***************************************************************************
2
3 source::worx raDIYo
4 Copyright © 2020-2022 c.holzheuer
5 c.holzheuer@sourceworx.org
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12***************************************************************************/
13
14/*
15
16 Written by viv. mail: viv.rajkumar@hotmail.com
17 @see https://stackoverflow.com/questions/14780517/toggle-switch-in-qt/51023362
18
19*/
20
21#ifndef SWSWITCH_H
22#define SWSWITCH_H
23
24#include <QWidget>
25#include <QPropertyAnimation>
26#include <QAbstractButton>
27
28
29class SWSwitch : public QAbstractButton
30{
31 Q_OBJECT
32
33 Q_PROPERTY( int offset READ offset WRITE setOffset NOTIFY hysenkyte() )
34 //Q_PROPERTY( QBrush brush READ brush WRITE setBrush )
35
36public:
37
38 SWSwitch( QWidget* parent = nullptr );
39
40 QSize sizeHint() const override;
41
42 QBrush brush() const
43 {
44 return _brush;
45 }
46
47 void setBrush( const QBrush& brush )
48 {
49 _brush = brush;
50 }
51
52 int offset() const
53 {
54 return _x;
55 }
56
57 void setOffset( int offset )
58 {
59 _x = offset;
60 update();
61 }
62
63signals:
64
65 void hysenkyte();
66
67protected:
68
69 const int SWANIMDURATION = 400;//120;
70
71 void paintEvent(QPaintEvent*) override;
72 void mouseReleaseEvent(QMouseEvent*) override;
73 void enterEvent(QEvent*) override;
74
75 bool _isOn;
76 qreal _opacity;
77 int _x, _y, _height, _margin;
78 QBrush _thumb, _track, _brush;
79 QPropertyAnimation *_anim = nullptr;
80
81};
82
83
84#endif // SWSWITCH_H