aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChris Adams <christopher.adams@nokia.com>2011-08-03 09:20:18 +1000
committerQt by Nokia <qt-info@nokia.com>2011-08-18 05:10:14 +0200
commit36767e3fe1f0038441ae06ef5b5e1cb19a3738fa (patch)
treeb0585a341061534876ac1e5ce1ce85f78a504ef1 /src
parent74f3a67fe80fecf7ba2fd76e1758b6c0f68ce918 (diff)
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 <qt_sanity_bot@ovi.com> Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/declarative/qml/qdeclarativevaluetype.cpp82
-rw-r--r--src/declarative/qml/qdeclarativevaluetype_p.h30
2 files changed, 112 insertions, 0 deletions
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<QColor>(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<int> 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