Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Scale.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_SCALE_H_
21 #define OMNI_UI_MIXIN_SCALE_H_
22 
23 #include <map>
24 
25 namespace omni {
26  namespace ui {
27  namespace mixin {
28  /// Scales a set of sliders and sets a common unit suffix
29  template<typename SLIDER, typename SCALAR = float>
30  class Scale {
31  public:
32  typedef SLIDER slider_type;
33  typedef SCALAR scalar_type;
34 
35  Scale() : suffix_("m") {}
36 
37  /// Set scale and apply to sliders
38  virtual void setScale(float _scale) {
39  float _oldScale = scale_;
40 
41  scale_ = _scale;
42 
43  for (auto& _slider_info : sliders_) {
44  auto& _slider = _slider_info.first;
45  auto& _info = _slider_info.second;
46  _slider->setRange(_info.min_ * scale_, _info.max_ * scale_);
47 
48  if (rescaleValues_) {
49  auto _value = _slider->value() / _oldScale * scale_;
50  _slider->setValue(_value);
51  }
52 
53  _slider->setSingleStep(_info.singleStep_ * scale_);
54  _slider->setDefaultValue(_info.defaultValue_ * scale_);
55  _slider->setPageStep(_info.pageStep_ * scale_);
56  _slider->setPrecision(getPrecision(scale_));
57  _slider->setPivot(_info.pivot_);
58  }
59  }
60 
61  bool rescaleValues() const {
62  return rescaleValues_;
63  }
64 
65  void setRescaleValues(bool _rescaleValues) {
66  rescaleValues_ = _rescaleValues;
67  }
68 
69  /// Return scale value
70  float scale() const {
71  return scale_;
72  }
73 
74  /// Common unit of sliders
75  QString suffix() const {
76  return suffix_;
77  }
78 
79  /// Set suffix (unit) of slider
80  void setSuffix(QString const& _suffix) {
81  suffix_ = _suffix;
82 
83  for (auto& _slider_info : sliders_) {
84  _slider_info.first->setSuffix(suffix_);
85  }
86  }
87 
88  /// Register slider to be scaled
90  if (sliders_.count(_slider)) return;
91 
92  sliders_[_slider] = SliderInfo(_slider->minimum(), _slider->maximum(),
93  _slider->pageStep(),
94  _slider->singleStep(),
95  _slider->defaultValue(),
96  _slider->pivot());
97  }
98 
99  /// Remove slider from list, keep scale
101  if (!sliders_.count(_slider)) return;
102 
103  sliders_.erase(_slider);
104  }
105 
106  /// Return precesion by scale
107  static int getPrecision(float _scale) {
108  if (_scale <= 0.1) {
109  return 4;
110  }
111 
112  if (_scale <= 1) {
113  return 3;
114  }
115 
116  if (_scale <= 10) {
117  return 2;
118  }
119 
120  if (_scale <= 100) {
121  return 1;
122  }
123  return 2;
124  }
125 
126  private:
127  /// Slider Info struct saves min, max and step values
128  struct SliderInfo {
130 
131  SliderInfo(float _min, float _max, float _pageStep,
132  float _singleStep, float _defaultValue, float _pivot) :
133  min_(_min),
134  max_(_max),
135  pageStep_(_pageStep),
136  singleStep_(_singleStep),
137  defaultValue_(_defaultValue),
138  pivot_(_pivot) {}
139 
140  float min_, max_;
143  };
144 
145  /// Common suffix is meter
146  QString suffix_;
147  int precision_ = 2;
148  float scale_ = 1.0;
149  bool rescaleValues_ = true;
150  std::map<slider_type *, SliderInfo> sliders_;
151  };
152  }
153  }
154 }
155 
156 #endif /* OMNI_UI_MIXIN_SCALE_H_ */
float scale() const
Return scale value.
Definition: Scale.h:70
Scale()
Definition: Scale.h:35
void setRescaleValues(bool _rescaleValues)
Definition: Scale.h:65
value_type pageStep() const
Return value for page step.
Definition: Step.h:73
void registerScaledSlider(slider_type *_slider)
Register slider to be scaled.
Definition: Scale.h:89
float min_
Definition: Scale.h:140
bool rescaleValues() const
Definition: Scale.h:61
float defaultValue_
Definition: Scale.h:142
QString suffix_
Common suffix is meter.
Definition: Scale.h:146
float singleStep_
Definition: Scale.h:141
SliderInfo(float _min, float _max, float _pageStep, float _singleStep, float _defaultValue, float _pivot)
Definition: Scale.h:131
std::map< slider_type *, SliderInfo > sliders_
Definition: Scale.h:150
float pageStep_
Definition: Scale.h:141
SLIDER slider_type
Definition: Scale.h:32
float scale_
Definition: Scale.h:148
SCALAR scalar_type
Definition: Scale.h:33
value_type singleStep() const
Return value for single step.
Definition: Step.h:67
int precision_
Definition: Scale.h:147
A slider with float value within a minimum and maximum.
Definition: RangedFloat.h:30
bool rescaleValues_
Definition: Scale.h:149
value_type maximum() const
Return maximum value.
Definition: Range.h:60
float pivot_
Definition: Scale.h:142
Scales a set of sliders and sets a common unit suffix.
Definition: Scale.h:30
value_type defaultValue() const
Return default value.
Definition: RangedValue.h:71
virtual void setScale(float _scale)
Set scale and apply to sliders.
Definition: Scale.h:38
static int getPrecision(float _scale)
Return precesion by scale.
Definition: Scale.h:107
QString suffix() const
Common unit of sliders.
Definition: Scale.h:75
SliderInfo()
Definition: Scale.h:129
value_type minimum() const
Return minimum value.
Definition: Range.h:54
void setSuffix(QString const &_suffix)
Set suffix (unit) of slider.
Definition: Scale.h:80
void unregisterScaledSlider(slider_type *_slider)
Remove slider from list, keep scale.
Definition: Scale.h:100
value_type pivot() const
Return pivot value.
Definition: RangedValue.h:77
float max_
Definition: Scale.h:140
Slider Info struct saves min, max and step values.
Definition: Scale.h:128