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/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/qml')
-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 |
3 files changed, 41 insertions, 1 deletions
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)); |