diff options
author | Matthew Vogt <matthew.vogt@nokia.com> | 2012-07-05 16:09:27 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-07-11 17:37:55 +0200 |
commit | b5eb3d69b40c4b750a1bbece7be2acbe7cf918e3 (patch) | |
tree | f4009d3023d0fa9a511e94e8cc145c222c3b7552 /src/qml | |
parent | aeb2b05e3cad95164779e8778351b492b0e2fffa (diff) |
Allow color to be explicitly compared to a string
Add the Qt.colorEqual() function which compares any combination of
two supplied color and string arguments, by converting the string
arguments to colors as necessary.
Task-number: QTBUG-18754
Change-Id: I75baef9a2edd30a5f8b9cb5e151e4adba6f6a371
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/doc/src/typesystem/basictypes.qdoc | 5 | ||||
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 39 | ||||
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions_p.h | 1 | ||||
-rw-r--r-- | src/qml/qml/v8/qv8engine.cpp | 2 |
4 files changed, 46 insertions, 1 deletions
diff --git a/src/qml/doc/src/typesystem/basictypes.qdoc b/src/qml/doc/src/typesystem/basictypes.qdoc index 94ff682ee3..0f30a8af13 100644 --- a/src/qml/doc/src/typesystem/basictypes.qdoc +++ b/src/qml/doc/src/typesystem/basictypes.qdoc @@ -293,6 +293,7 @@ property is only invoked when the property is reassigned to a different object v \qmlbasictype color \ingroup qmlbasictypes \brief an ARGB color value. + \target qmlbasictypecolor The \c color type refers to an ARGB color value. It can be specified in a number of ways: @@ -326,6 +327,10 @@ property is only invoked when the property is reassigned to a different object v } \endqml + To test color values for equality, use the \l{QML:Qt::colorEqual()}{Qt.colorEqual()} + function. This allows colors to be accurately compared whether they are in property + form or in any of the acceptable string specification forms. + When integrating with C++, note that any QColor value \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically converted into a \c color value, and vice-versa. diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp index 5abc7cf7bb..b99d35029a 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp @@ -445,6 +445,45 @@ v8::Handle<v8::Value> hsla(const v8::Arguments &args) } /*! +\qmlmethod color Qt::colorEqual(color lhs, string rhs) + +Returns true if both \c lhs and \c rhs yield equal color values. Both arguments +may be either color values or string values. If a string value is supplied it +must be convertible to a color, as described for the \l{qmlbasictypecolor}{color} +basic type. +*/ +v8::Handle<v8::Value> colorEqual(const v8::Arguments &args) +{ + if (args.Length() != 2) + V8THROW_ERROR("Qt.colorEqual(): Invalid arguments"); + + bool ok = false; + + QVariant lhs = V8ENGINE()->toVariant(args[0], -1); + if (lhs.userType() == QVariant::String) { + lhs = QQmlStringConverters::colorFromString(lhs.toString(), &ok); + if (!ok) { + V8THROW_ERROR("Qt.colorEqual(): Invalid color name"); + } + } else if (lhs.userType() != QVariant::Color) { + V8THROW_ERROR("Qt.colorEqual(): Invalid arguments"); + } + + QVariant rhs = V8ENGINE()->toVariant(args[1], -1); + if (rhs.userType() == QVariant::String) { + rhs = QQmlStringConverters::colorFromString(rhs.toString(), &ok); + if (!ok) { + V8THROW_ERROR("Qt.colorEqual(): Invalid color name"); + } + } else if (rhs.userType() != QVariant::Color) { + V8THROW_ERROR("Qt.colorEqual(): Invalid arguments"); + } + + bool equal = (lhs == rhs); + return V8ENGINE()->fromVariant(equal); +} + +/*! \qmlmethod rect Qt::rect(int x, int y, int width, int height) Returns a \c rect with the top-left corner at \c x, \c y and the specified \c width and \c height. diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h index 0f43298338..c05f4ed3b8 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h +++ b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h @@ -75,6 +75,7 @@ v8::Handle<v8::Value> consoleException(const v8::Arguments &args); v8::Handle<v8::Value> isQtObject(const v8::Arguments &args); v8::Handle<v8::Value> rgba(const v8::Arguments &args); v8::Handle<v8::Value> hsla(const v8::Arguments &args); +v8::Handle<v8::Value> colorEqual(const v8::Arguments &args); v8::Handle<v8::Value> font(const v8::Arguments &args); v8::Handle<v8::Value> rect(const v8::Arguments &args); v8::Handle<v8::Value> point(const v8::Arguments &args); diff --git a/src/qml/qml/v8/qv8engine.cpp b/src/qml/qml/v8/qv8engine.cpp index be3c7def85..7eabd96745 100644 --- a/src/qml/qml/v8/qv8engine.cpp +++ b/src/qml/qml/v8/qv8engine.cpp @@ -605,8 +605,8 @@ void QV8Engine::initializeGlobal(v8::Handle<v8::Object> global) qt->Set(v8::String::New("isQtObject"), V8FUNCTION(isQtObject, this)); qt->Set(v8::String::New("rgba"), V8FUNCTION(rgba, this)); qt->Set(v8::String::New("hsla"), V8FUNCTION(hsla, this)); + qt->Set(v8::String::New("colorEqual"), V8FUNCTION(colorEqual, this)); qt->Set(v8::String::New("font"), V8FUNCTION(font, this)); - qt->Set(v8::String::New("rect"), V8FUNCTION(rect, this)); qt->Set(v8::String::New("point"), V8FUNCTION(point, this)); qt->Set(v8::String::New("size"), V8FUNCTION(size, this)); |