23 #include "spinbox2.moc" 
   24 #include "spinbox2_p.moc" 
   28 #include <QMouseEvent> 
   29 #include <QStyleOptionSpinBox> 
   30 #include <QGraphicsPixmapItem> 
   37 #include <QApplication> 
   47     "QPlastiqueStyle", 
"QCleanlooksStyle",
 
   56     QPixmap p(r.isEmpty() ? w->size() : r.size());
 
   57     w->render(&p, QPoint(0,0), r, QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask);
 
   61 int SpinBox2::mRightToLeft = -1;
 
   65       mReverseWithLayout(true)
 
   67     mSpinboxFrame = 
new QFrame(
this);
 
   76       mReverseWithLayout(true)
 
   78     mSpinboxFrame = 
new QFrame(
this);
 
   80     mSpinbox = 
new MainSpinBox(minValue, maxValue, 
this, mSpinboxFrame);
 
   88         mRightToLeft = QApplication::isRightToLeft() ? 1 : 0;
 
   89     mMinValue        = mSpinbox->minimum();
 
   90     mMaxValue        = mSpinbox->maximum();
 
   91     mSingleStep      = mSpinbox->singleStep();
 
   92     mSingleShiftStep = mSpinbox->singleShiftStep();
 
   95     mSpinbox->setSelectOnStep(
false);    
 
   97     setFocusProxy(mSpinbox);
 
   98     mUpdown2->setFocusPolicy(Qt::NoFocus);
 
   99     mSpinMirror = 
new SpinMirror(mUpdown2, mSpinbox, 
this);
 
  100     mSpinbox->installEventFilter(
this);
 
  101     mUpdown2->installEventFilter(
this);
 
  105     connect(mUpdown2, SIGNAL(stepped(
int)), SLOT(
stepPage(
int)));
 
  106     connect(mUpdown2, SIGNAL(painted()), SLOT(paintTimer()));
 
  111     if (static_cast<int>(ro) != static_cast<int>(mSpinbox->isReadOnly()))
 
  113         mSpinbox->setReadOnly(ro);
 
  121     if (reverse != mReverseWithLayout)
 
  123         mReverseWithLayout = reverse;
 
  131     QFrame::setEnabled(enabled);
 
  132     mSpinbox->setEnabled(enabled);
 
  133     mUpdown2->setEnabled(enabled);
 
  139     mSpinbox->setWrapping(on);
 
  140     mUpdown2->setWrapping(on);
 
  145     return mUpdown2->
upRect();
 
  159         mSpinbox->setSingleStep(step);
 
  164     mSingleStep = single;
 
  169         mSpinbox->setSingleStep(page);
 
  173         mSpinbox->setSingleStep(single);
 
  180     mSingleShiftStep = single;
 
  181     mPageShiftStep   = page;
 
  185         mSpinbox->setSingleShiftStep(page);
 
  189         mSpinbox->setSingleShiftStep(single);
 
  196     if (mSpinbox->buttonSymbols() == newSymbols)
 
  198     mSpinbox->setButtonSymbols(newSymbols);
 
  199     mUpdown2->setButtonSymbols(newSymbols);
 
  204     return (val < mMinValue) ? mMinValue : (val > mMaxValue) ? mMaxValue : val;
 
  210     mSpinbox->setMinimum(val);
 
  217     mSpinbox->setMaximum(val);
 
  223     int val = mSpinbox->value();
 
  224     bool blocked = mUpdown2->signalsBlocked();
 
  225     mUpdown2->blockSignals(
true);
 
  226     mUpdown2->setValue(val);
 
  227     mUpdown2->blockSignals(blocked);
 
  245     QSize size = mSpinbox->sizeHint();
 
  253     QSize size = mSpinbox->minimumSizeHint();
 
  267     QFrame::paintEvent(e);
 
  268     QTimer::singleShot(0, 
this, SLOT(updateMirrorFrame()));
 
  271 void SpinBox2::paintTimer()
 
  273     QTimer::singleShot(0, 
this, SLOT(updateMirrorButtons()));
 
  276 void SpinBox2::updateMirrorButtons()
 
  281 void SpinBox2::updateMirrorFrame()
 
  286 void SpinBox2::spinboxResized(QResizeEvent* e)
 
  288     int h = e->size().height();
 
  289     if (h != mUpdown2->height())
 
  291         mUpdown2->setFixedSize(mUpdown2->width(), e->size().height());
 
  300 void SpinBox2::setUpdown2Size()
 
  309 void SpinBox2::updateMirror()
 
  315 bool SpinBox2::eventFilter(
QObject* obj, QEvent* e)
 
  317     bool updateButtons = 
