-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathScrollBar.cpp
More file actions
130 lines (104 loc) · 3.35 KB
/
ScrollBar.cpp
File metadata and controls
130 lines (104 loc) · 3.35 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "ScrollBar.h"
ScrollBar::ScrollBar(Qt::Orientation orientation, QWidget *parent) : QScrollBar(orientation, parent) {
init();
}
ScrollBar::ScrollBar(QWidget *parent) : QScrollBar(parent) {
init();
}
void ScrollBar::setDarkMode(bool value) {
isDarkMode = value;
update();
}
void ScrollBar::init() {
isDarkMode = false;
animation1 = new QPropertyAnimation(this, "opacity");
animation1->setDuration(300);
animation1->setEasingCurve(QEasingCurve::InOutQuad);
animation2 = new QPropertyAnimation(this, "handle");
animation2->setDuration(300);
animation2->setEasingCurve(QEasingCurve::InOutQuad);
setSingleStep(20);
setPageStep(20);
}
void ScrollBar::setOpacity(qreal o) {
opacity = o;
update();
}
qreal ScrollBar::getOpacity() const { return opacity; }
void ScrollBar::setHandle(qreal w) {
handle = w;
update();
}
qreal ScrollBar::getHandle() { return handle; }
void ScrollBar::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QColor light_area = QColor("#F0F0F0");
QColor dark_area = QColor("#383838");
QColor currentColorArea = isHover ? (isDarkMode ? dark_area : light_area) : Qt::transparent;
QRect area = rect();
int areaWidth, areaHeight, radius = 4;
if (orientation() == Qt::Vertical) {
areaWidth = isHover ? 6 : 0;
areaHeight = area.height();
} else {
areaWidth = area.width();
areaHeight = isHover ? 6 : 0;
}
painter.setPen(Qt::NoPen);
painter.setBrush(currentColorArea);
painter.setOpacity(opacity);
painter.drawRoundedRect(QRect(area.x() + 2, area.y(), areaWidth, areaHeight), radius, radius);
QColor light_handle = QColor("#CCCCCC");
QColor dark_handle = QColor("#707070");
QColor currentColorHandle = isDarkMode ? dark_handle : light_handle;
int height, width, x, y;
int range = maximum() - minimum();
if (range < 0) return;
if (orientation() == Qt::Vertical) {
height = std::max(20, static_cast<int>(double(pageStep()) / (range + pageStep()) * areaHeight) + 20);
width = static_cast<int>(handle);
x = 2;
y = static_cast<int>((area.height() - height) * (static_cast<double>(value()) / range));
} else {
height = static_cast<int>(handle);
width = std::max(20, static_cast<int>(double(pageStep()) / (range + pageStep()) * areaWidth));
x = static_cast<int>((area.width() - width) * (static_cast<double>(value()) / range));
y = 2;
}
painter.setPen(Qt::NoPen);
painter.setBrush(currentColorHandle);
painter.setOpacity(1.0);
painter.drawRoundedRect(QRect(x, y, width, height), radius, radius);
}
void ScrollBar::mousePressEvent(QMouseEvent *event) {
isPressed = true;
QScrollBar::mousePressEvent(event);
update();
}
void ScrollBar::enterEvent(QEnterEvent *event) {
isHover = true;
animation1->stop();
animation1->setStartValue(opacity);
animation1->setEndValue(1.0);
animation1->start();
animation2->stop();
animation2->setStartValue(handle);
animation2->setEndValue(6.0);
animation2->start();
QScrollBar::enterEvent(event);
update();
}
void ScrollBar::leaveEvent(QEvent *event) {
isHover = false;
animation1->stop();
animation1->setStartValue(opacity);
animation1->setEndValue(0.0);
animation1->start();
animation2->stop();
animation2->setStartValue(handle);
animation2->setEndValue(2.0);
animation2->start();
QScrollBar::leaveEvent(event);
update();
}