summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan-Arve Sæther <jan-arve.saether@nokia.com>2011-02-09 15:07:24 +0100
committerThierry Bastian <thierry.bastian@nokia.com>2011-05-12 15:47:26 +0200
commit913ff732004c2c61c39bcdd82ff05ea1827f328b (patch)
tree7e0b50b23cabaf3643fd64806e8ab6577e7e86d5 /src
parent1ce725cb60358a31b1107ab5c892abb7ceca8453 (diff)
Avoid flicker when invalidate is propagated in a widget/layout hierarchy
* Do not call invalidate from activateRecursive(). This resulted in that a layout was invalidated as many times as there were items in the layout. * Several improvements. Do not call resize(size()) too often. Calling resize() from the widgetEvent() is not very nice though... * Remove commented out code * make sure layout is activated even if the widget does not change size * activate the layout if the resize is same as size() * In order to not break existing apps, make this an opt-in feature with QGraphicsLayout::setInstantInvalidatePropagation(true); Reviewed-by: Frederik Gladhorn Reviewed-by: John Tapsell
Diffstat (limited to 'src')
-rw-r--r--src/gui/graphicsview/qgraphicslayout.cpp139
-rw-r--r--src/gui/graphicsview/qgraphicslayout.h2
-rw-r--r--src/gui/graphicsview/qgraphicslayout_p.cpp11
-rw-r--r--src/gui/graphicsview/qgraphicslinearlayout.cpp9
-rw-r--r--src/gui/graphicsview/qgraphicswidget.cpp105
5 files changed, 190 insertions, 76 deletions
diff --git a/src/gui/graphicsview/qgraphicslayout.cpp b/src/gui/graphicsview/qgraphicslayout.cpp
index 5bd298061d..a67ae48cc7 100644
--- a/src/gui/graphicsview/qgraphicslayout.cpp
+++ b/src/gui/graphicsview/qgraphicslayout.cpp
@@ -269,12 +269,20 @@ void QGraphicsLayout::activate()
return;
Q_ASSERT(!parentItem->isLayout());
- setGeometry(parentItem->contentsRect()); // relayout children
+ if (QGraphicsLayout::instantInvalidatePropagation()) {
+ QGraphicsWidget *parentWidget = static_cast<QGraphicsWidget*>(parentItem);
+ if (!parentWidget->parentLayoutItem()) {
+ // we've reached the topmost widget, resize it
+ bool wasResized = parentWidget->testAttribute(Qt::WA_Resized);
+ parentWidget->resize(parentWidget->size());
+ parentWidget->setAttribute(Qt::WA_Resized, wasResized);
+ }
- // ### bug, should be parentItem ?
- parentLayoutItem()->updateGeometry(); // bubble up; will set activated to false
- // ### too many resizes? maybe we should walk up the chain to the
- // ### top-level layouted layoutItem and call activate there.
+ setGeometry(parentItem->contentsRect()); // relayout children
+ } else {
+ setGeometry(parentItem->contentsRect()); // relayout children
+ parentLayoutItem()->updateGeometry();
+ }
}
/*!
@@ -300,32 +308,36 @@ bool QGraphicsLayout::isActivated() const
*/
void QGraphicsLayout::invalidate()
{
- // only mark layouts as invalid (activated = false) if we can post a LayoutRequest event.
- QGraphicsLayoutItem *layoutItem = this;
- while (layoutItem && layoutItem->isLayout()) {
- // we could call updateGeometry(), but what if that method
- // does not call the base implementation? In addition, updateGeometry()
- // does more than we need.
- layoutItem->d_func()->sizeHintCacheDirty = true;
- layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
- layoutItem = layoutItem->parentLayoutItem();
- }
- if (layoutItem) {
- layoutItem->d_func()->sizeHintCacheDirty = true;
- layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
- }
-
- bool postIt = layoutItem ? !layoutItem->isLayout() : false;
- if (postIt) {
- layoutItem = this;
- while (layoutItem && layoutItem->isLayout()
- && static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated) {
- static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated = false;
+ if (QGraphicsLayout::instantInvalidatePropagation()) {
+ updateGeometry();
+ } else {
+ // only mark layouts as invalid (activated = false) if we can post a LayoutRequest event.
+ QGraphicsLayoutItem *layoutItem = this;
+ while (layoutItem && layoutItem->isLayout()) {
+ // we could call updateGeometry(), but what if that method
+ // does not call the base implementation? In addition, updateGeometry()
+ // does more than we need.
+ layoutItem->d_func()->sizeHintCacheDirty = true;
+ layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
layoutItem = layoutItem->parentLayoutItem();
}
- if (layoutItem && !layoutItem->isLayout()) {
- // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
- QApplication::postEvent(static_cast<QGraphicsWidget *>(layoutItem), new QEvent(QEvent::LayoutRequest));
+ if (layoutItem) {
+ layoutItem->d_func()->sizeHintCacheDirty = true;
+ layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true;
+ }
+
+ bool postIt = layoutItem ? !layoutItem->isLayout() : false;
+ if (postIt) {
+ layoutItem = this;
+ while (layoutItem && layoutItem->isLayout()
+ && static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated) {
+ static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated = false;
+ layoutItem = layoutItem->parentLayoutItem();
+ }
+ if (layoutItem && !layoutItem->isLayout()) {
+ // If a layout has a parent that is not a layout it must be a QGraphicsWidget.
+ QApplication::postEvent(static_cast<QGraphicsWidget *>(layoutItem), new QEvent(QEvent::LayoutRequest));
+ }
}
}
}
@@ -335,12 +347,27 @@ void QGraphicsLayout::invalidate()
*/
void QGraphicsLayout::updateGeometry()
{
- QGraphicsLayoutItem::updateGeometry();
- if (QGraphicsLayoutItem *parentItem = parentLayoutItem()) {
- if (parentItem->isLayout()) {
+ Q_D(QGraphicsLayout);
+ if (QGraphicsLayout::instantInvalidatePropagation()) {
+ d->activated = false;
+ QGraphicsLayoutItem::updateGeometry();
+
+ QGraphicsLayoutItem *parentItem = parentLayoutItem();
+ if (!parentItem)
+ return;
+
+ if (parentItem->isLayout())
+ static_cast<QGraphicsLayout *>(parentItem)->invalidate();
+ else
parentItem->updateGeometry();
- } else {
- invalidate();
+ } else {
+ QGraphicsLayoutItem::updateGeometry();
+ if (QGraphicsLayoutItem *parentItem = parentLayoutItem()) {
+ if (parentItem->isLayout()) {
+ parentItem->updateGeometry();
+ } else {
+ invalidate();
+ }
}
}
}
@@ -446,6 +473,50 @@ void QGraphicsLayout::addChildLayoutItem(QGraphicsLayoutItem *layoutItem)
d->addChildLayoutItem(layoutItem);
}
+static bool g_instantInvalidatePropagation = false;
+
+/*!
+ \internal
+ \since 4.8
+ \see instantInvalidatePropagation
+
+ Calling this function with \a enable set to true will enable a feature that
+ makes propagation of invalidation up to ancestor layout items to be done in
+ one go. It will propagate up the parentLayoutItem() hierarchy until it has
+ reached the root. If the root item is a QGraphicsWidget, it will *post* a
+ layout request to it. When the layout request is consumed it will traverse
+ down the hierarchy of layouts and widgets and activate all layouts that is
+ invalid (not activated). This is the recommended behaviour.
+
+ If not set it will also propagate up the parentLayoutItem() hierarchy, but
+ it will stop at the \i first \i widget it encounters, and post a layout
+ request to the widget. When the layout request is consumed, this might
+ cause it to continue propagation up to the parentLayoutItem() of the
+ widget. It will continue in this fashion until it has reached a widget with
+ no parentLayoutItem(). This strategy might cause drawing artifacts, since
+ it is not done in one go, and the consumption of layout requests might be
+ interleaved by consumption of paint events, which might cause significant
+ flicker.
+ Note, this is not the recommended behavior, but for compatibility reasons
+ this is the default behaviour.
+*/
+void QGraphicsLayout::setInstantInvalidatePropagation(bool enable)
+{
+ g_instantInvalidatePropagation = enable;
+}
+
+/*!
+ \internal
+ \since 4.8
+ \see setInstantInvalidatePropagation
+
+ returns true if the complete widget/layout hierarchy is rearranged in one go.
+*/
+bool QGraphicsLayout::instantInvalidatePropagation()
+{
+ return g_instantInvalidatePropagation;
+}
+
QT_END_NAMESPACE
#endif //QT_NO_GRAPHICSVIEW
diff --git a/src/gui/graphicsview/qgraphicslayout.h b/src/gui/graphicsview/qgraphicslayout.h
index c622fb881e..60311744a2 100644
--- a/src/gui/graphicsview/qgraphicslayout.h
+++ b/src/gui/graphicsview/qgraphicslayout.h
@@ -76,6 +76,8 @@ public:
virtual QGraphicsLayoutItem *itemAt(int i) const = 0;
virtual void removeAt(int index) = 0;
+ static void setInstantInvalidatePropagation(bool enable);
+ static bool instantInvalidatePropagation();
protected:
QGraphicsLayout(QGraphicsLayoutPrivate &, QGraphicsLayoutItem *);
void addChildLayoutItem(QGraphicsLayoutItem *layoutItem);
diff --git a/src/gui/graphicsview/qgraphicslayout_p.cpp b/src/gui/graphicsview/qgraphicslayout_p.cpp
index c325602cc6..05e6cdf599 100644
--- a/src/gui/graphicsview/qgraphicslayout_p.cpp
+++ b/src/gui/graphicsview/qgraphicslayout_p.cpp
@@ -180,9 +180,14 @@ void QGraphicsLayoutPrivate::activateRecursive(QGraphicsLayoutItem *item)
{
if (item->isLayout()) {
QGraphicsLayout *layout = static_cast<QGraphicsLayout *>(item);
- if (layout->d_func()->activated)
- layout->invalidate();
-
+ if (layout->d_func()->activated) {
+ if (QGraphicsLayout::instantInvalidatePropagation()) {
+ return;
+ } else {
+ layout->invalidate(); // ### LOOKS SUSPICIOUSLY WRONG!!???
+ }
+ }
+
for (int i = layout->count() - 1; i >= 0; --i) {
QGraphicsLayoutItem *childItem = layout->itemAt(i);
if (childItem)
diff --git a/src/gui/graphicsview/qgraphicslinearlayout.cpp b/src/gui/graphicsview/qgraphicslinearlayout.cpp
index 5591638395..1229379ee8 100644
--- a/src/gui/graphicsview/qgraphicslinearlayout.cpp
+++ b/src/gui/graphicsview/qgraphicslinearlayout.cpp
@@ -275,17 +275,16 @@ void QGraphicsLinearLayout::insertItem(int index, QGraphicsLayoutItem *item)
qWarning("QGraphicsLinearLayout::insertItem: cannot insert itself");
return;
}
- Q_ASSERT(item);
- //the order of the following instructions is very important because
- //invalidating the layout before adding the child item will make the layout happen
- //before we try to paint the item
- invalidate();
d->addChildLayoutItem(item);
d->fixIndex(&index);
d->engine.insertRow(index, d->orientation);
new QGridLayoutItem(&d->engine, item, d->gridRow(index), d->gridColumn(index), 1, 1, 0, index);
+ //the order of the following instructions is very important because
+ //invalidating the layout before adding the child item will make the layout happen
+ //before we try to paint the item
+ invalidate();
}
/*!
diff --git a/src/gui/graphicsview/qgraphicswidget.cpp b/src/gui/graphicsview/qgraphicswidget.cpp
index 675a5c5c77..141e305acd 100644
--- a/src/gui/graphicsview/qgraphicswidget.cpp
+++ b/src/gui/graphicsview/qgraphicswidget.cpp
@@ -354,8 +354,10 @@ void QGraphicsWidget::setGeometry(const QRectF &rect)
newGeom = rect;
newGeom.setSize(rect.size().expandedTo(effectiveSizeHint(Qt::MinimumSize))
.boundedTo(effectiveSizeHint(Qt::MaximumSize)));
- if (newGeom == d->geom)
- return;
+
+ if (newGeom == d->geom) {
+ goto relayoutChildrenAndReturn;
+ }
// setPos triggers ItemPositionChange, which can adjust position
wd->inSetGeometry = 1;
@@ -363,8 +365,9 @@ void QGraphicsWidget::setGeometry(const QRectF &rect)
wd->inSetGeometry = 0;
newGeom.moveTopLeft(pos());
- if (newGeom == d->geom)
- return;
+ if (newGeom == d->geom) {
+ goto relayoutChildrenAndReturn;
+ }
// Update and prepare to change the geometry (remove from index) if the size has changed.
if (wd->scene) {
@@ -375,35 +378,54 @@ void QGraphicsWidget::setGeometry(const QRectF &rect)
}
// Update the layout item geometry
- bool moved = oldPos != pos();
- if (moved) {
- // Send move event.
- QGraphicsSceneMoveEvent event;
- event.setOldPos(oldPos);
- event.setNewPos(pos());
- QApplication::sendEvent(this, &event);
- if (wd->inSetPos) {
- //set the new pos
- d->geom.moveTopLeft(pos());
- emit geometryChanged();
- return;
+ {
+ bool moved = oldPos != pos();
+ if (moved) {
+ // Send move event.
+ QGraphicsSceneMoveEvent event;
+ event.setOldPos(oldPos);
+ event.setNewPos(pos());
+ QApplication::sendEvent(this, &event);
+ if (wd->inSetPos) {
+ //set the new pos
+ d->geom.moveTopLeft(pos());
+ emit geometryChanged();
+ goto relayoutChildrenAndReturn;
+ }
+ }
+ QSizeF oldSize = size();
+ QGraphicsLayoutItem::setGeometry(newGeom);
+ // Send resize event
+ bool resized = newGeom.size() != oldSize;
+ if (resized) {
+ QGraphicsSceneResizeEvent re;
+ re.setOldSize(oldSize);
+ re.setNewSize(newGeom.size());
+ if (oldSize.width() != newGeom.size().width())
+ emit widthChanged();
+ if (oldSize.height() != newGeom.size().height())
+ emit heightChanged();
+ QGraphicsLayout *lay = wd->layout;
+ if (QGraphicsLayout::instantInvalidatePropagation()) {
+ if (!lay || lay->isActivated()) {
+ QApplication::sendEvent(this, &re);
+ }
+ } else {
+ QApplication::sendEvent(this, &re);
+ }
}
}
- QSizeF oldSize = size();
- QGraphicsLayoutItem::setGeometry(newGeom);
- // Send resize event
- bool resized = newGeom.size() != oldSize;
- if (resized) {
- QGraphicsSceneResizeEvent re;
- re.setOldSize(oldSize);
- re.setNewSize(newGeom.size());
- if (oldSize.width() != newGeom.size().width())
- emit widthChanged();
- if (oldSize.height() != newGeom.size().height())
- emit heightChanged();
- QApplication::sendEvent(this, &re);
- }
+
emit geometryChanged();
+relayoutChildrenAndReturn:
+ if (QGraphicsLayout::instantInvalidatePropagation()) {
+ if (QGraphicsLayout *lay = wd->layout) {
+ if (!lay->isActivated()) {
+ QEvent layoutRequest(QEvent::LayoutRequest);
+ QApplication::sendEvent(this, &layoutRequest);
+ }
+ }
+ }
}
/*!
@@ -1052,16 +1074,31 @@ void QGraphicsWidget::updateGeometry()
QGraphicsLayoutItem *parentItem = parentLayoutItem();
if (parentItem && parentItem->isLayout()) {
- parentItem->updateGeometry();
+ if (QGraphicsLayout::instantInvalidatePropagation()) {
+ static_cast<QGraphicsLayout *>(parentItem)->invalidate();
+ } else {
+ parentItem->updateGeometry();
+ }
} else {
if (parentItem) {
+ // This is for custom layouting
QGraphicsWidget *parentWid = parentWidget(); //###
if (parentWid->isVisible())
QApplication::postEvent(parentWid, new QEvent(QEvent::LayoutRequest));
+ } else {
+ /**
+ * If this is the topmost widget, post a LayoutRequest event to the widget.
+ * When the event is received, it will start flowing all the way down to the leaf
+ * widgets in one go. This will make a relayout flicker-free.
+ */
+ if (QGraphicsLayout::instantInvalidatePropagation())
+ QApplication::postEvent(static_cast<QGraphicsWidget *>(this), new QEvent(QEvent::LayoutRequest));
+ }
+ if (!QGraphicsLayout::instantInvalidatePropagation()) {
+ bool wasResized = testAttribute(Qt::WA_Resized);
+ resize(size()); // this will restrict the size
+ setAttribute(Qt::WA_Resized, wasResized);
}
- bool wasResized = testAttribute(Qt::WA_Resized);
- resize(size()); // this will restrict the size
- setAttribute(Qt::WA_Resized, wasResized);
}
}