1
+ #include " custom_segmented_button_item.hpp"
2
+
3
+ CustomSegmentedButtonItem::CustomSegmentedButtonItem (const QString & text, QWidget * parent)
4
+ : QPushButton(text, parent),
5
+ bgColor(" #0F1417" ),
6
+ checkedBgColor(" #354A54" ),
7
+ inactiveTextColor(" #8a9297" ),
8
+ activeTextColor(" #d0e6f2" )
9
+ {
10
+ setCheckable (true );
11
+ setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Preferred);
12
+ }
13
+
14
+ void CustomSegmentedButtonItem::setColors (
15
+ const QColor & bg, const QColor & checkedBg, const QColor & activeText,
16
+ const QColor & inactiveText)
17
+ {
18
+ bgColor = bg;
19
+ checkedBgColor = checkedBg;
20
+ activeTextColor = activeText;
21
+ inactiveTextColor = inactiveText;
22
+ update ();
23
+ }
24
+
25
+ // void CustomSegmentedButtonItem::updateSize()
26
+ // {
27
+ // QFontMetrics fm(font());
28
+ // int width = fm.horizontalAdvance(text()) + 40; // Add padding
29
+ // int height = fm.height() + 20; // Add padding
30
+ // setFixedSize(width, height);
31
+ // }
32
+
33
+ void CustomSegmentedButtonItem::paintEvent (QPaintEvent *)
34
+ {
35
+ QPainter painter (this );
36
+ painter.setRenderHint (QPainter::Antialiasing);
37
+
38
+ // Determine the button's color based on its state
39
+ QColor buttonColor;
40
+ if (isHovered && !isChecked ()) {
41
+ buttonColor = hoverColor;
42
+ } else {
43
+ buttonColor = isChecked () ? checkedBgColor : bgColor;
44
+ }
45
+ // Determine if this is the first or last button
46
+ bool isFirstButton = false ;
47
+ bool isLastButton = false ;
48
+
49
+ QHBoxLayout * parentLayout = qobject_cast<QHBoxLayout *>(parentWidget ()->layout ());
50
+ if (parentLayout) {
51
+ int index = parentLayout->indexOf (this );
52
+ isFirstButton = (index == 0 );
53
+ isLastButton = (index == parentLayout->count () - 1 );
54
+ }
55
+
56
+ // Draw button background
57
+
58
+ QRect buttonRect = rect ().adjusted (1 , 1 , -1 , -1 ); // Adjust to fill the space;
59
+
60
+ // make it shorter in height by making both top and bottom 1 less
61
+ buttonRect.setTop (buttonRect.top () + 1 );
62
+ buttonRect.setBottom (buttonRect.bottom () - 1 );
63
+ QPainterPath path;
64
+ int radius = (height () - 2 ) / 2 ;
65
+
66
+ if (isFirstButton) {
67
+ path.moveTo (buttonRect.right (), buttonRect.top ());
68
+ path.arcTo (buttonRect.left (), buttonRect.top () - 0.5 , 2 * radius, 2 * radius, 90 , 180 );
69
+ path.lineTo (buttonRect.right (), buttonRect.bottom ());
70
+ path.lineTo (buttonRect.right (), buttonRect.top ());
71
+ } else if (isLastButton) {
72
+ path.moveTo (buttonRect.left (), buttonRect.top ());
73
+ path.arcTo (
74
+ buttonRect.right () - 2 * radius, buttonRect.top () - 0.5 , 2 * radius, 2 * radius, 90 , -180 );
75
+ path.lineTo (buttonRect.left (), buttonRect.bottom ());
76
+ path.lineTo (buttonRect.left (), buttonRect.top ());
77
+ } else {
78
+ path.addRect (buttonRect);
79
+ }
80
+ painter.fillPath (path, buttonColor);
81
+
82
+ // Draw button text
83
+ painter.setPen (isChecked () ? activeTextColor : inactiveTextColor);
84
+ painter.drawText (rect (), Qt::AlignCenter, text ());
85
+ }
86
+
87
+ void CustomSegmentedButtonItem::enterEvent (QEvent * event)
88
+ {
89
+ isHovered = true ;
90
+ update ();
91
+ QPushButton::enterEvent (event);
92
+ }
93
+
94
+ void CustomSegmentedButtonItem::leaveEvent (QEvent * event)
95
+ {
96
+ isHovered = false ;
97
+ update ();
98
+ QPushButton::leaveEvent (event);
99
+ }
0 commit comments