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 /src/qml/qml | |
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 'src/qml/qml')
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 28 | ||||
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions_p.h | 2 |
2 files changed, 30 insertions, 0 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); |