From 946134b0ff303a1b44ae1ce5e3dcde7bfd8febcc Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Tue, 27 Apr 2010 19:06:32 +1000 Subject: Setting conflicting anchors to items inside positioners should print a warning Task-number: QTBUG-9025 Reviewed-by: Martin Jones --- .../graphicsitems/qdeclarativepositioners.cpp | 69 +++++++++++++++++++++ .../graphicsitems/qdeclarativepositioners_p.h | 6 +- .../tst_qdeclarativepositioners.cpp | 72 ++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) diff --git a/src/declarative/graphicsitems/qdeclarativepositioners.cpp b/src/declarative/graphicsitems/qdeclarativepositioners.cpp index 21c33e2..32a512b 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners.cpp +++ b/src/declarative/graphicsitems/qdeclarativepositioners.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -165,6 +166,7 @@ void QDeclarativeBasePositioner::componentComplete() QDeclarativeItem::componentComplete(); positionedItems.reserve(d->QGraphicsItemPrivate::children.count()); prePositioning(); + reportConflictingAnchors(); } QVariant QDeclarativeBasePositioner::itemChange(GraphicsItemChange change, @@ -436,6 +438,26 @@ void QDeclarativeColumn::doPositioning(QSizeF *contentSize) contentSize->setHeight(voffset - spacing()); } +void QDeclarativeColumn::reportConflictingAnchors() +{ + bool childsWithConflictingAnchors(false); + for (int ii = 0; ii < positionedItems.count(); ++ii) { + const PositionedItem &child = positionedItems.at(ii); + if (child.item) { + QDeclarativeAnchors::Anchors usedAnchors = child.item->anchors()->usedAnchors(); + if (usedAnchors & QDeclarativeAnchors::TopAnchor || + usedAnchors & QDeclarativeAnchors::BottomAnchor || + usedAnchors & QDeclarativeAnchors::VCenterAnchor) { + childsWithConflictingAnchors = true; + break; + } + } + } + if (childsWithConflictingAnchors) { + qmlInfo(this) << "Cannot specify top, bottom or verticalCenter anchors for items inside Column"; + } +} + /*! \qmlclass Row QDeclarativeRow \since 4.7 @@ -551,6 +573,25 @@ void QDeclarativeRow::doPositioning(QSizeF *contentSize) contentSize->setWidth(hoffset - spacing()); } +void QDeclarativeRow::reportConflictingAnchors() +{ + bool childsWithConflictingAnchors(false); + for (int ii = 0; ii < positionedItems.count(); ++ii) { + const PositionedItem &child = positionedItems.at(ii); + if (child.item) { + QDeclarativeAnchors::Anchors usedAnchors = child.item->anchors()->usedAnchors(); + if (usedAnchors & QDeclarativeAnchors::LeftAnchor || + usedAnchors & QDeclarativeAnchors::RightAnchor || + usedAnchors & QDeclarativeAnchors::HCenterAnchor) { + childsWithConflictingAnchors = true; + break; + } + } + } + if (childsWithConflictingAnchors) { + qmlInfo(this) << "Cannot specify left, right or horizontalCenter anchors for items inside Row"; + } +} /*! \qmlclass Grid QDeclarativeGrid @@ -823,6 +864,20 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) } } +void QDeclarativeGrid::reportConflictingAnchors() +{ + bool childsWithConflictingAnchors(false); + for (int ii = 0; ii < positionedItems.count(); ++ii) { + const PositionedItem &child = positionedItems.at(ii); + if (child.item && child.item->anchors()->usedAnchors()) { + childsWithConflictingAnchors = true; + break; + } + } + if (childsWithConflictingAnchors) { + qmlInfo(this) << "Cannot specify anchors for items inside Grid"; + } +} /*! \qmlclass Flow QDeclarativeFlow @@ -966,5 +1021,19 @@ void QDeclarativeFlow::doPositioning(QSizeF *contentSize) } } +void QDeclarativeFlow::reportConflictingAnchors() +{ + bool childsWithConflictingAnchors(false); + for (int ii = 0; ii < positionedItems.count(); ++ii) { + const PositionedItem &child = positionedItems.at(ii); + if (child.item && child.item->anchors()->usedAnchors()) { + childsWithConflictingAnchors = true; + break; + } + } + if (childsWithConflictingAnchors) { + qmlInfo(this) << "Cannot specify anchors for items inside Flow"; + } +} QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativepositioners_p.h b/src/declarative/graphicsitems/qdeclarativepositioners_p.h index b5fc979..787dcd3 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners_p.h +++ b/src/declarative/graphicsitems/qdeclarativepositioners_p.h @@ -94,6 +94,7 @@ protected Q_SLOTS: protected: virtual void doPositioning(QSizeF *contentSize)=0; + virtual void reportConflictingAnchors()=0; struct PositionedItem { PositionedItem(QDeclarativeItem *i) : item(i), isNew(false), isVisible(true) {} bool operator==(const PositionedItem &other) const { return other.item == item; } @@ -118,6 +119,7 @@ public: QDeclarativeColumn(QDeclarativeItem *parent=0); protected: virtual void doPositioning(QSizeF *contentSize); + virtual void reportConflictingAnchors(); private: Q_DISABLE_COPY(QDeclarativeColumn) }; @@ -129,6 +131,7 @@ public: QDeclarativeRow(QDeclarativeItem *parent=0); protected: virtual void doPositioning(QSizeF *contentSize); + virtual void reportConflictingAnchors(); private: Q_DISABLE_COPY(QDeclarativeRow) }; @@ -161,6 +164,7 @@ Q_SIGNALS: protected: virtual void doPositioning(QSizeF *contentSize); + virtual void reportConflictingAnchors(); private: int m_rows; @@ -187,7 +191,7 @@ Q_SIGNALS: protected: virtual void doPositioning(QSizeF *contentSize); - + virtual void reportConflictingAnchors(); protected: QDeclarativeFlow(QDeclarativeFlowPrivate &dd, QDeclarativeItem *parent); private: diff --git a/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp b/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp index b4ac0e1..8dc1416 100644 --- a/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp +++ b/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -69,6 +70,7 @@ private slots: void test_repeater(); void test_flow(); void test_flow_resize(); + void test_conflictinganchors(); private: QDeclarativeView *createView(const QString &filename); }; @@ -620,6 +622,76 @@ void tst_QDeclarativePositioners::test_flow_resize() QCOMPARE(five->y(), 50.0); } +QString warningMessage; + +void interceptWarnings(QtMsgType type, const char *msg) +{ + Q_UNUSED( type ); + warningMessage = msg; +} + +void tst_QDeclarativePositioners::test_conflictinganchors() +{ + qInstallMsgHandler(interceptWarnings); + QDeclarativeEngine engine; + QDeclarativeComponent component(&engine); + + component.setData("import Qt 4.7\nColumn { Item {} }", QUrl::fromLocalFile("")); + QDeclarativeItem *item = qobject_cast(component.create()); + QVERIFY(item); + QVERIFY(warningMessage.isEmpty()); + + component.setData("import Qt 4.7\nRow { Item {} }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QVERIFY(warningMessage.isEmpty()); + + component.setData("import Qt 4.7\nGrid { Item {} }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QVERIFY(warningMessage.isEmpty()); + + component.setData("import Qt 4.7\nFlow { Item {} }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QVERIFY(warningMessage.isEmpty()); + + component.setData("import Qt 4.7\nColumn { Item { anchors.top: parent.top } }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QCOMPARE(warningMessage, QString("file::2:1: QML Column: Cannot specify top, bottom or verticalCenter anchors for items inside Column")); + warningMessage.clear(); + + component.setData("import Qt 4.7\nColumn { Item { anchors.left: parent.left } }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QVERIFY(warningMessage.isEmpty()); + warningMessage.clear(); + + component.setData("import Qt 4.7\nRow { Item { anchors.left: parent.left } }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QCOMPARE(warningMessage, QString("file::2:1: QML Row: Cannot specify left, right or horizontalCenter anchors for items inside Row")); + warningMessage.clear(); + + component.setData("import Qt 4.7\nRow { Item { anchors.top: parent.top } }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QVERIFY(warningMessage.isEmpty()); + warningMessage.clear(); + + component.setData("import Qt 4.7\nGrid { Item { anchors.horizontalCenter: parent.horizontalCenter } }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QCOMPARE(warningMessage, QString("file::2:1: QML Grid: Cannot specify anchors for items inside Grid")); + warningMessage.clear(); + + component.setData("import Qt 4.7\nFlow { Item { anchors.verticalCenter: parent.verticalCenter } }", QUrl::fromLocalFile("")); + item = qobject_cast(component.create()); + QVERIFY(item); + QCOMPARE(warningMessage, QString("file::2:1: QML Flow: Cannot specify anchors for items inside Flow")); +} + QDeclarativeView *tst_QDeclarativePositioners::createView(const QString &filename) { QDeclarativeView *canvas = new QDeclarativeView(0); -- cgit v0.12