From 26a1acf139f7a444bc68d9a1540d418c41aa89ec Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 21 Jul 2010 14:16:34 +1000 Subject: Bounding rect of text was not always calculated correctly. The boundingRect depended upon the image cache which may not become valid until after boundingRect is called. Task-number: QTBUG-12291 Reviewed-by: Michael Brasser (cherry picked from commit 3aeafb4839f49f524f10eae65be27fd189d37060) --- src/declarative/graphicsitems/qdeclarativetext.cpp | 80 ++++++++-------------- .../declarative/qdeclarativetext/data/rotated.qml | 18 +++++ .../qdeclarativetext/tst_qdeclarativetext.cpp | 19 +++++ 3 files changed, 64 insertions(+), 53 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativetext/data/rotated.qml diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 9b0391d..83911cb 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -710,63 +710,37 @@ QRectF QDeclarativeText::boundingRect() const int x = 0; int y = 0; - // Could include font max left/right bearings to either side of rectangle. - - if (d->cache || d->style != Normal) { - QDeclarativeTextPrivate *dd = const_cast(d); - dd->checkImgCache(); - switch (d->hAlign) { - case AlignLeft: - x = 0; - break; - case AlignRight: - x = w - d->imgCache.width(); - break; - case AlignHCenter: - x = (w - d->imgCache.width()) / 2; - break; - } - - switch (d->vAlign) { - case AlignTop: - y = 0; - break; - case AlignBottom: - y = h - d->imgCache.height(); - break; - case AlignVCenter: - y = (h - d->imgCache.height()) / 2; - break; - } + QSize size = d->cachedLayoutSize; + if (d->style != Normal) + size += QSize(2,2); - return QRectF(x,y,d->imgCache.width(),d->imgCache.height()); - } else { - switch (d->hAlign) { - case AlignLeft: - x = 0; - break; - case AlignRight: - x = w - d->cachedLayoutSize.width(); - break; - case AlignHCenter: - x = (w - d->cachedLayoutSize.width()) / 2; - break; - } + // Could include font max left/right bearings to either side of rectangle. - switch (d->vAlign) { - case AlignTop: - y = 0; - break; - case AlignBottom: - y = h - d->cachedLayoutSize.height(); - break; - case AlignVCenter: - y = (h - d->cachedLayoutSize.height()) / 2; - break; - } + switch (d->hAlign) { + case AlignLeft: + x = 0; + break; + case AlignRight: + x = w - size.width(); + break; + case AlignHCenter: + x = (w - size.width()) / 2; + break; + } - return QRectF(x,y,d->cachedLayoutSize.width(),d->cachedLayoutSize.height()); + switch (d->vAlign) { + case AlignTop: + y = 0; + break; + case AlignBottom: + y = h - size.height(); + break; + case AlignVCenter: + y = (h - size.height()) / 2; + break; } + + return QRectF(x,y,size.width(),size.height()); } void QDeclarativeText::geometryChanged(const QRectF &newGeometry, diff --git a/tests/auto/declarative/qdeclarativetext/data/rotated.qml b/tests/auto/declarative/qdeclarativetext/data/rotated.qml new file mode 100644 index 0000000..01eec44 --- /dev/null +++ b/tests/auto/declarative/qdeclarativetext/data/rotated.qml @@ -0,0 +1,18 @@ +import Qt 4.7 + +Rectangle { + width : 200 + height : 100 + + Text { + objectName: "text" + x: 20 + y: 20 + height : 20 + width : 80 + text : "Something" + rotation : 30 + transformOrigin : Item.TopLeft + } +} + diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index 821394d..658f381 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -97,6 +97,8 @@ private slots: void clickLink(); + void QTBUG_12291(); + private: QStringList standard; QStringList richText; @@ -898,6 +900,23 @@ void tst_qdeclarativetext::wordSpacing() } } +void tst_qdeclarativetext::QTBUG_12291() +{ + QDeclarativeView *canvas = createView(SRCDIR "/data/rotated.qml"); + + canvas->show(); + QApplication::setActiveWindow(canvas); + QTest::qWaitForWindowShown(canvas); + QTRY_COMPARE(QApplication::activeWindow(), static_cast(canvas)); + + QObject *ob = canvas->rootObject(); + QVERIFY(ob != 0); + + QDeclarativeText *text = ob->findChild("text"); + QVERIFY(text); + QVERIFY(text->boundingRect().isValid()); +} + class EventSender : public QGraphicsItem { public: -- cgit v0.12