floatpropertywidget.h 10.7 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
32
33
34
35
36
#ifndef FLOATPROPERTYWIDGET_H__
#define FLOATPROPERTYWIDGET_H__

#include "application/gui/properties/abstractpropertywidget.h"
#include "core/properties/numericproperty.h"

#include <QDoubleSpinBox>
37
#include <QSlider>
schultezub's avatar
schultezub committed
38

schultezub's avatar
schultezub committed
39
namespace campvis {
schultezub's avatar
schultezub committed
40
    /**
41
     * Widget for a FloatProperty
schultezub's avatar
schultezub committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
     */
    class FloatPropertyWidget : public AbstractPropertyWidget {
        Q_OBJECT;

    public:
        /**
         * Creates a new FloatPropertyWidget for the property \a property.
         * \param   property    The property the widget shall handle
         * \param   parent      Parent Qt widget
         */
        FloatPropertyWidget(FloatProperty* property, QWidget* parent = 0);

        /**
         * Destructor
         */
        virtual ~FloatPropertyWidget();

    protected:
        /**
         * Gets called when the property has changed, so that widget can update its state.
         */
        virtual void updateWidgetFromProperty();

    private slots:
66
67
68
69
        /// 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);
schultezub's avatar
schultezub committed
70
71
72
73
74

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

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        /**
         * 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
schultezub's avatar
schultezub committed
101
102

    };
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

// ================================================================================================

    namespace {
        template<size_t SIZE>
        struct VecPropertyWidgetTraits {};

        template<>
        struct VecPropertyWidgetTraits<2> {
            typedef Vec2Property PropertyType;
            typedef tgt::vec2 BaseType;
        };

        template<>
        struct VecPropertyWidgetTraits<3> {
            typedef Vec3Property PropertyType;
            typedef tgt::vec3 BaseType;
        };

        template<>
        struct VecPropertyWidgetTraits<4> {
            typedef Vec4Property PropertyType;
            typedef tgt::vec4 BaseType;
        };
    }

// ================================================================================================

    /**
     * Generic base class for Vec property widgets.
     * Unfortunately Q_OBJECT and templates do not fit together, so we an additional level of 
     * indirection helps as usual...
     */
    template<size_t SIZE>
    class VecPropertyWidget : public AbstractPropertyWidget {
    public:
        enum { size = SIZE };
        typedef typename VecPropertyWidgetTraits<SIZE>::PropertyType PropertyType;

        /**
         * Creates a new VecPropertyWidget for the property \a property.
         * \param   property    The property the widget shall handle
         * \param   parent      Parent Qt widget
         */
        VecPropertyWidget(PropertyType* property, QWidget* parent = 0);

        /**
         * Destructor
         */
        virtual ~VecPropertyWidget();

    protected:
        /**
         * Gets called when the property has changed, so that widget can update its state.
         */
        virtual void updateWidgetFromProperty();

        void onValueChangedImpl();

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

        QDoubleSpinBox* _spinBox[size];
    };

// ================================================================================================

    template<size_t SIZE>
schultezub's avatar
schultezub committed
171
    campvis::VecPropertyWidget<SIZE>::VecPropertyWidget(PropertyType* property, QWidget* parent /*= 0*/)
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        : AbstractPropertyWidget(property, parent)
    {
        for (size_t i = 0; i < size; ++i) {
            _spinBox[i] = new QDoubleSpinBox(this);
            _spinBox[i]->setMinimum(property->getMinValue()[i]);
            _spinBox[i]->setMaximum(property->getMaxValue()[i]);
            _spinBox[i]->setDecimals(3);
            _spinBox[i]->setSingleStep(0.01);
            _spinBox[i]->setValue(property->getValue()[i]);
            addWidget(_spinBox[i]);
        }

        property->s_minMaxChanged.connect(this, &VecPropertyWidget::onPropertyMinMaxChanged);

    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
189
    campvis::VecPropertyWidget<SIZE>::~VecPropertyWidget() {
190
191
192
193
        static_cast<PropertyType*>(_property)->s_minMaxChanged.disconnect(this);
    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
194
    void campvis::VecPropertyWidget<SIZE>::updateWidgetFromProperty() {
195
196
197
198
199
200
201
202
203
        PropertyType* prop = static_cast<PropertyType*>(_property);
        for (size_t i = 0; i < size; ++i) {
            _spinBox[i]->blockSignals(true);
            _spinBox[i]->setValue(prop->getValue()[i]);
            _spinBox[i]->blockSignals(false);
        }
    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
204
    void campvis::VecPropertyWidget<SIZE>::onValueChangedImpl() {
schultezub's avatar
schultezub committed
205
        ++_ignorePropertyUpdates;
206
207
208
209
210
        PropertyType* prop = static_cast<PropertyType*>(_property);
        typename VecPropertyWidgetTraits<SIZE>::BaseType newValue;
        for (size_t i = 0; i < size; ++i)
            newValue[i] = _spinBox[i]->value();
        prop->setValue(newValue);
schultezub's avatar
schultezub committed
211
        --_ignorePropertyUpdates;
212
213
214
    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
215
    void campvis::VecPropertyWidget<SIZE>::onPropertyMinMaxChanged(const AbstractProperty* property) {
schultezub's avatar
schultezub committed
216
        if (_ignorePropertyUpdates == 0) {
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
            PropertyType* prop = static_cast<PropertyType*>(_property);
            for (size_t i = 0; i < size; ++i) {
                _spinBox[i]->setMinimum(prop->getMinValue()[i]);
                _spinBox[i]->setMaximum(prop->getMaxValue()[i]);
            }
        }
    }

// ================================================================================================

    class Vec2PropertyWidget : public VecPropertyWidget<2> {
        Q_OBJECT
    public:
        Vec2PropertyWidget(PropertyType* property, QWidget* parent = 0)
            : VecPropertyWidget<2>(property, parent)
        {
            for (size_t i = 0; i < size; ++i) {
                connect(_spinBox[i], SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
            }
        }

    private slots:
        void onValueChanged(double value) { onValueChangedImpl(); };
    }; 

// ================================================================================================
    
    class Vec3PropertyWidget : public VecPropertyWidget<3> {
        Q_OBJECT
    public:
        Vec3PropertyWidget(PropertyType* property, QWidget* parent = 0)
            : VecPropertyWidget<3>(property, parent)
        {
            for (size_t i = 0; i < size; ++i) {
                connect(_spinBox[i], SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
            }
        }

        private slots:
            void onValueChanged(double value) { onValueChangedImpl(); };
    }; 

// ================================================================================================

    class Vec4PropertyWidget : public VecPropertyWidget<4> {
        Q_OBJECT
    public:
        Vec4PropertyWidget(PropertyType* property, QWidget* parent = 0)
            : VecPropertyWidget<4>(property, parent)
        {
            for (size_t i = 0; i < size; ++i) {
                connect(_spinBox[i], SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
            }
        }

        private slots:
            void onValueChanged(double value) { onValueChangedImpl(); };
    }; 

schultezub's avatar
schultezub committed
276
277
278
}

#endif // FLOATPROPERTYWIDGET_H__