• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.9.1 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
dialog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 by Alessandro Diaferia <alediaferia@gmail.com>
3  * Copyright 2007 by Alexis Ménard <darktears31@gmail.com>
4  * Copyright 2007 Sebastian Kuegler <sebas@kde.org>
5  * Copyright 2006 Aaron Seigo <aseigo@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301 USA
21  */
22 
23 #include "dialog.h"
24 #include "private/dialog_p.h"
25 
26 #include <QPainter>
27 #include <QSvgRenderer>
28 #include <QResizeEvent>
29 #include <QMouseEvent>
30 #ifdef Q_WS_X11
31 #include <QX11Info>
32 #endif
33 #include <QBitmap>
34 #include <QTimer>
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>
43 
44 #include <kdebug.h>
45 #include <kwindowsystem.h>
46 #include <netwm.h>
47 
48 #include "plasma/applet.h"
49 #include "plasma/animator.h"
50 #include "plasma/containment.h"
51 #include "plasma/corona.h"
52 #include "plasma/extenders/extender.h"
53 #include "plasma/private/extender_p.h"
54 #include "plasma/framesvg.h"
55 #include "plasma/theme.h"
56 #include "plasma/widgets/scrollwidget.h"
57 #include "plasma/windoweffects.h"
58 
59 #ifdef Q_WS_X11
60 #include <X11/Xlib.h>
61 #endif
62 
63 namespace Plasma
64 {
65 
66 void DialogPrivate::scheduleBorderCheck(bool triggeredByResize)
67 {
68  //kDebug();
69  if (triggeredByResize) {
70  resizeChecksWithBorderCheck = true;
71 
72  // to keep the UI as fluid as possible, we call checkBorders
73  // immediately when there is a resize, and therefore stop any
74  // move-triggered scheduled calls to it. this keeps things
75  // looking reasonable during resize while avoiding as many
76  // calls to checkBorders as possible
77  if (moveTimer) {
78  moveTimer->stop();
79  }
80 
81  checkBorders();
82  return;
83  }
84 
85  if (!moveTimer) {
86  moveTimer = new QTimer(q);
87  moveTimer->setSingleShot(true);
88  QObject::connect(moveTimer, SIGNAL(timeout()), q, SLOT(checkBorders()));
89  }
90 
91  moveTimer->start(0);
92 }
93 
94 void DialogPrivate::themeChanged()
95 {
96  checkBorders(false);
97 
98  const bool translucency = Plasma::Theme::defaultTheme()->windowTranslucencyEnabled();
99  // WA_NoSystemBackground is going to fail combined with sliding popups, but is needed
100  // when we aren't compositing
101  q->setAttribute(Qt::WA_NoSystemBackground, !translucency);
102  updateMask();
103  q->update();
104 }
105 
106 void DialogPrivate::updateMask()
107 {
108  const bool translucency = Plasma::Theme::defaultTheme()->windowTranslucencyEnabled();
109  WindowEffects::enableBlurBehind(q->winId(), translucency,
110  translucency ? background->mask() : QRegion());
111  if (translucency) {
112  q->clearMask();
113  } else {
114  q->setMask(background->mask());
115  }
116 }
117 
118 void DialogPrivate::checkBorders()
119 {
120  checkBorders(true);
121 }
122 
123 void DialogPrivate::delayedAdjustSize()
124 {
125  q->syncToGraphicsWidget();
126 }
127 
128 void DialogPrivate::checkBorders(bool updateMaskIfNeeded)
129 {
130  if (resizeChecksWithBorderCheck) {
131  background->resizeFrame(q->size());
132  }
133 
134  QGraphicsWidget *graphicsWidget = graphicsWidgetPtr.data();
135  const FrameSvg::EnabledBorders currentBorders = background->enabledBorders();
136  FrameSvg::EnabledBorders borders = FrameSvg::AllBorders;
137 
138  Extender *extender = qobject_cast<Extender*>(graphicsWidget);
139  Plasma::Applet *applet = appletPtr.data();
140 
141  //used to remove borders at the edge of the desktop
142  QRect avail;
143  QRect screenGeom;
144  QDesktopWidget *desktop = QApplication::desktop();
145  Plasma::Corona *c = 0;
146  if (applet) {
147  c = qobject_cast<Plasma::Corona *>(applet->scene());
148  } else if (graphicsWidget) {
149  c = qobject_cast<Plasma::Corona *>(graphicsWidget->scene());
150  }
151  if (c) {
152  QRegion r = c->availableScreenRegion(desktop->screenNumber(q));
153  QRect maxRect;
154  foreach (QRect rect, r.rects()) {
155  if (rect.width() > maxRect.width() && rect.height() > maxRect.height()) {
156  maxRect = rect;
157  }
158  }
159  avail = maxRect;
160  screenGeom = c->screenGeometry(desktop->screenNumber(q));
161  } else {
162  avail = desktop->availableGeometry(desktop->screenNumber(q));
163  screenGeom = desktop->screenGeometry(desktop->screenNumber(q));
164  }
165 
166  QRect dialogGeom = q->geometry();
167 
168  qreal topHeight(0);
169  qreal leftWidth(0);
170  qreal rightWidth(0);
171  qreal bottomHeight(0);
172 
173  //decide about disabling the border attached to the panel
174  //don't cut borders on tooltips
175  if (applet && !q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip)) {
176  background->getMargins(leftWidth, topHeight, rightWidth, bottomHeight);
177 
178  switch (applet->location()) {
179  case BottomEdge:
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;
184  leftWidth = 0;
185  rightWidth = 0;
186  bottomHeight = 0;
187  }
188  break;
189 
190  case TopEdge:
191  if (applet->containment() &&
192  dialogGeom.top() <= screenGeom.top() + applet->containment()->size().height() &&
193  dialogGeom.width() <= applet->containment()->size().width()) {
194  borders &= ~FrameSvg::TopBorder;
195  topHeight = 0;
196  leftWidth = 0;
197  rightWidth = 0;
198  }
199  break;
200 
201  case LeftEdge:
202  if (applet->containment() &&
203  dialogGeom.left() <= screenGeom.left() + applet->containment()->size().width() &&
204  dialogGeom.height() <= applet->containment()->size().height()) {
205  borders &= ~FrameSvg::LeftBorder;
206  leftWidth = 0;
207  rightWidth = 0;
208  }
209  break;
210 
211  case RightEdge:
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;
216  leftWidth = 0;
217  rightWidth = 0;
218  }
219  break;
220 
221  default:
222  break;
223  }
224  }
225 
226  //decide if to disable the other borders
227  if (q->isVisible() && !q->testAttribute(Qt::WA_X11NetWmWindowTypeToolTip)) {
228  if (dialogGeom.left() <= avail.left()) {
229  borders &= ~FrameSvg::LeftBorder;
230  }
231  if (dialogGeom.top() <= avail.top()) {
232  borders &= ~FrameSvg::TopBorder;
233  }
234  //FIXME: that 2 pixels offset has probably something to do with kwin
235  if (dialogGeom.right() + 2 > avail.right()) {
236  borders &= ~FrameSvg::RightBorder;
237  }
238  if (dialogGeom.bottom() + 2 > avail.bottom()) {
239  borders &= ~FrameSvg::BottomBorder;
240  }
241  }
242 
243  background->setEnabledBorders(borders);
244 
245  if (extender) {
246  FrameSvg::EnabledBorders disabledBorders = FrameSvg::NoBorder;
247  if (!(borders & FrameSvg::LeftBorder)) {
248  disabledBorders |= FrameSvg::LeftBorder;
249  }
250  if (!(borders & FrameSvg::RightBorder)) {
251  disabledBorders |= FrameSvg::RightBorder;
252  }
253  extender->d->setDisabledBordersHint(disabledBorders);
254 
255  //if there is a scrollbar, reserve a margin to not draw it over the shadow
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) {
260  leftWidth = left;
261  } else {
262  rightWidth = right;
263  }
264  }
265  } else {
266  background->getMargins(leftWidth, topHeight, rightWidth, bottomHeight);
267  }
268 
269  //kDebug() << leftWidth << topHeight << rightWidth << bottomHeight;
270  q->setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
271 
272  if (resizeChecksWithBorderCheck) {
273  updateResizeCorners();
274  updateMask();
275  q->update();
276  } else if (currentBorders != borders) {
277  if (updateMaskIfNeeded) {
278  updateMask();
279  }
280 
281  q->update();
282  }
283 
284  resizeChecksWithBorderCheck = false;
285 }
286 
287 void Dialog::syncToGraphicsWidget()
288 {
289  d->adjustViewTimer->stop();
290  QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
291  if (d->view && graphicsWidget && d->resizeStartCorner != -1) {
292  const int prevStartCorner = d->resizeStartCorner;
293  d->resizeStartCorner = -1;
294  QSize prevSize = size();
295  /*
296  kDebug() << "Widget size:" << graphicsWidget->size()
297  << "| Widget size hint:" << graphicsWidget->effectiveSizeHint(Qt::PreferredSize)
298  << "| Widget minsize hint:" << graphicsWidget->minimumSize()
299  << "| Widget maxsize hint:" << graphicsWidget->maximumSize()
300  << "| Widget bounding rect:" << graphicsWidget->sceneBoundingRect();
301  */
302  //set the sizehints correctly:
303  int left, top, right, bottom;
304  getContentsMargins(&left, &top, &right, &bottom);
305 
306  QDesktopWidget *desktop = QApplication::desktop();
307  QSize maxSize = desktop->availableGeometry(desktop->screenNumber(this)).size();
308 
309  graphicsWidget->setMaximumSize(maxSize - QSize(left + right, top + bottom).boundedTo(graphicsWidget->effectiveSizeHint(Qt::MaximumSize).toSize()));
310 
311  setMinimumSize(0, 0);
312  setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
313 
314  QSize newSize(qMin(int(graphicsWidget->size().width()) + left + right, maxSize.width()),
315  qMin(int(graphicsWidget->size().height()) + top + bottom, maxSize.height()));
316 
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()));
320 
321 
322  QSize newMaximumSize(qMin(int(graphicsWidget->maximumSize().width()) + left + right, maxSize.width()),
323  qMin(int(graphicsWidget->maximumSize().height()) + top + bottom, maxSize.height()));
324 
325 
326  Plasma::Applet *applet = d->appletPtr.data();
327  if (applet) {
328  QRect currentGeometry(geometry());
329  currentGeometry.setSize(newSize);
330  if (applet->location() == Plasma::TopEdge ||
331  applet->location() == Plasma::LeftEdge) {
332  currentGeometry.setSize(newSize);
333  } else if (applet->location() == Plasma::RightEdge) {
334  currentGeometry.moveTopRight(geometry().topRight());
335  //BottomEdge and floating
336  } else {
337  currentGeometry.moveBottomLeft(geometry().bottomLeft());
338  }
339  setGeometry(currentGeometry);
340  } else {
341  resize(newSize);
342  }
343 
344  setMinimumSize(newMinimumSize);
345  setMaximumSize(newMaximumSize);
346 
347 
348  updateGeometry();
349 
350  //reposition and resize the view.
351  //force a valid rect, otherwise it will take up the whole scene
352  QRectF sceneRect(graphicsWidget->sceneBoundingRect());
353 
354  sceneRect.setWidth(qMax(qreal(1), sceneRect.width()));
355  sceneRect.setHeight(qMax(qreal(1), sceneRect.height()));
356  d->view->setSceneRect(sceneRect);
357 
358  //d->view->resize(graphicsWidget->size().toSize());
359  d->view->centerOn(graphicsWidget);
360 
361  if (size() != prevSize) {
362  //the size of the dialog has changed, emit the signal:
363  emit dialogResized();
364  }
365 
366  d->resizeStartCorner = prevStartCorner;
367  }
368 }
369 
370 int DialogPrivate::calculateWidthForHeightAndRatio(int height, qreal ratio)
371 {
372  switch (aspectRatioMode) {
373  case KeepAspectRatio:
374  return qRound(height * ratio);
375  break;
376  case Square:
377  return height;
378  break;
379  case ConstrainedSquare:
380  return height;
381  break;
382  default:
383  return -1;
384  }
385 }
386 
387 Dialog::Dialog(QWidget *parent, Qt::WindowFlags f)
388  : QWidget(parent, f | Qt::FramelessWindowHint),
389  d(new DialogPrivate(this))
390 {
391  setMouseTracking(true);
392  setAttribute(Qt::WA_TranslucentBackground);
393  d->background = new FrameSvg(this);
394  d->background->setImagePath("dialogs/background");
395  d->background->setEnabledBorders(FrameSvg::AllBorders);
396  d->background->resizeFrame(size());
397  connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(themeChanged()));
398 
399  QPalette pal = palette();
400  pal.setColor(backgroundRole(), Qt::transparent);
401  setPalette(pal);
402  WindowEffects::overrideShadow(winId(), true);
403 
404  d->adjustViewTimer = new QTimer(this);
405  d->adjustViewTimer->setSingleShot(true);
406  connect(d->adjustViewTimer, SIGNAL(timeout()), this, SLOT(syncToGraphicsWidget()));
407 
408  d->adjustSizeTimer = new QTimer(this);
409  d->adjustSizeTimer->setSingleShot(true);
410  connect(d->adjustSizeTimer, SIGNAL(timeout()), this, SLOT(delayedAdjustSize()));
411 
412  d->themeChanged();
413 }
414 
415 Dialog::~Dialog()
416 {
417  delete d;
418 }
419 
420 void Dialog::paintEvent(QPaintEvent *e)
421 {
422  QPainter p(this);
423  p.setCompositionMode(QPainter::CompositionMode_Source);
424  d->background->paintFrame(&p, e->rect(), e->rect());
425 }
426 
427 void Dialog::mouseMoveEvent(QMouseEvent *event)
428 {
429  if (event->modifiers() == Qt::AltModifier) {
430  unsetCursor();
431  } else if (d->resizeAreas[Dialog::NorthEast].contains(event->pos())) {
432  setCursor(Qt::SizeBDiagCursor);
433  } else if (d->resizeAreas[Dialog::NorthWest].contains(event->pos())) {
434  setCursor(Qt::SizeFDiagCursor);
435  } else if (d->resizeAreas[Dialog::SouthEast].contains(event->pos())) {
436  setCursor(Qt::SizeFDiagCursor);
437  } else if (d->resizeAreas[Dialog::SouthWest].contains(event->pos())) {
438  setCursor(Qt::SizeBDiagCursor);
439  } else if (!(event->buttons() & Qt::LeftButton)) {
440  unsetCursor();
441  }
442 
443  // here we take care of resize..
444  if (d->resizeStartCorner != Dialog::NoCorner) {
445  int newWidth;
446  int newHeight;
447  QPoint position;
448 
449  qreal aspectRatio = (qreal)width() / (qreal)height();
450 
451  switch(d->resizeStartCorner) {
452  case Dialog::NorthEast:
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()));
457  }
458  position = QPoint(x(), y() + height() - newHeight);
459  break;
460  case Dialog::NorthWest:
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()));
465  }
466  position = QPoint(x() + width() - newWidth, y() + height() - newHeight);
467  break;
468  case Dialog::SouthWest:
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()));
473  }
474  position = QPoint(x() + width() - newWidth, y());
475  break;
476  case Dialog::SouthEast:
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()));
481  }
482  position = QPoint(x(), y());
483  break;
484  default:
485  newHeight = qMin(maximumHeight(), qMax(minimumHeight(), height()));
486  newWidth = d->calculateWidthForHeightAndRatio(newHeight, aspectRatio);
487  if (newWidth == -1) {
488  newWidth = qMin(maximumWidth(), qMax(minimumWidth(), width()));
489  }
490  position = QPoint(x(), y());
491  break;
492  }
493 
494  QRect newGeom(position, QSize(newWidth, newHeight));
495 
496  // now sanity check the resize results again min constraints, if any
497  if (d->leftResizeMin > -1 && newGeom.left() > d->leftResizeMin) {
498  newGeom.setLeft(d->leftResizeMin);
499  }
500 
501  if (d->topResizeMin > -1 && newGeom.top() > d->topResizeMin) {
502  newGeom.setTop(d->topResizeMin);
503  }
504 
505  if (d->rightResizeMin > -1 && newGeom.right() < d->rightResizeMin) {
506  newGeom.setRight(d->rightResizeMin);
507  }
508 
509  if (d->bottomResizeMin > -1 && newGeom.bottom() < d->bottomResizeMin) {
510  newGeom.setBottom(d->bottomResizeMin);
511  }
512 
513  if ((newGeom.width() >= minimumSize().width()) && (newGeom.height() >= minimumSize().height())) {
514  setGeometry(newGeom);
515  }
516  }
517 
518  QWidget::mouseMoveEvent(event);
519 }
520 
521 void Dialog::mousePressEvent(QMouseEvent *event)
522 {
523  if (d->resizeAreas[Dialog::NorthEast].contains(event->pos())) {
524  d->resizeStartCorner = Dialog::NorthEast;
525  } else if (d->resizeAreas[Dialog::NorthWest].contains(event->pos())) {
526  d->resizeStartCorner = Dialog::NorthWest;
527  } else if (d->resizeAreas[Dialog::SouthEast].contains(event->pos())) {
528  d->resizeStartCorner = Dialog::SouthEast;
529  } else if (d->resizeAreas[Dialog::SouthWest].contains(event->pos())) {
530  d->resizeStartCorner = Dialog::SouthWest;
531  } else {
532  d->resizeStartCorner = Dialog::NoCorner;
533  }
534 
535  QWidget::mousePressEvent(event);
536 }
537 
538 void Dialog::mouseReleaseEvent(QMouseEvent *event)
539 {
540  if (d->resizeStartCorner != Dialog::NoCorner) {
541  emit dialogResized();
542  d->resizeStartCorner = Dialog::NoCorner;
543  unsetCursor();
544  }
545 
546  QWidget::mouseReleaseEvent(event);
547 }
548 
549 void Dialog::keyPressEvent(QKeyEvent *event)
550 {
551  if (event->key() == Qt::Key_Escape) {
552  hide();
553  }
554 }
555 
556 bool Dialog::event(QEvent *event)
557 {
558  return QWidget::event(event);
559 }
560 
561 void Dialog::resizeEvent(QResizeEvent *event)
562 {
563  Q_UNUSED(event)
564  //kDebug();
565  d->scheduleBorderCheck(true);
566 
567  if (d->resizeStartCorner != -1 && d->view && d->graphicsWidgetPtr) {
568  QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
569  graphicsWidget->resize(d->view->size());
570 
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);
576  }
577 }
578 
579 void DialogPrivate::updateResizeCorners()
580 {
581  const int resizeAreaMargin = 20;
582  const QRect r = q->rect();
583  const FrameSvg::EnabledBorders borders = background->enabledBorders();
584 
585  // IMPLEMENTATION NOTE: we set resize corners for the corners set, but also
586  // for the complimentary corners if we've cut out an edge of our SVG background
587  // which implies we are up against an immovable edge (e.g. a screen edge)
588 
589  resizeAreas.clear();
590  if (resizeCorners & Dialog::NorthEast ||
591  (resizeCorners & Dialog::NorthWest && !(borders & FrameSvg::LeftBorder)) ||
592  (resizeCorners & Dialog::SouthEast && !(borders & FrameSvg::BottomBorder))) {
593  resizeAreas[Dialog::NorthEast] = QRect(r.right() - resizeAreaMargin, 0,
594  resizeAreaMargin, resizeAreaMargin);
595  }
596 
597  if (resizeCorners & Dialog::NorthWest ||
598  (resizeCorners & Dialog::NorthEast && !(borders & FrameSvg::RightBorder)) ||
599  (resizeCorners & Dialog::SouthWest && !(borders & FrameSvg::BottomBorder))) {
600  resizeAreas[Dialog::NorthWest] = QRect(0, 0, resizeAreaMargin, resizeAreaMargin);
601  }
602 
603  if (resizeCorners & Dialog::SouthEast ||
604  (resizeCorners & Dialog::SouthWest && !(borders & FrameSvg::LeftBorder)) ||
605  (resizeCorners & Dialog::NorthEast && !(borders & FrameSvg::TopBorder))) {
606  resizeAreas[Dialog::SouthEast] = QRect(r.right() - resizeAreaMargin,
607  r.bottom() - resizeAreaMargin,
608  resizeAreaMargin, resizeAreaMargin);
609  }
610 
611  if (resizeCorners & Dialog::SouthWest ||
612  (resizeCorners & Dialog::SouthEast && !(borders & FrameSvg::RightBorder)) ||
613  (resizeCorners & Dialog::NorthWest && !(borders & FrameSvg::TopBorder))) {
614  resizeAreas[Dialog::SouthWest] = QRect(0, r.bottom() - resizeAreaMargin,
615  resizeAreaMargin, resizeAreaMargin);
616  }
617 }
618 
619 void Dialog::setGraphicsWidget(QGraphicsWidget *widget)
620 {
621  if (d->graphicsWidgetPtr) {
622  d->graphicsWidgetPtr.data()->removeEventFilter(this);
623  }
624 
625  d->graphicsWidgetPtr = widget;
626 
627  if (widget) {
628  Plasma::Corona *c = qobject_cast<Plasma::Corona *>(widget->scene());
629  if (c) {
630  c->addOffscreenWidget(widget);
631  }
632 
633  if (!layout()) {
634  QVBoxLayout *lay = new QVBoxLayout(this);
635  lay->setMargin(0);
636  lay->setSpacing(0);
637  }
638 
639  d->checkBorders();
640 
641  if (!d->view) {
642  d->view = new QGraphicsView(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);
648  }
649 
650  d->view->setScene(widget->scene());
651 
652  //try to have the proper size -before- showing the dialog
653  d->view->centerOn(widget);
654  if (widget->layout()) {
655  widget->layout()->activate();
656  }
657  static_cast<QGraphicsLayoutItem *>(widget)->updateGeometry();
658  widget->resize(widget->size().expandedTo(widget->effectiveSizeHint(Qt::MinimumSize)));
659 
660  syncToGraphicsWidget();
661 
662  //d->adjustSizeTimer->start(150);
663 
664  widget->installEventFilter(this);
665  d->view->installEventFilter(this);
666  } else {
667  delete d->view;
668  d->view = 0;
669  }
670 }
671 
672 //KDE5 FIXME: should be const
673 QGraphicsWidget *Dialog::graphicsWidget()
674 {
675  return d->graphicsWidgetPtr.data();
676 }
677 
678 bool Dialog::eventFilter(QObject *watched, QEvent *event)
679 {
680  if (d->resizeStartCorner == Dialog::NoCorner && watched == d->graphicsWidgetPtr.data() &&
681  (event->type() == QEvent::GraphicsSceneResize || event->type() == QEvent::GraphicsSceneMove)) {
682  d->adjustViewTimer->start(150);
683  }
684 
685  // when moving the cursor with a 45° angle from the outside
686  // to the inside passing over a resize angle the cursor changes its
687  // shape to a resize cursor. As a side effect this is the only case
688  // when the cursor immediately enters the view without giving
689  // the dialog the chance to restore the original cursor shape.
690  if (event->type() == QEvent::Enter && watched == d->view) {
691  unsetCursor();
692  }
693 
694  return QWidget::eventFilter(watched, event);
695 }
696 
697 void Dialog::hideEvent(QHideEvent * event)
698 {
699  Q_UNUSED(event);
700  emit dialogVisible(false);
701 }
702 
703 void Dialog::showEvent(QShowEvent * event)
704 {
705  Q_UNUSED(event);
706 
707  //check if the widget size is still synced with the view
708  d->checkBorders();
709  d->updateResizeCorners();
710 
711  QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
712  if (graphicsWidget &&
713  ((d->view && graphicsWidget->size().toSize() != d->view->size()) ||
714  d->oldGraphicsWidgetMinimumSize != graphicsWidget->minimumSize() ||
715  d->oldGraphicsWidgetMaximumSize != graphicsWidget->maximumSize())) {
716  //here have to be done immediately, ideally should have to be done -before- shwing, but is not possible to catch show() so early
717  syncToGraphicsWidget();
718  d->oldGraphicsWidgetMinimumSize = graphicsWidget->minimumSize().toSize();
719  d->oldGraphicsWidgetMaximumSize = graphicsWidget->maximumSize().toSize();
720  }
721 
722  if (d->view) {
723  d->view->setFocus();
724  }
725 
726  if (graphicsWidget) {
727  graphicsWidget->setFocus();
728  }
729 
730  emit dialogVisible(true);
731  WindowEffects::overrideShadow(winId(), true);
732 }
733 
734 void Dialog::focusInEvent(QFocusEvent *event)
735 {
736  Q_UNUSED(event)
737 
738  if (d->view) {
739  d->view->setFocus();
740  }
741 
742  QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
743  if (graphicsWidget) {
744  graphicsWidget->setFocus();
745  }
746 }
747 
748 void Dialog::moveEvent(QMoveEvent *event)
749 {
750  Q_UNUSED(event)
751  //kDebug();
752  d->scheduleBorderCheck();
753 }
754 
755 void Dialog::setResizeHandleCorners(ResizeCorners corners)
756 {
757  if ((d->resizeCorners != corners) && (aspectRatioMode() != FixedSize)) {
758  d->resizeCorners = corners;
759  d->updateResizeCorners();
760  }
761 }
762 
763 Dialog::ResizeCorners Dialog::resizeCorners() const
764 {
765  return d->resizeCorners;
766 }
767 
768 bool Dialog::isUserResizing() const
769 {
770  return d->resizeStartCorner > NoCorner;
771 }
772 
773 void Dialog::setMinimumResizeLimits(int left, int top, int right, int bottom)
774 {
775  d->leftResizeMin = left;
776  d->topResizeMin = top;
777  d->rightResizeMin = right;
778  d->bottomResizeMin = bottom;
779 }
780 
781 void Dialog::getMinimumResizeLimits(int *left, int *top, int *right, int *bottom)
782 {
783  if (left) {
784  *left = d->leftResizeMin;
785  }
786 
787  if (top) {
788  *top = d->topResizeMin;
789  }
790 
791  if (right) {
792  *right = d->rightResizeMin;
793  }
794 
795  if (bottom) {
796  *bottom = d->bottomResizeMin;
797  }
798 }
799 
800 void Dialog::animatedHide(Plasma::Direction direction)
801 {
802  if (!isVisible()) {
803  return;
804  }
805 
806  if (!Plasma::Theme::defaultTheme()->windowTranslucencyEnabled()) {
807  hide();
808  return;
809  }
810 
811  Location location = Desktop;
812  switch (direction) {
813  case Down:
814  location = BottomEdge;
815  break;
816  case Right:
817  location = RightEdge;
818  break;
819  case Left:
820  location = LeftEdge;
821  break;
822  case Up:
823  location = TopEdge;
824  break;
825  default:
826  break;
827  }
828 
829  Plasma::WindowEffects::slideWindow(this, location);
830  hide();
831 }
832 
833 void Dialog::animatedShow(Plasma::Direction direction)
834 {
835  if (!Plasma::Theme::defaultTheme()->windowTranslucencyEnabled()) {
836  show();
837  return;
838  }
839 
840  //copied to not add new api
841  Location location = Desktop;
842  switch (direction) {
843  case Up:
844  location = BottomEdge;
845  break;
846  case Left:
847  location = RightEdge;
848  break;
849  case Right:
850  location = LeftEdge;
851  break;
852  case Down:
853  location = TopEdge;
854  break;
855  default:
856  break;
857  }
858 
859  if (Plasma::Theme::defaultTheme()->windowTranslucencyEnabled()) {
860  Plasma::WindowEffects::slideWindow(this, location);
861  }
862 
863  show();
864 }
865 
866 bool Dialog::inControlArea(const QPoint &point)
867 {
868  foreach (const QRect &r, d->resizeAreas) {
869  if (r.contains(point)) {
870  return true;
871  }
872  }
873  return false;
874 }
875 
876 Plasma::AspectRatioMode Dialog::aspectRatioMode() const
877 {
878  return d->aspectRatioMode;
879 }
880 
881 void Dialog::setAspectRatioMode(Plasma::AspectRatioMode mode)
882 {
883  if (mode == FixedSize) {
884  setResizeHandleCorners(NoCorner);
885  }
886 
887  d->aspectRatioMode = mode;
888 }
889 
890 }
891 #include "dialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 11:22:12 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.9.1 API Reference

Skip menu "kdelibs-4.9.1 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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