From 57d6bf52074761a0c6e9a79829ec845a7b0061fa Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 10 Dec 2009 12:22:29 +0100 Subject: Add anchors.margins Task-number: QTBUG-6676 --- .../graphicsitems/qmlgraphicsanchors.cpp | 25 ++++++++++++++++++ .../graphicsitems/qmlgraphicsanchors_p.h | 5 ++++ .../graphicsitems/qmlgraphicsanchors_p_p.h | 5 ++-- tests/auto/declarative/anchors/data/margins.qml | 13 ++++++++++ tests/auto/declarative/anchors/tst_anchors.cpp | 30 ++++++++++++++++++++-- 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 tests/auto/declarative/anchors/data/margins.qml diff --git a/src/declarative/graphicsitems/qmlgraphicsanchors.cpp b/src/declarative/graphicsitems/qmlgraphicsanchors.cpp index c28ca02..96d76cf 100644 --- a/src/declarative/graphicsitems/qmlgraphicsanchors.cpp +++ b/src/declarative/graphicsitems/qmlgraphicsanchors.cpp @@ -859,6 +859,31 @@ void QmlGraphicsAnchors::setRightMargin(qreal offset) emit rightMarginChanged(); } +qreal QmlGraphicsAnchors::margins() const +{ + Q_D(const QmlGraphicsAnchors); + return d->margins; +} + +void QmlGraphicsAnchors::setMargins(qreal offset) +{ + Q_D(QmlGraphicsAnchors); + if (d->margins == offset) + return; + //###Is it significantly faster to set them directly so we can call fillChanged only once? + if(!d->rightMargin || d->rightMargin == d->margins) + setRightMargin(offset); + if(!d->leftMargin || d->leftMargin == d->margins) + setLeftMargin(offset); + if(!d->topMargin || d->topMargin == d->margins) + setTopMargin(offset); + if(!d->bottomMargin || d->bottomMargin == d->margins) + setBottomMargin(offset); + d->margins = offset; + emit marginsChanged(); + +} + qreal QmlGraphicsAnchors::horizontalCenterOffset() const { Q_D(const QmlGraphicsAnchors); diff --git a/src/declarative/graphicsitems/qmlgraphicsanchors_p.h b/src/declarative/graphicsitems/qmlgraphicsanchors_p.h index 2f2ca74..a67a9f8 100644 --- a/src/declarative/graphicsitems/qmlgraphicsanchors_p.h +++ b/src/declarative/graphicsitems/qmlgraphicsanchors_p.h @@ -67,6 +67,7 @@ class Q_DECLARATIVE_EXPORT QmlGraphicsAnchors : public QObject Q_PROPERTY(QmlGraphicsAnchorLine bottom READ bottom WRITE setBottom RESET resetBottom NOTIFY bottomChanged) Q_PROPERTY(QmlGraphicsAnchorLine verticalCenter READ verticalCenter WRITE setVerticalCenter RESET resetVerticalCenter NOTIFY verticalCenterChanged) Q_PROPERTY(QmlGraphicsAnchorLine baseline READ baseline WRITE setBaseline RESET resetBaseline NOTIFY baselineChanged) + Q_PROPERTY(qreal margins READ margins WRITE setMargins NOTIFY marginsChanged) Q_PROPERTY(qreal leftMargin READ leftMargin WRITE setLeftMargin NOTIFY leftMarginChanged) Q_PROPERTY(qreal rightMargin READ rightMargin WRITE setRightMargin NOTIFY rightMarginChanged) Q_PROPERTY(qreal horizontalCenterOffset READ horizontalCenterOffset WRITE setHorizontalCenterOffset NOTIFY horizontalCenterOffsetChanged()) @@ -137,6 +138,9 @@ public: qreal bottomMargin() const; void setBottomMargin(qreal); + qreal margins() const; + void setMargins(qreal); + qreal verticalCenterOffset() const; void setVerticalCenterOffset(qreal); @@ -172,6 +176,7 @@ Q_SIGNALS: void rightMarginChanged(); void topMarginChanged(); void bottomMarginChanged(); + void marginsChanged(); void verticalCenterOffsetChanged(); void horizontalCenterOffsetChanged(); void baselineOffsetChanged(); diff --git a/src/declarative/graphicsitems/qmlgraphicsanchors_p_p.h b/src/declarative/graphicsitems/qmlgraphicsanchors_p_p.h index be3b6dc..4f7fde0 100644 --- a/src/declarative/graphicsitems/qmlgraphicsanchors_p_p.h +++ b/src/declarative/graphicsitems/qmlgraphicsanchors_p_p.h @@ -98,8 +98,8 @@ public: QmlGraphicsAnchorsPrivate() : updatingMe(false), updatingHorizontalAnchor(0), updatingVerticalAnchor(0), updatingFill(0), updatingCenterIn(0), item(0), usedAnchors(0), fill(0), - centerIn(0), leftMargin(0), rightMargin(0), topMargin(0), - bottomMargin(0), vCenterOffset(0), hCenterOffset(0), baselineOffset(0), + centerIn(0), leftMargin(0), rightMargin(0), topMargin(0), bottomMargin(0), + margins(0), vCenterOffset(0), hCenterOffset(0), baselineOffset(0), componentComplete(true) { } @@ -159,6 +159,7 @@ public: qreal rightMargin; qreal topMargin; qreal bottomMargin; + qreal margins; qreal vCenterOffset; qreal hCenterOffset; qreal baselineOffset; diff --git a/tests/auto/declarative/anchors/data/margins.qml b/tests/auto/declarative/anchors/data/margins.qml new file mode 100644 index 0000000..4a29e77 --- /dev/null +++ b/tests/auto/declarative/anchors/data/margins.qml @@ -0,0 +1,13 @@ +import Qt 4.6 + +Rectangle { + width: 200; height: 200 + Rectangle { + objectName: "filler" + width: 50; height: 50; color: "blue" + anchors.fill: parent; + anchors.margins: 10 + anchors.leftMargin: 5 + anchors.topMargin: 6 + } +} diff --git a/tests/auto/declarative/anchors/tst_anchors.cpp b/tests/auto/declarative/anchors/tst_anchors.cpp index 8967725..defc841 100644 --- a/tests/auto/declarative/anchors/tst_anchors.cpp +++ b/tests/auto/declarative/anchors/tst_anchors.cpp @@ -73,6 +73,7 @@ private slots: void crash1(); void centerIn(); void fill(); + void margins(); }; /* @@ -389,8 +390,8 @@ void tst_anchors::fill() view->execute(); qApp->processEvents(); QmlGraphicsRectangle* rect = findItem(view->root(), QLatin1String("filler")); - QCOMPARE(rect->x(), 0.0 + 10); - QCOMPARE(rect->y(), 0.0 + 30); + QCOMPARE(rect->x(), 0.0 + 10.0); + QCOMPARE(rect->y(), 0.0 + 30.0); QCOMPARE(rect->width(), 200.0 - 10.0 - 20.0); QCOMPARE(rect->height(), 200.0 - 30.0 - 40.0); //Alter Offsets (QTBUG-6631) @@ -426,6 +427,31 @@ void tst_anchors::centerIn() delete view; } +void tst_anchors::margins() +{ + QmlView *view = new QmlView; + + view->setUrl(QUrl("file://" SRCDIR "/data/margins.qml")); + + view->execute(); + qApp->processEvents(); + QmlGraphicsRectangle* rect = findItem(view->root(), QLatin1String("filler")); + QCOMPARE(rect->x(), 5.0); + QCOMPARE(rect->y(), 6.0); + QCOMPARE(rect->width(), 200.0 - 5.0 - 10.0); + QCOMPARE(rect->height(), 200.0 - 6.0 - 10.0); + + rect->anchors()->setTopMargin(0.0); + rect->anchors()->setMargins(20.0); + + QCOMPARE(rect->x(), 5.0); + QCOMPARE(rect->y(), 20.0); + QCOMPARE(rect->width(), 200.0 - 5.0 - 20.0); + QCOMPARE(rect->height(), 200.0 - 20.0 - 20.0); + + delete view; +} + QTEST_MAIN(tst_anchors) #include "tst_anchors.moc" -- cgit v0.12