1
1
// CustomToggleSwitch.cpp
2
2
#include " custom_toggle_switch.hpp"
3
3
4
- #include < QMouseEvent>
5
- #include < QPainter>
6
-
7
- CustomToggleSwitch::CustomToggleSwitch (QWidget * parent) : QCheckBox(parent)
4
+ CustomToggleSwitch::CustomToggleSwitch (QWidget * parent) : QCheckBox(parent), isDragging(false )
8
5
{
9
6
setSizePolicy (QSizePolicy::Fixed , QSizePolicy::Fixed );
10
7
setCursor (Qt::PointingHandCursor);
11
8
}
12
9
13
10
QSize CustomToggleSwitch::sizeHint () const
14
11
{
15
- return QSize (50 , 20 ); // Preferred size of the toggle switch
12
+ return QSize (60 , 30 ); // Preferred size of the toggle switch
16
13
}
17
14
18
15
void CustomToggleSwitch::paintEvent (QPaintEvent *)
@@ -27,40 +24,57 @@ void CustomToggleSwitch::paintEvent(QPaintEvent *)
27
24
QRect r = rect ().adjusted (margin, margin, -margin, -margin);
28
25
bool isChecked = this ->isChecked ();
29
26
30
- // Draw Border that is larger than the background with a bit of thickness
31
- QRect borederR = r.adjusted (-margin, -margin, margin, margin);
32
- p.setPen ( QPen (! isChecked ? QColor (" #00E678 " ) : QColor (" #525252 " ), 2 ));
33
- p.setBrush (Qt::NoBrush );
34
- p.drawRoundedRect (borederR , circleRadius + 4 , circleRadius + 4 );
27
+ // Draw border that is larger than the background with a bit of thickness
28
+ QRect borderR = r.adjusted (-margin, -margin, margin, margin);
29
+ p.setBrush ( isChecked ? QColor (" #8bd0f0 " ) : QColor (" #303538 " ));
30
+ p.setPen (Qt::NoPen );
31
+ p.drawRoundedRect (borderR , circleRadius + 4 , circleRadius + 4 );
35
32
36
33
// Draw background
37
- p.setBrush (isChecked ? QColor (" #00E678 " ) : QColor (" #525252 " ));
34
+ p.setBrush (isChecked ? QColor (" #8bd0f0 " ) : QColor (" #303538 " ));
38
35
p.setPen (Qt::NoPen);
39
36
p.drawRoundedRect (r, circleRadius + 4 , circleRadius + 4 );
40
37
41
38
// Draw indicator
42
- int circleX = isChecked ? (r.right () - circleRadius * 2 - margin) : r.left () + margin;
39
+ int minX = r.left () + margin * 2 ;
40
+ int maxX = r.right () - circleRadius * 2 - margin;
41
+ int circleX =
42
+ isDragging ? qBound (minX, dragStartPoint.x () - circleRadius, maxX) : (isChecked ? maxX : minX);
43
43
QRect circleRect (circleX, r.top () + margin, circleRadius * 2 , circleRadius * 2 );
44
- p.setBrush (QColor (" #FFF " ));
44
+ p.setBrush (isChecked ? QColor (" #003546 " ) : QColor ( " #8a9297 " ));
45
45
p.drawEllipse (circleRect);
46
46
}
47
47
48
48
void CustomToggleSwitch::mousePressEvent (QMouseEvent * event)
49
49
{
50
- if (event->buttons () & Qt::LeftButton) {
50
+ if (event->button () == Qt::LeftButton) {
51
51
isDragging = true ;
52
52
dragStartPoint = event->pos ();
53
53
}
54
54
QCheckBox::mousePressEvent (event);
55
55
}
56
56
57
+ void CustomToggleSwitch::mouseMoveEvent (QMouseEvent * event)
58
+ {
59
+ if (isDragging) {
60
+ dragStartPoint = event->pos ();
61
+ update (); // Force repaint
62
+ }
63
+ QCheckBox::mouseMoveEvent (event);
64
+ }
65
+
57
66
void CustomToggleSwitch::mouseReleaseEvent (QMouseEvent * event)
58
67
{
59
68
if (isDragging) {
60
- if (qAbs (event->pos ().x () - dragStartPoint.x ()) < 10 ) { // Simple click
61
- setChecked (!isChecked ());
69
+ if (rect ().contains (event->pos ())) { // Ensure click is within the switch's bounds
70
+ int minX = rect ().left () + 2 ;
71
+ int maxX = rect ().right () - height () / 2 - 2 ;
72
+ int middleX = (minX + maxX) / 2 ;
73
+ bool newState = dragStartPoint.x () >= middleX;
74
+ setChecked (newState);
62
75
}
63
76
isDragging = false ;
77
+ update (); // Force repaint
64
78
}
65
79
QCheckBox::mouseReleaseEvent (event);
66
80
}
0 commit comments