false;
 
  325                 QApplication::postEvent(mUpdown2, 
new QEvent(e->type()));
 
  326                 updateButtons = 
true;
 
  328             case QEvent::HoverEnter:
 
  330                 QHoverEvent* he = (QHoverEvent*)e;
 
  331                 QApplication::postEvent(mUpdown2, 
new QHoverEvent(e->type(), QPoint(1, he->pos().y()), he->oldPos()));
 
  332                 updateButtons = 
true;
 
  335             case QEvent::HoverLeave:
 
  337                 QHoverEvent* he = (QHoverEvent*)e;
 
  338                 QApplication::postEvent(mUpdown2, 
new QHoverEvent(e->type(), he->pos(), QPoint(1, he->oldPos().y())));
 
  339                 updateButtons = 
true;
 
  342             case QEvent::FocusIn:
 
  343             case QEvent::FocusOut:
 
  345                 QFocusEvent* fe = (QFocusEvent*)e;
 
  346                 QApplication::postEvent(mUpdown2, 
new QFocusEvent(e->type(), fe->reason()));
 
  347                 updateButtons = 
true;
 
  354     else if (obj == mUpdown2)
 
  360             case QEvent::HoverEnter:
 
  361             case QEvent::HoverLeave:
 
  362             case QEvent::EnabledChange:
 
  363                 updateButtons = 
true;
 
  370         QTimer::singleShot(0, 
this, SLOT(updateMirrorButtons()));
 
  377 void SpinBox2::arrange()
 
  380     mUpdown2->move(-mUpdown2->width(), 0);   
 
  381     QRect arrowRect = style()->visualRect((mRightToLeft ? Qt::RightToLeft : Qt::LeftToRight), rect(), QRect(0, 0, 
wUpdown2, height()));
 
  385     mSpinboxFrame->setGeometry(r);
 
  389     mSpinMirror->resize(
wUpdown2, mUpdown2->height());
 
  390     mSpinMirror->setGeometry(arrowRect);
 
  401     QStyleOptionSpinBox option;
 
  403     QStyle* udStyle = mUpdown2->style();
 
  404     QRect butRect = udStyle->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxUp)
 
  405                   | udStyle->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxDown);
 
  406     if (style()->inherits(
"PlastikStyle"))
 
  407         butRect.setLeft(butRect.left() - 1);    
 
  409     wSpinboxHide = mRightToLeft ? mSpinbox->style()->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxFrame).right() - r.right() : r.left();
 
  417             butx = butRect.left();
 
  421             int x = edRect.right() + 1;
 
  423             butx = butRect.left() - x;
 
  428         r = udStyle->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxFrame);
 
  431             wUpdown2 = edRect.left() - r.left();
 
  432             butx = 
wUpdown2 - (butRect.left() - r.left() + butRect.width());
 
  436             wUpdown2 = r.width() - edRect.right() - 1;
 
  437             butx = r.right() - butRect.right();
 
  452         mSpinbox->setValue(mUpdown2->value());
 
  456         int oldValue = mSpinbox->value();
 
  464                 oldValue -= oldValue % mUpdown2->
singleStep();
 
  466                 oldValue += (-oldValue) % mUpdown2->
singleStep();
 
  468         int adjust = mSpinbox->shiftStepAdjustment(oldValue, step);
 
  470         &&  ((step > 0  &&  oldValue + step >= mSpinbox->maximum())
 
  471           || (step < 0  &&  oldValue + step <= mSpinbox->
minimum())))
 
  473         mSpinbox->addValue(adjust + step);
 
  475     mSpinbox->setFocus();
 
  476     if (mSpinbox->selectOnStep())
 
  477         mSpinbox->selectAll();
 
  494 int SpinBox2::MainSpinBox::shiftStepAdjustment(
int oldValue, 
int shiftStep)
 
  496     if (owner->reverseButtons())
 
  503             oldValue -= oldValue % singleStep();
 
  505             oldValue += (-oldValue) % singleStep();
 
  521     if (!mInhibitPaintSignal)
 
  524         --mInhibitPaintSignal;
 
  535       mMainSpinbox(mainspin),
 
  539     setScene(
new QGraphicsScene(
this));
 
  540     setAttribute(Qt::WA_Hover);
 
  541     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
  542     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
  543     setFrameStyle(QFrame::NoFrame);
 
  544     setMouseTracking(mSpinbox->hasMouseTracking());
 
  545     mButtons = scene()->addPixmap(QPixmap());
 
  546     mButtons->setZValue(1);
 
  547     mButtons->setAcceptedMouseButtons(Qt::LeftButton);
 
  552 void SpinMirror::setMirroredState(
bool clear)
 
  556         setMatrix(QMatrix(-1, 0, 0, 1, width() - 1 , 0));  
 
  558         setMatrix(QMatrix());
 
  568     QGraphicsScene* c = scene();
 
  569     QStyleOptionSpinBox option;
 
  570     option.initFrom(mMainSpinbox);
 
  572     bool rtl = QApplication::isRightToLeft();
 
  576         int x = rtl ? 0 : mMainSpinbox->width() - width();
 
  577         p = 
