Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Range.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2015 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OMNI_UI_MIXIN_RANGE_H_
21 #define OMNI_UI_MIXIN_RANGE_H_
22 
23 #include <limits>
24 #include "Step.h"
25 
26 namespace omni {
27  namespace ui {
28  namespace mixin {
29  /// Mixin for a ranged value with min and maximum
30  template<typename VALUE>
31  struct Range
32  {
33  typedef VALUE value_type;
34 
36  value_type _minimum = 0.0,
37  value_type _maximum = 100.0) :
38  minimum_(_minimum),
39  maximum_(_maximum)
40  {}
41 
42  /// Return ratio of value v
43  qreal ratio(value_type _v) const
44  {
45  return qreal(_v - minimum()) / range();
46  }
47 
48  /// Return range (difference between maximum and minimum)
49  value_type range() const {
50  return maximum() - minimum();
51  }
52 
53  /// Return minimum value
55  {
56  return minimum_;
57  }
58 
59  /// Return maximum value
61  {
62  return maximum_;
63  }
64 
65  /// Set new minimum value and value and emit rangeChanged event
66  void setMinimum(value_type _minimum)
67  {
68  minimum_ = _minimum;
69  validate();
71  }
72 
73  /// Disable minimum by setting the value to infinite
75  {
76  setMinimum(std::numeric_limits<value_type>::min());
77  }
78 
79  /// Set new maximum value and value and emit rangeChanged event
80  void setMaximum(value_type _maximum)
81  {
82  maximum_ = _maximum;
83  validate();
85  }
86 
87  /// Disable maximum by setting maximum to infinite value
89  {
90  setMaximum(std::numeric_limits<value_type>::max());
91  }
92 
93  /// Set range and validate
94  void setRange(value_type _minimum, value_type _maximum)
95  {
96  minimum_ = _minimum;
97  maximum_ = _maximum;
98  validate();
100  }
101 
102  /// Disable range by setting minimum and maximum to infinite value
104  {
105  setRange(
106  std::numeric_limits<value_type>::min(),
107  std::numeric_limits<value_type>::max());
108  }
109 
110  bool minimumUsed() const
111  {
112  return minimum_ != std::numeric_limits<value_type>::min();
113  }
114 
115  bool maximumUsed() const
116  {
117  return maximum_ != std::numeric_limits<value_type>::max();
118  }
119 
120  protected:
121  /// Apply range to widget
122  template<typename WIDGET>
123  void apply(WIDGET *_widget) const
124  {
125  _widget->setMinimum(minimum());
126  _widget->setMaximum(maximum());
127  }
128 
129  /// Applay range to widget and set value
130  template<typename WIDGET, typename V>
131  void apply(WIDGET *_widget, V _value) const
132  {
133  apply(_widget);
134  _widget->setValue(_value);
135  }
136 
137  /// Validate minimum and maximum
138  virtual void validate()
139  {
140  if (minimum() > maximum()) std::swap(minimum_, maximum_);
141  }
142 
143  /// Handler to emit rangeChanged signal in derived widgets
144  virtual void rangeChangedEvent()
145  {}
146 
147  private:
150  };
151  }
152  }
153 }
154 
155 #endif /* OMNI_UI_MIXIN_RANGE_H_ */
Range(value_type _minimum=0.0, value_type _maximum=100.0)
Definition: Range.h:35
bool minimumUsed() const
Definition: Range.h:110
Mixin for a ranged value with min and maximum.
Definition: Range.h:31
value_type range() const
Return range (difference between maximum and minimum)
Definition: Range.h:49
bool maximumUsed() const
Definition: Range.h:115
virtual void validate()
Validate minimum and maximum.
Definition: Range.h:138
value_type minimum_
Definition: Range.h:148
void setMaximum(value_type _maximum)
Set new maximum value and value and emit rangeChanged event.
Definition: Range.h:80
virtual void rangeChangedEvent()
Handler to emit rangeChanged signal in derived widgets.
Definition: Range.h:144
void disableRange()
Disable range by setting minimum and maximum to infinite value.
Definition: Range.h:103
void apply(WIDGET *_widget) const
Apply range to widget.
Definition: Range.h:123
void setRange(value_type _minimum, value_type _maximum)
Set range and validate.
Definition: Range.h:94
qreal ratio(value_type _v) const
Return ratio of value v.
Definition: Range.h:43
value_type maximum() const
Return maximum value.
Definition: Range.h:60
VALUE value_type
Definition: Range.h:33
void disableMinimum()
Disable minimum by setting the value to infinite.
Definition: Range.h:74
value_type maximum_
Definition: Range.h:149
value_type minimum() const
Return minimum value.
Definition: Range.h:54
void disableMaximum()
Disable maximum by setting maximum to infinite value.
Definition: Range.h:88
void apply(WIDGET *_widget, V _value) const
Applay range to widget and set value.
Definition: Range.h:131
void setMinimum(value_type _minimum)
Set new minimum value and value and emit rangeChanged event.
Definition: Range.h:66