2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

abstractadjusterwidget.h 7.4 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 
// ================================================================================================

#ifndef ABSTRACTADJUSTERWIDGET_H__
#define ABSTRACTADJUSTERWIDGET_H__

#include <QDoubleSpinBox>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>

namespace campvis {

    namespace {
        template<typename T>
        struct AdjusterWidgetTraits {};

        template<>
        struct AdjusterWidgetTraits<double> {
            typedef QDoubleSpinBox SpinBoxType;
        };

        template<>
        struct AdjusterWidgetTraits<int> {
            typedef QSpinBox SpinBoxType;
        };
    }

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

    /**
     * Base class for widgets providing two ways of adjusting numeric values.
     *
     * AbstractAdjusterWidget consists of a slider, which can be used to quickly change numeric values, and a spin box,
     * which is better suited for precise adjustments.
     */
    template<typename T>
    class AbstractAdjusterWidget : public QWidget {

    public:
        /**
         * Creates a new AbstractAdjusterWidget.
         * \param   parent      parent Qt widget
         */
        AbstractAdjusterWidget(QWidget* parent = 0);

        /**
         * Return the adjuster's current value.
         */
        T value() const;

        /**
         * Return the adjuster's minimum value.
         */
        T minimum() const;

        /**
         * Return the adjuster's maximum value.
         */
        T maximum() const;

        /**
         * Return the adjuster's single step value.
         */
        T singleStep() const;

    protected:
        /**
         * Change the adjuster's current value.
         * \param   value      new value for the adjuster
         */
        void setValueImpl(T value);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        
        /**
         * Change the adjuster's minimum value.
         * \param   minimum      new minimum value for the adjuster
         */
        void setMinimumImpl(T minimum);
        
        /**
         * Change the adjuster's maximum value.
         * \param   maximum      new maximum value for the adjuster
         */
        void setMaximumImpl(T maximum);

        /**
         * Change the adjuster's single step value.
         * \param   value      new single step value for the adjuster
         */
        void setSingleStepImpl(T value);
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

        /// Slot getting called when the spin box's value changes
        void onSpinBoxValueChangedImpl(T value);

        /// Slot getting called when the slider's value changes
        void onSliderValueChangedImpl(T value);

        /**
         * Set the slider's value.
         *
         * The slider's value will be set to \p value.
         *
         * \param   value       the slider's new value
         */
        virtual void setSliderValue(T value);

        /**
         * Set the slider's properties.
         *
         * The slider's step, minimum, and maximum value will be updated with the provided arguments.
         *
         * \param   stepValue   the slider's new step value
         * \param   minValue    the slider's new minimum value
         * \param   maxValue    the slider's new maximum value
         */
        virtual void setSliderProperties(T stepValue, T minValue, T maxValue);

        QSlider* _slider;                                          ///< Slider allowing the user to quickly change the value
        typename AdjusterWidgetTraits<T>::SpinBoxType* _spinBox;   ///< Spin box displaying the current value

    };

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

    template<typename T>
    AbstractAdjusterWidget<T>::AbstractAdjusterWidget(QWidget* parent /*= 0*/)
        : QWidget(parent)
        , _slider(0)
        , _spinBox(0)
    {
        _spinBox = new typename AdjusterWidgetTraits<T>::SpinBoxType;
        _spinBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
        _slider = new QSlider(Qt::Horizontal);

        QHBoxLayout* layout = new QHBoxLayout;
        layout->setContentsMargins(0, 0, 0, 0);
        layout->addWidget(_slider);
        layout->addWidget(_spinBox);
        setLayout(layout);
    }

    template<typename T>
164
    T AbstractAdjusterWidget<T>::value() const {
165
166
167
168
        return _spinBox->value();
    }

    template<typename T>
169
    void AbstractAdjusterWidget<T>::setValueImpl(T value) {
170
171
172
173
174
        _spinBox->setValue(value);
        setSliderValue(value);
    }

    template<typename T>
175
    T AbstractAdjusterWidget<T>::minimum() const {
176
177
178
179
        return _spinBox->minimum();
    }

    template<typename T>
180
    void AbstractAdjusterWidget<T>::setMinimumImpl(T minimum) {
181
182
183
184
185
        _spinBox->setMinimum(minimum);
        setSliderProperties(_spinBox->singleStep(), minimum, _spinBox->maximum());
    }

    template<typename T>
186
    T AbstractAdjusterWidget<T>::maximum() const {
187
188
189
190
        return _spinBox->maximum();
    }

    template<typename T>
191
    void AbstractAdjusterWidget<T>::setMaximumImpl(T maximum) {
192
193
194
195
196
        _spinBox->setMaximum(maximum);
        setSliderProperties(_spinBox->singleStep(), _spinBox->minimum(), maximum);
    }

    template<typename T>
197
    T AbstractAdjusterWidget<T>::singleStep() const {
198
199
200
201
        return _spinBox->singleStep();
    }

    template<typename T>
202
    void AbstractAdjusterWidget<T>::setSingleStepImpl(T value) {
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        _spinBox->setSingleStep(value);
        setSliderProperties(value, _spinBox->minimum(), _spinBox->maximum());
    }

    template<typename T>
    void AbstractAdjusterWidget<T>::onSpinBoxValueChangedImpl(T value) {
        setSliderValue(value);
    }

    template<typename T>
    void AbstractAdjusterWidget<T>::onSliderValueChangedImpl(T value) {
        _spinBox->blockSignals(true);
        _spinBox->setValue(value);
        _spinBox->blockSignals(false);
    }

    template<typename T>
    void AbstractAdjusterWidget<T>::setSliderValue(T value) {
        _slider->blockSignals(true);
        _slider->setValue(value);
        _slider->blockSignals(false);
    }

    template<typename T>
    void AbstractAdjusterWidget<T>::setSliderProperties(T stepValue, T minValue, T maxValue) {
        _slider->blockSignals(true);
        _slider->setMinimum(minValue);
        _slider->setMaximum(maxValue);
        _slider->setSingleStep(stepValue);
        _slider->blockSignals(false);
    }

}

#endif // ABSTRACTADJUSTERWIDGET_H__