grabWidget(mMainSpinbox, QRect(x, 0, width(), height()));
 
  583         bool oxygen  = mMainSpinbox->style()->inherits(
"Oxygen::Style"); 
 
  584         bool oxygen1 = mMainSpinbox->style()->inherits(
"OxygenStyle");   
 
  585         int editOffsetY = oxygen ? 5 : oxygen1 ? 6 : 2;   
 
  586         int editOffsetX = (oxygen || oxygen1) ? (KDE::version() >= KDE_MAKE_VERSION(4,6,0) ? 4 : 2) : 2;   
 
  587         int x = rtl ? r.right() - editOffsetX : r.left() + editOffsetX;
 
  588         p = 
grabWidget(mMainSpinbox, QRect(x, 0, 1, height()));
 
  590         QPixmap dot = 
grabWidget(mMainSpinbox, QRect(x, editOffsetY, 1, 1));
 
  591         QPainter painter(&p);
 
  592         painter.drawTiledPixmap(0, editOffsetY, 1, height() - 2*editOffsetY, dot, 0, 0);
 
  595         p = p.scaled(size());
 
  600             int mr = mMainSpinbox->width() - 1;
 
  601             endr.setWidth(mr - r.right() + editOffsetX);
 
  605             endr.setWidth(r.left() + editOffsetX);
 
  606         x = rtl ? width() - endr.width() : 0;
 
  607         mMainSpinbox->render(&p, QPoint(x, 0), endr, QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask);
 
  609     c->setBackgroundBrush(p);
 
  615     QStyleOptionSpinBox option;
 
  617     QStyle* st = mSpinbox->style();
 
  618     QRect r = st->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxUp)
 
  619             | st->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxDown);
 
  624         r.setTop(r.top() + 1);
 
  625         r.setHeight(r.height() - 2);
 
  644     mButtons->setPos(x, y);
 
  649     QSize sz = e->size();
 
  650     scene()->setSceneRect(0, 0, sz.width(), sz.height());
 
  657     setMirroredState(
true);
 
  664 void SpinMirror::mouseEvent(QMouseEvent* e)
 
  668     QPoint pt = e->pos();
 
  669     QGraphicsItem* item = scene()->itemAt(pt);
 
  670     if (item == mButtons)
 
  671         pt = spinboxPoint(pt);
 
  674     QApplication::postEvent(mSpinbox, 
new QMouseEvent(e->type(), pt, e->button(), e->buttons(), e->modifiers()));
 
  685     QPoint pt = e->pos();
 
  686     QGraphicsItem* item = scene()->itemAt(pt);
 
  687     if (item == mButtons)
 
  689         pt = spinboxPoint(pt);
 
  690         QApplication::postEvent(mSpinbox, 
new QWheelEvent(pt, e->delta(), e->buttons(), e->modifiers()), e->orientation());
 
  697 QPoint SpinMirror::spinboxPoint(
const QPoint& param)
 const 
  699     QRect r = mSpinbox->
upRect();
 
  700     QPointF ptf = mButtons->mapFromScene(param.x(), param.y());
 
  701     QPoint pt(ptf.x(), ptf.y());
 
  702     pt.setX(ptf.x() + r.left());
 
  703     pt.setY(ptf.y() + r.top());
 
  718             if (mMainSpinbox->rect().contains(mMainSpinbox->mapFromGlobal(QCursor::pos())))
 
  722             QApplication::postEvent(mMainSpinbox, 
new QEvent(e->type()));
 
  724         case QEvent::HoverLeave:
 
  725             he = (QHoverEvent*)e;
 
  726             if (mMainSpinbox->rect().contains(mMainSpinbox->mapFromGlobal(QCursor::pos())))
 
  729         case QEvent::HoverEnter:
 
  730             he = (QHoverEvent*)e;
 
  731             QApplication::postEvent(mMainSpinbox, 
new QHoverEvent(e->type(), he->pos(), he->oldPos()));
 
  733         case QEvent::HoverMove:
 
  734             he = (QHoverEvent*)e;
 
  736         case QEvent::FocusIn:
 
  737             mMainSpinbox->setFocus();
 
  745         QApplication::postEvent(mSpinbox, 
new QHoverEvent(e->type(), spinboxPoint(he->pos()), spinboxPoint(he->oldPos())));
 
  749     return QGraphicsView::event(e);
 
  760         if (style->inherits(*s))
 
  767     return w->style()->inherits(
"Oxygen::Style")  ||  w->style()->inherits(
"OxygenStyle");
 
  772     QRect r = w->style()->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxEditField);
 
  775         int xadjust = (KDE::version() >= KDE_MAKE_VERSION(4,6,0)) ? 3 : 2;
 
  776         r.adjust(xadjust, 2, -xadjust, -2);
 
