diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2020-03-27 13:58:13 +0100 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2020-04-02 09:26:40 +0100 |
commit | 19850f129881a04c36c51859454bf71ed688f28a (patch) | |
tree | 710468d765071c2cffd983a9165b36feae5346e6 | |
parent | b3d28f72862433f54033d1b744715a5699c1ee59 (diff) |
Implement modifying colors using methods
Instead of having to use Qt.tint, Qt.lighter or Qt.lighter you can now
directly use these on colors (e.g. color.tint("red")).
Also adds Qt.color to explicitly convert color strings into actual color
objects.
[ChangeLog][QML][General] Added Qt.color to turn color strings
into color objects
[ChangeLog][Quick][General] Make Qt.tint, Qt.lighter and
Qt.darker methods that can directly operate on color objects
Task-number: QTBUG-77635
Change-Id: Ie10ced7ba7f1dc10afdebbcbc8664d74cd6efccf
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 28 | ||||
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions_p.h | 2 | ||||
-rw-r--r-- | src/quick/util/qquickvaluetypes.cpp | 16 | ||||
-rw-r--r-- | src/quick/util/qquickvaluetypes_p.h | 4 | ||||
-rw-r--r-- | tests/auto/qml/qqmlqt/data/color.qml | 10 | ||||
-rw-r--r-- | tests/auto/qml/qqmlqt/data/darker.qml | 6 | ||||
-rw-r--r-- | tests/auto/qml/qqmlqt/data/lighter.qml | 5 | ||||
-rw-r--r-- | tests/auto/qml/qqmlqt/data/tint.qml | 5 | ||||
-rw-r--r-- | tests/auto/qml/qqmlqt/tst_qqmlqt.cpp | 40 |
9 files changed, 114 insertions, 2 deletions
diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp index 10932a0091..158f05c743 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp @@ -110,6 +110,7 @@ void Heap::QtObject::init(QQmlEngine *qmlEngine) o->defineDefaultProperty(QStringLiteral("include"), QV4Include::method_include); o->defineDefaultProperty(QStringLiteral("isQtObject"), QV4::QtObject::method_isQtObject); + o->defineDefaultProperty(QStringLiteral("color"), QV4::QtObject::method_color); o->defineDefaultProperty(QStringLiteral("rgba"), QV4::QtObject::method_rgba); o->defineDefaultProperty(QStringLiteral("hsla"), QV4::QtObject::method_hsla); o->defineDefaultProperty(QStringLiteral("hsva"), QV4::QtObject::method_hsva); @@ -237,6 +238,33 @@ ReturnedValue QtObject::method_isQtObject(const FunctionObject *, const Value *, } /*! + \qmlmethod color Qt::color(string name) + + Returns the color corresponding to the given \a name (i.e. red or #ff0000). + If there is no such color, \c null is returned. +*/ +ReturnedValue QtObject::method_color(const FunctionObject *f, const Value *, const Value *argv, + int argc) +{ + QV4::Scope scope(f); + if (argc != 1) + THROW_GENERIC_ERROR("Qt.color(): Qt.color takes exactly one argument"); + + QVariant v = scope.engine->toVariant(argv[0], -1); + if (v.userType() == QMetaType::QString) { + bool ok = false; + v = QQmlStringConverters::colorFromString(v.toString(), &ok); + if (!ok) { + return QV4::Encode::null(); + } + } else { + THROW_GENERIC_ERROR("Qt.color(): Argument must be a string"); + } + + return scope.engine->fromVariant(v); +} + +/*! \qmlmethod color Qt::rgba(real red, real green, real blue, real alpha) Returns a color with the specified \a red, \a green, \a blue, and \a alpha diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h index 5cbb52471d..0e6d815457 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h +++ b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h @@ -97,6 +97,8 @@ struct QtObject : Object static OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target); static ReturnedValue method_isQtObject(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_color(const FunctionObject *b, const Value *thisObject, + const Value *argv, int argc); static ReturnedValue method_rgba(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); static ReturnedValue method_hsla(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); static ReturnedValue method_hsva(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); diff --git a/src/quick/util/qquickvaluetypes.cpp b/src/quick/util/qquickvaluetypes.cpp index 517ed5da38..2b86fe5ceb 100644 --- a/src/quick/util/qquickvaluetypes.cpp +++ b/src/quick/util/qquickvaluetypes.cpp @@ -44,7 +44,6 @@ #include <private/qcolorspace_p.h> #include <private/qfont_p.h> - QT_BEGIN_NAMESPACE namespace QQuickValueTypes { @@ -59,6 +58,21 @@ QString QQuickColorValueType::toString() const return v.name(v.alpha() != 255 ? QColor::HexArgb : QColor::HexRgb); } +QVariant QQuickColorValueType::lighter(qreal factor) const +{ + return QQml_colorProvider()->lighter(this->v, factor); +} + +QVariant QQuickColorValueType::darker(qreal factor) const +{ + return QQml_colorProvider()->darker(this->v, factor); +} + +QVariant QQuickColorValueType::tint(QVariant tintColor) const +{ + return QQml_colorProvider()->tint(this->v, tintColor); +} + qreal QQuickColorValueType::r() const { return v.redF(); diff --git a/src/quick/util/qquickvaluetypes_p.h b/src/quick/util/qquickvaluetypes_p.h index ccd9eefe47..b20fc6c0e3 100644 --- a/src/quick/util/qquickvaluetypes_p.h +++ b/src/quick/util/qquickvaluetypes_p.h @@ -90,6 +90,10 @@ class QQuickColorValueType public: Q_INVOKABLE QString toString() const; + Q_INVOKABLE QVariant lighter(qreal factor = 1.5) const; + Q_INVOKABLE QVariant darker(qreal factor = 2.0) const; + Q_INVOKABLE QVariant tint(QVariant factor) const; + qreal r() const; qreal g() const; qreal b() const; diff --git a/tests/auto/qml/qqmlqt/data/color.qml b/tests/auto/qml/qqmlqt/data/color.qml new file mode 100644 index 0000000000..89e2216ce5 --- /dev/null +++ b/tests/auto/qml/qqmlqt/data/color.qml @@ -0,0 +1,10 @@ +import QtQuick 2.0 + +QtObject { + property variant test1: Qt.color("red") + property variant test2: Qt.color("#ff00ff00") + property variant test3: Qt.color("taint") // Taint is not a valid color + property variant test4: Qt.color(0.5) + property variant test5: Qt.color() + property variant test6: Qt.color("blue", 0) +} diff --git a/tests/auto/qml/qqmlqt/data/darker.qml b/tests/auto/qml/qqmlqt/data/darker.qml index ce6dea0dfe..0e96990219 100644 --- a/tests/auto/qml/qqmlqt/data/darker.qml +++ b/tests/auto/qml/qqmlqt/data/darker.qml @@ -8,5 +8,9 @@ QtObject { property variant test5: Qt.darker("perfectred"); // Non-existent color property variant test6: Qt.darker(10); property variant test7: Qt.darker(Qt.rgba(1, 0.8, 0.3), 2.8, 10) -} + property variant testColor1: Qt.rgba(1, 0.8, 0.3).darker() + property variant testColor3: Qt.rgba(1, 0.8, 0.3).darker(2.8) + property variant testColor4: Qt.color("red").darker(); + property variant testColor7: Qt.rgba(1, 0.8, 0.3).darker(2.8, 10); +} diff --git a/tests/auto/qml/qqmlqt/data/lighter.qml b/tests/auto/qml/qqmlqt/data/lighter.qml index bf57e08004..fcae1fbc52 100644 --- a/tests/auto/qml/qqmlqt/data/lighter.qml +++ b/tests/auto/qml/qqmlqt/data/lighter.qml @@ -8,4 +8,9 @@ QtObject { property variant test5: Qt.lighter("perfectred"); // Non-existent color property variant test6: Qt.lighter(10); property variant test7: Qt.lighter(Qt.rgba(1, 0.8, 0.3), 1.8, 5) + + property variant testColor1: Qt.rgba(1, 0.8, 0.3).lighter() + property variant testColor3: Qt.rgba(1, 0.8, 0.3).lighter(1.8) + property variant testColor4: Qt.color("red").lighter(); + property variant testColor7: Qt.rgba(1, 0.8, 0.3).lighter(1.8, 5) } diff --git a/tests/auto/qml/qqmlqt/data/tint.qml b/tests/auto/qml/qqmlqt/data/tint.qml index 816e6e9b08..6faed1f39b 100644 --- a/tests/auto/qml/qqmlqt/data/tint.qml +++ b/tests/auto/qml/qqmlqt/data/tint.qml @@ -6,4 +6,9 @@ QtObject { property color test3: Qt.tint("red", Qt.rgba(0, 0, 1, 0.5)); property color test4: Qt.tint("red", Qt.rgba(0, 0, 1, 0.5), 10); property color test5: Qt.tint("red") + + property color testColor1: Qt.color("red").tint("blue"); + property color testColor2: Qt.rgba(1, 0, 0).tint(Qt.rgba(0, 0, 0, 0)); + property color testColor3: Qt.color("red").tint(Qt.rgba(0, 0, 1, 0.5)); + property color testColor5: Qt.color("red").tint() } diff --git a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp index 60ee2a4d1c..6c93b46167 100644 --- a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp +++ b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp @@ -80,6 +80,7 @@ private slots: void lighter(); void darker(); void tint(); + void color(); void openUrlExternally(); void openUrlExternally_pragmaLibrary(); void md5(); @@ -505,9 +506,14 @@ void tst_qqmlqt::lighter() QVERIFY(object != nullptr); QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromRgbF(1, 0.8, 0.3).lighter()); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor1")), + QColor::fromRgbF(1, 0.8, 0.3).lighter()); QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor()); QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor::fromRgbF(1, 0.8, 0.3).lighter(180)); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor3")), + QColor::fromRgbF(1, 0.8, 0.3).lighter(180)); QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor("red").lighter()); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor4")), QColor("red").lighter()); QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor()); QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor()); } @@ -525,9 +531,14 @@ void tst_qqmlqt::darker() QVERIFY(object != nullptr); QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromRgbF(1, 0.8, 0.3).darker()); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor1")), + QColor::fromRgbF(1, 0.8, 0.3).darker()); QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor()); QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor::fromRgbF(1, 0.8, 0.3).darker(280)); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor3")), + QColor::fromRgbF(1, 0.8, 0.3).darker(280)); QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor("red").darker()); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor4")), QColor("red").darker()); QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor()); QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor()); } @@ -538,19 +549,48 @@ void tst_qqmlqt::tint() QString warning1 = component.url().toString() + ":7: Error: Qt.tint(): Invalid arguments"; QString warning2 = component.url().toString() + ":8: Error: Qt.tint(): Invalid arguments"; + QString warning3 = component.url().toString() + ":13: Error: Insufficient arguments"; QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1)); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2)); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning3)); QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromRgbF(0, 0, 1)); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor1")), QColor::fromRgbF(0, 0, 1)); QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor::fromRgbF(1, 0, 0)); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor2")), QColor::fromRgbF(1, 0, 0)); QColor test3 = qvariant_cast<QColor>(object->property("test3")); + QColor testColor3 = qvariant_cast<QColor>(object->property("testColor3")); QCOMPARE(test3.rgba(), 0xFF7F0080); + QCOMPARE(testColor3.rgba(), 0xFF7F0080); + QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor()); + QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor()); + QCOMPARE(qvariant_cast<QColor>(object->property("testColor5")), QColor()); +} + +void tst_qqmlqt::color() +{ + QQmlComponent component(&engine, testFileUrl("color.qml")); + + QStringList warnings = { ":7: Error: Qt.color(): Argument must be a string", + ":8: Error: Qt.color(): Qt.color takes exactly one argument", + ":9: Error: Qt.color(): Qt.color takes exactly one argument" }; + + for (const QString &warning : warnings) + QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + warning)); + + QScopedPointer<QObject> object(component.create()); + QVERIFY(object != nullptr); + + QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor("red")); + QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor("#ff00ff00")); + QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor()); QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor()); QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor()); + QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor()); } class MyUrlHandler : public QObject |