floatpropertywidget.cpp 5.37 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// 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.
// 
// ================================================================================================

schultezub's avatar
schultezub committed
30
31
#include "floatpropertywidget.h"

32
33
#include <cmath>

schultezub's avatar
schultezub committed
34
namespace campvis {
schultezub's avatar
schultezub committed
35
36
    FloatPropertyWidget::FloatPropertyWidget(FloatProperty* property, QWidget* parent /*= 0*/)
        : AbstractPropertyWidget(property, parent)
37
        , _slider(0)
schultezub's avatar
schultezub committed
38
39
        , _spinBox(0)
    {
40
41
42
43
44
45
46
47
        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);

schultezub's avatar
schultezub committed
48
        _spinBox = new QDoubleSpinBox(this);
49
50
        _spinBox->setMinimum(minValue);
        _spinBox->setMaximum(maxValue);
schultezub's avatar
schultezub committed
51
        _spinBox->setDecimals(3);
52
53
54
55
56
        _spinBox->setSingleStep(stepValue);
        _spinBox->setValue(value);
        _spinBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);

        addWidget(_slider);
schultezub's avatar
schultezub committed
57
58
        addWidget(_spinBox);

59
60
        connect(_spinBox, SIGNAL(valueChanged(double)), this, SLOT(onSpinBoxValueChanged(double)));
        connect(_slider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int)));
schultezub's avatar
schultezub committed
61
62
63
64
65
66
67
68
69
70
71
72
        property->s_minMaxChanged.connect(this, &FloatPropertyWidget::onPropertyMinMaxChanged);
    }

    FloatPropertyWidget::~FloatPropertyWidget() {
        static_cast<FloatProperty*>(_property)->s_minMaxChanged.disconnect(this);
    }

    void FloatPropertyWidget::updateWidgetFromProperty() {
        FloatProperty* prop = static_cast<FloatProperty*>(_property);
        _spinBox->blockSignals(true);
        _spinBox->setValue(prop->getValue());
        _spinBox->blockSignals(false);
73
        setSliderValue(prop->getValue(), 0.01f, prop->getMinValue());
schultezub's avatar
schultezub committed
74
75
    }

76
    void FloatPropertyWidget::onSpinBoxValueChanged(double value) {
schultezub's avatar
schultezub committed
77
        ++_ignorePropertyUpdates;
schultezub's avatar
schultezub committed
78
79
        FloatProperty* prop = static_cast<FloatProperty*>(_property);
        prop->setValue(value);
80
81
82
83
84
85
86
87
88
89
90
91
92
        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);
schultezub's avatar
schultezub committed
93
        --_ignorePropertyUpdates;
schultezub's avatar
schultezub committed
94
95
96
    }

    void FloatPropertyWidget::onPropertyMinMaxChanged(const AbstractProperty* property) {
schultezub's avatar
schultezub committed
97
        if (_ignorePropertyUpdates == 0) {
schultezub's avatar
schultezub committed
98
            FloatProperty* prop = static_cast<FloatProperty*>(_property);
99
100
101
102
103
104
            const float& maxValue = prop->getMaxValue();
            const float& minValue = prop->getMinValue();

            _spinBox->setMinimum(minValue);
            _spinBox->setMaximum(maxValue);
            setSliderProperties(prop->getValue(), 0.01f, minValue, maxValue);
schultezub's avatar
schultezub committed
105
106
        }
    }
107
108
109
110
111
112
113
114
115
116
117
118
119

    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);
    }
}