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 <QtQml/qqml.h>
46 #include <QtQml/qqmlexpression.h>
47 #include <QtCore/qpoint.h>
48 #include <QtCore/qsize.h>
49 #include <QtQml/qqmllist.h>
50 #include <QtCore/qrect.h>
51 #include <QtGui/qmatrix.h>
52 #include <QtGui/qcolor.h>
53 #include <QtGui/qvector3d.h>
54 #include <QtGui/QPixmap>
55 #include <QtCore/qdatetime.h>
56 #include <QtQml/qjsvalue.h>
57 #include <QtQml/qqmlscriptstring.h>
58 #include <QtQml/qqmlcomponent.h>
60 #include <private/qqmlengine_p.h>
61 #include <private/qv8engine_p.h>
63 class MyQmlAttachedObject : public QObject
66 Q_PROPERTY(int value READ value CONSTANT)
67 Q_PROPERTY(int value2 READ value2 WRITE setValue2 NOTIFY value2Changed)
69 MyQmlAttachedObject(QObject *parent) : QObject(parent), m_value2(0) {}
71 int value() const { return 19; }
72 int value2() const { return m_value2; }
73 void setValue2(int v) { if (m_value2 == v) return; m_value2 = v; emit value2Changed(); }
75 void emitMySignal() { emit mySignal(); }
85 class MyQmlObject : public QObject
90 Q_PROPERTY(int deleteOnSet READ deleteOnSet WRITE setDeleteOnSet)
91 Q_PROPERTY(bool trueProperty READ trueProperty CONSTANT)
92 Q_PROPERTY(bool falseProperty READ falseProperty CONSTANT)
93 Q_PROPERTY(int value READ value WRITE setValue)
94 Q_PROPERTY(int console READ console CONSTANT)
95 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringChanged)
96 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty NOTIFY urlChanged)
97 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectChanged)
98 Q_PROPERTY(QQmlListProperty<QObject> objectListProperty READ objectListProperty CONSTANT)
99 Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty)
100 Q_PROPERTY(QRegExp regExp READ regExp WRITE setRegExp)
101 Q_PROPERTY(int nonscriptable READ nonscriptable WRITE setNonscriptable SCRIPTABLE false)
102 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intChanged)
105 MyQmlObject(): myinvokableObject(0), m_methodCalled(false), m_methodIntCalled(false), m_object(0), m_value(0), m_resetProperty(13), m_intProperty(0) {}
107 enum MyEnum { EnumValue1 = 0, EnumValue2 = 1 };
108 enum MyEnum2 { EnumValue3 = 2, EnumValue4 = 3 };
110 bool trueProperty() const { return true; }
111 bool falseProperty() const { return false; }
113 QString stringProperty() const { return m_string; }
114 void setStringProperty(const QString &s)
119 emit stringChanged();
122 QUrl urlProperty() const { return m_url; }
123 void setUrlProperty(const QUrl &url)
131 QObject *objectProperty() const { return m_object; }
132 void setObjectProperty(QObject *obj) {
136 emit objectChanged();
139 QQmlListProperty<QObject> objectListProperty() { return QQmlListProperty<QObject>(this, m_objectQList); }
141 bool methodCalled() const { return m_methodCalled; }
142 bool methodIntCalled() const { return m_methodIntCalled; }
144 QString string() const { return m_string; }
146 static MyQmlAttachedObject *qmlAttachedProperties(QObject *o) {
147 return new MyQmlAttachedObject(o);
150 int deleteOnSet() const { return 1; }
151 void setDeleteOnSet(int v) { if(v) delete this; }
153 int value() const { return m_value; }
154 void setValue(int v) { m_value = v; }
156 int resettableProperty() const { return m_resetProperty; }
157 void setResettableProperty(int v) { m_resetProperty = v; }
158 void resetProperty() { m_resetProperty = 13; }
160 QRegExp regExp() { return m_regExp; }
161 void setRegExp(const QRegExp ®Exp) { m_regExp = regExp; }
163 int console() const { return 11; }
165 int nonscriptable() const { return 0; }
166 void setNonscriptable(int) {}
168 MyQmlObject *myinvokableObject;
169 Q_INVOKABLE MyQmlObject *returnme() { return this; }
174 QVariant variant() const { return m_variant; }
175 QJSValue qjsvalue() const { return m_qjsvalue; }
177 int intProperty() const { return m_intProperty; }
178 void setIntProperty(int i) { m_intProperty = i; emit intChanged(); }
182 void argumentSignal(int a, QString b, qreal c, MyEnum2 d, Qt::MouseButtons e);
183 void unnamedArgumentSignal(int a, qreal, QString c);
184 void stringChanged();
186 void objectChanged();
187 void anotherBasicSignal();
188 void thirdBasicSignal();
189 void signalWithUnknownType(const MyQmlObject::MyType &arg);
190 void signalWithVariant(const QVariant &arg);
191 void signalWithQJSValue(const QJSValue &arg);
192 void signalWithGlobalName(int parseInt);
196 void deleteMe() { delete this; }
197 void methodNoArgs() { m_methodCalled = true; }
198 void method(int a) { if(a == 163) m_methodIntCalled = true; }
199 void setString(const QString &s) { m_string = s; }
200 void myinvokable(MyQmlObject *o) { myinvokableObject = o; }
201 void variantMethod(const QVariant &v) { m_variant = v; }
202 void qjsvalueMethod(const QJSValue &v) { m_qjsvalue = v; }
203 void v8function(QQmlV8Function*);
206 friend class tst_qqmlecmascript;
208 bool m_methodIntCalled;
213 QList<QObject *> m_objectQList;
222 QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
224 class MyQmlContainer : public QObject
227 Q_PROPERTY(QQmlListProperty<MyQmlObject> children READ children CONSTANT)
231 QQmlListProperty<MyQmlObject> children() { return QQmlListProperty<MyQmlObject>(this, m_children); }
234 QList<MyQmlObject*> m_children;
238 class MyExpression : public QQmlExpression
242 MyExpression(QQmlContext *ctxt, const QString &expr)
243 : QQmlExpression(ctxt, 0, expr), changed(false)
245 QObject::connect(this, SIGNAL(valueChanged()), this, SLOT(expressionValueChanged()));
246 setNotifyOnValueChanged(true);
252 void expressionValueChanged() {
258 class MyDefaultObject1 : public QObject
261 Q_PROPERTY(int horseLegs READ horseLegs CONSTANT)
262 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
263 Q_PROPERTY(int emuLegs READ emuLegs CONSTANT)
265 int horseLegs() const { return 4; }
266 int antLegs() const { return 6; }
267 int emuLegs() const { return 2; }
270 class MyDefaultObject3 : public QObject
273 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
274 Q_PROPERTY(int humanLegs READ humanLegs CONSTANT)
276 int antLegs() const { return 7; } // Mutant
277 int humanLegs() const { return 2; }
278 int millipedeLegs() const { return 1000; }
281 class MyDeferredObject : public QObject
284 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
285 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
286 Q_PROPERTY(QObject *objectProperty2 READ objectProperty2 WRITE setObjectProperty2)
287 Q_CLASSINFO("DeferredPropertyNames", "value,objectProperty,objectProperty2")
290 MyDeferredObject() : m_value(0), m_object(0), m_object2(0) {}
292 int value() const { return m_value; }
293 void setValue(int v) { m_value = v; emit valueChanged(); }
295 QObject *objectProperty() const { return m_object; }
296 void setObjectProperty(QObject *obj) { m_object = obj; }
298 QObject *objectProperty2() const { return m_object2; }
299 void setObjectProperty2(QObject *obj) { m_object2 = obj; }
310 class MyBaseExtendedObject : public QObject
313 Q_PROPERTY(int baseProperty READ baseProperty WRITE setBaseProperty)
315 MyBaseExtendedObject() : m_value(0) {}
317 int baseProperty() const { return m_value; }
318 void setBaseProperty(int v) { m_value = v; }
324 class MyExtendedObject : public MyBaseExtendedObject
327 Q_PROPERTY(int coreProperty READ coreProperty WRITE setCoreProperty)
329 MyExtendedObject() : m_value(0) {}
331 int coreProperty() const { return m_value; }
332 void setCoreProperty(int v) { m_value = v; }
338 class MyTypeObject : public QObject
344 Q_PROPERTY(QString id READ id WRITE setId)
345 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
346 Q_PROPERTY(QQmlComponent *componentProperty READ componentProperty WRITE setComponentProperty)
347 Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty)
348 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
349 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
350 Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty)
351 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
352 Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty)
353 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
354 Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty)
355 Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty)
356 Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty)
357 Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty)
358 Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty)
359 Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty)
360 Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty)
361 Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty)
362 Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty)
363 Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged)
364 Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2)
365 Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty)
366 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
367 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
368 Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty)
369 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty)
371 Q_PROPERTY(QQmlScriptString scriptProperty READ scriptProperty WRITE setScriptProperty)
375 : objectPropertyValue(0), componentPropertyValue(0) {}
381 void setId(const QString &v) {
385 QObject *objectPropertyValue;
386 QObject *objectProperty() const {
387 return objectPropertyValue;
389 void setObjectProperty(QObject *v) {
390 objectPropertyValue = v;
393 QQmlComponent *componentPropertyValue;
394 QQmlComponent *componentProperty() const {
395 return componentPropertyValue;
397 void setComponentProperty(QQmlComponent *v) {
398 componentPropertyValue = v;
401 enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 };
402 Q_DECLARE_FLAGS(MyFlags, MyFlag)
403 MyFlags flagPropertyValue;
404 MyFlags flagProperty() const {
405 return flagPropertyValue;
407 void setFlagProperty(MyFlags v) {
408 flagPropertyValue = v;
411 enum MyEnum { EnumVal1, EnumVal2 };
412 MyEnum enumPropertyValue;
413 MyEnum enumProperty() const {
414 return enumPropertyValue;
416 void setEnumProperty(MyEnum v) {
417 enumPropertyValue = v;
420 QString stringPropertyValue;
421 QString stringProperty() const {
422 return stringPropertyValue;
424 void setStringProperty(const QString &v) {
425 stringPropertyValue = v;
428 uint uintPropertyValue;
429 uint uintProperty() const {
430 return uintPropertyValue;
432 void setUintProperty(const uint &v) {
433 uintPropertyValue = v;
436 int intPropertyValue;
437 int intProperty() const {
438 return intPropertyValue;
440 void setIntProperty(const int &v) {
441 intPropertyValue = v;
444 qreal realPropertyValue;
445 qreal realProperty() const {
446 return realPropertyValue;
448 void setRealProperty(const qreal &v) {
449 realPropertyValue = v;
452 double doublePropertyValue;
453 double doubleProperty() const {
454 return doublePropertyValue;
456 void setDoubleProperty(const double &v) {
457 doublePropertyValue = v;
460 float floatPropertyValue;
461 float floatProperty() const {
462 return floatPropertyValue;
464 void setFloatProperty(const float &v) {
465 floatPropertyValue = v;
468 QColor colorPropertyValue;
469 QColor colorProperty() const {
470 return colorPropertyValue;
472 void setColorProperty(const QColor &v) {
473 colorPropertyValue = v;
476 QDate datePropertyValue;
477 QDate dateProperty() const {
478 return datePropertyValue;
480 void setDateProperty(const QDate &v) {
481 datePropertyValue = v;
484 QTime timePropertyValue;
485 QTime timeProperty() const {
486 return timePropertyValue;
488 void setTimeProperty(const QTime &v) {
489 timePropertyValue = v;
492 QDateTime dateTimePropertyValue;
493 QDateTime dateTimeProperty() const {
494 return dateTimePropertyValue;
496 void setDateTimeProperty(const QDateTime &v) {
497 dateTimePropertyValue = v;
500 QPoint pointPropertyValue;
501 QPoint pointProperty() const {
502 return pointPropertyValue;
504 void setPointProperty(const QPoint &v) {
505 pointPropertyValue = v;
508 QPointF pointFPropertyValue;
509 QPointF pointFProperty() const {
510 return pointFPropertyValue;
512 void setPointFProperty(const QPointF &v) {
513 pointFPropertyValue = v;
516 QSize sizePropertyValue;
517 QSize sizeProperty() const {
518 return sizePropertyValue;
520 void setSizeProperty(const QSize &v) {
521 sizePropertyValue = v;
524 QSizeF sizeFPropertyValue;
525 QSizeF sizeFProperty() const {
526 return sizeFPropertyValue;
528 void setSizeFProperty(const QSizeF &v) {
529 sizeFPropertyValue = v;
532 QRect rectPropertyValue;
533 QRect rectProperty() const {
534 return rectPropertyValue;
536 void setRectProperty(const QRect &v) {
537 rectPropertyValue = v;
538 emit rectPropertyChanged();
541 QRect rectPropertyValue2;
542 QRect rectProperty2() const {
543 return rectPropertyValue2;
545 void setRectProperty2(const QRect &v) {
546 rectPropertyValue2 = v;
549 QRectF rectFPropertyValue;
550 QRectF rectFProperty() const {
551 return rectFPropertyValue;
553 void setRectFProperty(const QRectF &v) {
554 rectFPropertyValue = v;
557 bool boolPropertyValue;
558 bool boolProperty() const {
559 return boolPropertyValue;
561 void setBoolProperty(const bool &v) {
562 boolPropertyValue = v;
565 QVariant variantPropertyValue;
566 QVariant variantProperty() const {
567 return variantPropertyValue;
569 void setVariantProperty(const QVariant &v) {
570 variantPropertyValue = v;
573 QVector3D vectorPropertyValue;
574 QVector3D vectorProperty() const {
575 return vectorPropertyValue;
577 void setVectorProperty(const QVector3D &v) {
578 vectorPropertyValue = v;
581 QUrl urlPropertyValue;
582 QUrl urlProperty() const {
583 return urlPropertyValue;
585 void setUrlProperty(const QUrl &v) {
586 urlPropertyValue = v;
589 QQmlScriptString scriptPropertyValue;
590 QQmlScriptString scriptProperty() const {
591 return scriptPropertyValue;
593 void setScriptProperty(const QQmlScriptString &v) {
594 scriptPropertyValue = v;
597 void doAction() { emit action(); }
600 void rectPropertyChanged();
602 Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags)
604 class MyDerivedObject : public MyTypeObject
608 Q_INVOKABLE bool intProperty() const {
613 Q_DECLARE_METATYPE(QJSValue);
614 class MyInvokableBaseObject : public QObject
618 inline ~MyInvokableBaseObject() = 0;
620 Q_INVOKABLE inline void method_inherited(int a);
621 Q_INVOKABLE inline void method_overload();
624 class MyInvokableObject : public MyInvokableBaseObject
629 enum TestEnum { EnumValue1, EnumValue2 };
630 MyInvokableObject() { reset(); }
632 int invoked() const { return m_invoked; }
633 bool error() const { return m_invokedError; }
634 const QVariantList &actuals() const { return m_actuals; }
635 void reset() { m_invoked = -1; m_invokedError = false; m_actuals.clear(); }
637 Q_INVOKABLE QPointF method_get_QPointF() { return QPointF(99.3, -10.2); }
638 Q_INVOKABLE QPoint method_get_QPoint() { return QPoint(9, 12); }
640 Q_INVOKABLE void method_NoArgs() { invoke(0); }
641 Q_INVOKABLE int method_NoArgs_int() { invoke(1); return 6; }
642 Q_INVOKABLE qreal method_NoArgs_real() { invoke(2); return 19.75; }
643 Q_INVOKABLE QPointF method_NoArgs_QPointF() { invoke(3); return QPointF(123, 4.5); }
644 Q_INVOKABLE QObject *method_NoArgs_QObject() { invoke(4); return this; }
645 Q_INVOKABLE MyInvokableObject *method_NoArgs_unknown() { invoke(5); return this; }
646 Q_INVOKABLE QJSValue method_NoArgs_QScriptValue() { invoke(6); return QJSValue("Hello world"); }
647 Q_INVOKABLE QVariant method_NoArgs_QVariant() { invoke(7); return QVariant("QML rocks"); }
649 Q_INVOKABLE void method_int(int a) { invoke(8); m_actuals << a; }
650 Q_INVOKABLE void method_intint(int a, int b) { invoke(9); m_actuals << a << b; }
651 Q_INVOKABLE void method_real(qreal a) { invoke(10); m_actuals << a; }
652 Q_INVOKABLE void method_QString(QString a) { invoke(11); m_actuals << a; }
653 Q_INVOKABLE void method_QPointF(QPointF a) { invoke(12); m_actuals << a; }
654 Q_INVOKABLE void method_QObject(QObject *a) { invoke(13); m_actuals << qVariantFromValue(a); }
655 Q_INVOKABLE void method_QScriptValue(QJSValue a) { invoke(14); m_actuals << qVariantFromValue(a); }
656 Q_INVOKABLE void method_intQScriptValue(int a, QJSValue b) { invoke(15); m_actuals << a << qVariantFromValue(b); }
658 Q_INVOKABLE void method_overload(int a) { invoke(16); m_actuals << a; }
659 Q_INVOKABLE void method_overload(int a, int b) { invoke(17); m_actuals << a << b; }
660 Q_INVOKABLE void method_overload(QString a) { invoke(18); m_actuals << a; }
662 Q_INVOKABLE void method_with_enum(TestEnum e) { invoke(19); m_actuals << (int)e; }
664 Q_INVOKABLE int method_default(int a, int b = 19) { invoke(20); m_actuals << a << b; return b; }
666 Q_INVOKABLE void method_QVariant(QVariant a, QVariant b = QVariant()) { invoke(21); m_actuals << a << b; }
669 friend class MyInvokableBaseObject;
670 void invoke(int idx) { if (m_invoked != -1) m_invokedError = true; m_invoked = idx;}
673 QVariantList m_actuals;
676 MyInvokableBaseObject::~MyInvokableBaseObject() {}
678 void MyInvokableBaseObject::method_inherited(int a)
680 static_cast<MyInvokableObject *>(this)->invoke(-3);
681 static_cast<MyInvokableObject *>(this)->m_actuals << a;
684 // This is a hidden overload of the MyInvokableObject::method_overload() method
685 void MyInvokableBaseObject::method_overload()
687 static_cast<MyInvokableObject *>(this)->invoke(-2);
690 class NumberAssignment : public QObject
694 Q_PROPERTY(qreal test1 READ test1 WRITE setTest1)
696 qreal test1() const { return _test1; }
697 void setTest1(qreal v) { _test1 = v; }
699 Q_PROPERTY(qreal test2 READ test2 WRITE setTest2)
701 qreal test2() const { return _test2; }
702 void setTest2(qreal v) { _test2 = v; }
704 Q_PROPERTY(qreal test3 READ test3 WRITE setTest3)
706 qreal test3() const { return _test3; }
707 void setTest3(qreal v) { _test3 = v; }
709 Q_PROPERTY(qreal test4 READ test4 WRITE setTest4)
711 qreal test4() const { return _test4; }
712 void setTest4(qreal v) { _test4 = v; }
714 Q_PROPERTY(int test5 READ test5 WRITE setTest5)
716 int test5() const { return _test5; }
717 void setTest5(int v) { _test5 = v; }
719 Q_PROPERTY(int test6 READ test6 WRITE setTest6)
721 int test6() const { return _test6; }
722 void setTest6(int v) { _test6 = v; }
724 Q_PROPERTY(int test7 READ test7 WRITE setTest7)
726 int test7() const { return _test7; }
727 void setTest7(int v) { _test7 = v; }
729 Q_PROPERTY(int test8 READ test8 WRITE setTest8)
731 int test8() const { return _test8; }
732 void setTest8(int v) { _test8 = v; }
734 Q_PROPERTY(unsigned int test9 READ test9 WRITE setTest9)
736 unsigned int test9() const { return _test9; }
737 void setTest9(unsigned int v) { _test9 = v; }
739 Q_PROPERTY(unsigned int test10 READ test10 WRITE setTest10)
740 unsigned int _test10;
741 unsigned int test10() const { return _test10; }
742 void setTest10(unsigned int v) { _test10 = v; }
744 Q_PROPERTY(unsigned int test11 READ test11 WRITE setTest11)
745 unsigned int _test11;
746 unsigned int test11() const { return _test11; }
747 void setTest11(unsigned int v) { _test11 = v; }
749 Q_PROPERTY(unsigned int test12 READ test12 WRITE setTest12)
750 unsigned int _test12;
751 unsigned int test12() const { return _test12; }
752 void setTest12(unsigned int v) { _test12 = v; }
755 class DefaultPropertyExtendedObject : public QObject
758 Q_PROPERTY(QObject *firstProperty READ firstProperty WRITE setFirstProperty)
759 Q_PROPERTY(QObject *secondProperty READ secondProperty WRITE setSecondProperty)
761 DefaultPropertyExtendedObject(QObject *parent = 0) : QObject(parent), m_firstProperty(0), m_secondProperty(0) {}
763 QObject *firstProperty() const { return m_firstProperty; }
764 QObject *secondProperty() const { return m_secondProperty; }
765 void setFirstProperty(QObject *property) { m_firstProperty = property; }
766 void setSecondProperty(QObject *property) { m_secondProperty = property; }
768 QObject* m_firstProperty;
769 QObject* m_secondProperty;
772 class OverrideDefaultPropertyObject : public DefaultPropertyExtendedObject
775 Q_CLASSINFO("DefaultProperty", "secondProperty")
777 OverrideDefaultPropertyObject() {}
780 class MyRevisionedBaseClassRegistered : public QObject
783 Q_PROPERTY(qreal propA READ propA WRITE setPropA NOTIFY propAChanged)
784 Q_PROPERTY(qreal propB READ propB WRITE setPropB NOTIFY propBChanged REVISION 1)
787 MyRevisionedBaseClassRegistered() : m_pa(1), m_pb(2) {}
789 qreal propA() const { return m_pa; }
790 void setPropA(qreal p) {
796 qreal propB() const { return m_pb; }
797 void setPropB(qreal p) {
804 Q_INVOKABLE void methodA() { }
805 Q_INVOKABLE Q_REVISION(1) void methodB() { }
812 Q_REVISION(1) void signalB();
819 class MyRevisionedBaseClassUnregistered : public MyRevisionedBaseClassRegistered
822 Q_PROPERTY(qreal propC READ propC WRITE setPropC NOTIFY propCChanged)
823 Q_PROPERTY(qreal propD READ propD WRITE setPropD NOTIFY propDChanged REVISION 1)
826 MyRevisionedBaseClassUnregistered() : m_pc(1), m_pd(2) {}
828 qreal propC() const { return m_pc; }
829 void setPropC(qreal p) {
835 qreal propD() const { return m_pd; }
836 void setPropD(qreal p) {
843 Q_INVOKABLE void methodC() { }
844 Q_INVOKABLE Q_REVISION(1) void methodD() { }
851 Q_REVISION(1) void signalD();
858 class MyRevisionedClass : public MyRevisionedBaseClassUnregistered
861 Q_PROPERTY(qreal prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
862 Q_PROPERTY(qreal prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed REVISION 1)
865 MyRevisionedClass() {}
867 qreal prop1() const { return m_p1; }
868 void setProp1(qreal p) {
874 qreal prop2() const { return m_p2; }
875 void setProp2(qreal p) {
882 Q_INVOKABLE void method1() { }
883 Q_INVOKABLE Q_REVISION(1) void method2() { }
890 Q_REVISION(1) void signal2();
897 class MyRevisionedSubclass : public MyRevisionedClass
900 Q_PROPERTY(qreal prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed)
901 Q_PROPERTY(qreal prop4 READ prop4 WRITE setProp4 NOTIFY prop4Changed REVISION 1)
904 MyRevisionedSubclass() : m_p3(3), m_p4(4) {}
906 qreal prop3() const { return m_p3; }
907 void setProp3(qreal p) {
913 qreal prop4() const { return m_p4; }
914 void setProp4(qreal p) {
921 Q_INVOKABLE void method3() { }
922 Q_INVOKABLE Q_REVISION(1) void method4() { }
929 Q_REVISION(1) void signal4();
936 QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered)
937 QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered)
938 QML_DECLARE_TYPE(MyRevisionedClass)
939 QML_DECLARE_TYPE(MyRevisionedSubclass)
940 Q_DECLARE_METATYPE(MyQmlObject::MyType)
943 class ScarceResourceObject : public QObject
946 Q_PROPERTY(QPixmap scarceResource READ scarceResource WRITE setScarceResource NOTIFY scarceResourceChanged)
948 ScarceResourceObject(QObject *parent = 0) : QObject(parent), m_value(100, 100) { m_value.fill(Qt::blue); }
949 ~ScarceResourceObject() {}
951 QPixmap scarceResource() const { return m_value; }
952 void setScarceResource(QPixmap v) { m_value = v; emit scarceResourceChanged(); }
954 bool scarceResourceIsDetached() const { return m_value.isDetached(); }
956 // this particular one returns a new one each time
957 // this means that every Scarce Resource Copy will
958 // consume resources (so that we can track disposal
959 // of v8 handles with circular references).
960 Q_INVOKABLE QPixmap newScarceResource() const
962 QPixmap retn(800, 600);
963 retn.fill(QColor(100, 110, 120, 45));
968 void scarceResourceChanged();
973 QML_DECLARE_TYPE(ScarceResourceObject)
975 class testQObjectApi : public QObject
979 Q_PROPERTY (int qobjectTestProperty READ qobjectTestProperty NOTIFY qobjectTestPropertyChanged)
980 Q_PROPERTY (int qobjectTestWritableProperty READ qobjectTestWritableProperty WRITE setQObjectTestWritableProperty NOTIFY qobjectTestWritablePropertyChanged)
981 Q_PROPERTY (int qobjectTestWritableFinalProperty READ qobjectTestWritableFinalProperty WRITE setQObjectTestWritableFinalProperty NOTIFY qobjectTestWritableFinalPropertyChanged FINAL)
984 testQObjectApi(QObject* parent = 0)
985 : QObject(parent), m_testProperty(0), m_testWritableProperty(0), m_testWritableFinalProperty(0), m_methodCallCount(0), m_trackedObject(0)
991 enum MyEnum { EnumValue1 = 25, EnumValue2 = 42 };
992 Q_INVOKABLE int qobjectEnumTestMethod(MyEnum val) { return (static_cast<int>(val) + 5); }
993 Q_INVOKABLE int qobjectTestMethod(int increment = 1) { m_methodCallCount += increment; return m_methodCallCount; }
995 Q_INVOKABLE void trackObject(QObject *obj) { m_trackedObject = obj; }
996 Q_INVOKABLE QObject *trackedObject() const { return m_trackedObject; }
997 Q_INVOKABLE void setTrackedObjectProperty(const QString &propName) const { m_trackedObject->setProperty(qPrintable(propName), QVariant(5)); }
998 Q_INVOKABLE QVariant trackedObjectProperty(const QString &propName) const { return m_trackedObject->property(qPrintable(propName)); }
1000 int qobjectTestProperty() const { return m_testProperty; }
1001 void setQObjectTestProperty(int tp) { m_testProperty = tp; emit qobjectTestPropertyChanged(tp); }
1003 int qobjectTestWritableProperty() const { return m_testWritableProperty; }
1004 void setQObjectTestWritableProperty(int tp) { m_testWritableProperty = tp; emit qobjectTestWritablePropertyChanged(tp); }
1006 int qobjectTestWritableFinalProperty() const { return m_testWritableFinalProperty; }
1007 void setQObjectTestWritableFinalProperty(int tp) { m_testWritableFinalProperty = tp; emit qobjectTestWritableFinalPropertyChanged(); }
1010 void qobjectTestPropertyChanged(int testProperty);
1011 void qobjectTestWritablePropertyChanged(int testWritableProperty);
1012 void qobjectTestWritableFinalPropertyChanged();
1016 int m_testWritableProperty;
1017 int m_testWritableFinalProperty;
1018 int m_methodCallCount;
1019 QObject *m_trackedObject;
1022 class CircularReferenceObject : public QObject,
1023 public QV8GCCallback::Node
1028 CircularReferenceObject(QObject *parent = 0)
1029 : QObject(parent), QV8GCCallback::Node(callback), m_referenced(0), m_dtorCount(0)
1031 QV8GCCallback::addGcCallbackNode(this);
1034 ~CircularReferenceObject()
1036 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1039 Q_INVOKABLE void setDtorCount(int *dtorCount)
1041 m_dtorCount = dtorCount;
1044 Q_INVOKABLE CircularReferenceObject *generate(QObject *parent = 0)
1046 CircularReferenceObject *retn = new CircularReferenceObject(parent);
1047 retn->m_dtorCount = m_dtorCount;
1048 retn->m_engine = m_engine;
1052 Q_INVOKABLE void addReference(QObject *other)
1054 m_referenced = other;
1057 static void callback(QV8GCCallback::Node *n)
1059 CircularReferenceObject *cro = static_cast<CircularReferenceObject*>(n);
1060 if (cro->m_referenced) {
1061 cro->m_engine->addRelationshipForGC(cro, cro->m_referenced);
1065 void setEngine(QQmlEngine* declarativeEngine)
1067 m_engine = QQmlEnginePrivate::get(declarativeEngine)->v8engine();
1071 QObject *m_referenced;
1073 QV8Engine* m_engine;
1075 Q_DECLARE_METATYPE(CircularReferenceObject*)
1077 class CircularReferenceHandle : public QObject,
1078 public QV8GCCallback::Node
1083 CircularReferenceHandle(QObject *parent = 0)
1084 : QObject(parent), QV8GCCallback::Node(gccallback), m_dtorCount(0), m_engine(0)
1086 QV8GCCallback::addGcCallbackNode(this);
1089 ~CircularReferenceHandle()
1091 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1094 Q_INVOKABLE void setDtorCount(int *dtorCount)
1096 m_dtorCount = dtorCount;
1099 Q_INVOKABLE CircularReferenceHandle *generate(QObject *parent = 0)
1101 CircularReferenceHandle *retn = new CircularReferenceHandle(parent);
1102 retn->m_dtorCount = m_dtorCount;
1103 retn->m_engine = m_engine;
1107 Q_INVOKABLE void addReference(v8::Persistent<v8::Value> handle)
1109 m_referenced = qPersistentNew(handle);
1110 m_referenced.MakeWeak(static_cast<void*>(this), wrcallback);
1113 static void wrcallback(v8::Persistent<v8::Value> handle, void *params)
1115 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(params);
1116 qPersistentDispose(handle);
1117 crh->m_referenced.Clear();
1120 static void gccallback(QV8GCCallback::Node *n)
1122 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(n);
1123 crh->m_engine->addRelationshipForGC(crh, crh->m_referenced);
1126 void setEngine(QQmlEngine* declarativeEngine)
1128 m_engine = QQmlEnginePrivate::get(declarativeEngine)->v8engine();
1132 v8::Persistent<v8::Value> m_referenced;
1134 QV8Engine* m_engine;
1136 Q_DECLARE_METATYPE(CircularReferenceHandle*)
1138 class MyDynamicCreationDestructionObject : public QObject
1141 Q_PROPERTY (int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged)
1144 MyDynamicCreationDestructionObject(QObject *parent = 0) : QObject(parent), m_intProperty(0), m_dtorCount(0)
1148 ~MyDynamicCreationDestructionObject()
1155 int intProperty() const { return m_intProperty; }
1156 void setIntProperty(int val) { m_intProperty = val; emit intPropertyChanged(); }
1158 Q_INVOKABLE MyDynamicCreationDestructionObject *createNew()
1160 // no parent == ownership transfers to JS; same dtor counter.
1161 MyDynamicCreationDestructionObject *retn = new MyDynamicCreationDestructionObject;
1162 retn->setDtorCount(m_dtorCount);
1166 void setDtorCount(int *dtorCount)
1168 m_dtorCount = dtorCount;
1172 void intPropertyChanged();
1179 class WriteCounter : public QObject
1182 Q_PROPERTY(int value READ value WRITE setValue);
1184 WriteCounter() : m_value(0), m_count(0) {}
1186 int value() const { return m_value; }
1187 void setValue(int v) { m_value = v; ++m_count; }
1189 int count() const { return m_count; }
1196 class MySequenceConversionObject : public QObject
1200 Q_PROPERTY (QList<int> intListProperty READ intListProperty WRITE setIntListProperty NOTIFY intListPropertyChanged)
1201 Q_PROPERTY (QList<int> intListProperty2 READ intListProperty2 WRITE setIntListProperty2 NOTIFY intListProperty2Changed)
1202 Q_PROPERTY (QList<qreal> qrealListProperty READ qrealListProperty WRITE setQrealListProperty NOTIFY qrealListPropertyChanged)
1203 Q_PROPERTY (QList<bool> boolListProperty READ boolListProperty WRITE setBoolListProperty NOTIFY boolListPropertyChanged)
1204 Q_PROPERTY (QList<QString> stringListProperty READ stringListProperty WRITE setStringListProperty NOTIFY stringListPropertyChanged)
1205 Q_PROPERTY (QList<QUrl> urlListProperty READ urlListProperty WRITE setUrlListProperty NOTIFY urlListPropertyChanged)
1206 Q_PROPERTY (QStringList qstringListProperty READ qstringListProperty WRITE setQStringListProperty NOTIFY qstringListPropertyChanged)
1208 Q_PROPERTY (QList<QPoint> pointListProperty READ pointListProperty WRITE setPointListProperty NOTIFY pointListPropertyChanged)
1209 Q_PROPERTY (QList<QVariant> variantListProperty READ variantListProperty WRITE setVariantListProperty NOTIFY variantListPropertyChanged)
1211 Q_PROPERTY (qint32 maxIndex READ maxIndex CONSTANT)
1212 Q_PROPERTY (quint32 tooBigIndex READ tooBigIndex CONSTANT)
1213 Q_PROPERTY (qint32 negativeIndex READ negativeIndex CONSTANT)
1216 MySequenceConversionObject()
1218 m_intList << 1 << 2 << 3 << 4;
1219 m_intList2 << 1 << 2 << 3 << 4;
1220 m_qrealList << 1.1 << 2.2 << 3.3 << 4.4;
1221 m_boolList << true << false << true << false;
1222 m_stringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1223 m_urlList << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com");
1224 m_qstringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1226 m_pointList << QPoint(1, 2) << QPoint(3, 4) << QPoint(5, 6);
1227 m_variantList << QVariant(QLatin1String("one")) << QVariant(true) << QVariant(3);
1230 ~MySequenceConversionObject() {}
1232 qint32 maxIndex() const
1236 quint32 tooBigIndex() const
1242 qint32 negativeIndex() const
1247 QList<int> intListProperty() const { return m_intList; }
1248 void setIntListProperty(const QList<int> &list) { m_intList = list; emit intListPropertyChanged(); }
1249 QList<int> intListProperty2() const { return m_intList2; }
1250 void setIntListProperty2(const QList<int> &list) { m_intList2 = list; emit intListProperty2Changed(); }
1251 QList<qreal> qrealListProperty() const { return m_qrealList; }
1252 void setQrealListProperty(const QList<qreal> &list) { m_qrealList = list; emit qrealListPropertyChanged(); }
1253 QList<bool> boolListProperty() const { return m_boolList; }
1254 void setBoolListProperty(const QList<bool> &list) { m_boolList = list; emit boolListPropertyChanged(); }
1255 QList<QString> stringListProperty() const { return m_stringList; }
1256 void setStringListProperty(const QList<QString> &list) { m_stringList = list; emit stringListPropertyChanged(); }
1257 QList<QUrl> urlListProperty() const { return m_urlList; }
1258 void setUrlListProperty(const QList<QUrl> &list) { m_urlList = list; emit urlListPropertyChanged(); }
1259 QStringList qstringListProperty() const { return m_qstringList; }
1260 void setQStringListProperty(const QStringList &list) { m_qstringList = list; emit qstringListPropertyChanged(); }
1261 QList<QPoint> pointListProperty() const { return m_pointList; }
1262 void setPointListProperty(const QList<QPoint> &list) { m_pointList = list; emit pointListPropertyChanged(); }
1263 QList<QVariant> variantListProperty() const { return m_variantList; }
1264 void setVariantListProperty(const QList<QVariant> &list) { m_variantList = list; emit variantListPropertyChanged(); }
1266 // now for "copy resource" sequences:
1267 Q_INVOKABLE QList<int> generateIntSequence() const { QList<int> retn; retn << 1 << 2 << 3; return retn; }
1268 Q_INVOKABLE QList<qreal> generateQrealSequence() const { QList<qreal> retn; retn << 1.1 << 2.2 << 3.3; return retn; }
1269 Q_INVOKABLE QList<bool> generateBoolSequence() const { QList<bool> retn; retn << true << false << true; return retn; }
1270 Q_INVOKABLE QList<QString> generateStringSequence() const { QList<QString> retn; retn << "one" << "two" << "three"; return retn; }
1271 Q_INVOKABLE QList<QUrl> generateUrlSequence() const { QList<QUrl> retn; retn << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com"); return retn; }
1272 Q_INVOKABLE QStringList generateQStringSequence() const { QStringList retn; retn << "one" << "two" << "three"; return retn; }
1274 // "reference resource" underlying qobject deletion test:
1275 Q_INVOKABLE MySequenceConversionObject *generateTestObject() const { return new MySequenceConversionObject; }
1276 Q_INVOKABLE void deleteTestObject(QObject *object) const { delete object; }
1279 void intListPropertyChanged();
1280 void intListProperty2Changed();
1281 void qrealListPropertyChanged();
1282 void boolListPropertyChanged();
1283 void stringListPropertyChanged();
1284 void urlListPropertyChanged();
1285 void qstringListPropertyChanged();
1286 void pointListPropertyChanged();
1287 void variantListPropertyChanged();
1290 QList<int> m_intList;
1291 QList<int> m_intList2;
1292 QList<qreal> m_qrealList;
1293 QList<bool> m_boolList;
1294 QList<QString> m_stringList;
1295 QList<QUrl> m_urlList;
1296 QStringList m_qstringList;
1298 QList<QPoint> m_pointList; // not a supported sequence type
1299 QList<QVariant> m_variantList; // not a supported sequence type, but QVariantList support is hardcoded.
1302 class MyDeleteObject : public QObject
1305 Q_PROPERTY(QObject *nestedObject READ nestedObject NOTIFY nestedObjectChanged);
1306 Q_PROPERTY(int deleteNestedObject READ deleteNestedObject NOTIFY deleteNestedObjectChanged);
1309 MyDeleteObject() : m_nestedObject(new MyQmlObject) {}
1311 QObject *nestedObject() const { return m_nestedObject; }
1312 int deleteNestedObject() { delete m_nestedObject; m_nestedObject = 0; return 1; }
1315 void nestedObjectChanged();
1316 void deleteNestedObjectChanged();
1319 MyQmlObject *m_nestedObject;
1322 void registerTypes();
1324 #endif // TESTTYPES_H