void setReadOnly(bool ro)
 
virtual void setEnabled(bool enabled)
Sets whether the widget is enabled. 
 
int singleStep() const 
Returns the unshifted step increment, i.e. 
 
virtual void stepPage(int)
 
QRect upRect() const 
Returns the rectangle containing the up arrow. 
 
QRect down2Rect() const 
Returns the geometry of the left-hand "down" button. 
 
QRect up2Rect() const 
Returns the geometry of the left-hand "up" button. 
 
QRect downRect() const 
Returns the rectangle containing the down arrow. 
 
void initStyleOption(QStyleOptionSpinBox &) const 
Initialise a QStyleOptionSpinBox with this instance's details. 
 
virtual void getMetrics() const 
 
void setSingleStep(int step)
Sets the unshifted step increment, i.e. 
 
void setSelectOnStep(bool sel)
Sets whether the spin box value text should be selected when its value is stepped. 
 
SpinBox2(QWidget *parent=0)
Constructor. 
 
void setSteps(int line, int page)
Sets the unshifted step increments for the two pairs of spin buttons, i.e. 
 
virtual void paintEvent(QPaintEvent *)
 
void setMinimum(int val)
Sets the minimum value of the spin box. 
 
static QPixmap grabWidget(QWidget *w, QRect r=QRect())
 
static QRect spinBoxEditFieldRect(const QWidget *, const QStyleOptionSpinBox &)
 
static const char * mirrorStyles[]
 
void setWrapping(bool on)
Sets whether it is possible to step the value from the highest value to the lowest value and vice ver...
 
void setReverseWithLayout(bool reverse)
Sets whether the spin button pairs should be reversed for a right-to-left language. 
 
virtual bool event(QEvent *)
 
bool reverseButtons() const 
Returns whether the spin button pairs will be reversed for a right-to-left language. 
 
void setShiftSteps(int line, int page)
Sets the shifted step increments for the two pairs of spin buttons, i.e. 
 
virtual void styleChange(QStyle &)
 
void setButtonSymbols(QSpinBox::ButtonSymbols)
Sets the button symbols to use (arrows or plus/minus). 
 
virtual void valueChange()
 
void valueChanged(int value)
Signal which is emitted whenever the value of the spin box changes. 
 
virtual void setReadOnly(bool readOnly)
Sets whether the spin box can be changed by the user. 
 
static bool isOxygenStyle(const QWidget *)
 
virtual QSize minimumSizeHint() const 
 
virtual int shiftStepAdjustment(int oldValue, int shiftStep)
Returns the initial adjustment to the value for a shift step up or down. 
 
virtual void wheelEvent(QWheelEvent *)
 
void setButtonPos(const QPoint &)
 
Spin box with accelerated shift key stepping and read-only option. 
 
virtual void setMinimum(int val)
Sets the minimum value of the spin box. 
 
void setMaximum(int val)
Sets the maximum value of the spin box. 
 
int bound(int val) const 
Returns the specified value clamped to the range of the spin box. 
 
void setSingleShiftStep(int step)
Sets the shifted step increment, i.e. 
 
int singleShiftStep() const 
Returns the shifted step increment, i.e. 
 
virtual void styleChange(QStyle &)
 
virtual void showEvent(QShowEvent *)
 
virtual void paintEvent(QPaintEvent *)
 
virtual QSize sizeHint() const 
 
void setSingleStep(int step)
Sets the unshifted step increment for the right-hand spin buttons, i.e. 
 
virtual void setMaximum(int val)
Sets the maximum value of the spin box. 
 
SpinMirror(ExtraSpinBox *, SpinBox *, QWidget *parent=0)
 
static bool isMirrorStyle(const QStyle *)
 
virtual void resizeEvent(QResizeEvent *)
 
virtual void setReadOnly(bool readOnly)
Sets whether the spin box can be changed by the user. 
 
int minimum() const 
Returns the minimum value of the spin box.