• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm/lib

  • sources
  • kde-4.12
  • kdepim
  • kalarm
  • lib
spinbox2.cpp
Go to the documentation of this file.
1 /*
2  * spinbox2.cpp - spin box with extra pair of spin buttons (for Qt 3)
3  * Program: kalarm
4  * Copyright © 2001-2009,2011 by David Jarvie <djarvie@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 
23 #include "spinbox2.moc"
24 #include "spinbox2_p.moc"
25 
26 #include <kdebug.h>
27 
28 #include <QMouseEvent>
29 #include <QStyleOptionSpinBox>
30 #include <QGraphicsPixmapItem>
31 #include <QPainter>
32 #include <QTimer>
33 #include <QFrame>
34 #include <QBrush>
35 #include <QStyle>
36 #include <QObject>
37 #include <QApplication>
38 #include <QPixmap>
39 #include <QMatrix>
40 
41 #include <stdlib.h>
42 
43 /* List of styles which look better using spin buttons mirrored left-to-right.
44  * This is needed for some styles which use rounded corners.
45  */
46 static const char* mirrorStyles[] = {
47  "QPlastiqueStyle", "QCleanlooksStyle",
48  0 // list terminator
49 };
50 static bool isMirrorStyle(const QStyle*);
51 static bool isOxygenStyle(const QWidget*);
52 static QRect spinBoxEditFieldRect(const QWidget*, const QStyleOptionSpinBox&);
53 
54 static inline QPixmap grabWidget(QWidget* w, QRect r = QRect())
55 {
56  QPixmap p(r.isEmpty() ? w->size() : r.size());
57  w->render(&p, QPoint(0,0), r, QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask);
58  return p;
59 }
60 
61 int SpinBox2::mRightToLeft = -1;
62 
63 SpinBox2::SpinBox2(QWidget* parent)
64  : QFrame(parent),
65  mReverseWithLayout(true)
66 {
67  mSpinboxFrame = new QFrame(this);
68  mUpdown2 = new ExtraSpinBox(this);
69 // mSpinbox = new MainSpinBox(0, 1, this, mSpinboxFrame);
70  mSpinbox = new MainSpinBox(this, mSpinboxFrame);
71  init();
72 }
73 
74 SpinBox2::SpinBox2(int minValue, int maxValue, int pageStep, QWidget* parent)
75  : QFrame(parent),
76  mReverseWithLayout(true)
77 {
78  mSpinboxFrame = new QFrame(this);
79  mUpdown2 = new ExtraSpinBox(minValue, maxValue, this);
80  mSpinbox = new MainSpinBox(minValue, maxValue, this, mSpinboxFrame);
81  setSteps(1, pageStep);
82  init();
83 }
84 
85 void SpinBox2::init()
86 {
87  if (mRightToLeft < 0)
88  mRightToLeft = QApplication::isRightToLeft() ? 1 : 0;
89  mMinValue = mSpinbox->minimum();
90  mMaxValue = mSpinbox->maximum();
91  mSingleStep = mSpinbox->singleStep();
92  mSingleShiftStep = mSpinbox->singleShiftStep();
93  mPageStep = mUpdown2->singleStep();
94  mPageShiftStep = mUpdown2->singleShiftStep();
95  mSpinbox->setSelectOnStep(false); // default
96  mUpdown2->setSelectOnStep(false); // always false
97  setFocusProxy(mSpinbox);
98  mUpdown2->setFocusPolicy(Qt::NoFocus);
99  mSpinMirror = new SpinMirror(mUpdown2, mSpinbox, this);
100  mSpinbox->installEventFilter(this);
101  mUpdown2->installEventFilter(this);
102  connect(mSpinbox, SIGNAL(valueChanged(int)), SLOT(valueChange()));
103  connect(mSpinbox, SIGNAL(valueChanged(int)), SIGNAL(valueChanged(int)));
104  connect(mSpinbox, SIGNAL(valueChanged(QString)), SIGNAL(valueChanged(QString)));
105  connect(mUpdown2, SIGNAL(stepped(int)), SLOT(stepPage(int)));
106  connect(mUpdown2, SIGNAL(painted()), SLOT(paintTimer()));
107 }
108 
109 void SpinBox2::setReadOnly(bool ro)
110 {
111  if (static_cast<int>(ro) != static_cast<int>(mSpinbox->isReadOnly()))
112  {
113  mSpinbox->setReadOnly(ro);
114  mUpdown2->setReadOnly(ro);
115  mSpinMirror->setReadOnly(ro);
116  }
117 }
118 
119 void SpinBox2::setReverseWithLayout(bool reverse)
120 {
121  if (reverse != mReverseWithLayout)
122  {
123  mReverseWithLayout = reverse;
124  setSteps(mSingleStep, mPageStep);
125  setShiftSteps(mSingleShiftStep, mPageShiftStep);
126  }
127 }
128 
129 void SpinBox2::setEnabled(bool enabled)
130 {
131  QFrame::setEnabled(enabled);
132  mSpinbox->setEnabled(enabled);
133  mUpdown2->setEnabled(enabled);
134  updateMirror();
135 }
136 
137 void SpinBox2::setWrapping(bool on)
138 {
139  mSpinbox->setWrapping(on);
140  mUpdown2->setWrapping(on);
141 }
142 
143 QRect SpinBox2::up2Rect() const
144 {
145  return mUpdown2->upRect();
146 }
147 
148 QRect SpinBox2::down2Rect() const
149 {
150  return mUpdown2->downRect();
151 }
152 
153 void SpinBox2::setSingleStep(int step)
154 {
155  mSingleStep = step;
156  if (reverseButtons())
157  mUpdown2->setSingleStep(step); // reverse layout, but still set the right buttons
158  else
159  mSpinbox->setSingleStep(step);
160 }
161 
162 void SpinBox2::setSteps(int single, int page)
163 {
164  mSingleStep = single;
165  mPageStep = page;
166  if (reverseButtons())
167  {
168  mUpdown2->setSingleStep(single); // reverse layout, but still set the right buttons
169  mSpinbox->setSingleStep(page);
170  }
171  else
172  {
173  mSpinbox->setSingleStep(single);
174  mUpdown2->setSingleStep(page);
175  }
176 }
177 
178 void SpinBox2::setShiftSteps(int single, int page)
179 {
180  mSingleShiftStep = single;
181  mPageShiftStep = page;
182  if (reverseButtons())
183  {
184  mUpdown2->setSingleShiftStep(single); // reverse layout, but still set the right buttons
185  mSpinbox->setSingleShiftStep(page);
186  }
187  else
188  {
189  mSpinbox->setSingleShiftStep(single);
190  mUpdown2->setSingleShiftStep(page);
191  }
192 }
193 
194 void SpinBox2::setButtonSymbols(QSpinBox::ButtonSymbols newSymbols)
195 {
196  if (mSpinbox->buttonSymbols() == newSymbols)
197  return;
198  mSpinbox->setButtonSymbols(newSymbols);
199  mUpdown2->setButtonSymbols(newSymbols);
200 }
201 
202 int SpinBox2::bound(int val) const
203 {
204  return (val < mMinValue) ? mMinValue : (val > mMaxValue) ? mMaxValue : val;
205 }
206 
207 void SpinBox2::setMinimum(int val)
208 {
209  mMinValue = val;
210  mSpinbox->setMinimum(val);
211  mUpdown2->setMinimum(val);
212 }
213 
214 void SpinBox2::setMaximum(int val)
215 {
216  mMaxValue = val;
217  mSpinbox->setMaximum(val);
218  mUpdown2->setMaximum(val);
219 }
220 
221 void SpinBox2::valueChange()
222 {
223  int val = mSpinbox->value();
224  bool blocked = mUpdown2->signalsBlocked();
225  mUpdown2->blockSignals(true);
226  mUpdown2->setValue(val);
227  mUpdown2->blockSignals(blocked);
228 }
229 
230 /******************************************************************************
231 * Called when the widget is about to be displayed.
232 * (At construction time, the spin button widths cannot be determined correctly,
233 * so we need to wait until now to definitively rearrange the widget.)
234 */
235 void SpinBox2::showEvent(QShowEvent*)
236 {
237  setUpdown2Size(); // set the new size of the second pair of spin buttons
238  arrange();
239  mSpinMirror->setFrame();
240 }
241 
242 QSize SpinBox2::sizeHint() const
243 {
244  getMetrics();
245  QSize size = mSpinbox->sizeHint();
246  size.setWidth(size.width() - wSpinboxHide + wUpdown2);
247  return size;
248 }
249 
250 QSize SpinBox2::minimumSizeHint() const
251 {
252  getMetrics();
253  QSize size = mSpinbox->minimumSizeHint();
254  size.setWidth(size.width() - wSpinboxHide + wUpdown2);
255  return size;
256 }
257 
258 void SpinBox2::styleChange(QStyle&)
259 {
260  setUpdown2Size(); // set the new size of the second pair of spin buttons
261  arrange();
262  mSpinMirror->setFrame();
263 }
264 
265 void SpinBox2::paintEvent(QPaintEvent* e)
266 {
267  QFrame::paintEvent(e);
268  QTimer::singleShot(0, this, SLOT(updateMirrorFrame()));
269 }
270 
271 void SpinBox2::paintTimer()
272 {
273  QTimer::singleShot(0, this, SLOT(updateMirrorButtons()));
274 }
275 
276 void SpinBox2::updateMirrorButtons()
277 {
278  mSpinMirror->setButtons();
279 }
280 
281 void SpinBox2::updateMirrorFrame()
282 {
283  mSpinMirror->setFrame();
284 }
285 
286 void SpinBox2::spinboxResized(QResizeEvent* e)
287 {
288  int h = e->size().height();
289  if (h != mUpdown2->height())
290  {
291  mUpdown2->setFixedSize(mUpdown2->width(), e->size().height());
292  setUpdown2Size();
293  }
294 }
295 
296 /******************************************************************************
297 * Set the size of the second spin button widget.
298 * It is necessary to fix the size to avoid infinite recursion in arrange().
299 */
300 void SpinBox2::setUpdown2Size()
301 {
302  mSpinMirror->setButtons();
303 }
304 
305 /******************************************************************************
306 * Called when the extra pair of spin buttons has repainted after a style change.
307 * Updates the mirror image of the spin buttons.
308 */
309 void SpinBox2::updateMirror()
310 {
311  mSpinMirror->setButtons();
312  mSpinMirror->setFrame();
313 }
314 
315 bool SpinBox2::eventFilter(QObject* obj, QEvent* e)
316 {
317  bool updateButtons = false;
318  if (obj == mSpinbox)
319  {
320 //if (e->type() != QEvent::Paint) kDebug()<<e->type();
321  switch (e->type())
322  {
323  case QEvent::Enter:
324  case QEvent::Leave:
325  QApplication::postEvent(mUpdown2, new QEvent(e->type()));
326  updateButtons = true;
327  break;
328  case QEvent::HoverEnter:
329  {
330  QHoverEvent* he = (QHoverEvent*)e;
331  QApplication::postEvent(mUpdown2, new QHoverEvent(e->type(), QPoint(1, he->pos().y()), he->oldPos()));
332  updateButtons = true;
333  break;
334  }
335  case QEvent::HoverLeave:
336  {
337  QHoverEvent* he = (QHoverEvent*)e;
338  QApplication::postEvent(mUpdown2, new QHoverEvent(e->type(), he->pos(), QPoint(1, he->oldPos().y())));
339  updateButtons = true;
340  break;
341  }
342  case QEvent::FocusIn:
343  case QEvent::FocusOut:
344  {
345  QFocusEvent* fe = (QFocusEvent*)e;
346  QApplication::postEvent(mUpdown2, new QFocusEvent(e->type(), fe->reason()));
347  updateButtons = true;
348  break;
349  }
350  default:
351  break;
352  }
353  }
354  else if (obj == mUpdown2)
355  {
356  switch (e->type())
357  {
358  case QEvent::Enter:
359  case QEvent::Leave:
360  case QEvent::HoverEnter:
361  case QEvent::HoverLeave:
362  case QEvent::EnabledChange:
363  updateButtons = true;
364  break;
365  default:
366  break;
367  }
368  }
369  if (updateButtons)
370  QTimer::singleShot(0, this, SLOT(updateMirrorButtons()));
371  return false;
372 }
373 
374 /******************************************************************************
375 * Set the positions and sizes of all the child widgets.
376 */
377 void SpinBox2::arrange()
378 {
379  getMetrics();
380  mUpdown2->move(-mUpdown2->width(), 0); // keep completely hidden
381  QRect arrowRect = style()->visualRect((mRightToLeft ? Qt::RightToLeft : Qt::LeftToRight), rect(), QRect(0, 0, wUpdown2, height()));
382  QRect r(wUpdown2, 0, width() - wUpdown2, height());
383  if (mRightToLeft)
384  r.moveLeft(0);
385  mSpinboxFrame->setGeometry(r);
386  mSpinbox->setGeometry(mRightToLeft ? 0 : -wSpinboxHide, 0, mSpinboxFrame->width() + wSpinboxHide, height());
387 // kDebug() << "arrowRect="<<arrowRect<<", mUpdown2="<<mUpdown2->geometry()<<", mSpinboxFrame="<<mSpinboxFrame->geometry()<<", mSpinbox="<<mSpinbox->geometry()<<", width="<<width();
388 
389  mSpinMirror->resize(wUpdown2, mUpdown2->height());
390  mSpinMirror->setGeometry(arrowRect);
391  mSpinMirror->setButtonPos(mButtonPos);
392  mSpinMirror->setButtons();
393 }
394 
395 /******************************************************************************
396 * Calculate the width and position of the extra pair of spin buttons.
397 * Style-specific adjustments are made for a better appearance.
398 */
399 void SpinBox2::getMetrics() const
400 {
401  QStyleOptionSpinBox option;
402  mUpdown2->initStyleOption(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); // Plastik excludes left border from spin widget rectangle
408  QRect r = spinBoxEditFieldRect(mSpinbox, option);
409  wSpinboxHide = mRightToLeft ? mSpinbox->style()->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxFrame).right() - r.right() : r.left();
410  QRect edRect = spinBoxEditFieldRect(mUpdown2, option);
411  int butx;
412  if (isMirrorStyle(udStyle))
413  {
414  if (mRightToLeft)
415  {
416  wUpdown2 = edRect.left();
417  butx = butRect.left();
418  }
419  else
420  {
421  int x = edRect.right() + 1;
422  wUpdown2 = mUpdown2->width() - x;
423  butx = butRect.left() - x;
424  }
425  }
426  else
427  {
428  r = udStyle->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxFrame);
429  if (mRightToLeft)
430  {
431  wUpdown2 = edRect.left() - r.left();
432  butx = wUpdown2 - (butRect.left() - r.left() + butRect.width());
433  }
434  else
435  {
436  wUpdown2 = r.width() - edRect.right() - 1;
437  butx = r.right() - butRect.right();
438  }
439  }
440  mButtonPos = QPoint(butx, butRect.top());
441 // kDebug() << "wUpdown2="<<wUpdown2<<", wSpinboxHide="<<wSpinboxHide<<", frame right="<<r.right() - butRect.right();
442 }
443 
444 /******************************************************************************
445 * Called when the extra pair of spin buttons is clicked to step the value.
446 * Normally this is a page step, but with a right-to-left language where the
447 * button functions are reversed, this is a line step.
448 */
449 void SpinBox2::stepPage(int step)
450 {
451  if (abs(step) == mUpdown2->singleStep())
452  mSpinbox->setValue(mUpdown2->value());
453  else
454  {
455  // It's a shift step
456  int oldValue = mSpinbox->value();
457  if (!reverseButtons())
458  {
459  // The button pairs have the normal function.
460  // Page shift stepping - step up or down to a multiple of the
461  // shift page increment, leaving unchanged the part of the value
462  // which is the remainder from the page increment.
463  if (oldValue >= 0)
464  oldValue -= oldValue % mUpdown2->singleStep();
465  else
466  oldValue += (-oldValue) % mUpdown2->singleStep();
467  }
468  int adjust = mSpinbox->shiftStepAdjustment(oldValue, step);
469  if (adjust == -step
470  && ((step > 0 && oldValue + step >= mSpinbox->maximum())
471  || (step < 0 && oldValue + step <= mSpinbox->minimum())))
472  adjust = 0; // allow stepping to the minimum or maximum value
473  mSpinbox->addValue(adjust + step);
474  }
475  mSpinbox->setFocus();
476  if (mSpinbox->selectOnStep())
477  mSpinbox->selectAll();
478 
479  // Make the covering arrows image show the pressed arrow
480  mSpinMirror->setButtons();
481 }
482 
483 
484 /*=============================================================================
485 = Class SpinBox2::MainSpinBox
486 =============================================================================*/
487 
488 /******************************************************************************
489 * Return the initial adjustment to the value for a shift step up or down, for
490 * the main (visible) spin box.
491 * Normally this is a line step, but with a right-to-left language where the
492 * button functions are reversed, this is a page step.
493 */
494 int SpinBox2::MainSpinBox::shiftStepAdjustment(int oldValue, int shiftStep)
495 {
496  if (owner->reverseButtons())
497  {
498  // The button pairs have the opposite function from normal.
499  // Page shift stepping - step up or down to a multiple of the
500  // shift page increment, leaving unchanged the part of the value
501  // which is the remainder from the page increment.
502  if (oldValue >= 0)
503  oldValue -= oldValue % singleStep();
504  else
505  oldValue += (-oldValue) % singleStep();
506  }
507  return SpinBox::shiftStepAdjustment(oldValue, shiftStep);
508 }
509 
510 
511 /*=============================================================================
512 = Class ExtraSpinBox
513 =============================================================================*/
514 
515 /******************************************************************************
516 * Repaint the widget.
517 */
518 void ExtraSpinBox::paintEvent(QPaintEvent* e)
519 {
520  SpinBox::paintEvent(e);
521  if (!mInhibitPaintSignal)
522  emit painted();
523  else
524  --mInhibitPaintSignal;
525 }
526 
527 
528 /*=============================================================================
529 = Class SpinMirror
530 =============================================================================*/
531 
532 SpinMirror::SpinMirror(ExtraSpinBox* spinbox, SpinBox* mainspin, QWidget* parent)
533  : QGraphicsView(parent),
534  mSpinbox(spinbox),
535  mMainSpinbox(mainspin),
536  mReadOnly(false),
537  mMirrored(false)
538 {
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);
548  mMirrored = isMirrorStyle(style());
549  setMirroredState();
550 }
551 
552 void SpinMirror::setMirroredState(bool clear)
553 {
554  // Some styles only look right when the buttons are mirrored
555  if (mMirrored)
556  setMatrix(QMatrix(-1, 0, 0, 1, width() - 1 , 0)); // mirror left to right
557  else if (clear)
558  setMatrix(QMatrix());
559 }
560 
561 void SpinMirror::setFrame()
562 {
563  // Paint the left hand frame of the main spinbox.
564  // Use the part to the left of the edit field, plus a slice at
565  // the left of the edit field stretched for the rest of the width.
566  // This avoids possibly grabbing text and displaying it in the
567  // spin button area.
568  QGraphicsScene* c = scene();
569  QStyleOptionSpinBox option;
570  option.initFrom(mMainSpinbox);
571  QRect r = spinBoxEditFieldRect(mMainSpinbox, option);
572  bool rtl = QApplication::isRightToLeft();
573  QPixmap p;
574  if (mMirrored)
575  {
576  int x = rtl ? 0 : mMainSpinbox->width() - width();
577  p = grabWidget(mMainSpinbox, QRect(x, 0, width(), height()));
578  }
579  else
580  {
581  // Grab a single pixel wide vertical slice through the main spinbox, between the
582  // frame and edit field.
583  bool oxygen = mMainSpinbox->style()->inherits("Oxygen::Style"); // KDE >= 4.4 Oxygen style
584  bool oxygen1 = mMainSpinbox->style()->inherits("OxygenStyle"); // KDE <= 4.3 Oxygen style
585  int editOffsetY = oxygen ? 5 : oxygen1 ? 6 : 2; // offset to edit field
586  int editOffsetX = (oxygen || oxygen1) ? (KDE::version() >= KDE_MAKE_VERSION(4,6,0) ? 4 : 2) : 2; // offset to edit field
587  int x = rtl ? r.right() - editOffsetX : r.left() + editOffsetX;
588  p = grabWidget(mMainSpinbox, QRect(x, 0, 1, height()));
589  // Blot out edit field stuff from the middle of the slice
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);
593  painter.end();
594  // Horizontally fill the mirror widget with the vertical slice
595  p = p.scaled(size());
596  // Grab the left hand border of the main spinbox, and draw it into the mirror widget.
597  QRect endr = rect();
598  if (rtl)
599  {
600  int mr = mMainSpinbox->width() - 1;
601  endr.setWidth(mr - r.right() + editOffsetX);
602  endr.moveRight(mr);
603  }
604  else
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);
608  }
609  c->setBackgroundBrush(p);
610 }
611 
612 void SpinMirror::setButtons()
613 {
614  mSpinbox->inhibitPaintSignal(2);
615  QStyleOptionSpinBox option;
616  mSpinbox->initStyleOption(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);
620  if (isOxygenStyle(mSpinbox))
621  {
622  // They don't use all their height, so shorten them to
623  // allow frame highlighting to work properly.
624  r.setTop(r.top() + 1);
625  r.setHeight(r.height() - 2);
626  }
627  mSpinbox->inhibitPaintSignal(1);
628  mButtons->setPixmap(grabWidget(mSpinbox, r));
629  mSpinbox->inhibitPaintSignal(0);
630 }
631 
632 void SpinMirror::setButtonPos(const QPoint& pos)
633 {
634  //kDebug()<<pos;
635  int x = pos.x();
636  int y = pos.y();
637  if (isOxygenStyle(this))
638  {
639  // Oxygen spin buttons don't use all their height. Prevent
640  // the top overlapping the frame highlighting. Their height
641  // is shortened in setButton() above.
642  ++y;
643  }
644  mButtons->setPos(x, y);
645 }
646 
647 void SpinMirror::resizeEvent(QResizeEvent* e)
648 {
649  QSize sz = e->size();
650  scene()->setSceneRect(0, 0, sz.width(), sz.height());
651  setMirroredState();
652 }
653 
654 void SpinMirror::styleChange(QStyle& st)
655 {
656  mMirrored = isMirrorStyle(&st);
657  setMirroredState(true);
658 }
659 
660 /******************************************************************************
661 * Pass on to the extra spinbox all mouse events which occur over the spin
662 * button area.
663 */
664 void SpinMirror::mouseEvent(QMouseEvent* e)
665 {
666  if (mReadOnly)
667  return;
668  QPoint pt = e->pos();
669  QGraphicsItem* item = scene()->itemAt(pt);
670  if (item == mButtons)
671  pt = spinboxPoint(pt);
672  else
673  pt = QPoint(0, 0); // allow auto-repeat to stop
674  QApplication::postEvent(mSpinbox, new QMouseEvent(e->type(), pt, e->button(), e->buttons(), e->modifiers()));
675 }
676 
677 /******************************************************************************
678 * Pass on to the extra spinbox all wheel events which occur over the spin
679 * button area.
680 */
681 void SpinMirror::wheelEvent(QWheelEvent* e)
682 {
683  if (mReadOnly)
684  return;
685  QPoint pt = e->pos();
686  QGraphicsItem* item = scene()->itemAt(pt);
687  if (item == mButtons)
688  {
689  pt = spinboxPoint(pt);
690  QApplication::postEvent(mSpinbox, new QWheelEvent(pt, e->delta(), e->buttons(), e->modifiers()), e->orientation());
691  }
692 }
693 
694 /******************************************************************************
695 * Translate SpinMirror coordinates to those of the mirrored spinbox.
696 */
697 QPoint SpinMirror::spinboxPoint(const QPoint& param) const
698 {
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());
704  return pt;
705 }
706 
707 /******************************************************************************
708 * Pass on to the main spinbox events which are needed to activate mouseover and
709 * other graphic effects when the mouse cursor enters and leaves the widget.
710 */
711 bool SpinMirror::event(QEvent* e)
712 {
713 //kDebug()<<e->type();
714  QHoverEvent *he = 0;
715  switch (e->type())
716  {
717  case QEvent::Leave:
718  if (mMainSpinbox->rect().contains(mMainSpinbox->mapFromGlobal(QCursor::pos())))
719  break;
720  // fall through to QEvent::Enter
721  case QEvent::Enter:
722  QApplication::postEvent(mMainSpinbox, new QEvent(e->type()));
723  break;
724  case QEvent::HoverLeave:
725  he = (QHoverEvent*)e;
726  if (mMainSpinbox->rect().contains(mMainSpinbox->mapFromGlobal(QCursor::pos())))
727  break;
728  // fall through to QEvent::HoverEnter
729  case QEvent::HoverEnter:
730  he = (QHoverEvent*)e;
731  QApplication::postEvent(mMainSpinbox, new QHoverEvent(e->type(), he->pos(), he->oldPos()));
732  break;
733  case QEvent::HoverMove:
734  he = (QHoverEvent*)e;
735  break;
736  case QEvent::FocusIn:
737  mMainSpinbox->setFocus();
738  break;
739  default:
740  break;
741  }
742 
743  if (he)
744  {
745  QApplication::postEvent(mSpinbox, new QHoverEvent(e->type(), spinboxPoint(he->pos()), spinboxPoint(he->oldPos())));
746  setButtons();
747  }
748 
749  return QGraphicsView::event(e);
750 }
751 
752 
753 /******************************************************************************
754 * Determine whether the extra pair of spin buttons needs to be mirrored
755 * left-to-right in the specified style.
756 */
757 static bool isMirrorStyle(const QStyle* style)
758 {
759  for (const char** s = mirrorStyles; *s; ++s)
760  if (style->inherits(*s))
761  return true;
762  return false;
763 }
764 
765 static bool isOxygenStyle(const QWidget* w)
766 {
767  return w->style()->inherits("Oxygen::Style") || w->style()->inherits("OxygenStyle");
768 }
769 
770 static QRect spinBoxEditFieldRect(const QWidget* w, const QStyleOptionSpinBox& option)
771 {
772  QRect r = w->style()->subControlRect(QStyle::CC_SpinBox, &option, QStyle::SC_SpinBoxEditField);
773  if (isOxygenStyle(w))
774  {
775  int xadjust = (KDE::version() >= KDE_MAKE_VERSION(4,6,0)) ? 3 : 2;
776  r.adjust(xadjust, 2, -xadjust, -2);
777  }
778  return r;
779 }
780 
781 // vim: et sw=4:
SpinMirror::setButtons
void setButtons()
Definition: spinbox2.cpp:612
SpinMirror::setReadOnly
void setReadOnly(bool ro)
Definition: spinbox2_p.h:73
SpinBox2::setEnabled
virtual void setEnabled(bool enabled)
Sets whether the widget is enabled.
Definition: spinbox2.cpp:129
ExtraSpinBox::paintEvent
virtual void paintEvent(QPaintEvent *)
Definition: spinbox2.cpp:518
SpinBox::singleStep
int singleStep() const
Returns the unshifted step increment, i.e.
Definition: spinbox.h:89
SpinBox2::stepPage
virtual void stepPage(int)
Definition: spinbox2.cpp:449
SpinBox::upRect
QRect upRect() const
Returns the rectangle containing the up arrow.
Definition: spinbox.cpp:494
SpinMirror
Definition: spinbox2_p.h:68
SpinBox2::down2Rect
QRect down2Rect() const
Returns the geometry of the left-hand "down" button.
Definition: spinbox2.cpp:148
ExtraSpinBox::inhibitPaintSignal
void inhibitPaintSignal(int i)
Definition: spinbox2_p.h:47
SpinBox2::up2Rect
QRect up2Rect() const
Returns the geometry of the left-hand "up" button.
Definition: spinbox2.cpp:143
SpinMirror::setFrame
void setFrame()
Definition: spinbox2.cpp:561
SpinBox::downRect
QRect downRect() const
Returns the rectangle containing the down arrow.
Definition: spinbox.cpp:501
SpinBox2::wSpinboxHide
int wSpinboxHide
Definition: spinbox2.h:249
SpinBox::initStyleOption
void initStyleOption(QStyleOptionSpinBox &) const
Initialise a QStyleOptionSpinBox with this instance's details.
Definition: spinbox.cpp:529
SpinBox2::getMetrics
virtual void getMetrics() const
Definition: spinbox2.cpp:399
SpinBox::setSingleStep
void setSingleStep(int step)
Sets the unshifted step increment, i.e.
Definition: spinbox.cpp:106
QWidget
SpinBox::setSelectOnStep
void setSelectOnStep(bool sel)
Sets whether the spin box value text should be selected when its value is stepped.
Definition: spinbox.h:67
SpinBox2::SpinBox2
SpinBox2(QWidget *parent=0)
Constructor.
Definition: spinbox2.cpp:63
SpinBox2::setSteps
void setSteps(int line, int page)
Sets the unshifted step increments for the two pairs of spin buttons, i.e.
Definition: spinbox2.cpp:162
SpinBox::paintEvent
virtual void paintEvent(QPaintEvent *)
Definition: spinbox.cpp:516
QObject
SpinBox::setMinimum
void setMinimum(int val)
Sets the minimum value of the spin box.
Definition: spinbox.cpp:92
grabWidget
static QPixmap grabWidget(QWidget *w, QRect r=QRect())
Definition: spinbox2.cpp:54
spinBoxEditFieldRect
static QRect spinBoxEditFieldRect(const QWidget *, const QStyleOptionSpinBox &)
Definition: spinbox2.cpp:770
mirrorStyles
static const char * mirrorStyles[]
Definition: spinbox2.cpp:46
SpinBox2::setWrapping
void setWrapping(bool on)
Sets whether it is possible to step the value from the highest value to the lowest value and vice ver...
Definition: spinbox2.cpp:137
SpinBox2::setReverseWithLayout
void setReverseWithLayout(bool reverse)
Sets whether the spin button pairs should be reversed for a right-to-left language.
Definition: spinbox2.cpp:119
SpinMirror::event
virtual bool event(QEvent *)
Definition: spinbox2.cpp:711
SpinBox2::reverseButtons
bool reverseButtons() const
Returns whether the spin button pairs will be reversed for a right-to-left language.
Definition: spinbox2.h:85
SpinBox2::setShiftSteps
void setShiftSteps(int line, int page)
Sets the shifted step increments for the two pairs of spin buttons, i.e.
Definition: spinbox2.cpp:178
SpinMirror::styleChange
virtual void styleChange(QStyle &)
Definition: spinbox2.cpp:654
SpinBox2::setButtonSymbols
void setButtonSymbols(QSpinBox::ButtonSymbols)
Sets the button symbols to use (arrows or plus/minus).
Definition: spinbox2.cpp:194
SpinBox2::valueChange
virtual void valueChange()
Definition: spinbox2.cpp:221
SpinBox2::valueChanged
void valueChanged(int value)
Signal which is emitted whenever the value of the spin box changes.
ExtraSpinBox::painted
void painted()
SpinBox2::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the spin box can be changed by the user.
Definition: spinbox2.cpp:109
isOxygenStyle
static bool isOxygenStyle(const QWidget *)
Definition: spinbox2.cpp:765
SpinBox2::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: spinbox2.cpp:250
SpinBox::shiftStepAdjustment
virtual int shiftStepAdjustment(int oldValue, int shiftStep)
Returns the initial adjustment to the value for a shift step up or down.
Definition: spinbox.cpp:459
SpinMirror::wheelEvent
virtual void wheelEvent(QWheelEvent *)
Definition: spinbox2.cpp:681
SpinMirror::setButtonPos
void setButtonPos(const QPoint &)
Definition: spinbox2.cpp:632
SpinBox
Spin box with accelerated shift key stepping and read-only option.
Definition: spinbox.h:44
SpinBox2::setMinimum
virtual void setMinimum(int val)
Sets the minimum value of the spin box.
Definition: spinbox2.cpp:207
SpinBox::setMaximum
void setMaximum(int val)
Sets the maximum value of the spin box.
Definition: spinbox.cpp:99
SpinBox2::bound
int bound(int val) const
Returns the specified value clamped to the range of the spin box.
Definition: spinbox2.cpp:202
SpinBox::setSingleShiftStep
void setSingleShiftStep(int step)
Sets the shifted step increment, i.e.
Definition: spinbox.cpp:113
SpinBox::singleShiftStep
int singleShiftStep() const
Returns the shifted step increment, i.e.
Definition: spinbox.h:97
SpinBox2::styleChange
virtual void styleChange(QStyle &)
Definition: spinbox2.cpp:258
SpinBox2::MainSpinBox
friend class MainSpinBox
Definition: spinbox2.h:310
SpinBox2::showEvent
virtual void showEvent(QShowEvent *)
Definition: spinbox2.cpp:235
SpinBox2::paintEvent
virtual void paintEvent(QPaintEvent *)
Definition: spinbox2.cpp:265
SpinBox2::sizeHint
virtual QSize sizeHint() const
Definition: spinbox2.cpp:242
QGraphicsView
SpinBox2::setSingleStep
void setSingleStep(int step)
Sets the unshifted step increment for the right-hand spin buttons, i.e.
Definition: spinbox2.cpp:153
SpinBox2::setMaximum
virtual void setMaximum(int val)
Sets the maximum value of the spin box.
Definition: spinbox2.cpp:214
SpinBox2::mButtonPos
QPoint mButtonPos
Definition: spinbox2.h:250
SpinMirror::SpinMirror
SpinMirror(ExtraSpinBox *, SpinBox *, QWidget *parent=0)
Definition: spinbox2.cpp:532
isMirrorStyle
static bool isMirrorStyle(const QStyle *)
Definition: spinbox2.cpp:757
QFrame
ExtraSpinBox
Definition: spinbox2_p.h:39
SpinMirror::resizeEvent
virtual void resizeEvent(QResizeEvent *)
Definition: spinbox2.cpp:647
SpinBox::setReadOnly
virtual void setReadOnly(bool readOnly)
Sets whether the spin box can be changed by the user.
Definition: spinbox.cpp:76
SpinBox2::minimum
int minimum() const
Returns the minimum value of the spin box.
Definition: spinbox2.h:132
SpinBox2::wUpdown2
int wUpdown2
Definition: spinbox2.h:248
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm/lib

Skip menu "kalarm/lib"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal