1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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 <QtGui/qvector4d.h>
51 #include <QtDeclarative/qdeclarative.h>
52 #include <QtDeclarative/qdeclarativecomponent.h>
53 #include <QtDeclarative/qdeclarativeparserstatus.h>
54 #include <QtDeclarative/qdeclarativepropertyvaluesource.h>
55 #include <QtDeclarative/qdeclarativescriptstring.h>
56 #include <QtDeclarative/qdeclarativeproperty.h>
58 #include <private/qdeclarativecustomparser_p.h>
60 QVariant myCustomVariantTypeConverter(const QString &data);
65 MyInterface() : id(913) {}
70 Q_DECLARE_INTERFACE(MyInterface, "com.trolltech.Qt.Test.MyInterface");
72 QML_DECLARE_INTERFACE(MyInterface);
74 struct MyCustomVariantType
76 MyCustomVariantType() : a(0) {}
79 Q_DECLARE_METATYPE(MyCustomVariantType);
81 class MyAttachedObject : public QObject
84 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
85 Q_PROPERTY(int value2 READ value2 WRITE setValue2)
87 MyAttachedObject(QObject *parent) : QObject(parent), m_value(0), m_value2(0) {}
89 int value() const { return m_value; }
90 void setValue(int v) { if (m_value != v) { m_value = v; emit valueChanged(); } }
92 int value2() const { return m_value2; }
93 void setValue2(int v) { m_value2 = v; }
103 class MyQmlObject : public QObject, public MyInterface
106 Q_PROPERTY(int value READ value WRITE setValue FINAL)
107 Q_PROPERTY(QString readOnlyString READ readOnlyString)
108 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled)
109 Q_PROPERTY(QRect rect READ rect WRITE setRect)
110 Q_PROPERTY(QMatrix matrix READ matrix WRITE setMatrix) //assumed to be unsupported by QML
111 Q_PROPERTY(MyInterface *interfaceProperty READ interface WRITE setInterface)
112 Q_PROPERTY(int onLiteralSignal READ onLiteralSignal WRITE setOnLiteralSignal)
113 Q_PROPERTY(MyCustomVariantType customType READ customType WRITE setCustomType)
114 Q_PROPERTY(MyQmlObject *qmlobjectProperty READ qmlobject WRITE setQmlobject)
115 Q_PROPERTY(int propertyWithNotify READ propertyWithNotify WRITE setPropertyWithNotify NOTIFY oddlyNamedNotifySignal)
116 Q_PROPERTY(int nonScriptable READ nonScriptable WRITE setNonScriptable SCRIPTABLE false)
118 Q_INTERFACES(MyInterface)
120 MyQmlObject() : m_value(-1), m_interface(0), m_qmlobject(0) { qRegisterMetaType<MyCustomVariantType>("MyCustomVariantType"); }
122 int value() const { return m_value; }
123 void setValue(int v) { m_value = v; }
125 QString readOnlyString() const { return QLatin1String(""); }
127 bool enabled() const { return false; }
128 void setEnabled(bool) {}
130 QRect rect() const { return QRect(); }
131 void setRect(const QRect&) {}
133 QMatrix matrix() const { return QMatrix(); }
134 void setMatrix(const QMatrix&) {}
136 MyInterface *interface() const { return m_interface; }
137 void setInterface(MyInterface *iface) { m_interface = iface; }
139 static MyAttachedObject *qmlAttachedProperties(QObject *other) {
140 return new MyAttachedObject(other);
142 Q_CLASSINFO("DefaultMethod", "basicSlot()")
144 int onLiteralSignal() const { return m_value; }
145 void setOnLiteralSignal(int v) { m_value = v; }
147 MyQmlObject *qmlobject() const { return m_qmlobject; }
148 void setQmlobject(MyQmlObject *o) { m_qmlobject = o; }
150 MyCustomVariantType customType() const { return m_custom; }
151 void setCustomType(const MyCustomVariantType &v) { m_custom = v; }
153 int propertyWithNotify() const { return m_propertyWithNotify; }
154 void setPropertyWithNotify(int i) { m_propertyWithNotify = i; emit oddlyNamedNotifySignal(); }
156 int nonScriptable() const { return 0; }
157 void setNonScriptable(int) {}
159 void basicSlot() { qWarning("MyQmlObject::basicSlot"); }
160 void basicSlotWithArgs(int v) { qWarning("MyQmlObject::basicSlotWithArgs(%d)", v); }
164 void basicParameterizedSignal(int parameter);
165 void oddlyNamedNotifySignal();
168 friend class tst_qdeclarativelanguage;
170 MyInterface *m_interface;
171 MyQmlObject *m_qmlobject;
172 MyCustomVariantType m_custom;
173 int m_propertyWithNotify;
175 QML_DECLARE_TYPE(MyQmlObject)
176 QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
178 class MyGroupedObject : public QObject
181 Q_PROPERTY(QDeclarativeScriptString script READ script WRITE setScript)
182 Q_PROPERTY(int value READ value WRITE setValue)
184 QDeclarativeScriptString script() const { return m_script; }
185 void setScript(const QDeclarativeScriptString &s) { m_script = s; }
187 int value() const { return m_value; }
188 void setValue(int v) { m_value = v; }
192 QDeclarativeScriptString m_script;
196 class MyTypeObject : public QObject
202 Q_PROPERTY(QString id READ id WRITE setId)
203 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
204 Q_PROPERTY(QDeclarativeComponent *componentProperty READ componentProperty WRITE setComponentProperty)
205 Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty)
206 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
207 Q_PROPERTY(MyEnum readOnlyEnumProperty READ readOnlyEnumProperty)
208 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
209 Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty)
210 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
211 Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty)
212 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
213 Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty)
214 Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty)
215 Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty)
216 Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty)
217 Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty)
218 Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty)
219 Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty)
220 Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty)
221 Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty)
222 Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged)
223 Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2)
224 Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty)
225 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
226 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
227 Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty)
228 Q_PROPERTY(QVector4D vector4Property READ vector4Property WRITE setVector4Property)
229 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty)
231 Q_PROPERTY(QDeclarativeScriptString scriptProperty READ scriptProperty WRITE setScriptProperty)
232 Q_PROPERTY(MyGroupedObject *grouped READ grouped CONSTANT)
233 Q_PROPERTY(MyGroupedObject *nullGrouped READ nullGrouped CONSTANT)
237 : objectPropertyValue(0), componentPropertyValue(0) {}
243 void setId(const QString &v) {
247 QObject *objectPropertyValue;
248 QObject *objectProperty() const {
249 return objectPropertyValue;
251 void setObjectProperty(QObject *v) {
252 objectPropertyValue = v;
255 QDeclarativeComponent *componentPropertyValue;
256 QDeclarativeComponent *componentProperty() const {
257 return componentPropertyValue;
259 void setComponentProperty(QDeclarativeComponent *v) {
260 componentPropertyValue = v;
263 enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 };
264 Q_DECLARE_FLAGS(MyFlags, MyFlag)
265 MyFlags flagPropertyValue;
266 MyFlags flagProperty() const {
267 return flagPropertyValue;
269 void setFlagProperty(MyFlags v) {
270 flagPropertyValue = v;
273 enum MyEnum { EnumVal1, EnumVal2 };
274 MyEnum enumPropertyValue;
275 MyEnum enumProperty() const {
276 return enumPropertyValue;
278 void setEnumProperty(MyEnum v) {
279 enumPropertyValue = v;
282 MyEnum readOnlyEnumProperty() const {
286 QString stringPropertyValue;
287 QString stringProperty() const {
288 return stringPropertyValue;
290 void setStringProperty(const QString &v) {
291 stringPropertyValue = v;
294 uint uintPropertyValue;
295 uint uintProperty() const {
296 return uintPropertyValue;
298 void setUintProperty(const uint &v) {
299 uintPropertyValue = v;
302 int intPropertyValue;
303 int intProperty() const {
304 return intPropertyValue;
306 void setIntProperty(const int &v) {
307 intPropertyValue = v;
310 qreal realPropertyValue;
311 qreal realProperty() const {
312 return realPropertyValue;
314 void setRealProperty(const qreal &v) {
315 realPropertyValue = v;
318 double doublePropertyValue;
319 double doubleProperty() const {
320 return doublePropertyValue;
322 void setDoubleProperty(const double &v) {
323 doublePropertyValue = v;
326 float floatPropertyValue;
327 float floatProperty() const {
328 return floatPropertyValue;
330 void setFloatProperty(const float &v) {
331 floatPropertyValue = v;
334 QColor colorPropertyValue;
335 QColor colorProperty() const {
336 return colorPropertyValue;
338 void setColorProperty(const QColor &v) {
339 colorPropertyValue = v;
342 QDate datePropertyValue;
343 QDate dateProperty() const {
344 return datePropertyValue;
346 void setDateProperty(const QDate &v) {
347 datePropertyValue = v;
350 QTime timePropertyValue;
351 QTime timeProperty() const {
352 return timePropertyValue;
354 void setTimeProperty(const QTime &v) {
355 timePropertyValue = v;
358 QDateTime dateTimePropertyValue;
359 QDateTime dateTimeProperty() const {
360 return dateTimePropertyValue;
362 void setDateTimeProperty(const QDateTime &v) {
363 dateTimePropertyValue = v;
366 QPoint pointPropertyValue;
367 QPoint pointProperty() const {
368 return pointPropertyValue;
370 void setPointProperty(const QPoint &v) {
371 pointPropertyValue = v;
374 QPointF pointFPropertyValue;
375 QPointF pointFProperty() const {
376 return pointFPropertyValue;
378 void setPointFProperty(const QPointF &v) {
379 pointFPropertyValue = v;
382 QSize sizePropertyValue;
383 QSize sizeProperty() const {
384 return sizePropertyValue;
386 void setSizeProperty(const QSize &v) {
387 sizePropertyValue = v;
390 QSizeF sizeFPropertyValue;
391 QSizeF sizeFProperty() const {
392 return sizeFPropertyValue;
394 void setSizeFProperty(const QSizeF &v) {
395 sizeFPropertyValue = v;
398 QRect rectPropertyValue;
399 QRect rectProperty() const {
400 return rectPropertyValue;
402 void setRectProperty(const QRect &v) {
403 rectPropertyValue = v;
404 emit rectPropertyChanged();
407 QRect rectPropertyValue2;
408 QRect rectProperty2() const {
409 return rectPropertyValue2;
411 void setRectProperty2(const QRect &v) {
412 rectPropertyValue2 = v;
415 QRectF rectFPropertyValue;
416 QRectF rectFProperty() const {
417 return rectFPropertyValue;
419 void setRectFProperty(const QRectF &v) {
420 rectFPropertyValue = v;
423 bool boolPropertyValue;
424 bool boolProperty() const {
425 return boolPropertyValue;
427 void setBoolProperty(const bool &v) {
428 boolPropertyValue = v;
431 QVariant variantPropertyValue;
432 QVariant variantProperty() const {
433 return variantPropertyValue;
435 void setVariantProperty(const QVariant &v) {
436 variantPropertyValue = v;
439 QVector3D vectorPropertyValue;
440 QVector3D vectorProperty() const {
441 return vectorPropertyValue;
443 void setVectorProperty(const QVector3D &v) {
444 vectorPropertyValue = v;
447 QVector4D vector4PropertyValue;
448 QVector4D vector4Property() const {
449 return vector4PropertyValue;
451 void setVector4Property(const QVector4D &v) {
452 vector4PropertyValue = v;
455 QUrl urlPropertyValue;
456 QUrl urlProperty() const {
457 return urlPropertyValue;
459 void setUrlProperty(const QUrl &v) {
460 urlPropertyValue = v;
463 QDeclarativeScriptString scriptPropertyValue;
464 QDeclarativeScriptString scriptProperty() const {
465 return scriptPropertyValue;
467 void setScriptProperty(const QDeclarativeScriptString &v) {
468 scriptPropertyValue = v;
471 MyGroupedObject groupedValue;
472 MyGroupedObject *grouped() { return &groupedValue; }
474 MyGroupedObject *nullGrouped() { return 0; }
476 void doAction() { emit action(); }
479 void rectPropertyChanged();
481 Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags)
484 class MyContainer : public QObject
487 Q_PROPERTY(QDeclarativeListProperty<QObject> children READ children)
488 Q_PROPERTY(QDeclarativeListProperty<MyContainer> containerChildren READ containerChildren)
489 Q_PROPERTY(QDeclarativeListProperty<MyInterface> qlistInterfaces READ qlistInterfaces)
490 Q_CLASSINFO("DefaultProperty", "children")
494 QDeclarativeListProperty<QObject> children() { return QDeclarativeListProperty<QObject>(this, m_children); }
495 QDeclarativeListProperty<MyContainer> containerChildren() { return QDeclarativeListProperty<MyContainer>(this, m_containerChildren); }
496 QList<QObject *> *getChildren() { return &m_children; }
497 QDeclarativeListProperty<MyInterface> qlistInterfaces() { return QDeclarativeListProperty<MyInterface>(this, m_interfaces); }
498 QList<MyInterface *> *getQListInterfaces() { return &m_interfaces; }
500 QList<MyContainer*> m_containerChildren;
501 QList<QObject*> m_children;
502 QList<MyInterface *> m_interfaces;
506 class MyPropertyValueSource : public QObject, public QDeclarativePropertyValueSource
509 Q_INTERFACES(QDeclarativePropertyValueSource)
511 MyPropertyValueSource()
512 : QDeclarativePropertyValueSource() {}
514 QDeclarativeProperty prop;
515 virtual void setTarget(const QDeclarativeProperty &p)
521 class UnavailableType : public QObject
528 class MyDotPropertyObject : public QObject
531 Q_PROPERTY(MyQmlObject *obj READ obj)
532 Q_PROPERTY(MyQmlObject *readWriteObj READ readWriteObj WRITE setReadWriteObj)
534 MyDotPropertyObject() : m_rwobj(0), m_ownRWObj(false) {}
535 ~MyDotPropertyObject()
541 MyQmlObject *obj() { return 0; }
543 MyQmlObject *readWriteObj()
546 m_rwobj = new MyQmlObject;
552 void setReadWriteObj(MyQmlObject *obj)
563 MyQmlObject *m_rwobj;
568 namespace MyNamespace {
569 class MyNamespacedType : public QObject
574 class MySecondNamespacedType : public QObject
577 Q_PROPERTY(QDeclarativeListProperty<MyNamespace::MyNamespacedType> list READ list)
579 QDeclarativeListProperty<MyNamespacedType> list() { return QDeclarativeListProperty<MyNamespacedType>(this, m_list); }
582 QList<MyNamespacedType *> m_list;
586 class MyCustomParserType : public QObject
591 class MyCustomParserTypeParser : public QDeclarativeCustomParser
594 QByteArray compile(const QList<QDeclarativeCustomParserProperty> &) { return QByteArray(); }
595 void setCustomData(QObject *, const QByteArray &) {}
598 class MyParserStatus : public QObject, public QDeclarativeParserStatus
600 Q_INTERFACES(QDeclarativeParserStatus)
603 MyParserStatus() : m_cbc(0), m_ccc(0) {}
605 int classBeginCount() const { return m_cbc; }
606 int componentCompleteCount() const { return m_ccc; }
608 virtual void classBegin() { m_cbc++; }
609 virtual void componentComplete() { m_ccc++; }
615 class MyRevisionedBaseClassRegistered : public QObject
618 Q_PROPERTY(qreal propA READ propA WRITE setPropA NOTIFY propAChanged)
619 Q_PROPERTY(qreal propB READ propB WRITE setPropB NOTIFY propBChanged REVISION 1)
622 MyRevisionedBaseClassRegistered() : m_pa(1), m_pb(2) {}
624 qreal propA() const { return m_pa; }
625 void setPropA(qreal p) {
631 qreal propB() const { return m_pb; }
632 void setPropB(qreal p) {
639 Q_INVOKABLE void methodA() { }
640 Q_INVOKABLE Q_REVISION(1) void methodB() { }
647 Q_REVISION(1) void signalB();
654 class MyRevisionedIllegalOverload : public MyRevisionedBaseClassRegistered
657 Q_PROPERTY(qreal propA READ propA WRITE setPropA REVISION 1);
660 class MyRevisionedLegalOverload : public MyRevisionedBaseClassRegistered
663 Q_PROPERTY(qreal propB READ propB WRITE setPropB REVISION 1);
666 class MyRevisionedBaseClassUnregistered : public MyRevisionedBaseClassRegistered
669 Q_PROPERTY(qreal propC READ propC WRITE setPropC NOTIFY propCChanged)
670 Q_PROPERTY(qreal propD READ propD WRITE setPropD NOTIFY propDChanged REVISION 1)
673 MyRevisionedBaseClassUnregistered() : m_pc(1), m_pd(2) {}
675 qreal propC() const { return m_pc; }
676 void setPropC(qreal p) {
682 qreal propD() const { return m_pd; }
683 void setPropD(qreal p) {
690 Q_INVOKABLE void methodC() { }
691 Q_INVOKABLE Q_REVISION(1) void methodD() { }
698 Q_REVISION(1) void signalD();
705 class MyRevisionedClass : public MyRevisionedBaseClassUnregistered
708 Q_PROPERTY(qreal prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
709 Q_PROPERTY(qreal prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed REVISION 1)
712 MyRevisionedClass() : m_p1(1), m_p2(2) {}
714 qreal prop1() const { return m_p1; }
715 void setProp1(qreal p) {
721 qreal prop2() const { return m_p2; }
722 void setProp2(qreal p) {
729 Q_INVOKABLE void method1() { }
730 Q_INVOKABLE Q_REVISION(1) void method2() { }
737 Q_REVISION(1) void signal2();
744 class MyRevisionedSubclass : public MyRevisionedClass
747 Q_PROPERTY(qreal prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed)
748 Q_PROPERTY(qreal prop4 READ prop4 WRITE setProp4 NOTIFY prop4Changed REVISION 1)
751 MyRevisionedSubclass() : m_p3(3), m_p4(4) {}
753 qreal prop3() const { return m_p3; }
754 void setProp3(qreal p) {
760 qreal prop4() const { return m_p4; }
761 void setProp4(qreal p) {
768 Q_INVOKABLE void method3() { }
769 Q_INVOKABLE Q_REVISION(1) void method4() { }
776 Q_REVISION(1) void signal4();
783 class MySubclass : public MyRevisionedClass
786 Q_PROPERTY(qreal prop5 READ prop5 WRITE setProp5 NOTIFY prop5Changed)
789 MySubclass() : m_p5(5) {}
791 qreal prop5() const { return m_p5; }
792 void setProp5(qreal p) {
799 Q_INVOKABLE void method5() { }
808 class MyVersion2Class : public QObject
813 QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered)
814 QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered)
815 QML_DECLARE_TYPE(MyRevisionedClass)
816 QML_DECLARE_TYPE(MyRevisionedSubclass)
817 QML_DECLARE_TYPE(MySubclass)
821 void registerTypes();
823 #endif // TESTTYPES_H