Skip to content

Commit

Permalink
UI: Remove unnecessary ProxyStyle usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Warchamp7 committed Apr 28, 2024
1 parent 49b0a26 commit 7d549af
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 80 deletions.
81 changes: 67 additions & 14 deletions UI/absolute-slider.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
#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();
else if (val < minimum())
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)
Expand All @@ -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 changes: 8 additions & 0 deletions UI/absolute-slider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
2 changes: 0 additions & 2 deletions UI/cmake/legacy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions UI/cmake/ui-elements.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions UI/slider-absoluteset-style.cpp

This file was deleted.

12 changes: 0 additions & 12 deletions UI/slider-absoluteset-style.hpp

This file was deleted.

14 changes: 0 additions & 14 deletions UI/volume-control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
#include "obs-app.hpp"
#include "mute-checkbox.hpp"
#include "slider-ignorewheel.hpp"
#include "slider-absoluteset-style.hpp"
#include <QFontDatabase>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QPainter>
#include <QStyleFactory>

using namespace std;

Expand Down Expand Up @@ -399,18 +397,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();
}
Expand Down
16 changes: 0 additions & 16 deletions UI/window-basic-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -11157,19 +11154,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);
}

0 comments on commit 7d549af

Please sign in to comment.