1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QtCore/qobject.h>
45 #include <QtCore/qrect.h>
46 #include <QtCore/qdatetime.h>
47 #include <QtGui/qmatrix.h>
48 #include <QtGui/qcolor.h>
49 #include <QtGui/qvector3d.h>
50 #include <QtDeclarative/qdeclarative.h>
51 #include <QtDeclarative/qdeclarativecomponent.h>
52 #include <QtDeclarative/qdeclarativeparserstatus.h>
53 #include <QtDeclarative/qdeclarativepropertyvaluesource.h>
54 #include <QtDeclarative/qdeclarativescriptstring.h>
55 #include <QtDeclarative/qdeclarativeproperty.h>
57 #include <private/qdeclarativecustomparser_p.h>
59 QVariant myCustomVariantTypeConverter(const QString &data);
64 MyInterface() : id(913) {}
69 Q_DECLARE_INTERFACE(MyInterface, "com.trolltech.Qt.Test.MyInterface");
71 QML_DECLARE_INTERFACE(MyInterface);
73 struct MyCustomVariantType
75 MyCustomVariantType() : a(0) {}
78 Q_DECLARE_METATYPE(MyCustomVariantType);
80 class MyAttachedObject : public QObject
83 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
84 Q_PROPERTY(int value2 READ value2 WRITE setValue2)
86 MyAttachedObject(QObject *parent) : QObject(parent), m_value(0), m_value2(0) {}
88 int value() const { return m_value; }
89 void setValue(int v) { if (m_value != v) { m_value = v; emit valueChanged(); } }
91 int value2() const { return m_value2; }
92 void setValue2(int v) { m_value2 = v; }
102 class MyQmlObject : public QObject, public MyInterface
105 Q_PROPERTY(int value READ value WRITE setValue FINAL)
106 Q_PROPERTY(QString readOnlyString READ readOnlyString)
107 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled)
108 Q_PROPERTY(QRect rect READ rect WRITE setRect)
109 Q_PROPERTY(QMatrix matrix READ matrix WRITE setMatrix) //assumed to be unsupported by QML
110 Q_PROPERTY(MyInterface *interfaceProperty READ interface WRITE setInterface)
111 Q_PROPERTY(int onLiteralSignal READ onLiteralSignal WRITE setOnLiteralSignal)
112 Q_PROPERTY(MyCustomVariantType customType READ customType WRITE setCustomType)
113 Q_PROPERTY(MyQmlObject *qmlobjectProperty READ qmlobject WRITE setQmlobject)
114 Q_PROPERTY(int propertyWithNotify READ propertyWithNotify WRITE setPropertyWithNotify NOTIFY oddlyNamedNotifySignal)
115 Q_PROPERTY(int nonScriptable READ nonScriptable WRITE setNonScriptable SCRIPTABLE false)
117 Q_INTERFACES(MyInterface)
119 MyQmlObject() : m_value(-1), m_interface(0), m_qmlobject(0) { qRegisterMetaType<MyCustomVariantType>("MyCustomVariantType"); }
121 int value() const { return m_value; }
122 void setValue(int v) { m_value = v; }
124 QString readOnlyString() const { return QLatin1String(""); }
126 bool enabled() const { return false; }
127 void setEnabled(bool) {}
129 QRect rect() const { return QRect(); }
130 void setRect(const QRect&) {}
132 QMatrix matrix() const { return QMatrix(); }
133 void setMatrix(const QMatrix&) {}
135 MyInterface *interface() const { return m_interface; }
136 void setInterface(MyInterface *iface) { m_interface = iface; }
138 static MyAttachedObject *qmlAttachedProperties(QObject *other) {
139 return new MyAttachedObject(other);
141 Q_CLASSINFO("DefaultMethod", "basicSlot()")
143 int onLiteralSignal() const { return m_value; }
144 void setOnLiteralSignal(int v) { m_value = v; }
146 MyQmlObject *qmlobject() const { return m_qmlobject; }
147 void setQmlobject(MyQmlObject *o) { m_qmlobject = o; }
149 MyCustomVariantType customType() const { return m_custom; }
150 void setCustomType(const MyCustomVariantType &v) { m_custom = v; }
152 int propertyWithNotify() const { return m_propertyWithNotify; }
153 void setPropertyWithNotify(int i) { m_propertyWithNotify = i; emit oddlyNamedNotifySignal(); }
155 int nonScriptable() const { return 0; }
156 void setNonScriptable(int) {}
158 void basicSlot() { qWarning("MyQmlObject::basicSlot"); }
159 void basicSlotWithArgs(int v) { qWarning("MyQmlObject::basicSlotWithArgs(%d)", v); }
163 void basicParameterizedSignal(int parameter);
164 void oddlyNamedNotifySignal();
167 friend class tst_qdeclarativelanguage;
169 MyInterface *m_interface;
170 MyQmlObject *m_qmlobject;
171 MyCustomVariantType m_custom;
172 int m_propertyWithNotify;
174 QML_DECLARE_TYPE(MyQmlObject)
175 QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
177 class MyGroupedObject : public QObject
180 Q_PROPERTY(QDeclarativeScriptString script READ script WRITE setScript)
181 Q_PROPERTY(int value READ value WRITE setValue)
183 QDeclarativeScriptString script() const { return m_script; }
184 void setScript(const QDeclarativeScriptString &s) { m_script = s; }
186 int value() const { return m_value; }
187 void setValue(int v) { m_value = v; }
191 QDeclarativeScriptString m_script;
195 class MyTypeObject : public QObject
201 Q_PROPERTY(QString id READ id WRITE setId)
202 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
203 Q_PROPERTY(QDeclarativeComponent *componentProperty READ componentProperty WRITE setComponentProperty)
204 Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty)
205 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
206 Q_PROPERTY(MyEnum readOnlyEnumProperty READ readOnlyEnumProperty)
207 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
208 Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty)
209 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
210 Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty)
211 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
212 Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty)
213 Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty)
214 Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty)
215 Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty)
216 Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty)
217 Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty)
218 Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty)
219 Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty)
220 Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty)
221 Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged)
222 Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2)
223 Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty)
224 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
225 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
226 Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty)
227 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty)
229 Q_PROPERTY(QDeclarativeScriptString scriptProperty READ scriptProperty WRITE setScriptProperty)
230 Q_PROPERTY(MyGroupedObject *grouped READ grouped CONSTANT)
231 Q_PROPERTY(MyGroupedObject *nullGrouped READ nullGrouped CONSTANT)
235 : objectPropertyValue(0), componentPropertyValue(0) {}
241 void setId(const QString &v) {
245 QObject *objectPropertyValue;
246 QObject *objectProperty() const {
247 return objectPropertyValue;
249 void setObjectProperty(QObject *v) {
250 objectPropertyValue = v;
253 QDeclarativeComponent *componentPropertyValue;
254 QDeclarativeComponent *componentProperty() const {
255 return componentPropertyValue;
257 void setComponentProperty(QDeclarativeComponent *v) {
258 componentPropertyValue = v;
261 enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 };
262 Q_DECLARE_FLAGS(MyFlags, MyFlag)
263 MyFlags flagPropertyValue;
264 MyFlags flagProperty() const {
265 return flagPropertyValue;
267 void setFlagProperty(MyFlags v) {
268 flagPropertyValue = v;
271 enum MyEnum { EnumVal1, EnumVal2 };
272 MyEnum enumPropertyValue;
273 MyEnum enumProperty() const {
274 return enumPropertyValue;
276 void setEnumProperty(MyEnum v) {
277 enumPropertyValue = v;
280 MyEnum readOnlyEnumProperty() const {
284 QString stringPropertyValue;
285 QString stringProperty() const {
286 return stringPropertyValue;
288 void setStringProperty(const QString &v) {
289 stringPropertyValue = v;
292 uint uintPropertyValue;
293 uint uintProperty() const {
294 return uintPropertyValue;
296 void setUintProperty(const uint &v) {
297 uintPropertyValue = v;
300 int intPropertyValue;
301 int intProperty() const {
302 return intPropertyValue;
304 void setIntProperty(const int &v) {
305 intPropertyValue = v;
308 qreal realPropertyValue;
309 qreal realProperty() const {
310 return realPropertyValue;
312 void setRealProperty(const qreal &v) {
313 realPropertyValue = v;
316 double doublePropertyValue;
317 double doubleProperty() const {
318 return doublePropertyValue;
320 void setDoubleProperty(const double &v) {
321 doublePropertyValue = v;
324 float floatPropertyValue;
325 float floatProperty() const {
326 return floatPropertyValue;
328 void setFloatProperty(const float &v) {
329 floatPropertyValue = v;
332 QColor colorPropertyValue;
333 QColor colorProperty() const {
334 return colorPropertyValue;
336 void setColorProperty(const QColor &v) {
337 colorPropertyValue = v;
340 QDate datePropertyValue;
341 QDate dateProperty() const {
342 return datePropertyValue;
344 void setDateProperty(const QDate &v) {
345 datePropertyValue = v;
348 QTime timePropertyValue;
349 QTime timeProperty() const {
350 return timePropertyValue;
352 void setTimeProperty(const QTime &v) {
353 timePropertyValue = v;
356 QDateTime dateTimePropertyValue;
357 QDateTime dateTimeProperty() const {
358 return dateTimePropertyValue;
360 void setDateTimeProperty(const QDateTime &v) {
361 dateTimePropertyValue = v;
364 QPoint pointPropertyValue;
365 QPoint pointProperty() const {
366 return pointPropertyValue;
368 void setPointProperty(const QPoint &v) {
369 pointPropertyValue = v;
372 QPointF pointFPropertyValue;
373 QPointF pointFProperty() const {
374 return pointFPropertyValue;
376 void setPointFProperty(const QPointF &v) {
377 pointFPropertyValue = v;
380 QSize sizePropertyValue;
381 QSize sizeProperty() const {
382 return sizePropertyValue;
384 void setSizeProperty(const QSize &v) {
385 sizePropertyValue = v;
388 QSizeF sizeFPropertyValue;
389 QSizeF sizeFProperty() const {
390 return sizeFPropertyValue;
392 void setSizeFProperty(const QSizeF &v) {
393 sizeFPropertyValue = v;
396 QRect rectPropertyValue;
397 QRect rectProperty() const {
398 return rectPropertyValue;
400 void setRectProperty(const QRect &v) {
401 rectPropertyValue = v;
402 emit rectPropertyChanged();
405 QRect rectPropertyValue2;
406 QRect rectProperty2() const {
407 return rectPropertyValue2;
409 void setRectProperty2(const QRect &v) {
410 rectPropertyValue2 = v;
413 QRectF rectFPropertyValue;
414 QRectF rectFProperty() const {
415 return rectFPropertyValue;
417 void setRectFProperty(const QRectF &v) {
418 rectFPropertyValue = v;
421 bool boolPropertyValue;
422 bool boolProperty() const {
423 return boolPropertyValue;
425 void setBoolProperty(const bool &v) {
426 boolPropertyValue = v;
429 QVariant variantPropertyValue;
430 QVariant variantProperty() const {
431 return variantPropertyValue;
433 void setVariantProperty(const QVariant &v) {
434 variantPropertyValue = v;
437 QVector3D vectorPropertyValue;
438 QVector3D vectorProperty() const {
439 return vectorPropertyValue;
441 void setVectorProperty(const QVector3D &v) {
442 vectorPropertyValue = v;
445 QUrl urlPropertyValue;
446 QUrl urlProperty() const {
447 return urlPropertyValue;
449 void setUrlProperty(const QUrl &v) {
450 urlPropertyValue = v;
453 QDeclarativeScriptString scriptPropertyValue;
454 QDeclarativeScriptString scriptProperty() const {
455 return scriptPropertyValue;
457 void setScriptProperty(const QDeclarativeScriptString &v) {
458 scriptPropertyValue = v;
461 MyGroupedObject groupedValue;
462 MyGroupedObject *grouped() { return &groupedValue; }
464 MyGroupedObject *nullGrouped() { return 0; }
466 void doAction() { emit action(); }
469 void rectPropertyChanged();
471 Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags)
474 class MyContainer : public QObject
477 Q_PROPERTY(QDeclarativeListProperty<QObject> children READ children)
478 Q_PROPERTY(QDeclarativeListProperty<MyContainer> containerChildren READ containerChildren)
479 Q_PROPERTY(QDeclarativeListProperty<MyInterface> qlistInterfaces READ qlistInterfaces)
480 Q_CLASSINFO("DefaultProperty", "children")
484 QDeclarativeListProperty<QObject> children() { return QDeclarativeListProperty<QObject>(this, m_children); }
485 QDeclarativeListProperty<MyContainer> containerChildren() { return QDeclarativeListProperty<MyContainer>(this, m_containerChildren); }
486 QList<QObject *> *getChildren() { return &m_children; }
487 QDeclarativeListProperty<MyInterface> qlistInterfaces() { return QDeclarativeListProperty<MyInterface>(this, m_interfaces); }
488 QList<MyInterface *> *getQListInterfaces() { return &m_interfaces; }
490 QList<MyContainer*> m_containerChildren;
491 QList<QObject*> m_children;
492 QList<MyInterface *> m_interfaces;
496 class MyPropertyValueSource : public QObject, public QDeclarativePropertyValueSource
499 Q_INTERFACES(QDeclarativePropertyValueSource)
501 MyPropertyValueSource()
502 : QDeclarativePropertyValueSource() {}
504 QDeclarativeProperty prop;
505 virtual void setTarget(const QDeclarativeProperty &p)
511 class UnavailableType : public QObject
518 class MyDotPropertyObject : public QObject
521 Q_PROPERTY(MyQmlObject *obj READ obj)
522 Q_PROPERTY(MyQmlObject *readWriteObj READ readWriteObj WRITE setReadWriteObj)
524 MyDotPropertyObject() : m_rwobj(0), m_ownRWObj(false) {}
525 ~MyDotPropertyObject()
531 MyQmlObject *obj() { return 0; }
533 MyQmlObject *readWriteObj()
536 m_rwobj = new MyQmlObject;
542 void setReadWriteObj(MyQmlObject *obj)
553 MyQmlObject *m_rwobj;
558 namespace MyNamespace {
559 class MyNamespacedType : public QObject
564 class MySecondNamespacedType : public QObject
567 Q_PROPERTY(QDeclarativeListProperty<MyNamespace::MyNamespacedType> list READ list)
569 QDeclarativeListProperty<MyNamespacedType> list() { return QDeclarativeListProperty<MyNamespacedType>(this, m_list); }
572 QList<MyNamespacedType *> m_list;
576 class MyCustomParserType : public QObject
581 class MyCustomParserTypeParser : public QDeclarativeCustomParser
584 QByteArray compile(const QList<QDeclarativeCustomParserProperty> &) { return QByteArray(); }
585 void setCustomData(QObject *, const QByteArray &) {}
588 class MyParserStatus : public QObject, public QDeclarativeParserStatus
592 MyParserStatus() : m_cbc(0), m_ccc(0) {}
594 int classBeginCount() const { return m_cbc; }
595 int componentCompleteCount() const { return m_ccc; }
597 virtual void classBegin() { m_cbc++; }
598 virtual void componentComplete() { m_ccc++; }
604 class MyRevisionedBaseClassRegistered : public QObject
607 Q_PROPERTY(qreal propA READ propA WRITE setPropA NOTIFY propAChanged)
608 Q_PROPERTY(qreal propB READ propB WRITE setPropB NOTIFY propBChanged REVISION 1)
611 MyRevisionedBaseClassRegistered() : m_pa(1), m_pb(2) {}
613 qreal propA() const { return m_pa; }
614 void setPropA(qreal p) {
620 qreal propB() const { return m_pb; }
621 void setPropB(qreal p) {
628 Q_INVOKABLE void methodA() { }
629 Q_INVOKABLE Q_REVISION(1) void methodB() { }
636 Q_REVISION(1) void signalB();
643 class MyRevisionedIllegalOverload : public MyRevisionedBaseClassRegistered
646 Q_PROPERTY(qreal propA READ propA WRITE setPropA REVISION 1);
649 class MyRevisionedLegalOverload : public MyRevisionedBaseClassRegistered
652 Q_PROPERTY(qreal propB READ propB WRITE setPropB REVISION 1);
655 class MyRevisionedBaseClassUnregistered : public MyRevisionedBaseClassRegistered
658 Q_PROPERTY(qreal propC READ propC WRITE setPropC NOTIFY propCChanged)
659 Q_PROPERTY(qreal propD READ propD WRITE setPropD NOTIFY propDChanged REVISION 1)
662 MyRevisionedBaseClassUnregistered() : m_pc(1), m_pd(2) {}
664 qreal propC() const { return m_pc; }
665 void setPropC(qreal p) {
671 qreal propD() const { return m_pd; }
672 void setPropD(qreal p) {
679 Q_INVOKABLE void methodC() { }
680 Q_INVOKABLE Q_REVISION(1) void methodD() { }
687 Q_REVISION(1) void signalD();
694 class MyRevisionedClass : public MyRevisionedBaseClassUnregistered
697 Q_PROPERTY(qreal prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
698 Q_PROPERTY(qreal prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed REVISION 1)
701 MyRevisionedClass() : m_p1(1), m_p2(2) {}
703 qreal prop1() const { return m_p1; }
704 void setProp1(qreal p) {
710 qreal prop2() const { return m_p2; }
711 void setProp2(qreal p) {
718 Q_INVOKABLE void method1() { }
719 Q_INVOKABLE Q_REVISION(1) void method2() { }
726 Q_REVISION(1) void signal2();
733 class MyRevisionedSubclass : public MyRevisionedClass
736 Q_PROPERTY(qreal prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed)
737 Q_PROPERTY(qreal prop4 READ prop4 WRITE setProp4 NOTIFY prop4Changed REVISION 1)
740 MyRevisionedSubclass() : m_p3(3), m_p4(4) {}
742 qreal prop3() const { return m_p3; }
743 void setProp3(qreal p) {
749 qreal prop4() const { return m_p4; }
750 void setProp4(qreal p) {
757 Q_INVOKABLE void method3() { }
758 Q_INVOKABLE Q_REVISION(1) void method4() { }
765 Q_REVISION(1) void signal4();
772 class MySubclass : public MyRevisionedClass
775 Q_PROPERTY(qreal prop5 READ prop5 WRITE setProp5 NOTIFY prop5Changed)
778 MySubclass() : m_p5(5) {}
780 qreal prop5() const { return m_p5; }
781 void setProp5(qreal p) {
788 Q_INVOKABLE void method5() { }
797 class MyVersion2Class : public QObject
802 QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered)
803 QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered)
804 QML_DECLARE_TYPE(MyRevisionedClass)
805 QML_DECLARE_TYPE(MyRevisionedSubclass)
806 QML_DECLARE_TYPE(MySubclass)
810 void registerTypes();
812 #endif // TESTTYPES_H