Skip to content

Commit 5defcdf

Browse files
author
KhalilSelyan
committedMay 16, 2024
update custom slider with dragging functionality
Signed-off-by: KhalilSelyan <khalil@leodrive.ai>
1 parent 08881e8 commit 5defcdf

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// CustomToggleSwitch.h
2+
#ifndef CUSTOMTOGGLESWITCH_H
3+
#define CUSTOMTOGGLESWITCH_H
4+
5+
#include <QCheckBox>
6+
#include <QColor>
7+
#include <QMouseEvent>
8+
#include <QPainter>
9+
10+
class CustomToggleSwitch : public QCheckBox
11+
{
12+
Q_OBJECT
13+
14+
public:
15+
explicit CustomToggleSwitch(QWidget * parent = nullptr);
16+
QSize sizeHint() const override; // Declare the sizeHint method
17+
18+
protected:
19+
void paintEvent(QPaintEvent * event) override;
20+
void mousePressEvent(QMouseEvent * event) override;
21+
void mouseReleaseEvent(QMouseEvent * event) override;
22+
void mouseMoveEvent(QMouseEvent * event) override;
23+
24+
private:
25+
bool isDragging = false;
26+
QPoint dragStartPoint;
27+
};
28+
29+
#endif // CUSTOMTOGGLESWITCH_H
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
// CustomToggleSwitch.cpp
22
#include "custom_toggle_switch.hpp"
33

4-
#include <QMouseEvent>
5-
#include <QPainter>
6-
7-
CustomToggleSwitch::CustomToggleSwitch(QWidget * parent) : QCheckBox(parent)
4+
CustomToggleSwitch::CustomToggleSwitch(QWidget * parent) : QCheckBox(parent), isDragging(false)
85
{
96
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
107
setCursor(Qt::PointingHandCursor);
118
}
129

1310
QSize CustomToggleSwitch::sizeHint() const
1411
{
15-
return QSize(50, 20); // Preferred size of the toggle switch
12+
return QSize(60, 30); // Preferred size of the toggle switch
1613
}
1714

1815
void CustomToggleSwitch::paintEvent(QPaintEvent *)
@@ -27,40 +24,57 @@ void CustomToggleSwitch::paintEvent(QPaintEvent *)
2724
QRect r = rect().adjusted(margin, margin, -margin, -margin);
2825
bool isChecked = this->isChecked();
2926

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);
3532

3633
// Draw background
37-
p.setBrush(isChecked ? QColor("#00E678") : QColor("#525252"));
34+
p.setBrush(isChecked ? QColor("#8bd0f0") : QColor("#303538"));
3835
p.setPen(Qt::NoPen);
3936
p.drawRoundedRect(r, circleRadius + 4, circleRadius + 4);
4037

4138
// 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);
4343
QRect circleRect(circleX, r.top() + margin, circleRadius * 2, circleRadius * 2);
44-
p.setBrush(QColor("#FFF"));
44+
p.setBrush(isChecked ? QColor("#003546") : QColor("#8a9297"));
4545
p.drawEllipse(circleRect);
4646
}
4747

4848
void CustomToggleSwitch::mousePressEvent(QMouseEvent * event)
4949
{
50-
if (event->buttons() & Qt::LeftButton) {
50+
if (event->button() == Qt::LeftButton) {
5151
isDragging = true;
5252
dragStartPoint = event->pos();
5353
}
5454
QCheckBox::mousePressEvent(event);
5555
}
5656

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+
5766
void CustomToggleSwitch::mouseReleaseEvent(QMouseEvent * event)
5867
{
5968
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);
6275
}
6376
isDragging = false;
77+
update(); // Force repaint
6478
}
6579
QCheckBox::mouseReleaseEvent(event);
6680
}

0 commit comments

Comments
 (0)