21 #include <tdeversion.h>
22 #include <tqlineedit.h>
23 #include <tqobjectlist.h>
24 #include "spinbox.moc"
28 : TQSpinBox(0, 99999, 1, parent, name),
29 mMinValue(TQSpinBox::minValue()),
30 mMaxValue(TQSpinBox::maxValue())
35 SpinBox::SpinBox(
int minValue,
int maxValue,
int step, TQWidget* parent,
const char* name)
36 : TQSpinBox(minValue, maxValue, step, parent, name),
45 int step = TQSpinBox::lineStep();
47 mLineShiftStep = step;
48 mCurrentButton = NO_BUTTON;
50 mShiftMinBound =
false;
51 mShiftMaxBound =
false;
54 mSuppressSignals =
false;
59 TQObjectList* spinwidgets = queryList(
"TQSpinWidget", 0,
false,
true);
60 TQSpinWidget* spin = (TQSpinWidget*)spinwidgets->getFirst();
62 spin->installEventFilter(
this);
64 editor()->installEventFilter(
this);
66 #if KDE_IS_VERSION(3,1,90)
68 connect(editor(), TQ_SIGNAL(textChanged(
const TQString&)), TQ_SLOT(textEdited()));
74 if ((
int)ro != (
int)mReadOnly)
77 editor()->setReadOnly(ro);
79 setShiftStepping(
false, mCurrentButton);
85 return (val < mMinValue) ? mMinValue : (val > mMaxValue) ? mMaxValue : val;
91 TQSpinBox::setMinValue(val);
92 mShiftMinBound =
false;
98 TQSpinBox::setMaxValue(val);
99 mShiftMaxBound =
false;
106 TQSpinBox::setLineStep(step);
111 mLineShiftStep = step;
113 TQSpinBox::setLineStep(step);
118 int step = TQSpinBox::lineStep();
125 int step = -TQSpinBox::lineStep();
137 int newval = value() + change;
138 int maxval = current ? TQSpinBox::maxValue() : mMaxValue;
139 int minval = current ? TQSpinBox::minValue() : mMinValue;
142 int range = maxval - minval + 1;
144 newval = minval + (newval - maxval - 1) % range;
145 else if (newval < minval)
146 newval = maxval - (minval - 1 - newval) % range;
152 else if (newval < minval)
160 if (!mSuppressSignals)
163 if (mShiftMinBound && val >= mMinValue)
166 TQSpinBox::setMinValue(mMinValue);
167 mShiftMinBound =
false;
169 if (mShiftMaxBound && val <= mMaxValue)
172 TQSpinBox::setMaxValue(mMaxValue);
173 mShiftMaxBound =
false;
176 bool focus = !mSelectOnStep && hasFocus();
179 TQSpinBox::valueChange();
188 void SpinBox::textEdited()
196 TQSpinBox::updateDisplay();
210 case TQEvent::KeyPress:
213 TQKeyEvent* ke = (TQKeyEvent*)e;
215 if (key == TQt::Key_Up)
217 else if (key == TQt::Key_Down)
219 shift = ((ke->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton);
224 TQWheelEvent* we = (TQWheelEvent*)e;
225 step = (we->delta() > 0) ? 1 : -1;
226 shift = ((we->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton);
229 #if KDE_IS_VERSION(3,1,90)
247 step = mLineShiftStep - val % mLineShiftStep;
249 step = - ((val + mLineShiftStep - 1) % mLineShiftStep + 1);
252 step = (step > 0) ? mLineStep : -mLineStep;
259 int etype = e->type();
262 case TQEvent::MouseButtonPress:
263 case TQEvent::MouseButtonDblClick:
265 TQMouseEvent* me = (TQMouseEvent*)e;
266 if (me->button() == TQt::LeftButton)
271 mCurrentButton = whichButton(me->pos());
272 if (mCurrentButton == NO_BUTTON)
274 bool shift = (me->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
275 if (setShiftStepping(shift, mCurrentButton))
281 case TQEvent::MouseButtonRelease:
283 TQMouseEvent* me = (TQMouseEvent*)e;
284 if (me->button() == TQt::LeftButton && mShiftMouse)
286 setShiftStepping(
false, mCurrentButton);
291 case TQEvent::MouseMove:
293 TQMouseEvent* me = (TQMouseEvent*)e;
294 if (me->state() & TQt::LeftButton)
299 int newButton = whichButton(me->pos());
300 if (newButton != mCurrentButton)
304 mCurrentButton = newButton;
305 bool shift = (me->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
306 if (setShiftStepping(shift, mCurrentButton))
315 TQWheelEvent* we = (TQWheelEvent*)e;
316 bool shift = (we->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
317 if (setShiftStepping(shift, (we->delta() > 0 ? UP : DOWN)))
321 case TQEvent::KeyPress:
322 case TQEvent::KeyRelease:
323 case TQEvent::AccelOverride:
325 TQKeyEvent* ke = (TQKeyEvent*)e;
327 int state = ke->state();
328 if ((state & TQt::LeftButton)
329 && (key == TQt::Key_Shift || key == TQt::Key_Alt))
334 state ^= (key == TQt::Key_Shift) ? TQt::ShiftButton : TQt::AltButton;
335 bool shift = (state & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
336 if ((!shift && mShiftMouse) || (shift && !mShiftMouse))
340 if (setShiftStepping(shift, mCurrentButton))
348 return TQSpinBox::eventFilter(obj, e);
354 bool SpinBox::setShiftStepping(
bool shift,
int currentButton)
356 if (currentButton == NO_BUTTON)
358 if (shift && !mShiftMouse)
366 int step = (currentButton == UP) ? mLineShiftStep : (currentButton == DOWN) ? -mLineShiftStep : 0;
382 int newval = val + adjust + step;
383 int svt = specialValueText().isEmpty() ? 0 : 1;
384 int minval = mMinValue + svt;
385 if (newval <= minval || newval >= mMaxValue)
388 if (svt && newval <= mMinValue && val == mMinValue)
391 newval = (newval <= minval) ? minval : mMaxValue;
392 TQSpinBox::setValue(newval);
399 int tempval = val + adjust;
400 if (tempval < mMinValue)
402 TQSpinBox::setMinValue(tempval);
403 mShiftMinBound =
true;
405 else if (tempval > mMaxValue)
407 TQSpinBox::setMaxValue(tempval);
408 mShiftMaxBound =
true;
413 mSuppressSignals =
true;
414 bool blocked = signalsBlocked();
417 blockSignals(blocked);
418 mSuppressSignals =
false;
420 TQSpinBox::setLineStep(mLineShiftStep);
422 else if (!shift && mShiftMouse)
425 TQSpinBox::setLineStep(mLineStep);
426 TQSpinBox::setMinValue(mMinValue);
427 TQSpinBox::setMaxValue(mMaxValue);
428 mShiftMinBound = mShiftMaxBound =
false;
447 if (oldValue == 0 || shiftStep == 0)
452 return -(oldValue % shiftStep);
454 return (-oldValue - 1) % shiftStep + 1 - shiftStep;
458 shiftStep = -shiftStep;
460 return shiftStep - ((oldValue - 1) % shiftStep + 1);
462 return (-oldValue) % shiftStep;
469 int SpinBox::whichButton(
const TQPoint& pos)
471 if (upRect().contains(pos))
473 if (downRect().contains(pos))