24 #include "private/dialog_p.h"
27 #include <QSvgRenderer>
28 #include <QResizeEvent>
29 #include <QMouseEvent>
35 #include <QtGui/QVBoxLayout>
36 #include <QtGui/QGraphicsSceneEvent>
37 #include <QtGui/QGraphicsView>
38 #include <QtGui/QGraphicsWidget>
39 #include <QApplication>
40 #include <QDesktopWidget>
41 #include <QVarLengthArray>
42 #include <QGraphicsLayout>
45 #include <kwindowsystem.h>
53 #include "plasma/private/extender_p.h"
66 void DialogPrivate::scheduleBorderCheck(
bool triggeredByResize)
69 if (triggeredByResize) {
70 resizeChecksWithBorderCheck =
true;
86 moveTimer =
new QTimer(q);
87 moveTimer->setSingleShot(
true);
88 QObject::connect(moveTimer, SIGNAL(timeout()), q, SLOT(checkBorders()));
94 void DialogPrivate::themeChanged()
101 q->setAttribute(Qt::WA_NoSystemBackground, !translucency);
106 void DialogPrivate::updateMask()
110 translucency ? background->mask() : QRegion());
114 q->setMask(background->mask());
118 void DialogPrivate::checkBorders()
123 void DialogPrivate::delayedAdjustSize()
125 q->syncToGraphicsWidget();
128 void DialogPrivate::checkBorders(
bool updateMaskIfNeeded)
130 if (resizeChecksWithBorderCheck) {
131 background->resizeFrame(q->size());
135 const FrameSvg::EnabledBorders currentBorders = background->enabledBorders();
138 Extender *extender = qobject_cast<Extender*>(graphicsWidget);
144 QDesktopWidget *desktop = QApplication::desktop();
148 }
else if (graphicsWidget) {
154 foreach (QRect rect, r.rects()) {
155 if (rect.width() > maxRect.width() && rect.height() > maxRect.height()) {
162 avail = desktop->availableGeometry(desktop->screenNumber(q));
163 screenGeom = desktop->screenGeometry(desktop->screenNumber(q));
166 QRect dialogGeom = q->geometry();
171 qreal bottomHeight(0);
175 if (applet && !q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip)) {
176 background->getMargins(leftWidth, topHeight, rightWidth, bottomHeight);
178 switch (applet->location()) {
180 if (applet->containment() &&
181 dialogGeom.bottom() + 2 >= screenGeom.bottom() - applet->containment()->size().height() &&
182 dialogGeom.width() <= applet->containment()->size().width()) {
183 borders &= ~FrameSvg::BottomBorder;
191 if (applet->containment() &&
192 dialogGeom.top() <= screenGeom.top() + applet->containment()->size().height() &&
193 dialogGeom.width() <= applet->containment()->size().width()) {
194 borders &= ~FrameSvg::TopBorder;
202 if (applet->containment() &&
203 dialogGeom.left() <= screenGeom.left() + applet->containment()->size().width() &&
204 dialogGeom.height() <= applet->containment()->size().height()) {
205 borders &= ~FrameSvg::LeftBorder;
212 if (applet->containment() &&
213 dialogGeom.right() + 2 >= screenGeom.right() - applet->containment()->size().width() &&
214 dialogGeom.height() <= applet->containment()->size().height()) {
215 borders &= ~FrameSvg::RightBorder;
227 if (q->isVisible() && !q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip)) {
228 if (dialogGeom.left() <= avail.left()) {
229 borders &= ~FrameSvg::LeftBorder;
231 if (dialogGeom.top() <= avail.top()) {
232 borders &= ~FrameSvg::TopBorder;
235 if (dialogGeom.right() + 2 > avail.right()) {
236 borders &= ~FrameSvg::RightBorder;
238 if (dialogGeom.bottom() + 2 > avail.bottom()) {
239 borders &= ~FrameSvg::BottomBorder;
243 background->setEnabledBorders(borders);
253 extender->d->setDisabledBordersHint(disabledBorders);
256 qreal left, top, right, bottom;
257 background->getMargins(left, top, right, bottom);
258 if (extender->d->scrollWidget->viewportGeometry().height() < extender->d->scrollWidget->contentsSize().height()) {
259 if (QApplication::layoutDirection() == Qt::RightToLeft) {
266 background->getMargins(leftWidth, topHeight, rightWidth, bottomHeight);
270 q->setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
272 if (resizeChecksWithBorderCheck) {
273 updateResizeCorners();
276 }
else if (currentBorders != borders) {
277 if (updateMaskIfNeeded) {
284 resizeChecksWithBorderCheck =
false;
289 d->adjustViewTimer->stop();
291 if (d->view && graphicsWidget && d->resizeStartCorner != -1) {
292 const int prevStartCorner = d->resizeStartCorner;
293 d->resizeStartCorner = -1;
294 QSize prevSize = size();
303 int left, top, right, bottom;
304 getContentsMargins(&left, &top, &right, &bottom);
306 QDesktopWidget *desktop = QApplication::desktop();
307 QSize maxSize = desktop->availableGeometry(desktop->screenNumber(
this)).size();
309 graphicsWidget->setMaximumSize(maxSize - QSize(left + right, top + bottom).boundedTo(graphicsWidget->effectiveSizeHint(Qt::MaximumSize).toSize()));
311 setMinimumSize(0, 0);
312 setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
314 QSize newSize(qMin(
int(graphicsWidget->size().width()) + left + right, maxSize.width()),
315 qMin(
int(graphicsWidget->size().height()) + top + bottom, maxSize.height()));
317 const QSizeF minimum = graphicsWidget->effectiveSizeHint(Qt::MinimumSize);
318 QSize newMinimumSize(qMin(
int(minimum.width()) + left + right, maxSize.width()),
319 qMin(
int(minimum.height()) + top + bottom, maxSize.height()));
322 QSize newMaximumSize(qMin(
int(graphicsWidget->maximumSize().width()) + left + right, maxSize.width()),
323 qMin(
int(graphicsWidget->maximumSize().height()) + top + bottom, maxSize.height()));
328 QRect currentGeometry(geometry());
329 currentGeometry.setSize(newSize);
332 currentGeometry.setSize(newSize);
334 currentGeometry.moveTopRight(geometry().topRight());
337 currentGeometry.moveBottomLeft(geometry().bottomLeft());
339 setGeometry(currentGeometry);
344 setMinimumSize(newMinimumSize);
345 setMaximumSize(newMaximumSize);
352 QRectF sceneRect(graphicsWidget->sceneBoundingRect());
354 sceneRect.setWidth(qMax(qreal(1), sceneRect.width()));
355 sceneRect.setHeight(qMax(qreal(1), sceneRect.height()));
356 d->view->setSceneRect(sceneRect);
359 d->view->centerOn(graphicsWidget);
361 if (size() != prevSize) {
366 d->resizeStartCorner = prevStartCorner;
370 int DialogPrivate::calculateWidthForHeightAndRatio(
int height, qreal ratio)
372 switch (aspectRatioMode) {
374 return qRound(height * ratio);
388 :
QWidget(parent, f | Qt::FramelessWindowHint),
389 d(new DialogPrivate(this))
391 setMouseTracking(
true);
392 setAttribute(Qt::WA_TranslucentBackground);
394 d->background->setImagePath(
"dialogs/background");
396 d->background->resizeFrame(size());
397 connect(d->background, SIGNAL(repaintNeeded()),
this, SLOT(themeChanged()));
399 QPalette pal = palette();
400 pal.setColor(backgroundRole(), Qt::transparent);
404 d->adjustViewTimer =
new QTimer(
this);
405 d->adjustViewTimer->setSingleShot(
true);
408 d->adjustSizeTimer =
new QTimer(
this);
409 d->adjustSizeTimer->setSingleShot(
true);
410 connect(d->adjustSizeTimer, SIGNAL(timeout()),
this, SLOT(delayedAdjustSize()));
423 p.setCompositionMode(QPainter::CompositionMode_Source);
424 d->background->paintFrame(&p, e->rect(), e->rect());
429 if (event->modifiers() == Qt::AltModifier) {
432 setCursor(Qt::SizeBDiagCursor);
434 setCursor(Qt::SizeFDiagCursor);
436 setCursor(Qt::SizeFDiagCursor);
438 setCursor(Qt::SizeBDiagCursor);
439 }
else if (!(event->buttons() & Qt::LeftButton)) {
449 qreal aspectRatio = (qreal)width() / (qreal)height();
451 switch(d->resizeStartCorner) {
453 newHeight = qMin(maximumHeight(), qMax(minimumHeight(), height() - event->y()));
454 newWidth = d->calculateWidthForHeightAndRatio(newHeight, aspectRatio);
455 if (newWidth == -1) {
456 newWidth = qMin(maximumWidth(), qMax(minimumWidth(), event->x()));
458 position = QPoint(x(), y() + height() - newHeight);
461 newHeight = qMin(maximumHeight(), qMax(minimumHeight(), height() - event->y()));
462 newWidth = d->calculateWidthForHeightAndRatio(newHeight, aspectRatio);
463 if (newWidth == -1) {
464 newWidth = qMin(maximumWidth(), qMax(minimumWidth(), width() - event->x()));
466 position = QPoint(x() + width() - newWidth, y() + height() - newHeight);
469 newHeight = qMin(maximumHeight(), qMax(minimumHeight(), event->y()));
470 newWidth = d->calculateWidthForHeightAndRatio(newHeight, aspectRatio);
471 if (newWidth == -1) {
472 newWidth = qMin(maximumWidth(), qMax(minimumWidth(), width() - event->x()));
474 position = QPoint(x() + width() - newWidth, y());
477 newHeight = qMin(maximumHeight(), qMax(minimumHeight(), event->y()));
478 newWidth = d->calculateWidthForHeightAndRatio(newHeight, aspectRatio);
479 if (newWidth == -1) {
480 newWidth = qMin(maximumWidth(), qMax(minimumWidth(), event->x()));
482 position = QPoint(x(), y());
485 newHeight = qMin(maximumHeight(), qMax(minimumHeight(), height()));
486 newWidth = d->calculateWidthForHeightAndRatio(newHeight, aspectRatio);
487 if (newWidth == -1) {
488 newWidth = qMin(maximumWidth(), qMax(minimumWidth(), width()));
490 position = QPoint(x(), y());
494 QRect newGeom(position, QSize(newWidth, newHeight));
497 if (d->leftResizeMin > -1 && newGeom.left() > d->leftResizeMin) {
498 newGeom.setLeft(d->leftResizeMin);
501 if (d->topResizeMin > -1 && newGeom.top() > d->topResizeMin) {
502 newGeom.setTop(d->topResizeMin);
505 if (d->rightResizeMin > -1 && newGeom.right() < d->rightResizeMin) {
506 newGeom.setRight(d->rightResizeMin);
509 if (d->bottomResizeMin > -1 && newGeom.bottom() < d->bottomResizeMin) {
510 newGeom.setBottom(d->bottomResizeMin);
513 if ((newGeom.width() >= minimumSize().width()) && (newGeom.height() >= minimumSize().height())) {
514 setGeometry(newGeom);
551 if (event->key() == Qt::Key_Escape) {
565 d->scheduleBorderCheck(
true);
567 if (d->resizeStartCorner != -1 && d->view && d->graphicsWidgetPtr) {
569 graphicsWidget->resize(d->view->size());
571 QRectF sceneRect(graphicsWidget->sceneBoundingRect());
572 sceneRect.setWidth(qMax(qreal(1), sceneRect.width()));
573 sceneRect.setHeight(qMax(qreal(1), sceneRect.height()));
574 d->view->setSceneRect(sceneRect);
575 d->view->centerOn(graphicsWidget);
579 void DialogPrivate::updateResizeCorners()
581 const int resizeAreaMargin = 20;
582 const QRect r = q->rect();
583 const FrameSvg::EnabledBorders borders = background->enabledBorders();
594 resizeAreaMargin, resizeAreaMargin);
600 resizeAreas[
Dialog::NorthWest] = QRect(0, 0, resizeAreaMargin, resizeAreaMargin);
607 r.bottom() - resizeAreaMargin,
608 resizeAreaMargin, resizeAreaMargin);
615 resizeAreaMargin, resizeAreaMargin);
621 if (d->graphicsWidgetPtr) {
622 d->graphicsWidgetPtr.data()->removeEventFilter(
this);
625 d->graphicsWidgetPtr = widget;
634 QVBoxLayout *lay =
new QVBoxLayout(
this);
643 d->view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
644 d->view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
645 d->view->setFrameShape(QFrame::NoFrame);
646 d->view->viewport()->setAutoFillBackground(
false);
647 layout()->addWidget(d->view);
650 d->view->setScene(widget->scene());
653 d->view->centerOn(widget);
654 if (widget->layout()) {
655 widget->layout()->activate();
657 static_cast<QGraphicsLayoutItem *
>(widget)->updateGeometry();
658 widget->resize(widget->size().expandedTo(widget->effectiveSizeHint(Qt::MinimumSize)));
664 widget->installEventFilter(
this);
665 d->view->installEventFilter(
this);
675 return d->graphicsWidgetPtr.data();
680 if (d->resizeStartCorner ==
Dialog::NoCorner && watched == d->graphicsWidgetPtr.data() &&
681 (
event->type() == QEvent::GraphicsSceneResize ||
event->type() == QEvent::GraphicsSceneMove)) {
682 d->adjustViewTimer->start(150);
690 if (event->type() == QEvent::Enter && watched == d->view) {
709 d->updateResizeCorners();
712 if (graphicsWidget &&
713 ((d->view && graphicsWidget->size().toSize() != d->view->size()) ||
714 d->oldGraphicsWidgetMinimumSize != graphicsWidget->minimumSize() ||
715 d->oldGraphicsWidgetMaximumSize != graphicsWidget->maximumSize())) {
718 d->oldGraphicsWidgetMinimumSize = graphicsWidget->minimumSize().toSize();
719 d->oldGraphicsWidgetMaximumSize = graphicsWidget->maximumSize().toSize();
726 if (graphicsWidget) {
727 graphicsWidget->setFocus();
743 if (graphicsWidget) {
744 graphicsWidget->setFocus();
752 d->scheduleBorderCheck();
758 d->resizeCorners = corners;
759 d->updateResizeCorners();
765 return d->resizeCorners;
770 return d->resizeStartCorner >
NoCorner;
775 d->leftResizeMin = left;
776 d->topResizeMin = top;
777 d->rightResizeMin = right;
778 d->bottomResizeMin = bottom;
784 *left = d->leftResizeMin;
788 *top = d->topResizeMin;
792 *right = d->rightResizeMin;
796 *bottom = d->bottomResizeMin;
868 foreach (
const QRect &r, d->resizeAreas) {
869 if (r.contains(point)) {
878 return d->aspectRatioMode;
887 d->aspectRatioMode = mode;
891 #include "dialog.moc"