From 17d0bca7afab7432194db38609c7397bfd4806ae Mon Sep 17 00:00:00 2001 From: Alexis Menard Date: Tue, 18 Aug 2009 11:05:17 +0200 Subject: Fix regression in clip path calculation for QGraphicsWidget. This fixes a regression when computing the clip path for a QGraphicsWidget that clip its children to its shape. The clip path cache use the bounding rect which has a null width and height the very first time on a QGraphicsWidget. This might happen as well for any item that have a null width and height (e.g. QGraphicsRectItem before you set the rect). We should do better in mainline by refactoring this clip path cache and calling it in prepareGeometryChange which should be called when you update the shape and the boundingRect of the item. Do we really need also in 4.6/mainline the clip path as it is now? This should be investigate also. The auto-test cover the problem so we can use it after for refactoring the cached clip path. Task-number: 257232 Reviewed-by: andreas Reviewed-by: bnilsen --- src/gui/graphicsview/qgraphicswidget.cpp | 2 + tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp | 82 ++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/gui/graphicsview/qgraphicswidget.cpp b/src/gui/graphicsview/qgraphicswidget.cpp index d52337d..525cf29 100644 --- a/src/gui/graphicsview/qgraphicswidget.cpp +++ b/src/gui/graphicsview/qgraphicswidget.cpp @@ -411,6 +411,8 @@ void QGraphicsWidget::setGeometry(const QRectF &rect) QSizeF oldSize = size(); QGraphicsLayoutItem::setGeometry(newGeom); + wd->invalidateCachedClipPathRecursively(); + // Send resize event bool resized = newGeom.size() != oldSize; if (resized) { diff --git a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp index 8fa4dc3..cb5f057 100644 --- a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp +++ b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp @@ -149,6 +149,7 @@ private slots: void closePopupOnOutsideClick(); void defaultSize(); void shortcutsDeletion(); + void ensureClipping(); // Task fixes void task236127_bspTreeIndexFails(); @@ -1928,6 +1929,87 @@ void tst_QGraphicsWidget::task250119_shortcutContext() scene.removeItem(&w_signal); } +class ClippingAndTransformsScene : public QGraphicsScene +{ +public: + QList drawnItems; +protected: + void drawItems(QPainter *painter, int numItems, QGraphicsItem *items[], + const QStyleOptionGraphicsItem options[], QWidget *widget = 0) + { + drawnItems.clear(); + for (int i = 0; i < numItems; ++i) + drawnItems << items[i]; + QGraphicsScene::drawItems(painter, numItems, items, options, widget); + } +}; + +class RectWidget : public QGraphicsWidget +{ +public: + + RectWidget(Qt::GlobalColor color, QGraphicsItem *parent=0) : QGraphicsWidget(parent), mColor(color) {} + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) + { + painter->setBrush(QBrush(mColor)); + painter->drawRect(boundingRect()); + } + + Qt::GlobalColor mColor; +}; + +class RectItem : public QGraphicsItem +{ +public: + + RectItem(Qt::GlobalColor color, QGraphicsItem *parent=0) : QGraphicsItem(parent), mColor(color) {} + + QRectF boundingRect() const + {return QRectF(10,10,50,50);} + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) + { + painter->setBrush(QBrush(mColor)); + painter->drawRect(boundingRect()); + } + + Qt::GlobalColor mColor; +}; + +void tst_QGraphicsWidget::ensureClipping() +{ + ClippingAndTransformsScene scene; + scene.setSceneRect(-50, -50, 200, 200); + + //A root that clip children + RectWidget *clipWidget = new RectWidget(Qt::black); + scene.addItem(clipWidget); + + clipWidget->setFlag(QGraphicsItem::ItemClipsChildrenToShape); + + //a child + RectWidget *childWidget = new RectWidget(Qt::red, clipWidget); + clipWidget->setGeometry(QRectF(10, 10, 100, 100)); + childWidget->setGeometry(QRectF(25, 25, 50, 50)); + + //We put a QGraphicsItem to be sure this one is also paint + RectItem *childitem = new RectItem(Qt::blue, clipWidget); + + QGraphicsView view(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(250); + + QList expected; + expected << clipWidget << childWidget << childitem; + QVERIFY(scene.drawnItems.contains(clipWidget)); + QVERIFY(scene.drawnItems.contains(childWidget)); + QVERIFY(scene.drawnItems.contains(childitem)); +} + QTEST_MAIN(tst_QGraphicsWidget) #include "tst_qgraphicswidget.moc" -- cgit v0.12