File: AbsoluteSlider.cpp

package info (click to toggle)
obs-studio 32.0.4%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,868 kB
  • sloc: ansic: 191,636; cpp: 110,959; makefile: 877; python: 675; sh: 287; javascript: 19
file content (166 lines) | stat: -rw-r--r-- 4,442 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "AbsoluteSlider.hpp"

#include <QPainter>

#include "moc_AbsoluteSlider.cpp"

AbsoluteSlider::AbsoluteSlider(QWidget *parent) : SliderIgnoreScroll(parent)
{
	installEventFilter(this);
	setMouseTracking(true);

	tickColor.setRgb(0x5b, 0x62, 0x73);
}

AbsoluteSlider::AbsoluteSlider(Qt::Orientation orientation, QWidget *parent) : SliderIgnoreScroll(orientation, parent)
{
	installEventFilter(this);
	setMouseTracking(true);

	tickColor.setRgb(0x5b, 0x62, 0x73);
}

void AbsoluteSlider::mousePressEvent(QMouseEvent *event)
{
	dragging = (event->buttons() & Qt::LeftButton || event->buttons() & Qt::MiddleButton);

	if (dragging) {
		setSliderDown(true);
		setValue(posToRangeValue(event));
		emit AbsoluteSlider::sliderMoved(posToRangeValue(event));
	}

	event->accept();
}

void AbsoluteSlider::mouseReleaseEvent(QMouseEvent *event)
{
	dragging = false;
	setSliderDown(false);
	event->accept();
}

void AbsoluteSlider::mouseMoveEvent(QMouseEvent *event)
{
	int val = posToRangeValue(event);

	if (val > maximum())
		val = maximum();
	else if (val < minimum())
		val = minimum();

	emit absoluteSliderHovered(val);

	if (dragging) {
		setValue(posToRangeValue(event));
		emit AbsoluteSlider::sliderMoved(posToRangeValue(event));
	}

	QSlider::mouseMoveEvent(event);
	event->accept();
}

bool AbsoluteSlider::eventFilter(QObject *obj, QEvent *event)
{
	if (event->type() == QEvent::KeyPress) {
		QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

		if (keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down) {
			return true;
		}
	}

	return QSlider::eventFilter(obj, event);
}

int AbsoluteSlider::posToRangeValue(QMouseEvent *event)
{
	QStyleOptionSlider opt;
	initStyleOption(&opt);

	int pos;
	int sliderMin;
	int sliderMax;
	int handleLength;

	const QRect groove = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
	const QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);

	if (orientation() == Qt::Horizontal) {
		pos = event->pos().x();
		handleLength = handle.width();
		sliderMin = groove.left() + (handleLength / 2);
		sliderMax = groove.right() - (handleLength / 2) + 1;
	} else {
		pos = event->pos().y();
		handleLength = handle.height();
		sliderMin = groove.top() + (handleLength / 2);
		sliderMax = groove.bottom() - (handleLength / 2) + 1;
	}

	int sliderValue = style()->sliderValueFromPosition(minimum(), maximum(), pos - sliderMin, sliderMax - sliderMin,
							   opt.upsideDown);

	return sliderValue;
}

bool AbsoluteSlider::getDisplayTicks() const
{
	return displayTicks;
}

void AbsoluteSlider::setDisplayTicks(bool display)
{
	displayTicks = display;
}

QColor AbsoluteSlider::getTickColor() const
{
	return tickColor;
}

void AbsoluteSlider::setTickColor(QColor c)
{
	tickColor = std::move(c);
}

void AbsoluteSlider::paintEvent(QPaintEvent *event)
{
	if (!getDisplayTicks()) {
		QSlider::paintEvent(event);
		return;
	}

	QPainter painter(this);

	QStyleOptionSlider opt;
	initStyleOption(&opt);

	QRect groove = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
	QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);

	const bool isHorizontal = orientation() == Qt::Horizontal;

	const int sliderLength = isHorizontal ? groove.width() - handle.width() : groove.height() - handle.height();
	const int handleSize = isHorizontal ? handle.width() : handle.height();
	const int grooveSize = isHorizontal ? groove.height() : groove.width();
	const int grooveStart = isHorizontal ? groove.left() : groove.top();
	const int tickLinePos = isHorizontal ? groove.center().y() : groove.center().x();
	const int tickLength = std::max((int)(grooveSize * 1.5) + grooveSize, 8 + grooveSize);
	const int tickLineStart = tickLinePos - (tickLength / 2) + 1;

	for (double offset = minimum(); offset <= maximum(); offset += singleStep()) {
		double tickPercent = (offset - minimum()) / (maximum() - minimum());
		const int tickLineOffset = grooveStart + std::floor(sliderLength * tickPercent) + (handleSize / 2);

		const int xPos = isHorizontal ? tickLineOffset : tickLineStart;
		const int yPos = isHorizontal ? tickLineStart : tickLineOffset;

		const int tickWidth = isHorizontal ? 1 : tickLength;
		const int tickHeight = isHorizontal ? tickLength : 1;

		painter.fillRect(xPos, yPos, tickWidth, tickHeight, tickColor);
	}

	QSlider::paintEvent(event);
}