floatpropertywidget.h 10.4 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
#ifndef FLOATPROPERTYWIDGET_H__
#define FLOATPROPERTYWIDGET_H__

33
#include "application/gui/adjusterwidgets/doubleadjusterwidget.h"
schultezub's avatar
schultezub committed
34
#include "application/gui/properties/abstractpropertywidget.h"
35
#include "core/properties/floatingpointproperty.h"
schultezub's avatar
schultezub committed
36

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

    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:
64
65
        /// Slot getting called when the adjuster's value changes
        void onAdjusterValueChanged(double value);
schultezub's avatar
schultezub committed
66
67
68
69
70

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

71
72
73
        /// Slot getting called when the property's step value has changed, so that the widget can be updated.
        virtual void onPropertyStepChanged(const AbstractProperty* property);

74
        DoubleAdjusterWidget* _adjuster;        ///< Widget allowing the user to change the property's value
schultezub's avatar
schultezub committed
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
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

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

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

139
140
141
        /// Slot getting called when the property's step value has changed, so that the widget can be updated.
        virtual void onPropertyStepChanged(const AbstractProperty* property);

142
        DoubleAdjusterWidget* _adjusters[size];
143
144
145
146
147
    };

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

    template<size_t SIZE>
schultezub's avatar
schultezub committed
148
    campvis::VecPropertyWidget<SIZE>::VecPropertyWidget(PropertyType* property, QWidget* parent /*= 0*/)
149
        : AbstractPropertyWidget(property, true, parent)
150
151
    {
        for (size_t i = 0; i < size; ++i) {
152
153
154
155
            _adjusters[i] = new DoubleAdjusterWidget();
            _adjusters[i]->setMinimum(property->getMinValue()[i]);
            _adjusters[i]->setMaximum(property->getMaxValue()[i]);
            _adjusters[i]->setDecimals(3);
156
            _adjusters[i]->setSingleStep(property->getStepValue()[i]);
157
158
            _adjusters[i]->setValue(property->getValue()[i]);
            addWidget(_adjusters[i]);
159
160
161
        }

        property->s_minMaxChanged.connect(this, &VecPropertyWidget::onPropertyMinMaxChanged);
162
        property->s_stepChanged.connect(this, &VecPropertyWidget::onPropertyStepChanged);
163
164
165
    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
166
    campvis::VecPropertyWidget<SIZE>::~VecPropertyWidget() {
167
        static_cast<PropertyType*>(_property)->s_minMaxChanged.disconnect(this);
168
        static_cast<PropertyType*>(_property)->s_stepChanged.disconnect(this);
169
170
171
    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
172
    void campvis::VecPropertyWidget<SIZE>::updateWidgetFromProperty() {
173
174
        PropertyType* prop = static_cast<PropertyType*>(_property);
        for (size_t i = 0; i < size; ++i) {
175
176
177
            _adjusters[i]->blockSignals(true);
            _adjusters[i]->setValue(prop->getValue()[i]);
            _adjusters[i]->blockSignals(false);
178
179
180
181
        }
    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
182
    void campvis::VecPropertyWidget<SIZE>::onValueChangedImpl() {
schultezub's avatar
schultezub committed
183
        ++_ignorePropertyUpdates;
184
185
186
        PropertyType* prop = static_cast<PropertyType*>(_property);
        typename VecPropertyWidgetTraits<SIZE>::BaseType newValue;
        for (size_t i = 0; i < size; ++i)
187
            newValue[i] = _adjusters[i]->value();
188
        prop->setValue(newValue);
schultezub's avatar
schultezub committed
189
        --_ignorePropertyUpdates;
190
191
192
    }

    template<size_t SIZE>
schultezub's avatar
schultezub committed
193
    void campvis::VecPropertyWidget<SIZE>::onPropertyMinMaxChanged(const AbstractProperty* property) {
schultezub's avatar
schultezub committed
194
        if (_ignorePropertyUpdates == 0) {
195
196
            PropertyType* prop = static_cast<PropertyType*>(_property);
            for (size_t i = 0; i < size; ++i) {
197
198
                _adjusters[i]->setMinimum(prop->getMinValue()[i]);
                _adjusters[i]->setMaximum(prop->getMaxValue()[i]);
199
200
201
202
            }
        }
    }

203
204
205
206
207
208
209
210
211
212
    template<size_t SIZE>
    void campvis::VecPropertyWidget<SIZE>::onPropertyStepChanged(const AbstractProperty* property) {
        if (_ignorePropertyUpdates == 0) {
            PropertyType* prop = static_cast<PropertyType*>(_property);
            for (size_t i = 0; i < size; ++i) {
                _adjusters[i]->setSingleStep(prop->getStepValue()[i]);
            }
        }
    }

213
214
215
216
217
218
219
220
221
// ================================================================================================

    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) {
222
                connect(_adjusters[i], SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
            }
        }

    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) {
239
                connect(_adjusters[i], SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
            }
        }

        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) {
256
                connect(_adjusters[i], SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
257
258
259
260
261
262
263
            }
        }

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

schultezub's avatar
schultezub committed
264
265
266
}

#endif // FLOATPROPERTYWIDGET_H__