since the QGrahicsWidget we're given originates from the outside, don't assume it'll stay around nicely; use a QWeakPointer to track it

svn path=/trunk/KDE/kdelibs/; revision=1073410
This commit is contained in:
Aaron J. Seigo 2010-01-12 08:11:27 +00:00
parent 564d86e134
commit e7b129bea3

View File

@ -65,7 +65,6 @@ public:
: q(dialog), : q(dialog),
background(0), background(0),
view(0), view(0),
graphicsWidget(0),
resizeCorners(Dialog::NoCorner), resizeCorners(Dialog::NoCorner),
resizeStartCorner(Dialog::NoCorner), resizeStartCorner(Dialog::NoCorner),
moveTimer(0), moveTimer(0),
@ -89,7 +88,7 @@ public:
*/ */
Plasma::FrameSvg *background; Plasma::FrameSvg *background;
QGraphicsView *view; QGraphicsView *view;
QGraphicsWidget *graphicsWidget; QWeakPointer<QGraphicsWidget> graphicsWidgetPtr;
Dialog::ResizeCorners resizeCorners; Dialog::ResizeCorners resizeCorners;
QMap<Dialog::ResizeCorner, QRect> resizeAreas; QMap<Dialog::ResizeCorner, QRect> resizeAreas;
int resizeStartCorner; int resizeStartCorner;
@ -100,6 +99,8 @@ public:
void DialogPrivate::themeChanged() void DialogPrivate::themeChanged()
{ {
QGraphicsWidget *graphicsWidget = graphicsWidgetPtr.data();
qreal topHeight; qreal topHeight;
qreal leftWidth; qreal leftWidth;
qreal rightWidth; qreal rightWidth;
@ -197,7 +198,8 @@ void DialogPrivate::themeChanged()
void Dialog::syncToGraphicsWidget() void Dialog::syncToGraphicsWidget()
{ {
d->adjustViewTimer->stop(); d->adjustViewTimer->stop();
if (d->view && d->graphicsWidget) { QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
if (d->view && graphicsWidget) {
const int prevStartCorner = d->resizeStartCorner; const int prevStartCorner = d->resizeStartCorner;
d->resizeStartCorner = -1; d->resizeStartCorner = -1;
QSize prevSize = size(); QSize prevSize = size();
@ -216,24 +218,24 @@ void Dialog::syncToGraphicsWidget()
QSize maxSize = desktop->availableGeometry(desktop->screenNumber(this)).size(); QSize maxSize = desktop->availableGeometry(desktop->screenNumber(this)).size();
setMinimumSize(qMin(int(d->graphicsWidget->minimumSize().width()) + left + right, maxSize.width()), setMinimumSize(qMin(int(graphicsWidget->minimumSize().width()) + left + right, maxSize.width()),
qMin(int(d->graphicsWidget->minimumSize().height()) + top + bottom, maxSize.height())); qMin(int(graphicsWidget->minimumSize().height()) + top + bottom, maxSize.height()));
setMaximumSize(qMin(int(d->graphicsWidget->maximumSize().width()) + left + right, maxSize.width()), setMaximumSize(qMin(int(graphicsWidget->maximumSize().width()) + left + right, maxSize.width()),
qMin(int(d->graphicsWidget->maximumSize().height()) + top + bottom, maxSize.height())); qMin(int(graphicsWidget->maximumSize().height()) + top + bottom, maxSize.height()));
resize(qMin(int(d->graphicsWidget->size().width()) + left + right, maxSize.width()), resize(qMin(int(graphicsWidget->size().width()) + left + right, maxSize.width()),
qMin(int(d->graphicsWidget->size().height()) + top + bottom, maxSize.height())); qMin(int(graphicsWidget->size().height()) + top + bottom, maxSize.height()));
updateGeometry(); updateGeometry();
//reposition and resize the view. //reposition and resize the view.
//force a valid rect, otherwise it will take up the whole scene //force a valid rect, otherwise it will take up the whole scene
QRectF sceneRect(d->graphicsWidget->sceneBoundingRect()); QRectF sceneRect(graphicsWidget->sceneBoundingRect());
sceneRect.setWidth(qMax(qreal(1), sceneRect.width())); sceneRect.setWidth(qMax(qreal(1), sceneRect.width()));
sceneRect.setHeight(qMax(qreal(1), sceneRect.height())); sceneRect.setHeight(qMax(qreal(1), sceneRect.height()));
d->view->setSceneRect(sceneRect); d->view->setSceneRect(sceneRect);
d->view->resize(d->graphicsWidget->size().toSize()); d->view->resize(graphicsWidget->size().toSize());
d->view->centerOn(d->graphicsWidget); d->view->centerOn(graphicsWidget);
//if the view resized and a border is disabled move the dialog to make sure it will still look attached to panel/screen edge //if the view resized and a border is disabled move the dialog to make sure it will still look attached to panel/screen edge
qreal topHeight; qreal topHeight;
@ -445,14 +447,15 @@ void Dialog::resizeEvent(QResizeEvent *e)
setMask(QRect(QPoint(0, 0), size())); setMask(QRect(QPoint(0, 0), size()));
} }
if (d->resizeStartCorner != -1 && d->view && d->graphicsWidget) { if (d->resizeStartCorner != -1 && d->view && d->graphicsWidgetPtr) {
d->graphicsWidget->resize(d->view->size()); QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
graphicsWidget->resize(d->view->size());
QRectF sceneRect(d->graphicsWidget->sceneBoundingRect()); QRectF sceneRect(graphicsWidget->sceneBoundingRect());
sceneRect.setWidth(qMax(qreal(1), sceneRect.width())); sceneRect.setWidth(qMax(qreal(1), sceneRect.width()));
sceneRect.setHeight(qMax(qreal(1), sceneRect.height())); sceneRect.setHeight(qMax(qreal(1), sceneRect.height()));
d->view->setSceneRect(sceneRect); d->view->setSceneRect(sceneRect);
d->view->centerOn(d->graphicsWidget); d->view->centerOn(graphicsWidget);
} }
d->updateResizeCorners(); d->updateResizeCorners();
@ -501,11 +504,11 @@ void DialogPrivate::updateResizeCorners()
void Dialog::setGraphicsWidget(QGraphicsWidget *widget) void Dialog::setGraphicsWidget(QGraphicsWidget *widget)
{ {
if (d->graphicsWidget) { if (d->graphicsWidgetPtr) {
d->graphicsWidget->removeEventFilter(this); d->graphicsWidgetPtr.data()->removeEventFilter(this);
} }
d->graphicsWidget = widget; d->graphicsWidgetPtr = widget;
if (widget) { if (widget) {
if (!layout()) { if (!layout()) {
@ -540,12 +543,12 @@ void Dialog::setGraphicsWidget(QGraphicsWidget *widget)
//BIC FIXME: should be const //BIC FIXME: should be const
QGraphicsWidget *Dialog::graphicsWidget() QGraphicsWidget *Dialog::graphicsWidget()
{ {
return d->graphicsWidget; return d->graphicsWidgetPtr.data();
} }
bool Dialog::eventFilter(QObject *watched, QEvent *event) bool Dialog::eventFilter(QObject *watched, QEvent *event)
{ {
if (d->resizeStartCorner == Dialog::NoCorner && watched == d->graphicsWidget && if (d->resizeStartCorner == Dialog::NoCorner && watched == d->graphicsWidgetPtr.data() &&
(event->type() == QEvent::GraphicsSceneResize || event->type() == QEvent::GraphicsSceneMove)) { (event->type() == QEvent::GraphicsSceneResize || event->type() == QEvent::GraphicsSceneMove)) {
d->adjustViewTimer->start(150); d->adjustViewTimer->start(150);
} }
@ -567,7 +570,8 @@ void Dialog::showEvent(QShowEvent * event)
d->themeChanged(); d->themeChanged();
d->updateResizeCorners(); d->updateResizeCorners();
if (d->graphicsWidget && d->view && d->graphicsWidget->size().toSize() != d->view->size()) { QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
if (graphicsWidget && d->view && graphicsWidget->size().toSize() != d->view->size()) {
syncToGraphicsWidget(); syncToGraphicsWidget();
} }
@ -575,8 +579,8 @@ void Dialog::showEvent(QShowEvent * event)
d->view->setFocus(); d->view->setFocus();
} }
if (d->graphicsWidget) { if (graphicsWidget) {
d->graphicsWidget->setFocus(); graphicsWidget->setFocus();
} }
emit dialogVisible(true); emit dialogVisible(true);
@ -590,8 +594,9 @@ void Dialog::focusInEvent(QFocusEvent *event)
d->view->setFocus(); d->view->setFocus();
} }
if (d->graphicsWidget) { QGraphicsWidget *graphicsWidget = d->graphicsWidgetPtr.data();
d->graphicsWidget->setFocus(); if (graphicsWidget) {
graphicsWidget->setFocus();
} }
} }