From db9be0a9d6195bc52415ce28d605ace149d02782 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Fri, 22 May 2015 13:03:25 +0200 Subject: [PATCH] Add left, right, top and bottom properties to basic QML rect type. Task-number: QTBUG-45528 Task-number: QTBUG-45530 Change-Id: I83c4056b4bde37ef2dc4424ffddd823c1654d92e Reviewed-by: Simon Hausmann --- src/qml/doc/src/qmltypereference.qdoc | 3 ++ src/qml/qml/qqmlvaluetype.cpp | 39 +++++++++++++++++++ src/qml/qml/qqmlvaluetype_p.h | 18 +++++++++ src/qml/qml/qqmlvaluetypewrapper.cpp | 10 +++-- .../qml/qqmlvaluetypes/data/rect_compare.qml | 4 -- .../qml/qqmlvaluetypes/data/rect_read.qml | 4 ++ .../qml/qqmlvaluetypes/data/rectf_compare.qml | 4 -- .../qml/qqmlvaluetypes/data/rectf_read.qml | 4 ++ .../qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp | 8 ++++ 9 files changed, 82 insertions(+), 12 deletions(-) diff --git a/src/qml/doc/src/qmltypereference.qdoc b/src/qml/doc/src/qmltypereference.qdoc index 48f7ba1ba..31133c862 100644 --- a/src/qml/doc/src/qmltypereference.qdoc +++ b/src/qml/doc/src/qmltypereference.qdoc @@ -208,6 +208,9 @@ Or use the \l{QtQml::Qt::rect()}{Qt.rect()} function: CustomObject { myRectProperty: Qt.rect(50, 50, 100, 100) } \endqml +The \c rect type also exposes read-only \c left, \c right, \c top and \c bottom +attributes. These are the same as their \l {QRectF}{C++ counterparts}. + When integrating with C++, note that any QRect or QRectF value \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically converted into a \c rect value, and vice-versa. When a \c rect value is passed to C++, it diff --git a/src/qml/qml/qqmlvaluetype.cpp b/src/qml/qml/qqmlvaluetype.cpp index 341ddf802..b14726608 100644 --- a/src/qml/qml/qqmlvaluetype.cpp +++ b/src/qml/qml/qqmlvaluetype.cpp @@ -388,6 +388,26 @@ void QQmlRectFValueType::setHeight(qreal h) v.setHeight(h); } +qreal QQmlRectFValueType::left() const +{ + return v.left(); +} + +qreal QQmlRectFValueType::right() const +{ + return v.right(); +} + +qreal QQmlRectFValueType::top() const +{ + return v.top(); +} + +qreal QQmlRectFValueType::bottom() const +{ + return v.bottom(); +} + int QQmlRectValueType::x() const { return v.x(); @@ -428,6 +448,25 @@ void QQmlRectValueType::setHeight(int h) v.setHeight(h); } +int QQmlRectValueType::left() const +{ + return v.left(); +} + +int QQmlRectValueType::right() const +{ + return v.right(); +} + +int QQmlRectValueType::top() const +{ + return v.top(); +} + +int QQmlRectValueType::bottom() const +{ + return v.bottom(); +} QQmlEasingValueType::Type QQmlEasingValueType::type() const { diff --git a/src/qml/qml/qqmlvaluetype_p.h b/src/qml/qml/qqmlvaluetype_p.h index be453ae35..2c02cc0aa 100644 --- a/src/qml/qml/qqmlvaluetype_p.h +++ b/src/qml/qml/qqmlvaluetype_p.h @@ -153,6 +153,10 @@ struct QQmlRectFValueType Q_PROPERTY(qreal y READ y WRITE setY FINAL) Q_PROPERTY(qreal width READ width WRITE setWidth FINAL) Q_PROPERTY(qreal height READ height WRITE setHeight FINAL) + Q_PROPERTY(qreal left READ left DESIGNABLE false FINAL) + Q_PROPERTY(qreal right READ right DESIGNABLE false FINAL) + Q_PROPERTY(qreal top READ top DESIGNABLE false FINAL) + Q_PROPERTY(qreal bottom READ bottom DESIGNABLE false FINAL) Q_GADGET public: Q_INVOKABLE QString toString() const; @@ -165,6 +169,11 @@ public: qreal height() const; void setWidth(qreal); void setHeight(qreal); + + qreal left() const; + qreal right() const; + qreal top() const; + qreal bottom() const; }; struct QQmlRectValueType @@ -174,6 +183,10 @@ struct QQmlRectValueType Q_PROPERTY(int y READ y WRITE setY FINAL) Q_PROPERTY(int width READ width WRITE setWidth FINAL) Q_PROPERTY(int height READ height WRITE setHeight FINAL) + Q_PROPERTY(int left READ left DESIGNABLE false FINAL) + Q_PROPERTY(int right READ right DESIGNABLE false FINAL) + Q_PROPERTY(int top READ top DESIGNABLE false FINAL) + Q_PROPERTY(int bottom READ bottom DESIGNABLE false FINAL) Q_GADGET public: int x() const; @@ -185,6 +198,11 @@ public: int height() const; void setWidth(int); void setHeight(int); + + int left() const; + int right() const; + int top() const; + int bottom() const; }; struct QQmlEasingValueType diff --git a/src/qml/qml/qqmlvaluetypewrapper.cpp b/src/qml/qml/qqmlvaluetypewrapper.cpp index 67092438a..e87d9ede7 100644 --- a/src/qml/qml/qqmlvaluetypewrapper.cpp +++ b/src/qml/qml/qqmlvaluetypewrapper.cpp @@ -315,10 +315,12 @@ ReturnedValue QQmlValueTypeWrapper::method_toString(CallContext *ctx) const QMetaObject *mo = w->d()->propertyCache->metaObject(); const int propCount = mo->propertyCount(); for (int i = 0; i < propCount; ++i) { - QVariant value = mo->property(i).readOnGadget(w->d()->gadgetPtr); - result += value.toString(); - if (i < propCount - 1) - result += QStringLiteral(", "); + if (mo->property(i).isDesignable()) { + QVariant value = mo->property(i).readOnGadget(w->d()->gadgetPtr); + if (i > 0) + result += QLatin1String(", "); + result += value.toString(); + } } result += QLatin1Char(')'); } diff --git a/tests/auto/qml/qqmlvaluetypes/data/rect_compare.qml b/tests/auto/qml/qqmlvaluetypes/data/rect_compare.qml index c511c2dfc..104e7ba1e 100644 --- a/tests/auto/qml/qqmlvaluetypes/data/rect_compare.qml +++ b/tests/auto/qml/qqmlvaluetypes/data/rect_compare.qml @@ -1,10 +1,6 @@ import Test 1.0 MyTypeObject { - property int r_x: rect.x - property int r_y: rect.y - property int r_width: rect.width - property int r_height: rect.height property variant copy: rect property string tostring: rect.toString() diff --git a/tests/auto/qml/qqmlvaluetypes/data/rect_read.qml b/tests/auto/qml/qqmlvaluetypes/data/rect_read.qml index c3b37a709..da6deddd7 100644 --- a/tests/auto/qml/qqmlvaluetypes/data/rect_read.qml +++ b/tests/auto/qml/qqmlvaluetypes/data/rect_read.qml @@ -5,6 +5,10 @@ MyTypeObject { property int r_y: rect.y property int r_width: rect.width property int r_height: rect.height + property int r_left: rect.left + property int r_right: rect.right + property int r_top: rect.top + property int r_bottom: rect.bottom property variant copy: rect } diff --git a/tests/auto/qml/qqmlvaluetypes/data/rectf_compare.qml b/tests/auto/qml/qqmlvaluetypes/data/rectf_compare.qml index 6ac404955..eb61755bc 100644 --- a/tests/auto/qml/qqmlvaluetypes/data/rectf_compare.qml +++ b/tests/auto/qml/qqmlvaluetypes/data/rectf_compare.qml @@ -1,10 +1,6 @@ import Test 1.0 MyTypeObject { - property real r_x: rectf.x - property real r_y: rectf.y - property real r_width: rectf.width - property real r_height: rectf.height property variant copy: rectf property string tostring: rectf.toString() diff --git a/tests/auto/qml/qqmlvaluetypes/data/rectf_read.qml b/tests/auto/qml/qqmlvaluetypes/data/rectf_read.qml index 6ff3ce30b..878868254 100644 --- a/tests/auto/qml/qqmlvaluetypes/data/rectf_read.qml +++ b/tests/auto/qml/qqmlvaluetypes/data/rectf_read.qml @@ -5,6 +5,10 @@ MyTypeObject { property real r_y: rectf.y property real r_width: rectf.width property real r_height: rectf.height + property real r_left: rectf.left + property real r_right: rectf.right + property real r_top: rectf.top + property real r_bottom: rectf.bottom property variant copy: rectf } diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp index 2892e746d..578004b0a 100644 --- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp +++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp @@ -371,6 +371,10 @@ void tst_qqmlvaluetypes::rect() QCOMPARE(object->property("r_y").toInt(), 3); QCOMPARE(object->property("r_width").toInt(), 109); QCOMPARE(object->property("r_height").toInt(), 102); + QCOMPARE(object->property("r_left").toInt(), 2); + QCOMPARE(object->property("r_right").toInt(), 110); + QCOMPARE(object->property("r_top").toInt(), 3); + QCOMPARE(object->property("r_bottom").toInt(), 104); QCOMPARE(object->property("copy"), QVariant(QRect(2, 3, 109, 102))); delete object; @@ -418,6 +422,10 @@ void tst_qqmlvaluetypes::rectf() QCOMPARE(float(object->property("r_y").toDouble()), float(99.2)); QCOMPARE(float(object->property("r_width").toDouble()), float(88.1)); QCOMPARE(float(object->property("r_height").toDouble()), float(77.6)); + QCOMPARE(float(object->property("r_left").toDouble()), float(103.8)); + QCOMPARE(float(object->property("r_right").toDouble()), float(191.9)); + QCOMPARE(float(object->property("r_top").toDouble()), float(99.2)); + QCOMPARE(float(object->property("r_bottom").toDouble()), float(176.8)); QCOMPARE(object->property("copy"), QVariant(QRectF(103.8, 99.2, 88.1, 77.6))); delete object; -- 2.34.1