Commit 77653ee0 authored by Artur Grunau's avatar Artur Grunau
Browse files

FloatPropertyWidget: use DoubleAdjusterWidget

Previously, FloatPropertyWidget used standard spin boxes and sliders to
display and let users modify the value of its associated property.
Consequently, it had to synchronise the states of those widgets on its
own.

Now that we have a separate widget that implements the two ways of
changing double values that FloatPropertyWidget provides, it makes sense
to simplify FloatPropertyWidget's code by making it use
DoubleAdjusterWidget internally.
parent 778445ea
......@@ -29,35 +29,24 @@
#include "floatpropertywidget.h"
#include <cmath>
namespace campvis {
FloatPropertyWidget::FloatPropertyWidget(FloatProperty* property, QWidget* parent /*= 0*/)
: AbstractPropertyWidget(property, parent)
, _slider(0)
, _spinBox(0)
, _adjuster(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->setMinimum(minValue);
_spinBox->setMaximum(maxValue);
_spinBox->setDecimals(3);
_spinBox->setSingleStep(stepValue);
_spinBox->setValue(value);
_spinBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
_adjuster = new DoubleAdjusterWidget();
_adjuster->setMinimum(property->getMinValue());
_adjuster->setMaximum(property->getMaxValue());
_adjuster->setDecimals(3);
_adjuster->setSingleStep(stepValue);
_adjuster->setValue(property->getValue());
addWidget(_slider);
addWidget(_spinBox);
addWidget(_adjuster);
connect(_spinBox, SIGNAL(valueChanged(double)), this, SLOT(onSpinBoxValueChanged(double)));
connect(_slider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int)));
connect(_adjuster, SIGNAL(valueChanged(double)), this, SLOT(onAdjusterValueChanged(double)));
connect(this, SIGNAL(propertyValueChanged(double)), _adjuster, SLOT(setValue(double)));
property->s_minMaxChanged.connect(this, &FloatPropertyWidget::onPropertyMinMaxChanged);
}
......@@ -67,53 +56,23 @@ namespace campvis {
void FloatPropertyWidget::updateWidgetFromProperty() {
FloatProperty* prop = static_cast<FloatProperty*>(_property);
_spinBox->blockSignals(true);
_spinBox->setValue(prop->getValue());
_spinBox->blockSignals(false);
setSliderValue(prop->getValue(), 0.01f, prop->getMinValue());
_adjuster->blockSignals(true);
emit propertyValueChanged(prop->getValue());
_adjuster->blockSignals(false);
}
void FloatPropertyWidget::onSpinBoxValueChanged(double value) {
void FloatPropertyWidget::onAdjusterValueChanged(double value) {
++_ignorePropertyUpdates;
FloatProperty* prop = static_cast<FloatProperty*>(_property);
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;
}
void FloatPropertyWidget::onPropertyMinMaxChanged(const AbstractProperty* property) {
if (_ignorePropertyUpdates == 0) {
FloatProperty* prop = static_cast<FloatProperty*>(_property);
const float& maxValue = prop->getMaxValue();
const float& minValue = prop->getMinValue();
_spinBox->setMinimum(minValue);
_spinBox->setMaximum(maxValue);
setSliderProperties(prop->getValue(), 0.01f, minValue, maxValue);
const FloatProperty* prop = static_cast<const FloatProperty*>(property);
_adjuster->setMinimum(prop->getMinValue());
_adjuster->setMaximum(prop->getMaxValue());
}
}
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);
}
}
......@@ -31,17 +31,17 @@
#define FLOATPROPERTYWIDGET_H__
#include "application/gui/properties/abstractpropertywidget.h"
#include "application/gui/doubleadjusterwidget.h"
#include "core/properties/numericproperty.h"
#include <QDoubleSpinBox>
#include <QSlider>
namespace campvis {
/**
* Widget for a FloatProperty
*/
class FloatPropertyWidget : public AbstractPropertyWidget {
Q_OBJECT;
Q_OBJECT
public:
/**
......@@ -62,42 +62,19 @@ namespace campvis {
*/
virtual void updateWidgetFromProperty();
signals:
/// Signal emitted when the property's value changes
void propertyValueChanged(double value);
private slots:
/// 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);
/// Slot getting called when the adjuster's value changes
void onAdjusterValueChanged(double value);
private:
/// 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);
/**
* 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
DoubleAdjusterWidget* _adjuster; ///< Widget allowing the user to change the property's 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