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 /tests/auto/qml/qqmlqt | |
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>
Diffstat (limited to 'tests/auto/qml/qqmlqt')
-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 |
5 files changed, 65 insertions, 1 deletions
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 |