Commit 778445ea authored by Artur Grunau's avatar Artur Grunau
Browse files

DoubleAdjusterWidget: widget modifying doubles

DoubleAdjusterWidget displays and provides two ways of modifying
double-precision floating-point numbers: a slider that can be used to
quickly change numeric values, and a spin box, which is better suited
for precise adjustments.

DoubleAdjusterWidget is designed to be used by FloatPropertyWidget and
widgets for vector properties storing floats.
parent d585148e
......@@ -37,6 +37,7 @@ SET(CampvisApplicationToBeMocced
gui/pipelinemdisubwindow.h
gui/visualizationpipelinewidget.h
gui/visualizationpipelinewrapper.h
gui/doubleadjusterwidget.h
gui/properties/abstractpropertywidget.h
gui/properties/abstracttransferfunctioneditor.h
gui/properties/boolpropertywidget.h
......
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universitt Mnchen
// Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
//
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ================================================================================================
#include "doubleadjusterwidget.h"
#include <QHBoxLayout>
#include <cmath>
namespace campvis {
DoubleAdjusterWidget::DoubleAdjusterWidget(QWidget* parent /*= 0*/)
: QWidget(parent)
, _slider(0)
, _spinBox(0)
{
_spinBox = new QDoubleSpinBox();
_spinBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
_slider = new QSlider(Qt::Horizontal);
setSliderProperties(_spinBox->value(), _spinBox->singleStep(), _spinBox->minimum(), _spinBox->maximum());
QHBoxLayout* layout = new QHBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(_slider);
layout->addWidget(_spinBox);
setLayout(layout);
connect(_spinBox, SIGNAL(valueChanged(double)), this, SLOT(onSpinBoxValueChanged(double)));
connect(_slider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int)));
}
double DoubleAdjusterWidget::value() const
{
return _spinBox->value();
}
void DoubleAdjusterWidget::setValue(double value)
{
_spinBox->setValue(value);
setSliderValue(value, _spinBox->singleStep(), _spinBox->minimum());
}
void DoubleAdjusterWidget::setMinimum(double minimum)
{
_spinBox->setMinimum(minimum);
setSliderProperties(_spinBox->value(), _spinBox->singleStep(), minimum, _spinBox->maximum());
}
void DoubleAdjusterWidget::setMaximum(double maximum)
{
_spinBox->setMaximum(maximum);
setSliderProperties(_spinBox->value(), _spinBox->singleStep(), _spinBox->minimum(), maximum);
}
void DoubleAdjusterWidget::setSingleStep(double value)
{
_spinBox->setSingleStep(value);
setSliderProperties(_spinBox->value(), value, _spinBox->minimum(), _spinBox->maximum());
}
void DoubleAdjusterWidget::setDecimals(int prec)
{
_spinBox->setDecimals(prec);
setSliderProperties(_spinBox->value(), _spinBox->singleStep(), _spinBox->minimum(), _spinBox->maximum());
}
void DoubleAdjusterWidget::onSpinBoxValueChanged(double value) {
emit valueChanged(value);
setSliderValue(value, _spinBox->singleStep(), _spinBox->minimum());
}
void DoubleAdjusterWidget::onSliderValueChanged(int value) {
const double newValue = _spinBox->minimum() + value * _spinBox->singleStep();
_spinBox->blockSignals(true);
_spinBox->setValue(newValue);
_spinBox->blockSignals(false);
emit valueChanged(newValue);
}
void DoubleAdjusterWidget::setSliderValue(double value, double stepValue, double minValue) {
_slider->blockSignals(true);
_slider->setValue(std::ceil((value - minValue) / stepValue));
_slider->blockSignals(false);
}
void DoubleAdjusterWidget::setSliderProperties(double value, double stepValue, double minValue, double 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);
}
}
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universität München
// Boltzmannstr. 3, 85748 Garching b. München, Germany
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
//
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ================================================================================================
#ifndef DOUBLEADJUSTERWIDGET_H__
#define DOUBLEADJUSTERWIDGET_H__
#include <QDoubleSpinBox>
#include <QSlider>
namespace campvis {
/**
* Widget providing two ways of adjusting double-precision floating-point values.
*
* DoubleAdjusterWidget consists of a slider, which can be used to quickly change numeric values, and a spin box,
* which is better suited for precise adjustments.
*/
class DoubleAdjusterWidget : public QWidget {
Q_OBJECT
public:
/**
* Creates a new DoubleAdjusterWidget.
* \param parent Parent Qt widget
*/
DoubleAdjusterWidget(QWidget* parent = 0);
/**
* Return the adjuster's current value.
*/
double value() const;
/**
* Change the adjuster's minimum value.
* \param minimum New minimum value for the adjuster
*/
void setMinimum(double minimum);
/**
* Change the adjuster's maximum value.
* \param maximum New maximum value for the adjuster
*/
void setMaximum(double maximum);
/**
* Change the adjuster's single step value.
* \param value New single step value for the adjuster
*/
void setSingleStep(double value);
/**
* Set how many decimals the adjuster will use for displaying and interpreting doubles.
* \param prec Number of decimals the adjuster will use
*/
void setDecimals(int prec);
public slots:
/**
* Change the adjuster's current value.
* \param value New value for the adjuster
*/
void setValue(double value);
signals:
/**
* This signal is emitted whenever the adjuster's value is changed.
* \param value New value of the adjuster
*/
void valueChanged(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);
private:
/**
* 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(double value, double stepValue, double 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(double value, double stepValue, double minValue, double maxValue);
QSlider* _slider; ///< Slider allowing the user to quickly change the value
QDoubleSpinBox* _spinBox; ///< Spin box displaying the current value
};
}
#endif // DOUBLEADJUSTERWIDGET_H__
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