浏览代码

UI: Remove unnecessary ProxyStyle usage

Warchamp7 1 年之前
父节点
当前提交
8dcfae9a39

+ 67 - 14
UI/absolute-slider.cpp

@@ -1,29 +1,42 @@
-#include "slider-absoluteset-style.hpp"
 #include "absolute-slider.hpp"
-#include <QStyleFactory>
 
 AbsoluteSlider::AbsoluteSlider(QWidget *parent) : SliderIgnoreScroll(parent)
 {
 	installEventFilter(this);
 	setMouseTracking(true);
+}
 
-	QString styleName = style()->objectName();
-	QStyle *style;
-	style = QStyleFactory::create(styleName);
-	if (!style) {
-		style = new SliderAbsoluteSetStyle();
-	} else {
-		style = new SliderAbsoluteSetStyle(style);
+AbsoluteSlider::AbsoluteSlider(Qt::Orientation orientation, QWidget *parent)
+	: SliderIgnoreScroll(orientation, parent)
+{
+	installEventFilter(this);
+	setMouseTracking(true);
+}
+
+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));
 	}
 
-	style->setParent(this);
-	this->setStyle(style);
+	event->accept();
+}
+
+void AbsoluteSlider::mouseReleaseEvent(QMouseEvent *event)
+{
+	dragging = false;
+	setSliderDown(false);
+	event->accept();
 }
 
 void AbsoluteSlider::mouseMoveEvent(QMouseEvent *event)
 {
-	int val = minimum() +
-		  ((maximum() - minimum()) * event->pos().x()) / width();
+	int val = posToRangeValue(event);
 
 	if (val > maximum())
 		val = maximum();
@@ -31,8 +44,14 @@ void AbsoluteSlider::mouseMoveEvent(QMouseEvent *event)
 		val = minimum();
 
 	emit absoluteSliderHovered(val);
-	event->accept();
+
+	if (dragging) {
+		setValue(posToRangeValue(event));
+		emit AbsoluteSlider::sliderMoved(posToRangeValue(event));
+	}
+
 	QSlider::mouseMoveEvent(event);
+	event->accept();
 }
 
 bool AbsoluteSlider::eventFilter(QObject *obj, QEvent *event)
@@ -51,3 +70,37 @@ bool AbsoluteSlider::eventFilter(QObject *obj, QEvent *event)
 
 	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;
+}

+ 8 - 0
UI/absolute-slider.hpp

@@ -8,11 +8,19 @@ class AbsoluteSlider : public SliderIgnoreScroll {
 
 public:
 	AbsoluteSlider(QWidget *parent = nullptr);
+	AbsoluteSlider(Qt::Orientation orientation, QWidget *parent = nullptr);
 
 signals:
 	void absoluteSliderHovered(int value);
 
 protected:
 	virtual void mouseMoveEvent(QMouseEvent *event) override;
+	virtual void mousePressEvent(QMouseEvent *event) override;
+	virtual void mouseReleaseEvent(QMouseEvent *event) override;
 	virtual bool eventFilter(QObject *obj, QEvent *event) override;
+
+	int posToRangeValue(QMouseEvent *event);
+
+private:
+	bool dragging = false;
 };

+ 0 - 2
UI/cmake/legacy.cmake

