From 36767e3fe1f0038441ae06ef5b5e1cb19a3738fa Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Wed, 3 Aug 2011 09:20:18 +1000 Subject: Make QColor a value type This commit allows direct access to the r, g, b and a components of a color (in floating point format: 0 <= v <= 1). Since conversion from color to string is a common operation, this commit also adds unit tests to ensure that the previous behaviour is maintained in other cases (comparison with toString value, etc). Task-number: QTBUG-14731 Change-Id: I87b521dd4f9c1e96dfe5b20cf8053293cb14cfe4 Reviewed-on: http://codereview.qt.nokia.com/2527 Reviewed-by: Qt Sanity Bot Reviewed-by: Aaron Kennedy --- src/declarative/qml/qdeclarativevaluetype.cpp | 82 +++++++++++++++++++++++++++ src/declarative/qml/qdeclarativevaluetype_p.h | 30 ++++++++++ 2 files changed, 112 insertions(+) (limited to 'src') diff --git a/src/declarative/qml/qdeclarativevaluetype.cpp b/src/declarative/qml/qdeclarativevaluetype.cpp index 4aedf3ef13..3e77f4b503 100644 --- a/src/declarative/qml/qdeclarativevaluetype.cpp +++ b/src/declarative/qml/qdeclarativevaluetype.cpp @@ -152,6 +152,9 @@ QDeclarativeValueType *QDeclarativeValueTypeFactory::valueType(int t) case QVariant::Font: rv = new QDeclarativeFontValueType; break; + case QVariant::Color: + rv = new QDeclarativeColorValueType; + break; default: break; } @@ -1143,4 +1146,83 @@ void QDeclarativeFontValueType::setWordSpacing(qreal size) font.setWordSpacing(size); } +QDeclarativeColorValueType::QDeclarativeColorValueType(QObject *parent) +: QDeclarativeValueType(parent) +{ +} + +void QDeclarativeColorValueType::read(QObject *obj, int idx) +{ + void *a[] = { &color, 0 }; + QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a); +} + +void QDeclarativeColorValueType::write(QObject *obj, int idx, QDeclarativePropertyPrivate::WriteFlags flags) +{ + int status = -1; + void *a[] = { &color, 0, &status, &flags }; + QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a); +} + +QVariant QDeclarativeColorValueType::value() +{ + return QVariant(color); +} + +void QDeclarativeColorValueType::setValue(QVariant value) +{ + color = qvariant_cast(value); +} + +QString QDeclarativeColorValueType::toString() const +{ + // special case - to maintain behaviour with QtQuick 1.0, we just output normal toString() value. + return QVariant(color).toString(); +} + +bool QDeclarativeColorValueType::isEqual(const QVariant &value) const +{ + return (QVariant(color) == value); +} + +qreal QDeclarativeColorValueType::r() const +{ + return color.redF(); +} + +qreal QDeclarativeColorValueType::g() const +{ + return color.greenF(); +} + +qreal QDeclarativeColorValueType::b() const +{ + return color.blueF(); +} + +qreal QDeclarativeColorValueType::a() const +{ + return color.alphaF(); +} + +void QDeclarativeColorValueType::setR(qreal r) +{ + color.setRedF(r); +} + +void QDeclarativeColorValueType::setG(qreal g) +{ + color.setGreenF(g); +} + +void QDeclarativeColorValueType::setB(qreal b) +{ + color.setBlueF(b); +} + +void QDeclarativeColorValueType::setA(qreal a) +{ + color.setAlphaF(a); +} + QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativevaluetype_p.h b/src/declarative/qml/qdeclarativevaluetype_p.h index 01a1765fd8..03cb83fdad 100644 --- a/src/declarative/qml/qdeclarativevaluetype_p.h +++ b/src/declarative/qml/qdeclarativevaluetype_p.h @@ -582,6 +582,36 @@ private: mutable QDeclarativeNullableValue dpi; }; +class Q_AUTOTEST_EXPORT QDeclarativeColorValueType : public QDeclarativeValueType +{ + Q_PROPERTY(qreal r READ r WRITE setR) + Q_PROPERTY(qreal g READ g WRITE setG) + Q_PROPERTY(qreal b READ b WRITE setB) + Q_PROPERTY(qreal a READ a WRITE setA) + Q_OBJECT +public: + QDeclarativeColorValueType(QObject *parent = 0); + + virtual void read(QObject *, int); + virtual void write(QObject *, int, QDeclarativePropertyPrivate::WriteFlags); + virtual QVariant value(); + virtual void setValue(QVariant value); + virtual QString toString() const; + virtual bool isEqual(const QVariant &value) const; + + qreal r() const; + qreal g() const; + qreal b() const; + qreal a() const; + void setR(qreal); + void setG(qreal); + void setB(qreal); + void setA(qreal); + +private: + QColor color; +}; + QT_END_NAMESPACE #endif // QDECLARATIVEVALUETYPE_P_H -- cgit v1.2.3