Vuo  2.0.1
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);
24  setMinimum(buttonMinimum);
25  setMaximum(buttonMaximum);
26 }
27 
31 QAbstractSpinBox::StepEnabled VuoDoubleSpinBox::stepEnabled() const
32 {
33  double v = valueFromText(text());
34  QAbstractSpinBox::StepEnabled e = 0;
35  if (v > buttonMinimum)
36  e |= StepDownEnabled;
37  if (v < buttonMaximum)
38  e |= StepUpEnabled;
39  return e;
40 }
41 
45 void VuoDoubleSpinBox::stepBy(int steps)
46 {
47  double _value = valueFromText(text());
48  double _singleStep = singleStep();
49 
50  if (_singleStep > 0)
51  {
52  if (_value + steps * _singleStep < buttonMinimum)
53  {
54  setValue(buttonMinimum);
55  return;
56  }
57  else if (_value + steps * _singleStep > buttonMaximum)
58  {
59  setValue(buttonMaximum);
60  return;
61  }
62  }
63 
64  QDoubleSpinBox::stepBy(steps);
65 }
66 
71 QString VuoDoubleSpinBox::textFromValue(double value) const
72 {
73  QString newLineEditText = QLocale().toString(value, 'g', precision);
74 
75  if (qAbs(value) >= 1000.0)
76  newLineEditText.remove(QLocale().groupSeparator());
77 
78  return newLineEditText;
79 }
80 
84 void VuoDoubleSpinBox::setButtonMinimum(double buttonMinimum)
85 {
86  this->buttonMinimum = buttonMinimum;
87 }
88 
92 void VuoDoubleSpinBox::setButtonMaximum(double buttonMaximum)
93 {
94  this->buttonMaximum = buttonMaximum;
95 }
96 
100 double VuoDoubleSpinBox::sliderToDouble(int sliderMin, int sliderMax, double valueMin, double valueMax, int value)
101 {
102  double normalized = ((double)value - sliderMin) / (sliderMax - sliderMin);
103  return valueMin + (normalized * (valueMax - valueMin));
104 }
105 
109 int VuoDoubleSpinBox::doubleToSlider(int sliderMin, int sliderMax, double valueMin, double valueMax, double value)
110 {
111  double normalized = (value - valueMin) / (valueMax - valueMin);
112  int rounded = (int) (normalized * (sliderMax - sliderMin));
113  return sliderMin + rounded;
114 }
115 
120 void VuoDoubleSpinBox::focusOutEvent(QFocusEvent * event)
121 {
122  Qt::FocusReason reason = event->reason();
123 
124  // if tabbing lost focus, keep the value
125  if( reason == Qt::MouseFocusReason ||
126  reason == Qt::TabFocusReason ||
127  reason == Qt::BacktabFocusReason )
128  QDoubleSpinBox::focusOutEvent(event);
129 }
130 
135 void VuoDoubleSpinBox::hideEvent(QHideEvent * event)
136 {
137  // If hideEvent is called while the window still has focus
138  // that means the mouse clicked outside the window and we
139  // should commit the changes.
140  //
141  // This is necessary because in focusOutEvent the FocusReason
142  // does not distinguish the difference between clicking
143  // outside a window and hitting Escape to exit - both
144  // return "ActiveWindowFocusReason"
145 
146  if(hasFocus())
147  QDoubleSpinBox::hideEvent(event);
148 }
149 
150