From b18a343cc63f93a8e97a8c6c623e56a666775b57 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Tue, 15 Jun 2010 15:23:28 +1000 Subject: Fix bug with childrenRect resizing on startup. This also optimizes the implementation. Task-number: QTBUG-11383 --- src/declarative/graphicsitems/qdeclarativeitem.cpp | 28 ++++++++++------------ src/declarative/graphicsitems/qdeclarativeitem_p.h | 7 +++--- .../qdeclarativeitem/data/childrenRectBug.qml | 23 ++++++++++++++++++ .../qdeclarativeitem/tst_qdeclarativeitem.cpp | 17 +++++++++++++ 4 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativeitem/data/childrenRectBug.qml diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index 18806e7..42b370b 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -231,9 +231,10 @@ QT_BEGIN_NAMESPACE \brief The QDeclarativeContents class gives access to the height and width of an item's contents. */ - -QDeclarativeContents::QDeclarativeContents() : m_x(0), m_y(0), m_width(0), m_height(0) +QDeclarativeContents::QDeclarativeContents(QDeclarativeItem *item) : m_item(item), m_x(0), m_y(0), m_width(0), m_height(0) { + //### optimize + connect(this, SIGNAL(rectChanged(QRectF)), m_item, SIGNAL(childrenRectChanged(QRectF))); } QDeclarativeContents::~QDeclarativeContents() @@ -328,12 +329,8 @@ void QDeclarativeContents::calcWidth(QDeclarativeItem *changed) emit rectChanged(rectF()); } -void QDeclarativeContents::setItem(QDeclarativeItem *item) +void QDeclarativeContents::complete() { - m_item = item; - //### optimize - connect(this, SIGNAL(rectChanged(QRectF)), m_item, SIGNAL(childrenRectChanged(QRectF))); - QList children = m_item->childItems(); for (int i = 0; i < children.count(); ++i) { QDeclarativeItem *child = qobject_cast(children.at(i)); @@ -343,9 +340,7 @@ void QDeclarativeContents::setItem(QDeclarativeItem *item) //###what about changes to visibility? } - //### defer until componentComplete - calcHeight(); - calcWidth(); + calcGeometry(); } void QDeclarativeContents::itemGeometryChanged(QDeclarativeItem *changed, const QRectF &newGeometry, const QRectF &oldGeometry) @@ -360,16 +355,14 @@ void QDeclarativeContents::itemDestroyed(QDeclarativeItem *item) { if (item) QDeclarativeItemPrivate::get(item)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed); - calcWidth(); - calcHeight(); + calcGeometry(); } void QDeclarativeContents::childRemoved(QDeclarativeItem *item) { if (item) QDeclarativeItemPrivate::get(item)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed); - calcWidth(); - calcHeight(); + calcGeometry(); } void QDeclarativeContents::childAdded(QDeclarativeItem *item) @@ -1752,8 +1745,9 @@ QRectF QDeclarativeItem::childrenRect() { Q_D(QDeclarativeItem); if (!d->_contents) { - d->_contents = new QDeclarativeContents; - d->_contents->setItem(this); + d->_contents = new QDeclarativeContents(this); + if (d->_componentComplete) + d->_contents->complete(); } return d->_contents->rectF(); } @@ -2619,6 +2613,8 @@ void QDeclarativeItem::componentComplete() } if (d->keyHandler) d->keyHandler->componentComplete(); + if (d->_contents) + d->_contents->complete(); } QDeclarativeStateGroup *QDeclarativeItemPrivate::_states() diff --git a/src/declarative/graphicsitems/qdeclarativeitem_p.h b/src/declarative/graphicsitems/qdeclarativeitem_p.h index 184d6f1..fb416c2 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem_p.h +++ b/src/declarative/graphicsitems/qdeclarativeitem_p.h @@ -83,16 +83,17 @@ class QDeclarativeContents : public QObject, public QDeclarativeItemChangeListen { Q_OBJECT public: - QDeclarativeContents(); + QDeclarativeContents(QDeclarativeItem *item); ~QDeclarativeContents(); QRectF rectF() const; - void setItem(QDeclarativeItem *item); - void childRemoved(QDeclarativeItem *item); void childAdded(QDeclarativeItem *item); + void calcGeometry() { calcWidth(); calcHeight(); } + void complete(); + Q_SIGNALS: void rectChanged(QRectF); diff --git a/tests/auto/declarative/qdeclarativeitem/data/childrenRectBug.qml b/tests/auto/declarative/qdeclarativeitem/data/childrenRectBug.qml new file mode 100644 index 0000000..4a2f056 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeitem/data/childrenRectBug.qml @@ -0,0 +1,23 @@ +import Qt 4.7 + +Rectangle { + width: 400 + height: 200 + + Item { + objectName: "theItem" + anchors.centerIn: parent + width: childrenRect.width + height: childrenRect.height + Rectangle { + id: text1 + anchors.verticalCenter: parent.verticalCenter + width: 100; height: 100; color: "green" + } + Rectangle { + anchors.left: text1.right + anchors.verticalCenter: parent.verticalCenter + width: 100; height: 100; color: "green" + } + } +} diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index c2d3660..0a66245 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -72,6 +72,7 @@ private slots: void transforms(); void transforms_data(); void childrenRect(); + void childrenRectBug(); void childrenProperty(); void resourcesProperty(); @@ -736,6 +737,22 @@ void tst_QDeclarativeItem::childrenRect() delete o; } +// QTBUG-11383 +void tst_QDeclarativeItem::childrenRectBug() +{ + QDeclarativeView *canvas = new QDeclarativeView(0); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/childrenRectBug.qml")); + canvas->show(); + + QGraphicsObject *o = canvas->rootObject(); + QDeclarativeItem *item = o->findChild("theItem"); + QCOMPARE(item->width(), qreal(200)); + QCOMPARE(item->height(), qreal(100)); + QCOMPARE(item->x(), qreal(100)); + + delete canvas; +} + template T *tst_QDeclarativeItem::findItem(QGraphicsObject *parent, const QString &objectName) { -- cgit v0.12 From c1e6d97b9476c859abc3927046144b4006fcf735 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Tue, 15 Jun 2010 15:49:08 +1000 Subject: Remove duplicated code. --- src/declarative/graphicsitems/qdeclarativetextedit.cpp | 8 +------- src/declarative/graphicsitems/qdeclarativetextinput.cpp | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 3b4f2a7..3106daf 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -1113,13 +1113,7 @@ void QDeclarativeTextEdit::mousePressEvent(QGraphicsSceneMouseEvent *event) Q_D(QDeclarativeTextEdit); if (d->focusOnPress){ bool hadFocus = hasFocus(); - QGraphicsItem *p = parentItem();//###Is there a better way to find my focus scope? - while(p) { - if (p->flags() & QGraphicsItem::ItemIsFocusScope) - p->setFocus(); - p = p->parentItem(); - } - setFocus(true); + forceFocus(); if (d->showInputPanelOnFocus) { if (hasFocus() && hadFocus && !isReadOnly()) { // re-open input panel on press if already focused diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 633c01e..cba01ef 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -924,13 +924,7 @@ void QDeclarativeTextInput::mousePressEvent(QGraphicsSceneMouseEvent *event) Q_D(QDeclarativeTextInput); if(d->focusOnPress){ bool hadFocus = hasFocus(); - QGraphicsItem *p = parentItem();//###Is there a better way to find my focus scope? - while(p) { - if (p->flags() & QGraphicsItem::ItemIsFocusScope) - p->setFocus(); - p = p->parentItem(); - } - setFocus(true); + forceFocus(); if (d->showInputPanelOnFocus) { if (hasFocus() && hadFocus && !isReadOnly()) { // re-open input panel on press if already focused -- cgit v0.12