21#include <tqlineedit.h>
22#include <tqobjectlist.h>
27 : TQSpinBox(0, 99999, 1, parent, name),
28 mMinValue(TQSpinBox::minValue()),
29 mMaxValue(TQSpinBox::maxValue())
34SpinBox::SpinBox(
int minValue,
int maxValue,
int step, TQWidget* parent,
const char* name)
35 : TQSpinBox(minValue, maxValue, step, parent, name),
44 int step = TQSpinBox::lineStep();
46 mLineShiftStep = step;
47 mCurrentButton = NO_BUTTON;
49 mShiftMinBound =
false;
50 mShiftMaxBound =
false;
53 mSuppressSignals =
false;
58 TQObjectList* spinwidgets = queryList(
"TQSpinWidget", 0,
false,
true);
59 TQSpinWidget* spin = (TQSpinWidget*)spinwidgets->getFirst();
61 spin->installEventFilter(
this);
63 editor()->installEventFilter(
this);
66 connect(editor(), TQ_SIGNAL(textChanged(
const TQString&)), TQ_SLOT(textEdited()));
71 if ((
int)ro != (
int)mReadOnly)
74 editor()->setReadOnly(ro);
76 setShiftStepping(
false, mCurrentButton);
82 return (val < mMinValue) ? mMinValue : (val > mMaxValue) ? mMaxValue : val;
88 TQSpinBox::setMinValue(val);
89 mShiftMinBound =
false;
95 TQSpinBox::setMaxValue(val);
96 mShiftMaxBound =
false;
103 TQSpinBox::setLineStep(step);
108 mLineShiftStep = step;
110 TQSpinBox::setLineStep(step);
115 int step = TQSpinBox::lineStep();
122 int step = -TQSpinBox::lineStep();
134 int newval = value() + change;
135 int maxval = current ? TQSpinBox::maxValue() : mMaxValue;
136 int minval = current ? TQSpinBox::minValue() : mMinValue;
139 int range = maxval - minval + 1;
141 newval = minval + (newval - maxval - 1) % range;
142 else if (newval < minval)
143 newval = maxval - (minval - 1 - newval) % range;
149 else if (newval < minval)
157 if (!mSuppressSignals)
160 if (mShiftMinBound && val >= mMinValue)
163 TQSpinBox::setMinValue(mMinValue);
164 mShiftMinBound =
false;
166 if (mShiftMaxBound && val <= mMaxValue)
169 TQSpinBox::setMaxValue(mMaxValue);
170 mShiftMaxBound =
false;
173 bool focus = !mSelectOnStep && hasFocus();
176 TQSpinBox::valueChange();
185void SpinBox::textEdited()
193 TQSpinBox::updateDisplay();
207 case TQEvent::KeyPress:
210 TQKeyEvent* ke = (TQKeyEvent*)e;
212 if (key == TQt::Key_Up)
214 else if (key == TQt::Key_Down)
216 shift = ((ke->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton);
221 TQWheelEvent* we = (TQWheelEvent*)e;
222 step = (we->delta() > 0) ? 1 : -1;
223 shift = ((we->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton);
242 step = mLineShiftStep - val % mLineShiftStep;
244 step = - ((val + mLineShiftStep - 1) % mLineShiftStep + 1);
247 step = (step > 0) ? mLineStep : -mLineStep;
254 int etype = e->type();
257 case TQEvent::MouseButtonPress:
258 case TQEvent::MouseButtonDblClick:
260 TQMouseEvent* me = (TQMouseEvent*)e;
261 if (me->button() == TQt::LeftButton)
266 mCurrentButton = whichButton(me->pos());
267 if (mCurrentButton == NO_BUTTON)
269 bool shift = (me->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
270 if (setShiftStepping(shift, mCurrentButton))
276 case TQEvent::MouseButtonRelease:
278 TQMouseEvent* me = (TQMouseEvent*)e;
279 if (me->button() == TQt::LeftButton && mShiftMouse)
281 setShiftStepping(
false, mCurrentButton);
286 case TQEvent::MouseMove:
288 TQMouseEvent* me = (TQMouseEvent*)e;
289 if (me->state() & TQt::LeftButton)
294 int newButton = whichButton(me->pos());
295 if (newButton != mCurrentButton)
299 mCurrentButton = newButton;
300 bool shift = (me->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
301 if (setShiftStepping(shift, mCurrentButton))
310 TQWheelEvent* we = (TQWheelEvent*)e;
311 bool shift = (we->state() & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
312 if (setShiftStepping(shift, (we->delta() > 0 ? UP : DOWN)))
316 case TQEvent::KeyPress:
317 case TQEvent::KeyRelease:
318 case TQEvent::AccelOverride:
320 TQKeyEvent* ke = (TQKeyEvent*)e;
322 int state = ke->state();
323 if ((state & TQt::LeftButton)
324 && (key == TQt::Key_Shift || key == TQt::Key_Alt))
329 state ^= (key == TQt::Key_Shift) ? TQt::ShiftButton : TQt::AltButton;
330 bool shift = (state & (TQt::ShiftButton | TQt::AltButton)) == TQt::ShiftButton;
331 if ((!shift && mShiftMouse) || (shift && !mShiftMouse))
335 if (setShiftStepping(shift, mCurrentButton))
343 return TQSpinBox::eventFilter(obj, e);
349bool SpinBox::setShiftStepping(
bool shift,
int currentButton)
351 if (currentButton == NO_BUTTON)
353 if (shift && !mShiftMouse)
361 int step = (currentButton == UP) ? mLineShiftStep : (currentButton == DOWN) ? -mLineShiftStep : 0;
377 int newval = val + adjust + step;
378 int svt = specialValueText().isEmpty() ? 0 : 1;
379 int minval = mMinValue + svt;
380 if (newval <= minval || newval >= mMaxValue)
383 if (svt && newval <= mMinValue && val == mMinValue)
386 newval = (newval <= minval) ? minval : mMaxValue;
387 TQSpinBox::setValue(newval);
394 int tempval = val + adjust;
395 if (tempval < mMinValue)
397 TQSpinBox::setMinValue(tempval);
398 mShiftMinBound =
true;
400 else if (tempval > mMaxValue)
402 TQSpinBox::setMaxValue(tempval);
403 mShiftMaxBound =
true;
408 mSuppressSignals =
true;
409 bool blocked = signalsBlocked();
412 blockSignals(blocked);
413 mSuppressSignals =
false;
415 TQSpinBox::setLineStep(mLineShiftStep);
417 else if (!shift && mShiftMouse)
420 TQSpinBox::setLineStep(mLineStep);
421 TQSpinBox::setMinValue(mMinValue);
422 TQSpinBox::setMaxValue(mMaxValue);
423 mShiftMinBound = mShiftMaxBound =
false;
442 if (oldValue == 0 || shiftStep == 0)
447 return -(oldValue % shiftStep);
449 return (-oldValue - 1) % shiftStep + 1 - shiftStep;
453 shiftStep = -shiftStep;
455 return shiftStep - ((oldValue - 1) % shiftStep + 1);
457 return (-oldValue) % shiftStep;
464int SpinBox::whichButton(
const TQPoint& pos)
466 if (upRect().contains(pos))
468 if (downRect().contains(pos))
virtual bool eventFilter(TQObject *, TQEvent *)
Receives events destined for the spin widget or for the edit field.
virtual void valueChange()
A virtual method called whenever the value of the spin box has changed.
void stepped(int step)
Signal emitted when the spin box's value is stepped (by the shifted or unshifted increment).
void setLineShiftStep(int step)
Sets the shifted step increment, i.e.
virtual void setReadOnly(bool readOnly)
Sets whether the spin box can be changed by the user.
void setMinValue(int val)
Sets the minimum value of the spin box.
virtual void stepUp()
Increments the value of the spin box by the unshifted step increment.
SpinBox(TQWidget *parent=0, const char *name=0)
Constructor.
void setLineStep(int step)
Sets the unshifted step increment, i.e.
virtual int shiftStepAdjustment(int oldValue, int shiftStep)
Returns the initial adjustment to the value for a shift step up or down.
int bound(int val) const
Returns the specified value clamped to the range of the spin box.
void addValue(int change)
Adds a value to the current value of the spin box.
virtual void stepDown()
Decrements the value of the spin box by the unshifted step increment.
void setMaxValue(int val)
Sets the maximum value of the spin box.
virtual void updateDisplay()
Updates the contents of the embedded TQLineEdit to reflect the current value using mapValueToText().