Vuo  2.1.2
VuoSpinBox.cc
Go to the documentation of this file.
1 
10 #include "VuoSpinBox.hh"
11 
15 VuoSpinBox::VuoSpinBox(QWidget *parent)
16  : QSpinBox(parent)
17 {
18  buttonMinimum = INT_MIN;
19  buttonMaximum = INT_MAX;
20 
21  setKeyboardTracking(false);
22  setMinimum(buttonMinimum);
23  setMaximum(buttonMaximum);
24 }
25 
29 QAbstractSpinBox::StepEnabled VuoSpinBox::stepEnabled() const
30 {
31  int v = valueFromText(text());
32  QAbstractSpinBox::StepEnabled e = 0;
33  if (v > buttonMinimum)
34  e |= StepDownEnabled;
35  if (v < buttonMaximum)
36  e |= StepUpEnabled;
37  return e;
38 }
39 
43 void VuoSpinBox::stepBy(int steps)
44 {
45  int _value = valueFromText(text());
46  int _singleStep = singleStep();
47 
48  if (_singleStep > 0)
49  {
50  if (_value + steps * _singleStep < buttonMinimum)
51  {
52  setValue(buttonMinimum);
53  return;
54  }
55  else if (_value + steps * _singleStep > buttonMaximum)
56  {
57  setValue(buttonMaximum);
58  return;
59  }
60  }
61 
62  QSpinBox::stepBy(steps);
63 }
64 
68 void VuoSpinBox::setButtonMinimum(int buttonMinimum)
69 {
70  this->buttonMinimum = buttonMinimum;
71 }
72 
76 void VuoSpinBox::setButtonMaximum(int buttonMaximum)
77 {
78  this->buttonMaximum = buttonMaximum;
79 }
80 
85 void VuoSpinBox::focusOutEvent(QFocusEvent * event)
86 {
87  Qt::FocusReason reason = event->reason();
88 
89  // if tabbing lost focus, keep the value
90  if( reason == Qt::MouseFocusReason ||
91  reason == Qt::TabFocusReason ||
92  reason == Qt::BacktabFocusReason )
93  QSpinBox::focusOutEvent(event);
94 }
95 
100 void VuoSpinBox::hideEvent(QHideEvent * event)
101 {
102  // If hideEvent is called while the window still has focus
103  // that means the mouse clicked outside the window and we
104  // should commit the changes.
105  //
106  // This is necessary because in focusOutEvent the FocusReason
107  // does not distinguish the difference between clicking
108  // outside a window and hitting Escape to exit - both
109  // return "ActiveWindowFocusReason"
110 
111  if(hasFocus())
112  QSpinBox::hideEvent(event);
113 }