@@ -201,8 +201,6 @@ target_sources(
           scene-tree.cpp
           scene-tree.hpp
           screenshot-obj.hpp
-          slider-absoluteset-style.cpp
-          slider-absoluteset-style.hpp
           slider-ignorewheel.cpp
           slider-ignorewheel.hpp
           source-label.cpp

+ 0 - 2
UI/cmake/ui-elements.cmake

@@ -62,8 +62,6 @@ target_sources(
           scene-tree.cpp
           scene-tree.hpp
           screenshot-obj.hpp
-          slider-absoluteset-style.cpp
-          slider-absoluteset-style.hpp
           source-label.cpp
           source-label.hpp
           source-tree.cpp

+ 0 - 20
UI/slider-absoluteset-style.cpp

@@ -1,20 +0,0 @@
-#include "slider-absoluteset-style.hpp"
-
-SliderAbsoluteSetStyle::SliderAbsoluteSetStyle(const QString &baseStyle)
-	: QProxyStyle(baseStyle)
-{
-}
-SliderAbsoluteSetStyle::SliderAbsoluteSetStyle(QStyle *baseStyle)
-	: QProxyStyle(baseStyle)
-{
-}
-
-int SliderAbsoluteSetStyle::styleHint(QStyle::StyleHint hint,
-				      const QStyleOption *option = 0,
-				      const QWidget *widget = 0,
-				      QStyleHintReturn *returnData = 0) const
-{
-	if (hint == QStyle::SH_Slider_AbsoluteSetButtons)
-		return (Qt::LeftButton | Qt::MiddleButton);
-	return QProxyStyle::styleHint(hint, option, widget, returnData);
-}

+ 0 - 12
UI/slider-absoluteset-style.hpp

@@ -1,12 +0,0 @@
-#pragma once
-
-#include <QProxyStyle>
-
-class SliderAbsoluteSetStyle : public QProxyStyle {
-public:
-	SliderAbsoluteSetStyle(const QString &baseStyle);
-	SliderAbsoluteSetStyle(QStyle *baseStyle = Q_NULLPTR);
-	int styleHint(QStyle::StyleHint hint, const QStyleOption *option,
-		      const QWidget *widget,
-		      QStyleHintReturn *returnData) const;
-};

+ 3 - 17
UI/volume-control.cpp

@@ -3,15 +3,13 @@
 #include "qt-wrappers.hpp"
 #include "obs-app.hpp"
 #include "mute-checkbox.hpp"
-#include "slider-ignorewheel.hpp"
-#include "slider-absoluteset-style.hpp"
+#include "absolute-slider.hpp"
 #include "source-label.hpp"
 #include <QFontDatabase>
 #include <QHBoxLayout>
 #include <QPushButton>
 #include <QLabel>
 #include <QPainter>
-#include <QStyleFactory>
 
 using namespace std;
 
@@ -386,18 +384,6 @@ VolControl::VolControl(OBSSource source_, bool showConfig, bool vertical)
 	obs_fader_attach_source(obs_fader, source);
 	obs_volmeter_attach_source(obs_volmeter, source);
 
-	QString styleName = slider->style()->objectName();
-	QStyle *style;
-	style = QStyleFactory::create(styleName);
-	if (!style) {
-		style = new SliderAbsoluteSetStyle();
-	} else {
-		style = new SliderAbsoluteSetStyle(style);
-	}
-
-	style->setParent(slider);
-	slider->setStyle(style);
-
 	/* Call volume changed once to init the slider position and label */
 	VolumeChanged();
 }
@@ -1520,14 +1506,14 @@ void VolumeMeterTimer::timerEvent(QTimerEvent *)
 }
 
 VolumeSlider::VolumeSlider(obs_fader_t *fader, QWidget *parent)
-	: SliderIgnoreScroll(parent)
+	: AbsoluteSlider(parent)
 {
 	fad = fader;
 }
 
 VolumeSlider::VolumeSlider(obs_fader_t *fader, Qt::Orientation orientation,
 			   QWidget *parent)
-	: SliderIgnoreScroll(orientation, parent)
+	: AbsoluteSlider(orientation, parent)
 {
 	fad = fader;
 }

+ 2 - 2
UI/volume-control.hpp

@@ -8,7 +8,7 @@
 #include <QList>
 #include <QMenu>
 #include <QAccessibleWidget>
-#include "slider-ignorewheel.hpp"
+#include "absolute-slider.hpp"
 
 class QPushButton;
 class VolumeMeterTimer;
@@ -333,7 +333,7 @@ public:
 	void refreshColors();
 };
 
-class VolumeSlider : public SliderIgnoreScroll {
+class VolumeSlider : public AbsoluteSlider {
 	Q_OBJECT
 
 public:

+ 0 - 16
UI/window-basic-main.cpp

@@ -332,9 +332,6 @@ OBSBasic::OBSBasic(QWidget *parent)
 
 	ui->setupUi(this);
 	ui->previewDisabledWidget->setVisible(false);
-	QStyle *contextBarStyle = new OBSContextBarProxyStyle();
-	contextBarStyle->setParent(ui->contextContainer);
-	ui->contextContainer->setStyle(contextBarStyle);
 	ui->broadcastButton->setVisible(false);
 
 	startingDockLayout = saveState();
@@ -11142,19 +11139,6 @@ float OBSBasic::GetDevicePixelRatio()
 
 void OBSBasic::ThemeChanged()
 {
-	/* Since volume/media sliders are using QProxyStyle, they are not
-	* updated when themes are changed, so re-initialize them. */
-	vector<OBSSource> sources;
-	for (size_t i = 0; i != volumes.size(); i++)
-		sources.emplace_back(volumes[i]->GetSource());
-
-	ClearVolumeControls();
-
-	for (const auto &source : sources)
-		ActivateAudioSource(source);
-
-	UpdateContextBar(true);
-
 	if (api)
 		api->on_event(OBS_FRONTEND_EVENT_THEME_CHANGED);
 }