not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / desktop / positioningruler.cpp
blob4cee25e868af844f3cfe0cb0a70e8cbcbfc16b02
1 /*
2 * Copyright 2008 Marco Martin <notmart@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "positioningruler.h"
22 #include <QPainter>
23 #include <QBoxLayout>
24 #include <QMouseEvent>
25 #include <QToolButton>
26 #include <QApplication>
27 #include <QDesktopWidget>
28 #include <QToolTip>
30 #include <KIcon>
31 #include <KColorUtils>
32 #include <KWindowSystem>
34 #include <Plasma/Theme>
35 #include <Plasma/FrameSvg>
36 #include <Plasma/Containment>
39 class PositioningRuler::Private
41 public:
42 Private()
43 : location(Plasma::BottomEdge),
44 alignment(Qt::AlignLeft),
45 dragging(NoElement),
46 startDragPos(0,0),
47 offset(0),
48 minLength(0),
49 maxLength(0),
50 availableLength(0),
51 leftMaxSliderRect(QRect(0,0,0,0)),
52 rightMaxSliderRect(QRect(0,0,0,0)),
53 leftMinSliderRect(QRect(0,0,0,0)),
54 rightMinSliderRect(QRect(0,0,0,0)),
55 offsetSliderRect(QRect(0,0,0,0)),
56 sliderGraphics(0),
57 elementPrefix(QString()),
58 minimumBound(40)
62 ~Private()
66 bool moveSlider(QRect &sliderRect, QRect &symmetricSliderRect, const QPoint &newPos)
68 if (location == Plasma::LeftEdge || location == Plasma::RightEdge) {
69 if (newPos.y() < 0 || newPos.y() > availableLength) {
70 return false;
73 if (alignment == Qt::AlignCenter) {
74 int newTop = offsetSliderRect.center().y() + (offsetSliderRect.center().y() - newPos.y());
75 newTop = qBound(0, newTop, availableLength);
77 symmetricSliderRect.moveCenter(QPoint(symmetricSliderRect.center().x(), newTop));
79 sliderRect.moveCenter(QPoint(sliderRect.center().x(), newPos.y()));
80 } else {
81 if (newPos.x() < 0 || newPos.x() > availableLength) {
82 return false;
85 if (alignment == Qt::AlignCenter) {
86 int newLeft = offsetSliderRect.center().x() + (offsetSliderRect.center().x() - newPos.x());
87 newLeft = qBound(0, newLeft, availableLength);
89 symmetricSliderRect.moveCenter(QPoint(newLeft, symmetricSliderRect.center().y()));
91 sliderRect.moveCenter(QPoint(newPos.x(), sliderRect.center().y()));
94 return true;
97 int sliderRectToLength(const QRect &sliderRect)
99 int sliderPos;
100 int offsetPos;
102 if (location == Plasma::LeftEdge || location == Plasma::RightEdge) {
103 sliderPos = sliderRect.center().y();
104 offsetPos = offsetSliderRect.center().y();
105 } else {
106 sliderPos = sliderRect.center().x();
107 offsetPos = offsetSliderRect.center().x();
110 if (alignment == Qt::AlignCenter) {
111 return 2 * qAbs(sliderPos - offsetPos);
112 } else {
113 return qAbs(sliderPos - offsetPos);
117 void loadSlidersGraphics()
119 QString elementPrefix;
121 switch (location) {
122 case Plasma::LeftEdge:
123 elementPrefix = "west-";
124 break;
125 case Plasma::RightEdge:
126 elementPrefix = "east-";
127 break;
128 case Plasma::TopEdge:
129 elementPrefix = "north-";
130 break;
131 case Plasma::BottomEdge:
132 default:
133 elementPrefix = "south-";
134 break;
137 leftMaxSliderRect.setSize(sliderGraphics->elementSize(elementPrefix + "maxslider"));
138 leftMinSliderRect.setSize(sliderGraphics->elementSize(elementPrefix + "minslider"));
139 rightMaxSliderRect.setSize(leftMinSliderRect.size());
140 rightMinSliderRect.setSize(leftMaxSliderRect.size());
141 offsetSliderRect.setSize(sliderGraphics->elementSize(elementPrefix + "offsetslider"));
144 void setupSliders(const QSize &totalSize)
146 //FIXME: the following is waay too clumsy...
148 int rightMaxPos;
149 int leftMaxPos;
150 int rightMinPos;
151 int leftMinPos;
152 int offsetPos;
154 int totalLength;
155 if (location == Plasma::LeftEdge || location == Plasma::RightEdge) {
156 totalLength = totalSize.height();
157 } else {
158 totalLength = totalSize.width();
161 switch (alignment) {
162 case Qt::AlignLeft:
163 // Here subtracting one to everything because QRect.moveCenter(pos) moves the rect with
164 // the width/2 th pixel at pos.x (and so for y) resulting in the painted image moved
165 // one pixel to the right
166 rightMaxPos = offset + maxLength;
167 leftMaxPos = 0;
168 rightMinPos = offset + minLength;
169 leftMinPos = 0;
170 offsetPos = offset;
171 break;
172 case Qt::AlignRight:
173 leftMaxPos = totalLength - offset - maxLength;
174 rightMaxPos = 0;
175 leftMinPos = totalLength - offset - minLength;
176 rightMinPos = 0;
177 offsetPos = totalLength - offset;
178 break;
179 case Qt::AlignCenter:
180 default:
181 leftMaxPos = totalLength/2 + offset - maxLength/2;
182 rightMaxPos = totalLength/2 + offset + maxLength/2;
184 leftMinPos = totalLength/2 + offset - minLength/2;
185 rightMinPos = totalLength/2 + offset + minLength/2;
187 offsetPos = totalLength/2 + offset;
188 break;
191 switch (location) {
192 case Plasma::LeftEdge:
193 leftMaxSliderRect.moveCenter(QPoint(3*(totalSize.width()/4), leftMaxPos));
194 rightMaxSliderRect.moveCenter(QPoint(3*(totalSize.width()/4), rightMaxPos));
196 leftMinSliderRect.moveCenter(QPoint(totalSize.width()/4, leftMinPos));
197 rightMinSliderRect.moveCenter(QPoint(totalSize.width()/4, rightMinPos));
199 offsetSliderRect.moveCenter(QPoint(3*(totalSize.width()/4), offsetPos));
200 break;
201 case Plasma::RightEdge:
202 leftMaxSliderRect.moveCenter(QPoint(totalSize.width()/4, leftMaxPos));
203 rightMaxSliderRect.moveCenter(QPoint(totalSize.width()/4, rightMaxPos));
205 leftMinSliderRect.moveCenter(QPoint(3*(totalSize.width()/4), leftMinPos));
206 rightMinSliderRect.moveCenter(QPoint(3*(totalSize.width()/4), rightMinPos));
208 offsetSliderRect.moveCenter(QPoint(totalSize.width()/4, offsetPos));
209 break;
210 case Plasma::TopEdge:
211 leftMaxSliderRect.moveCenter(QPoint(leftMaxPos, 3*(totalSize.height()/4)));
212 rightMaxSliderRect.moveCenter(QPoint(rightMaxPos, 3*(totalSize.height()/4)));
214 leftMinSliderRect.moveCenter(QPoint(leftMinPos, totalSize.height()/4));
215 rightMinSliderRect.moveCenter(QPoint(rightMinPos, totalSize.height()/4));
217 offsetSliderRect.moveCenter(QPoint(offsetPos, 3*(totalSize.height()/4)));
218 break;
219 case Plasma::BottomEdge:
220 default:
221 leftMaxSliderRect.moveCenter(QPoint(leftMaxPos, totalSize.height()/4));
222 rightMaxSliderRect.moveCenter(QPoint(rightMaxPos, totalSize.height()/4));
224 leftMinSliderRect.moveCenter(QPoint(leftMinPos, 3*(totalSize.height()/4)));
225 rightMinSliderRect.moveCenter(QPoint(rightMinPos, 3*(totalSize.height()/4)));
227 offsetSliderRect.moveCenter(QPoint(offsetPos, totalSize.height()/4));
228 break;
233 enum SubElement { NoElement = 0,
234 LeftMaxSlider,
235 RightMaxSlider,
236 LeftMinSlider,
237 RightMinSlider,
238 OffsetSlider
241 Plasma::Location location;
242 Qt::Alignment alignment;
243 SubElement dragging;
244 QPoint startDragPos;
245 int offset;
246 int minLength;
247 int maxLength;
248 int availableLength;
249 QRect leftMaxSliderRect;
250 QRect rightMaxSliderRect;
251 QRect leftMinSliderRect;
252 QRect rightMinSliderRect;
253 QRect offsetSliderRect;
254 Plasma::FrameSvg *sliderGraphics;
255 QString elementPrefix;
257 int minimumBound;
260 PositioningRuler::PositioningRuler(QWidget* parent)
261 : QWidget(parent),
262 d(new Private())
264 d->sliderGraphics = new Plasma::FrameSvg(this);
265 d->sliderGraphics->setImagePath("widgets/containment-controls");
267 d->loadSlidersGraphics();
270 PositioningRuler::~PositioningRuler()
272 delete d;
275 QSize PositioningRuler::sizeHint() const
277 //FIXME:why must add 6 pixels????
279 switch (d->location) {
280 case Plasma::LeftEdge:
281 case Plasma::RightEdge:
282 return QSize(d->leftMaxSliderRect.width() + d->leftMinSliderRect.width() + 6, d->availableLength);
283 break;
284 case Plasma::TopEdge:
285 case Plasma::BottomEdge:
286 default:
287 return QSize(d->availableLength, d->leftMaxSliderRect.height() + d->leftMinSliderRect.height() + 6);
288 break;
293 void PositioningRuler::setLocation(const Plasma::Location &loc)
295 if (d->location == loc) {
296 return;
299 d->location = loc;
301 d->setupSliders(size());
302 d->loadSlidersGraphics();
304 update();
307 Plasma::Location PositioningRuler::location() const
309 return d->location;
312 void PositioningRuler::setAlignment(const Qt::Alignment &align)
314 if (d->alignment == align) {
315 return;
318 d->alignment = align;
320 d->setupSliders(size());
322 update();
325 Qt::Alignment PositioningRuler::alignment() const
327 return d->alignment;
330 void PositioningRuler::setOffset(int newOffset)
332 d->offset = newOffset;
334 d->setupSliders(size());
335 update();
338 int PositioningRuler::offset() const
340 return d->offset;
343 void PositioningRuler::setMaxLength(int newMax)
345 int deltaX;
346 int deltaY;
348 switch (d->location) {
349 case Plasma::LeftEdge:
350 case Plasma::RightEdge:
351 deltaX = 0;
352 deltaY = newMax - d->maxLength;
353 break;
354 case Plasma::TopEdge:
355 case Plasma::BottomEdge:
356 default:
357 deltaX = newMax - d->maxLength;
358 deltaY = 0;
359 break;
362 if (d->alignment == Qt::AlignLeft) {
363 d->rightMaxSliderRect.moveCenter(QPoint(d->rightMaxSliderRect.center().x() + deltaX,
364 d->rightMaxSliderRect.center().y() + deltaY));
365 } else if (d->alignment == Qt::AlignRight) {
366 d->leftMaxSliderRect.moveCenter(QPoint(d->leftMaxSliderRect.center().x() - deltaX,
367 d->leftMaxSliderRect.center().y() - deltaY));
368 //center
369 } else {
370 d->rightMaxSliderRect.moveCenter(QPoint(d->rightMaxSliderRect.center().x() + deltaX/2,
371 d->rightMaxSliderRect.center().y() + deltaY/2));
372 d->leftMaxSliderRect.moveCenter(QPoint(d->leftMaxSliderRect.center().x() - deltaX/2,
373 d->leftMaxSliderRect.center().y() - deltaY/2));
376 d->maxLength = newMax;
377 if (d->minLength > d->maxLength) {
378 setMinLength(newMax);
380 update();
383 int PositioningRuler::maxLength() const
385 return d->maxLength;
388 void PositioningRuler::setMinLength(int newMin)
390 int deltaX;
391 int deltaY;
393 int min = qMax(d->minimumBound, newMin);
395 switch (d->location) {
396 case Plasma::LeftEdge:
397 case Plasma::RightEdge:
398 deltaX = 0;
399 deltaY = min - d->minLength;
400 break;
401 case Plasma::TopEdge:
402 case Plasma::BottomEdge:
403 default:
404 deltaX = min - d->minLength;
405 deltaY = 0;
406 break;
410 if (d->alignment == Qt::AlignLeft) {
411 d->rightMinSliderRect.moveCenter(QPoint(d->rightMinSliderRect.center().x() + deltaX,
412 d->rightMinSliderRect.center().y() + deltaY));
413 } else if (d->alignment == Qt::AlignRight) {
414 d->leftMinSliderRect.moveCenter(QPoint(d->leftMinSliderRect.center().x() - deltaX,
415 d->leftMinSliderRect.center().y() - deltaY));
416 //center
417 } else {
418 d->rightMinSliderRect.moveCenter(QPoint(d->rightMinSliderRect.center().x() + deltaX/2,
419 d->rightMinSliderRect.center().y() + deltaY/2));
420 d->leftMinSliderRect.moveCenter(QPoint(d->leftMinSliderRect.center().x() - deltaX/2,
421 d->leftMinSliderRect.center().y() - deltaY/2));
424 d->minLength = min;
425 if (d->minLength > d->maxLength) {
426 setMaxLength(min);
428 update();
431 int PositioningRuler::minLength() const
433 return d->minLength;
436 void PositioningRuler::setAvailableLength(int newAvail)
438 d->availableLength = newAvail;
440 if (d->maxLength > newAvail) {
441 setMaxLength(newAvail);
444 if (d->minLength > newAvail) {
445 setMinLength(newAvail);
449 int PositioningRuler::availableLength() const
451 return d->availableLength;
454 bool PositioningRuler::event(QEvent *ev)
456 //Show a different tooltip on each slider
457 if (ev->type() == QEvent::ToolTip) {
458 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(ev);
459 if (d->offsetSliderRect.contains(helpEvent->pos())) {
460 QToolTip::showText(helpEvent->globalPos(), i18n("Move this slider to set the panel position"), this);
461 } else if ((d->alignment != Qt::AlignLeft && d->leftMaxSliderRect.contains(helpEvent->pos())) ||
462 (d->alignment != Qt::AlignRight && d->rightMaxSliderRect.contains(helpEvent->pos()))) {
463 QToolTip::showText(helpEvent->globalPos(), i18n("Move this slider to set the maximum panel size"), this);
464 } else if ((d->alignment != Qt::AlignLeft && d->leftMinSliderRect.contains(helpEvent->pos())) ||
465 (d->alignment != Qt::AlignRight && d->rightMinSliderRect.contains(helpEvent->pos()))) {
466 QToolTip::showText(helpEvent->globalPos(), i18n("Move this slider to set the minimum panel size"), this);
469 return true;
470 } else {
471 return QWidget::event(ev);
475 void PositioningRuler::paintEvent(QPaintEvent *event)
477 //Draw background
478 d->sliderGraphics->setElementPrefix(d->location);
480 QPainter painter(this);
482 d->sliderGraphics->resizeFrame(event->rect().size());
483 d->sliderGraphics->paintFrame(&painter);
485 //Draw center indicators
486 if (d->alignment == Qt::AlignCenter && (d->location == Plasma::LeftEdge || d->location == Plasma::RightEdge)) {
487 QSize indicatorSize = d->sliderGraphics->elementSize("vertical-centerindicator");
488 d->sliderGraphics->paint(&painter, QRect(QPoint(event->rect().left(), event->rect().center().y()), indicatorSize), "vertical-centerindicator");
489 //this because rect.moveCenter will cause a rect moved one pixel off respect where we need it
490 painter.translate(0, -1);
491 } else if (d->alignment == Qt::AlignCenter) {
492 QSize indicatorSize = d->sliderGraphics->elementSize("horizontal-centerindicator");
493 d->sliderGraphics->paint(&painter, QRect(QPoint(event->rect().center().x(), event->rect().top()), indicatorSize), "horizontal-centerindicator");
494 painter.translate(-1, 0);
497 //Draw handles
498 QString elementPrefix;
500 switch (d->location) {
501 case Plasma::LeftEdge:
502 elementPrefix = "west-";
503 break;
504 case Plasma::RightEdge:
505 elementPrefix = "east-";
506 break;
507 case Plasma::TopEdge:
508 elementPrefix = "north-";
509 break;
510 case Plasma::BottomEdge:
511 default:
512 elementPrefix = "south-";
513 break;
516 if (d->alignment != Qt::AlignLeft) {
517 painter.save();
518 QTransform transform;
520 switch (d->location) {
521 case Plasma::LeftEdge:
522 case Plasma::RightEdge:
523 transform.scale(1, -1);
524 break;
525 case Plasma::TopEdge:
526 case Plasma::BottomEdge:
527 default:
528 transform.scale(-1, 1);
529 break;
532 painter.setTransform(transform);
533 d->sliderGraphics->paint(&painter, transform.mapRect(d->leftMaxSliderRect), elementPrefix + "maxslider");
534 d->sliderGraphics->paint(&painter, transform.mapRect(d->leftMinSliderRect), elementPrefix + "minslider");
535 painter.restore();
538 if (d->alignment != Qt::AlignRight) {
539 d->sliderGraphics->paint(&painter, d->rightMaxSliderRect, elementPrefix + "maxslider");
540 d->sliderGraphics->paint(&painter, d->rightMinSliderRect, elementPrefix + "minslider");
543 d->sliderGraphics->paint(&painter, d->offsetSliderRect, elementPrefix + "offsetslider");
546 void PositioningRuler::wheelEvent(QWheelEvent *event)
548 QPoint movement;
549 int hMargins = 0;
550 int vMargins = 0;
552 if (d->location == Plasma::LeftEdge || d->location == Plasma::RightEdge) {
553 vMargins = 200;
554 if (event->delta() < 0) {
555 movement = QPoint(0, 20);
556 } else {
557 movement = QPoint(0, -20);
559 } else {
560 hMargins = 100;
561 if (event->delta() < 0) {
562 movement = QPoint(20, 0);
563 } else {
564 movement = QPoint(-20, 0);
568 if (d->alignment != Qt::AlignLeft && d->leftMaxSliderRect.adjusted(-hMargins, -vMargins, hMargins, vMargins).contains(event->pos())) {
569 d->dragging = Private::LeftMaxSlider;
570 movement += d->leftMaxSliderRect.center();
571 } else if (d->alignment != Qt::AlignRight && d->rightMaxSliderRect.adjusted(-hMargins, -vMargins, hMargins, vMargins).contains(event->pos())) {
572 d->dragging = Private::RightMaxSlider;
573 movement += d->rightMaxSliderRect.center();
574 } else if (d->alignment != Qt::AlignLeft && d->leftMinSliderRect.adjusted(-hMargins, -vMargins, hMargins, vMargins).contains(event->pos())) {
575 d->dragging = Private::LeftMinSlider;
576 movement += d->leftMinSliderRect.center();
577 } else if (d->alignment != Qt::AlignRight && d->rightMinSliderRect.adjusted(-hMargins, -vMargins, hMargins, vMargins).contains(event->pos())) {
578 d->dragging = Private::RightMinSlider;
579 movement += d->rightMinSliderRect.center();
580 } else if (d->offsetSliderRect.adjusted(-hMargins, -vMargins, hMargins, vMargins).contains(event->pos())) {
581 d->dragging = Private::OffsetSlider;
582 movement += d->offsetSliderRect.center();
583 } else {
584 d->dragging = Private::NoElement;
587 //do a fake mouse move event that drags the slider around
588 if (d->dragging != Private::NoElement) {
589 d->startDragPos = QPoint(0,0);
590 QMouseEvent mouseEvent(QEvent::MouseMove, movement, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
591 mouseMoveEvent(&mouseEvent);
592 d->dragging = Private::NoElement;
596 void PositioningRuler::mousePressEvent(QMouseEvent *event)
598 if (d->alignment != Qt::AlignLeft && d->leftMaxSliderRect.contains(event->pos())) {
599 d->dragging = Private::LeftMaxSlider;
600 d->startDragPos = QPoint(event->pos().x() - d->leftMaxSliderRect.center().x(), event->pos().y() - d->leftMaxSliderRect.center().y());
601 } else if (d->alignment != Qt::AlignRight && d->rightMaxSliderRect.contains(event->pos())) {
602 d->dragging = Private::RightMaxSlider;
603 d->startDragPos = QPoint(event->pos().x() - d->rightMaxSliderRect.center().x(), event->pos().y() - d->rightMaxSliderRect.center().y());
604 } else if (d->alignment != Qt::AlignLeft && d->leftMinSliderRect.contains(event->pos())) {
605 d->dragging = Private::LeftMinSlider;
606 d->startDragPos = QPoint(event->pos().x() - d->leftMinSliderRect.center().x(), event->pos().y() - d->leftMinSliderRect.center().y());
607 } else if (d->alignment != Qt::AlignRight && d->rightMinSliderRect.contains(event->pos())) {
608 d->dragging = Private::RightMinSlider;
609 d->startDragPos = QPoint(event->pos().x() - d->rightMinSliderRect.center().x(), event->pos().y() - d->rightMinSliderRect.center().y());
610 } else if (d->offsetSliderRect.contains(event->pos())) {
611 d->dragging = Private::OffsetSlider;
612 d->startDragPos = QPoint(event->pos().x() - d->offsetSliderRect.center().x(), event->pos().y() - d->offsetSliderRect.center().y());
613 } else {
614 d->dragging = Private::NoElement;
617 QWidget::mousePressEvent(event);
620 void PositioningRuler::mouseReleaseEvent(QMouseEvent *event)
622 Q_UNUSED(event)
624 d->startDragPos = QPoint(0, 0);
625 d->dragging = Private::NoElement;
628 void PositioningRuler::mouseMoveEvent(QMouseEvent *event)
630 if (d->dragging == Private::NoElement) {
631 return;
634 //bound to width, height
635 QPoint newPos = QPoint(qMin(event->pos().x() - d->startDragPos.x(), width()),
636 qMin(event->pos().y() - d->startDragPos.y(), height()));
637 //bound to 0,0
638 newPos = QPoint(qMax(newPos.x(), 0), qMax(newPos.y(), 0));
641 const bool horizontal = (d->location == Plasma::TopEdge || d->location == Plasma::BottomEdge);
642 const int widthBound = d->minimumBound;
643 const int snapSize = 5;
645 //Snap
646 if (horizontal) {
647 if (newPos.x() <= snapSize) {
648 newPos.setX(0);
649 } else if (qAbs(newPos.x() - d->availableLength/2) <= snapSize) {
650 newPos.setX(d->availableLength/2);
651 } else if (d->availableLength - newPos.x() <= snapSize) {
652 newPos.setX(d->availableLength);
654 } else {
655 if (newPos.y() <= snapSize) {
656 newPos.setY(0);
657 } else if (qAbs(newPos.y() - d->availableLength/2) <= snapSize) {
658 newPos.setY(d->availableLength/2);
659 } else if (d->availableLength - newPos.y() <= snapSize) {
660 newPos.setY(d->availableLength);
664 switch (d->dragging) {
665 case Private::LeftMaxSlider:
666 //don't let the slider "cross" with the offset slider
667 if (horizontal && newPos.x() > d->offsetSliderRect.left() - widthBound) {
668 return;
669 } else if (!horizontal && newPos.y() > d->offsetSliderRect.top() - widthBound) {
670 return;
673 if (!d->moveSlider(d->leftMaxSliderRect, d->rightMaxSliderRect, newPos)) {
674 return;
677 d->maxLength = d->sliderRectToLength(d->leftMaxSliderRect);
679 if (d->minLength > d->maxLength) {
680 d->moveSlider(d->leftMinSliderRect, d->rightMinSliderRect, newPos);
681 d->minLength = d->maxLength;
683 break;
684 case Private::RightMaxSlider:
685 if (horizontal && newPos.x() < d->offsetSliderRect.left() + widthBound) {
686 return;
687 } else if (!horizontal && newPos.y() < d->offsetSliderRect.top() + widthBound) {
688 return;
691 if (!d->moveSlider(d->rightMaxSliderRect, d->leftMaxSliderRect, newPos)) {
692 return;
695 d->maxLength = d->sliderRectToLength(d->rightMaxSliderRect);
697 if (d->minLength > d->maxLength) {
698 d->moveSlider(d->rightMinSliderRect, d->leftMinSliderRect, newPos);
699 d->minLength = d->maxLength;
701 break;
702 case Private::LeftMinSlider:
703 if (horizontal && newPos.x() > d->offsetSliderRect.left() - widthBound) {
704 return;
705 } else if (!horizontal && newPos.y() > d->offsetSliderRect.top() - widthBound) {
706 return;
709 if (!d->moveSlider(d->leftMinSliderRect, d->rightMinSliderRect, newPos)) {
710 return;
713 d->minLength = d->sliderRectToLength(d->leftMinSliderRect);
715 if (d->minLength > d->maxLength) {
716 d->moveSlider(d->leftMaxSliderRect, d->rightMaxSliderRect, newPos);
717 d->maxLength = d->minLength;
719 break;
720 case Private::RightMinSlider:
721 if (horizontal && newPos.x() < d->offsetSliderRect.left() + widthBound) {
722 return;
723 } else if (!horizontal && newPos.y() < d->offsetSliderRect.top() + widthBound) {
724 return;
727 if (!d->moveSlider(d->rightMinSliderRect, d->leftMinSliderRect, newPos)) {
728 return;
731 d->minLength = d->sliderRectToLength(d->rightMinSliderRect);
733 if (d->minLength > d->maxLength) {
734 d->moveSlider(d->rightMaxSliderRect, d->leftMaxSliderRect, newPos);
735 d->maxLength = d->minLength;
737 break;
738 case Private::OffsetSlider:
740 if (d->location == Plasma::LeftEdge || d->location == Plasma::RightEdge) {
741 d->offsetSliderRect.moveCenter(QPoint(d->offsetSliderRect.center().x(), newPos.y()));
742 d->offset = d->offsetSliderRect.center().y();
743 } else {
744 d->offsetSliderRect.moveCenter(QPoint(newPos.x(), d->offsetSliderRect.center().y()));
745 d->offset = d->offsetSliderRect.center().x();
748 if (d->alignment == Qt::AlignCenter) {
749 d->offset -= d->availableLength/2;
750 } else if (d->alignment == Qt::AlignRight) {
751 d->offset = d->availableLength - d->offset;
754 int centerFactor = 1;
755 if (d->alignment == Qt::AlignCenter) {
756 centerFactor = 2;
759 d->maxLength = centerFactor*qMin(d->maxLength/centerFactor, d->availableLength/centerFactor - qAbs(d->offset));
760 d->minLength = centerFactor*qMin(d->minLength/centerFactor, d->availableLength/centerFactor - qAbs(d->offset));
762 if (d->location == Plasma::LeftEdge || d->location == Plasma::RightEdge) {
763 d->leftMaxSliderRect.moveTop(d->offsetSliderRect.top() - d->maxLength/centerFactor);
764 d->leftMinSliderRect.moveTop(d->offsetSliderRect.top() - d->minLength/centerFactor);
765 d->rightMaxSliderRect.moveTop(d->offsetSliderRect.top() + d->maxLength/centerFactor);
766 d->rightMinSliderRect.moveTop(d->offsetSliderRect.top() + d->minLength/centerFactor);
767 } else {
768 d->leftMaxSliderRect.moveLeft(d->offsetSliderRect.left() - d->maxLength/centerFactor);
769 d->leftMinSliderRect.moveLeft(d->offsetSliderRect.left() - d->minLength/centerFactor);
770 d->rightMaxSliderRect.moveLeft(d->offsetSliderRect.left() + d->maxLength/centerFactor);
771 d->rightMinSliderRect.moveLeft(d->offsetSliderRect.left() + d->minLength/centerFactor);
774 break;
776 default:
777 break;
780 emit rulersMoved(d->offset, d->minLength, d->maxLength);
781 update();
784 void PositioningRuler::resizeEvent(QResizeEvent *event)
786 switch (d->location) {
787 case Plasma::LeftEdge:
788 case Plasma::RightEdge:
789 setAvailableLength(event->size().height());
790 break;
791 default:
792 setAvailableLength(event->size().width());
793 break;
796 d->setupSliders(event->size());
798 event->accept();
801 #include "positioningruler.moc"