From d6ef69a3d44da51fd4ecc5c9d9547c83f43f723d Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Wed, 27 Apr 2022 18:21:39 +0200 Subject: [PATCH] Add spinbox control to threshold slider --- .../opencv/threshold-slider.cpp | 48 ++++++++++--------- .../opencv/threshold-slider.hpp | 8 ++-- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/external-macro-modules/opencv/threshold-slider.cpp b/src/external-macro-modules/opencv/threshold-slider.cpp index 713abf77..f5f777b2 100644 --- a/src/external-macro-modules/opencv/threshold-slider.cpp +++ b/src/external-macro-modules/opencv/threshold-slider.cpp @@ -3,22 +3,26 @@ ThresholdSlider::ThresholdSlider(double min, double max, const QString &label, const QString &description, QWidget *parent) - : QWidget(parent) + : QWidget(parent), + _spinBox(new QDoubleSpinBox()), + _slider(new QSlider()) { - _slider = new QSlider(); _slider->setOrientation(Qt::Horizontal); _slider->setRange(min * _scale, max * _scale); - _value = new QLabel(); - QString labelText = label + QString("0."); - for (int i = 0; i < _precision; i++) { - labelText.append(QString("0")); - } - _value->setText(labelText); + _spinBox->setMinimum(min); + _spinBox->setMaximum(max); + _spinBox->setDecimals(5); + connect(_slider, SIGNAL(valueChanged(int)), this, - SLOT(NotifyValueChanged(int))); + SLOT(SliderValueChanged(int))); + connect(_spinBox, SIGNAL(valueChanged(double)), this, + SLOT(SpinBoxValueChanged(double))); QVBoxLayout *mainLayout = new QVBoxLayout(); QHBoxLayout *sliderLayout = new QHBoxLayout(); - sliderLayout->addWidget(_value); + if (!label.isEmpty()) { + sliderLayout->addWidget(new QLabel(label)); + } + sliderLayout->addWidget(_spinBox); sliderLayout->addWidget(_slider); mainLayout->addLayout(sliderLayout); if (!description.isEmpty()) { @@ -30,21 +34,21 @@ ThresholdSlider::ThresholdSlider(double min, double max, const QString &label, void ThresholdSlider::SetDoubleValue(double value) { + const QSignalBlocker b1(_slider); + const QSignalBlocker b2(_spinBox); _slider->setValue(value * _scale); - SetDoubleValueText(value); + _spinBox->setValue(value); } -void ThresholdSlider::NotifyValueChanged(int value) +void ThresholdSlider::SpinBoxValueChanged(double value) +{ + int sliderPos = value * _scale; + _slider->setValue(sliderPos); + emit DoubleValueChanged(value); +} + +void ThresholdSlider::SliderValueChanged(int value) { double doubleValue = value / _scale; - SetDoubleValueText(doubleValue); - emit DoubleValueChanged(doubleValue); -} - -void ThresholdSlider::SetDoubleValueText(double value) -{ - QString labelText = _value->text(); - labelText.chop(_precision + 2); // 2 for the part left of the "." - labelText.append(QString::number(value, 'f', _precision)); - _value->setText(labelText); + _spinBox->setValue(doubleValue); } diff --git a/src/external-macro-modules/opencv/threshold-slider.hpp b/src/external-macro-modules/opencv/threshold-slider.hpp index 08fa7b42..3ebb6e77 100644 --- a/src/external-macro-modules/opencv/threshold-slider.hpp +++ b/src/external-macro-modules/opencv/threshold-slider.hpp @@ -2,6 +2,7 @@ #include #include #include +#include class ThresholdSlider : public QWidget { Q_OBJECT @@ -12,14 +13,13 @@ public: const QString &description = "", QWidget *parent = 0); void SetDoubleValue(double); public slots: - void NotifyValueChanged(int value); + void SliderValueChanged(int value); + void SpinBoxValueChanged(double value); signals: void DoubleValueChanged(double value); private: - void SetDoubleValueText(double); - QLabel *_value; + QDoubleSpinBox *_spinBox; QSlider *_slider; double _scale = 100.0; - int _precision = 2; };