OpenWalnut  1.5.0dev
WQtIntervalEdit.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WQTINTERVALEDIT_H
26 #define WQTINTERVALEDIT_H
27 
28 #include <math.h>
29 #include <limits>
30 #include <iostream>
31 
32 #include <QSlider>
33 #include <QLineEdit>
34 #include <QLabel>
35 #include <QGridLayout>
36 
37 template< typename DataType, typename DisplayType >
38 DisplayType toDisplayType( const DataType& value )
39 {
40  return static_cast< DisplayType >( round( value ) );
41 }
42 
43 /**
44  * Base class for interval editing. You should not use this class. Use the derived \ref WQtIntervalEdit template.
45  */
46 class WQtIntervalEditBase: public QWidget
47 {
48  Q_OBJECT
49 public:
50  /**
51  * Default constructor.
52  *
53  * \param parent the parent widget
54  */
55  explicit WQtIntervalEditBase( QWidget* parent );
56 
57  /**
58  * Destructor.
59  */
60  virtual ~WQtIntervalEditBase();
61 
62  /**
63  * Update the current state
64  */
65  virtual void update() = 0;
66 
67 signals:
68  /**
69  * Called whenever the user changes the current minimum OR setMin was called.
70  */
72 
73  /**
74  * Called whenever the user changes the current ,aximum OR setMax was called.
75  */
77 
78 protected slots:
79 
80  /**
81  * Slot called when the minimum slider changes.
82  */
83  virtual void minSliderChanged();
84 
85  /**
86  * Slot called when the maximum slider changes.
87  */
88  virtual void maxSliderChanged();
89 
90  /**
91  * Slot called when the minimum edit changes.
92  */
93  virtual void minEditChanged();
94 
95  /**
96  * Slot called when the maximum edit changes.
97  */
98  virtual void maxEditChanged();
99 
100 protected:
101  /**
102  * Resolution of the sliders.
103  */
104  static int SliderResolution;
105 
106  /**
107  * The slider for the minimum.
108  */
109  QSlider m_minSlider;
110 
111  /**
112  * The slider for the maximumum.
113  */
114  QSlider m_maxSlider;
115 
116  /**
117  * The minimum edit.
118  */
119  QLineEdit m_minEdit;
120 
121  /**
122  * The maximum edit.
123  */
124  QLineEdit m_maxEdit;
125 
126  /**
127  * A label for the minimum.
128  */
129  QLabel m_minLabel;
130 
131  /**
132  * A label for the maximum.
133  */
134  QLabel m_maxLabel;
135 
136  /**
137  * The layout containing min and max layout.
138  */
139  QGridLayout m_layout;
140 
141 private:
142 };
143 
144 /**
145  * Simple widget allowing to define a certain interval.
146  *
147  * \tparam DataType interval for which integral type?
148  */
149 template< typename DataType, typename DisplayType >
151 {
152 public:
153  /**
154  * Default constructor.
155  *
156  * \param parent the parent widget
157  */
158  explicit WQtIntervalEdit( QWidget* parent );
159 
160  /**
161  * Destructor.
162  */
163  virtual ~WQtIntervalEdit();
164 
165  /**
166  * Set the allowed minimum.
167  *
168  * \param min the min.
169  */
170  void setAllowedMin( DataType min = std::numeric_limits< DataType >::min() );
171 
172  /**
173  * Set the allowed maximum.
174  *
175  * \param max the max
176  */
177  void setAllowedMax( DataType max = std::numeric_limits< DataType >::max() );
178 
179  /**
180  * Get the currently allowed min.
181  *
182  * \return the min
183  */
184  const DataType& getAllowedMin() const;
185 
186  /**
187  * Get the currently allowed max.
188  *
189  * \return the max
190  */
191  const DataType& getAllowedMax() const;
192 
193  /**
194  * Set the currently selected min
195  *
196  * \param min the min
197  */
198  void setMin( DataType min = std::numeric_limits< DataType >::min() );
199 
200  /**
201  * Set the currently selected max
202  *
203  * \param max the max
204  */
205  void setMax( DataType max = std::numeric_limits< DataType >::max() );
206 
207  /**
208  * Get the currently selected minimum.
209  *
210  * \return the min
211  */
212  const DataType& getMin() const;
213 
214  /**
215  * Get the currently selected maximum.
216  *
217  * \return the max
218  */
219  const DataType& getMax() const;
220 
221  /**
222  * Update the current state
223  */
224  virtual void update();
225 protected:
226  /**
227  * Slot called when the minimum slider changes.
228  */
229  virtual void minSliderChanged();
230 
231  /**
232  * Slot called when the maximum slider changes.
233  */
234  virtual void maxSliderChanged();
235 
236  /**
237  * Slot called when the minimum edit changes.
238  */
239  virtual void minEditChanged();
240 
241  /**
242  * Slot called when the maximum edit changes.
243  */
244  virtual void maxEditChanged();
245 
246 private:
247  /**
248  * The allowed maximum
249  */
251 
252  /**
253  * The allowed minimum
254  */
256 
257  /**
258  * The current minumum.
259  */
261 
262  /**
263  * The current maximum
264  */
266 };
267 
268 
269 template< typename DataType, typename DisplayType >
271  WQtIntervalEditBase( parent ),
272  m_allowedMax( 100 ),
273  m_allowedMin( 0 ),
274  m_min( 0 ),
275  m_max( 100 )
276 
277 {
278  // initialize members
279 }
280 
281 template< typename DataType, typename DisplayType >
283 {
284  // cleanup
285 }
286 
287 template< typename DataType, typename DisplayType >
289 {
290  if( ( min > m_allowedMax ) || ( m_allowedMin == min ) )
291  {
292  return;
293  }
294 
295  // check two cases:
296  // 1: if the new allowed min is larger than the currently set min -> change m_min too
297  // 2: the new min is smaller then the allowed one and the slider is still at 0 -> keep slider there
298  if( ( min > m_min ) || ( ( min < m_min ) && ( m_minSlider.value() == 0 ) ) )
299  {
300  m_min = min;
301  emit minimumChanged();
302  }
303  m_allowedMin = min;
304  update();
305 }
306 
307 template< typename DataType, typename DisplayType >
309 {
310  if( ( max < m_allowedMin ) || ( m_allowedMax == max ) )
311  {
312  return;
313  }
314 
315  // check two cases:
316  // 1: if the new allowed max is smaller than the currently set max -> change m_max too
317  // 2: the new max is larger then the allowed one and the slider is still at the max -> keep slider at 100
318  if( ( max < m_max ) || ( ( max > m_max ) && ( m_maxSlider.value() == SliderResolution ) ) )
319  {
320  m_max = max;
321  emit maximumChanged();
322  }
323  m_allowedMax = max;
324  update();
325 }
326 
327 template< typename DataType, typename DisplayType >
329 {
330  return m_allowedMin;
331 }
332 
333 template< typename DataType, typename DisplayType >
335 {
336  return m_allowedMax;
337 }
338 
339 template< typename DataType, typename DisplayType >
341 {
342  DataType newVal = ( min < m_allowedMin ) ? m_allowedMin : min;
343  if( m_min == newVal )
344  {
345  return;
346  }
347  m_min = newVal;
348 
349  // ensure the maximum is larger than the minimum
350  if( m_max < m_min )
351  {
352  m_max = m_min;
353  emit maximumChanged();
354  }
355  emit minimumChanged();
356  update();
357 }
358 
359 template< typename DataType, typename DisplayType >
361 {
362  DataType newVal = ( max > m_allowedMax ) ? m_allowedMax : max;
363  if( m_max == newVal )
364  {
365  return;
366  }
367  m_max = newVal;
368  // ensure the minimum is smaller than the specified max
369  if( m_max < m_min )
370  {
371  m_min = m_max;
372  emit minimumChanged();
373  }
374  emit maximumChanged();
375  update();
376 }
377 
378 template< typename DataType, typename DisplayType >
380 {
381  return m_min;
382 }
383 
384 template< typename DataType, typename DisplayType >
386 {
387  return m_max;
388 }
389 
390 template< typename DataType, typename DisplayType >
392 {
393  // set the new slider value
394  double percMin = static_cast< double >( SliderResolution ) *
395  ( static_cast< double >( m_min ) - static_cast< double >( m_allowedMin ) ) /
396  ( static_cast< double >( m_allowedMax ) - static_cast< double >( m_allowedMin ) );
397  double percMax = static_cast< double >( SliderResolution ) *
398  ( static_cast< double >( m_max ) - static_cast< double >( m_allowedMin ) ) /
399  ( static_cast< double >( m_allowedMax ) - static_cast< double >( m_allowedMin ) );
400 
401  if( m_allowedMin == m_allowedMax )
402  {
403  m_minSlider.setValue( 0 );
404  m_maxSlider.setValue( SliderResolution );
405  }
406  else
407  {
408  m_minSlider.setValue( percMin );
409  m_maxSlider.setValue( percMax );
410  }
411 
412  // set the text edits
413  m_minEdit.setText( QString::number( m_min ) );
414  m_maxEdit.setText( QString::number( m_max ) );
415 }
416 
417 template< typename DataType, typename DisplayType >
419 {
420  double perc = static_cast< double >( m_minSlider.value() ) / static_cast< double >( SliderResolution );
421  DataType newMin = static_cast< DataType >(
422  static_cast< double >( m_allowedMin ) + perc * ( static_cast< double >( m_allowedMax ) -
423  static_cast< double >( m_allowedMin ) ) );
424  setMin( newMin );
425 }
426 
427 template< typename DataType, typename DisplayType >
429 {
430  float perc = static_cast< float >( m_maxSlider.value() ) / static_cast< float >( SliderResolution );
431  DataType newMax = static_cast< DataType >(
432  static_cast< double >( m_allowedMin ) + perc * ( static_cast< double >( m_allowedMax ) -
433  static_cast< double >( m_allowedMin ) ) );
434 
435  setMax( newMax );
436 }
437 
438 template< typename DataType, typename DisplayType >
440 {
441  // try to get the value
442  bool valid;
443  double value = m_minEdit.text().toDouble( &valid );
444  if( !valid )
445  {
446  m_minEdit.setText( QString::number( static_cast< DataType >( m_min ) ) );
447  return;
448  }
449 
450  setMin( value );
451 }
452 
453 template< typename DataType, typename DisplayType >
455 {
456  // try to get the value
457  bool valid;
458  double value = m_maxEdit.text().toDouble( &valid );
459  if( !valid )
460  {
461  m_maxEdit.setText( QString::number( static_cast< DataType >( m_max ) ) );
462  return;
463  }
464 
465  setMax( value );
466 }
467 
468 #endif // WQTINTERVALEDIT_H
469 
Base class for interval editing.
QLineEdit m_maxEdit
The maximum edit.
void minimumChanged()
Called whenever the user changes the current minimum OR setMin was called.
void maximumChanged()
Called whenever the user changes the current ,aximum OR setMax was called.
virtual void maxEditChanged()
Slot called when the maximum edit changes.
QGridLayout m_layout
The layout containing min and max layout.
virtual void update()=0
Update the current state.
QLabel m_maxLabel
A label for the maximum.
static int SliderResolution
Resolution of the sliders.
virtual ~WQtIntervalEditBase()
Destructor.
virtual void minSliderChanged()
Slot called when the minimum slider changes.
WQtIntervalEditBase(QWidget *parent)
Default constructor.
virtual void maxSliderChanged()
Slot called when the maximum slider changes.
QSlider m_maxSlider
The slider for the maximumum.
QSlider m_minSlider
The slider for the minimum.
virtual void minEditChanged()
Slot called when the minimum edit changes.
QLineEdit m_minEdit
The minimum edit.
QLabel m_minLabel
A label for the minimum.
Simple widget allowing to define a certain interval.
void setAllowedMin(DataType min=std::numeric_limits< DataType >::min())
Set the allowed minimum.
virtual void minEditChanged()
Slot called when the minimum edit changes.
DataType m_allowedMax
The allowed maximum.
virtual void maxEditChanged()
Slot called when the maximum edit changes.
void setMax(DataType max=std::numeric_limits< DataType >::max())
Set the currently selected max.
const DataType & getAllowedMin() const
Get the currently allowed min.
DataType m_max
The current maximum.
virtual void minSliderChanged()
Slot called when the minimum slider changes.
virtual void maxSliderChanged()
Slot called when the maximum slider changes.
const DataType & getMax() const
Get the currently selected maximum.
const DataType & getAllowedMax() const
Get the currently allowed max.
DataType m_allowedMin
The allowed minimum.
void setAllowedMax(DataType max=std::numeric_limits< DataType >::max())
Set the allowed maximum.
DataType m_min
The current minumum.
const DataType & getMin() const
Get the currently selected minimum.
WQtIntervalEdit(QWidget *parent)
Default constructor.
void setMin(DataType min=std::numeric_limits< DataType >::min())
Set the currently selected min.
virtual void update()
Update the current state.
virtual ~WQtIntervalEdit()
Destructor.
An object that knows an appropriate dataType flag for the typename T.