Vuo  2.3.2
VuoDoubleSpinBox.cc
Go to the documentation of this file.
1 
10 #include "VuoDoubleSpinBox.hh"
11 
17 VuoDoubleSpinBox::VuoDoubleSpinBox(QWidget *parent, int precision)
18  : QDoubleSpinBox(parent)
19 {
20  buttonMinimum = -std::numeric_limits<double>::max();
21  buttonMaximum = std::numeric_limits<double>::max();
22  this->precision = precision;
23  setKeyboardTracking(false);
25 }
26 
30 QAbstractSpinBox::StepEnabled VuoDoubleSpinBox::stepEnabled() const
31 {
32  double v = valueFromText(text());
33  QAbstractSpinBox::StepEnabled e = 0;
34  if (v > buttonMinimum)
35  e |= StepDownEnabled;
36  if (v < buttonMaximum)
37  e |= StepUpEnabled;
38  return e;
39 }
40 
44 void VuoDoubleSpinBox::stepBy(int steps)
45 {
46  double _value = valueFromText(text());
47  double _singleStep = singleStep();
48 
49  if (_singleStep > 0)
50  {
51  if (_value + steps * _singleStep < buttonMinimum)
52  {
53  setValue(buttonMinimum);
54  return;
55  }
56  else if (_value + steps * _singleStep > buttonMaximum)
57  {
58  setValue(buttonMaximum);
59  return;
60  }
61  }
62 
63  QDoubleSpinBox::stepBy(steps);
64 }
65 
70 QString VuoDoubleSpinBox::textFromValue(double value) const
71 {
72  QString newLineEditText = QLocale().toString(value, 'g', precision);
73 
74  if (qAbs(value) >= 1000.0)
75  newLineEditText.remove(QLocale().groupSeparator());
76 
77  return newLineEditText;
78 }
79 
84 {
85  setMinimum(-std::numeric_limits<double>::max());
86  setMaximum(std::numeric_limits<double>::max());
87 }
88 
92 void VuoDoubleSpinBox::setButtonMinimum(double buttonMinimum)
93 {
94  this->buttonMinimum = buttonMinimum;
95 }
96 
100 void VuoDoubleSpinBox::setButtonMaximum(double buttonMaximum)
101 {
102  this->buttonMaximum = buttonMaximum;
103 }
104 
108 double VuoDoubleSpinBox::sliderToDouble(int sliderMin, int sliderMax, double valueMin, double valueMax, int value)
109 {
110  double normalized = ((double)value - sliderMin) / (sliderMax - sliderMin);
111  return valueMin + (normalized * (valueMax - valueMin));
112 }
113 
117 int VuoDoubleSpinBox::doubleToSlider(int sliderMin, int sliderMax, double valueMin, double valueMax, double value)
118 {
119  double normalized = (value - valueMin) / (valueMax - valueMin);
120  int rounded = (int) (normalized * (sliderMax - sliderMin));
121  return sliderMin + rounded;
122 }
123 
128 void VuoDoubleSpinBox::focusOutEvent(QFocusEvent * event)
129 {
130  Qt::FocusReason reason = event->reason();
131 
132  // if tabbing lost focus, keep the value
133  if( reason == Qt::MouseFocusReason ||
134  reason == Qt::TabFocusReason ||
135  reason == Qt::BacktabFocusReason )
136  QDoubleSpinBox::focusOutEvent(event);
137 }
138 
143 void VuoDoubleSpinBox::hideEvent(QHideEvent * event)
144 {
145  // If hideEvent is called while the window still has focus
146  // that means the mouse clicked outside the window and we
147  // should commit the changes.
148  //
149  // This is necessary because in focusOutEvent the FocusReason
150  // does not distinguish the difference between clicking
151  // outside a window and hitting Escape to exit - both
152  // return "ActiveWindowFocusReason"
153 
154  if(hasFocus())
155  QDoubleSpinBox::hideEvent(event);
156 }