Commit 163acaae authored by Artur Grunau's avatar Artur Grunau
Browse files

Add a slider to FloatPropertyWidget

Each FloatPropertyWidget now gives the user two ways of modifying its
underlying property's value: the slider can be used for quick changes,
the spin box — for precise adjustments.
parent 5fbb429e
...@@ -29,21 +29,35 @@ ...@@ -29,21 +29,35 @@
#include "floatpropertywidget.h" #include "floatpropertywidget.h"
#include <cmath>
namespace campvis { namespace campvis {
FloatPropertyWidget::FloatPropertyWidget(FloatProperty* property, QWidget* parent /*= 0*/) FloatPropertyWidget::FloatPropertyWidget(FloatProperty* property, QWidget* parent /*= 0*/)
: AbstractPropertyWidget(property, parent) : AbstractPropertyWidget(property, parent)
, _slider(0)
, _spinBox(0) , _spinBox(0)
{ {
const float& maxValue = property->getMaxValue();
const float& minValue = property->getMinValue();
const float& value = property->getValue();
const float stepValue = 0.01f;
_slider = new QSlider(Qt::Horizontal);
setSliderProperties(value, stepValue, minValue, maxValue);
_spinBox = new QDoubleSpinBox(this); _spinBox = new QDoubleSpinBox(this);
_spinBox->setMinimum(property->getMinValue()); _spinBox->setMinimum(minValue);
_spinBox->setMaximum(property->getMaxValue()); _spinBox->setMaximum(maxValue);
_spinBox->setDecimals(3); _spinBox->setDecimals(3);
_spinBox->setSingleStep(0.01); _spinBox->setSingleStep(stepValue);
_spinBox->setValue(property->getValue()); _spinBox->setValue(value);
_spinBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
addWidget(_slider);
addWidget(_spinBox); addWidget(_spinBox);
connect(_spinBox, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double))); connect(_spinBox, SIGNAL(valueChanged(double)), this, SLOT(onSpinBoxValueChanged(double)));
connect(_slider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int)));
property->s_minMaxChanged.connect(this, &FloatPropertyWidget::onPropertyMinMaxChanged); property->s_minMaxChanged.connect(this, &FloatPropertyWidget::onPropertyMinMaxChanged);
} }
...@@ -56,20 +70,50 @@ namespace campvis { ...@@ -56,20 +70,50 @@ namespace campvis {
_spinBox->blockSignals(true); _spinBox->blockSignals(true);
_spinBox->setValue(prop->getValue()); _spinBox->setValue(prop->getValue());
_spinBox->blockSignals(false); _spinBox->blockSignals(false);
setSliderValue(prop->getValue(), 0.01f, prop->getMinValue());
} }
void FloatPropertyWidget::onValueChanged(double value) { void FloatPropertyWidget::onSpinBoxValueChanged(double value) {
++_ignorePropertyUpdates; ++_ignorePropertyUpdates;
FloatProperty* prop = static_cast<FloatProperty*>(_property); FloatProperty* prop = static_cast<FloatProperty*>(_property);
prop->setValue(value); prop->setValue(value);
setSliderValue(value, 0.01f, prop->getMinValue());
--_ignorePropertyUpdates;
}
void FloatPropertyWidget::onSliderValueChanged(int value) {
FloatProperty* prop = static_cast<FloatProperty*>(_property);
++_ignorePropertyUpdates;
const float newPropValue = prop->getMinValue() + value * 0.01;
prop->setValue(newPropValue);
_spinBox->blockSignals(true);
_spinBox->setValue(newPropValue);
_spinBox->blockSignals(false);
--_ignorePropertyUpdates; --_ignorePropertyUpdates;
} }
void FloatPropertyWidget::onPropertyMinMaxChanged(const AbstractProperty* property) { void FloatPropertyWidget::onPropertyMinMaxChanged(const AbstractProperty* property) {
if (_ignorePropertyUpdates == 0) { if (_ignorePropertyUpdates == 0) {
FloatProperty* prop = static_cast<FloatProperty*>(_property); FloatProperty* prop = static_cast<FloatProperty*>(_property);
_spinBox->setMinimum(prop->getMinValue()); const float& maxValue = prop->getMaxValue();
_spinBox->setMaximum(prop->getMaxValue()); const float& minValue = prop->getMinValue();
_spinBox->setMinimum(minValue);
_spinBox->setMaximum(maxValue);
setSliderProperties(prop->getValue(), 0.01f, minValue, maxValue);
} }
} }
}
\ No newline at end of file void FloatPropertyWidget::setSliderValue(float value, float stepValue, float minValue) {
_slider->blockSignals(true);
_slider->setValue(std::ceil((value - minValue) / stepValue));
_slider->blockSignals(false);
}
void FloatPropertyWidget::setSliderProperties(float value, float stepValue, float minValue, float maxValue) {
// by default minimum and single step are 0 and 1, respectively, so we don't have to change them
_slider->setMaximum(std::ceil((maxValue - minValue) / stepValue));
setSliderValue(value, stepValue, minValue);
}
}
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "core/properties/numericproperty.h" #include "core/properties/numericproperty.h"
#include <QDoubleSpinBox> #include <QDoubleSpinBox>
#include <QSlider>
namespace campvis { namespace campvis {
/** /**
...@@ -62,13 +63,41 @@ namespace campvis { ...@@ -62,13 +63,41 @@ namespace campvis {
virtual void updateWidgetFromProperty(); virtual void updateWidgetFromProperty();
private slots: private slots:
void onValueChanged(double value); /// Slot getting called when the spin box's value changes
void onSpinBoxValueChanged(double value);
/// Slot getting called when the slider's value changes
void onSliderValueChanged(int value);
private: private:
/// Slot getting called when the property's min or max value has changed, so that the widget can be updated. /// Slot getting called when the property's min or max value has changed, so that the widget can be updated.
virtual void onPropertyMinMaxChanged(const AbstractProperty* property); virtual void onPropertyMinMaxChanged(const AbstractProperty* property);
QDoubleSpinBox* _spinBox; /**
* Calculate and set the slider's value.
*
* The slider's value will be based on the provided current, step, and minimum value of the property.
*
* \param value The property's current value
* \param stepValue The property's step value
* \param minValue The property's minimum value
*/
void setSliderValue(float value, float stepValue, float minValue);
/**
* Calculate and set the slider's properties.
*
* The slider's properties (current and maximum value) will be based on the provided current, step, minimum,
* and maximum value of the property.
*
* \param value The property's current value
* \param stepValue The property's step value
* \param minValue The property's minimum value
* \param maxValue The property's maximum value
*/
void setSliderProperties(float value, float stepValue, float minValue, float maxValue);
QSlider* _slider; ///< Slider allowing the user to quickly change the property's value
QDoubleSpinBox* _spinBox; ///< Spin box displaying the property's current value
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment