Add variable support to SliderSpinbox

This commit is contained in:
WarmUpTill
2023-03-12 20:56:14 +01:00
committed by WarmUpTill
parent bf5d8ca19e
commit 13b0e00b27
2 changed files with 36 additions and 12 deletions

View File

@@ -4,7 +4,7 @@
SliderSpinBox::SliderSpinBox(double min, double max, const QString &label, SliderSpinBox::SliderSpinBox(double min, double max, const QString &label,
const QString &description, QWidget *parent) const QString &description, QWidget *parent)
: QWidget(parent), : QWidget(parent),
_spinBox(new QDoubleSpinBox()), _spinBox(new VariableDoubleSpinBox()),
_slider(new QSlider()) _slider(new QSlider())
{ {
_slider->setOrientation(Qt::Horizontal); _slider->setOrientation(Qt::Horizontal);
@@ -15,8 +15,12 @@ SliderSpinBox::SliderSpinBox(double min, double max, const QString &label,
connect(_slider, SIGNAL(valueChanged(int)), this, connect(_slider, SIGNAL(valueChanged(int)), this,
SLOT(SliderValueChanged(int))); SLOT(SliderValueChanged(int)));
connect(_spinBox, SIGNAL(valueChanged(double)), this, QWidget::connect(
SLOT(SpinBoxValueChanged(double))); _spinBox,
SIGNAL(NumberVariableChanged(const NumberVariable<double> &)),
this,
SLOT(SpinBoxValueChanged(const NumberVariable<double> &)));
QVBoxLayout *mainLayout = new QVBoxLayout(); QVBoxLayout *mainLayout = new QVBoxLayout();
QHBoxLayout *sliderLayout = new QHBoxLayout(); QHBoxLayout *sliderLayout = new QHBoxLayout();
if (!label.isEmpty()) { if (!label.isEmpty()) {
@@ -33,22 +37,37 @@ SliderSpinBox::SliderSpinBox(double min, double max, const QString &label,
} }
void SliderSpinBox::SetDoubleValue(double value) void SliderSpinBox::SetDoubleValue(double value)
{
NumberVariable<double> temp = value;
SetDoubleValue(temp);
}
void SliderSpinBox::SetDoubleValue(const NumberVariable<double> &value)
{ {
const QSignalBlocker b1(_slider); const QSignalBlocker b1(_slider);
const QSignalBlocker b2(_spinBox); const QSignalBlocker b2(_spinBox);
_slider->setValue(value * _scale); _slider->setValue(value * _scale);
_spinBox->setValue(value); _spinBox->SetValue(value);
SetVisibility(value);
} }
void SliderSpinBox::SpinBoxValueChanged(double value) void SliderSpinBox::SpinBoxValueChanged(const NumberVariable<double> &value)
{ {
int sliderPos = value * _scale; if (value.IsFixedType()) {
_slider->setValue(sliderPos); int sliderPos = value * _scale;
_slider->setValue(sliderPos);
}
SetVisibility(value);
emit DoubleValueChanged(value); emit DoubleValueChanged(value);
} }
void SliderSpinBox::SliderValueChanged(int value) void SliderSpinBox::SliderValueChanged(int value)
{ {
double doubleValue = value / _scale; NumberVariable<double> doubleValue = value / _scale;
_spinBox->setValue(doubleValue); _spinBox->SetValue(doubleValue);
}
void SliderSpinBox::SetVisibility(const NumberVariable<double> &value)
{
_slider->setVisible(value.IsFixedType());
} }

View File

@@ -1,4 +1,6 @@
#pragma once #pragma once
#include "variable-spinbox.hpp"
#include <QWidget> #include <QWidget>
#include <QSlider> #include <QSlider>
#include <QLabel> #include <QLabel>
@@ -12,14 +14,17 @@ public:
const QString &label = "threshold", const QString &label = "threshold",
const QString &description = "", QWidget *parent = 0); const QString &description = "", QWidget *parent = 0);
void SetDoubleValue(double); void SetDoubleValue(double);
void SetDoubleValue(const NumberVariable<double> &);
public slots: public slots:
void SliderValueChanged(int value); void SliderValueChanged(int value);
void SpinBoxValueChanged(double value); void SpinBoxValueChanged(const NumberVariable<double> &value);
signals: signals:
void DoubleValueChanged(double value); void DoubleValueChanged(const NumberVariable<double> &value);
private: private:
QDoubleSpinBox *_spinBox; void SetVisibility(const NumberVariable<double> &);
VariableDoubleSpinBox *_spinBox;
QSlider *_slider; QSlider *_slider;
double _scale = 100.0; double _scale = 100.0;
}; };