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 <QtDeclarative/qdeclarative.h>
46 #include <QtDeclarative/qdeclarativeexpression.h>
47 #include <QtCore/qpoint.h>
48 #include <QtCore/qsize.h>
49 #include <QtDeclarative/qdeclarativelist.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 <QtDeclarative/qjsvalue.h>
57 #include <QtDeclarative/qdeclarativescriptstring.h>
58 #include <QtDeclarative/qdeclarativecomponent.h>
60 #include <private/qv8gccallback_p.h>
61 #include <private/qdeclarativeengine_p.h>
62 #include <private/qv8engine_p.h>
64 class MyQmlAttachedObject : public QObject
67 Q_PROPERTY(int value READ value CONSTANT)
68 Q_PROPERTY(int value2 READ value2 WRITE setValue2 NOTIFY value2Changed)
70 MyQmlAttachedObject(QObject *parent) : QObject(parent), m_value2(0) {}
72 int value() const { return 19; }
73 int value2() const { return m_value2; }
74 void setValue2(int v) { if (m_value2 == v) return; m_value2 = v; emit value2Changed(); }
76 void emitMySignal() { emit mySignal(); }
86 class MyQmlObject : public QObject
91 Q_PROPERTY(int deleteOnSet READ deleteOnSet WRITE setDeleteOnSet)
92 Q_PROPERTY(bool trueProperty READ trueProperty CONSTANT)
93 Q_PROPERTY(bool falseProperty READ falseProperty CONSTANT)
94 Q_PROPERTY(int value READ value WRITE setValue)
95 Q_PROPERTY(int console READ console CONSTANT)
96 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringChanged)
97 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectChanged)
98 Q_PROPERTY(QDeclarativeListProperty<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 QObject *objectProperty() const { return m_object; }
123 void setObjectProperty(QObject *obj) {
127 emit objectChanged();
130 QDeclarativeListProperty<QObject> objectListProperty() { return QDeclarativeListProperty<QObject>(this, m_objectQList); }
132 bool methodCalled() const { return m_methodCalled; }
133 bool methodIntCalled() const { return m_methodIntCalled; }
135 QString string() const { return m_string; }
137 static MyQmlAttachedObject *qmlAttachedProperties(QObject *o) {
138 return new MyQmlAttachedObject(o);
141 int deleteOnSet() const { return 1; }
142 void setDeleteOnSet(int v) { if(v) delete this; }
144 int value() const { return m_value; }
145 void setValue(int v) { m_value = v; }
147 int resettableProperty() const { return m_resetProperty; }
148 void setResettableProperty(int v) { m_resetProperty = v; }
149 void resetProperty() { m_resetProperty = 13; }
151 QRegExp regExp() { return m_regExp; }
152 void setRegExp(const QRegExp ®Exp) { m_regExp = regExp; }
154 int console() const { return 11; }
156 int nonscriptable() const { return 0; }
157 void setNonscriptable(int) {}
159 MyQmlObject *myinvokableObject;
160 Q_INVOKABLE MyQmlObject *returnme() { return this; }
165 QVariant variant() const { return m_variant; }
167 int intProperty() const { return m_intProperty; }
168 void setIntProperty(int i) { m_intProperty = i; emit intChanged(); }
172 void argumentSignal(int a, QString b, qreal c, MyEnum2 d, Qt::MouseButtons e);
173 void stringChanged();
174 void objectChanged();
175 void anotherBasicSignal();
176 void thirdBasicSignal();
177 void signalWithUnknownType(const MyQmlObject::MyType &arg);
178 void signalWithVariant(const QVariant &arg);
182 void deleteMe() { delete this; }
183 void methodNoArgs() { m_methodCalled = true; }
184 void method(int a) { if(a == 163) m_methodIntCalled = true; }
185 void setString(const QString &s) { m_string = s; }
186 void myinvokable(MyQmlObject *o) { myinvokableObject = o; }
187 void variantMethod(const QVariant &v) { m_variant = v; }
188 void v8function(QDeclarativeV8Function*);
191 friend class tst_qdeclarativeecmascript;
193 bool m_methodIntCalled;
197 QList<QObject *> m_objectQList;
205 QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
207 class MyQmlContainer : public QObject
210 Q_PROPERTY(QDeclarativeListProperty<MyQmlObject> children READ children CONSTANT)
214 QDeclarativeListProperty<MyQmlObject> children() { return QDeclarativeListProperty<MyQmlObject>(this, m_children); }
217 QList<MyQmlObject*> m_children;
221 class MyExpression : public QDeclarativeExpression
225 MyExpression(QDeclarativeContext *ctxt, const QString &expr)
226 : QDeclarativeExpression(ctxt, 0, expr), changed(false)
228 QObject::connect(this, SIGNAL(valueChanged()), this, SLOT(expressionValueChanged()));
229 setNotifyOnValueChanged(true);
235 void expressionValueChanged() {
241 class MyDefaultObject1 : public QObject
244 Q_PROPERTY(int horseLegs READ horseLegs CONSTANT)
245 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
246 Q_PROPERTY(int emuLegs READ emuLegs CONSTANT)
248 int horseLegs() const { return 4; }
249 int antLegs() const { return 6; }
250 int emuLegs() const { return 2; }
253 class MyDefaultObject3 : public QObject
256 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
257 Q_PROPERTY(int humanLegs READ humanLegs CONSTANT)
259 int antLegs() const { return 7; } // Mutant
260 int humanLegs() const { return 2; }
261 int millipedeLegs() const { return 1000; }
264 class MyDeferredObject : public QObject
267 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
268 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
269 Q_PROPERTY(QObject *objectProperty2 READ objectProperty2 WRITE setObjectProperty2)
270 Q_CLASSINFO("DeferredPropertyNames", "value,objectProperty,objectProperty2")
273 MyDeferredObject() : m_value(0), m_object(0), m_object2(0) {}
275 int value() const { return m_value; }
276 void setValue(int v) { m_value = v; emit valueChanged(); }
278 QObject *objectProperty() const { return m_object; }
279 void setObjectProperty(QObject *obj) { m_object = obj; }
281 QObject *objectProperty2() const { return m_object2; }
282 void setObjectProperty2(QObject *obj) { m_object2 = obj; }
293 class MyBaseExtendedObject : public QObject
296 Q_PROPERTY(int baseProperty READ baseProperty WRITE setBaseProperty)
298 MyBaseExtendedObject() : m_value(0) {}
300 int baseProperty() const { return m_value; }
301 void setBaseProperty(int v) { m_value = v; }
307 class MyExtendedObject : public MyBaseExtendedObject
310 Q_PROPERTY(int coreProperty READ coreProperty WRITE setCoreProperty)
312 MyExtendedObject() : m_value(0) {}
314 int coreProperty() const { return m_value; }
315 void setCoreProperty(int v) { m_value = v; }
321 class MyTypeObject : public QObject
327 Q_PROPERTY(QString id READ id WRITE setId)
328 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
329 Q_PROPERTY(QDeclarativeComponent *componentProperty READ componentProperty WRITE setComponentProperty)
330 Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty)
331 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
332 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
333 Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty)
334 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
335 Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty)
336 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
337 Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty)
338 Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty)
339 Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty)
340 Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty)
341 Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty)
342 Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty)
343 Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty)
344 Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty)
345 Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty)
346 Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged)
347 Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2)
348 Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty)
349 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
350 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
351 Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty)
352 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty)
354 Q_PROPERTY(QDeclarativeScriptString scriptProperty READ scriptProperty WRITE setScriptProperty)
358 : objectPropertyValue(0), componentPropertyValue(0) {}
364 void setId(const QString &v) {
368 QObject *objectPropertyValue;
369 QObject *objectProperty() const {
370 return objectPropertyValue;
372 void setObjectProperty(QObject *v) {
373 objectPropertyValue = v;
376 QDeclarativeComponent *componentPropertyValue;
377 QDeclarativeComponent *componentProperty() const {
378 return componentPropertyValue;
380 void setComponentProperty(QDeclarativeComponent *v) {
381 componentPropertyValue = v;
384 enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 };
385 Q_DECLARE_FLAGS(MyFlags, MyFlag)
386 MyFlags flagPropertyValue;
387 MyFlags flagProperty() const {
388 return flagPropertyValue;
390 void setFlagProperty(MyFlags v) {
391 flagPropertyValue = v;
394 enum MyEnum { EnumVal1, EnumVal2 };
395 MyEnum enumPropertyValue;
396 MyEnum enumProperty() const {
397 return enumPropertyValue;
399 void setEnumProperty(MyEnum v) {
400 enumPropertyValue = v;
403 QString stringPropertyValue;
404 QString stringProperty() const {
405 return stringPropertyValue;
407 void setStringProperty(const QString &v) {
408 stringPropertyValue = v;
411 uint uintPropertyValue;
412 uint uintProperty() const {
413 return uintPropertyValue;
415 void setUintProperty(const uint &v) {
416 uintPropertyValue = v;
419 int intPropertyValue;
420 int intProperty() const {
421 return intPropertyValue;
423 void setIntProperty(const int &v) {
424 intPropertyValue = v;
427 qreal realPropertyValue;
428 qreal realProperty() const {
429 return realPropertyValue;
431 void setRealProperty(const qreal &v) {
432 realPropertyValue = v;
435 double doublePropertyValue;
436 double doubleProperty() const {
437 return doublePropertyValue;
439 void setDoubleProperty(const double &v) {
440 doublePropertyValue = v;
443 float floatPropertyValue;
444 float floatProperty() const {
445 return floatPropertyValue;
447 void setFloatProperty(const float &v) {
448 floatPropertyValue = v;
451 QColor colorPropertyValue;
452 QColor colorProperty() const {
453 return colorPropertyValue;
455 void setColorProperty(const QColor &v) {
456 colorPropertyValue = v;
459 QDate datePropertyValue;
460 QDate dateProperty() const {
461 return datePropertyValue;
463 void setDateProperty(const QDate &v) {
464 datePropertyValue = v;
467 QTime timePropertyValue;
468 QTime timeProperty() const {
469 return timePropertyValue;
471 void setTimeProperty(const QTime &v) {
472 timePropertyValue = v;
475 QDateTime dateTimePropertyValue;
476 QDateTime dateTimeProperty() const {
477 return dateTimePropertyValue;
479 void setDateTimeProperty(const QDateTime &v) {
480 dateTimePropertyValue = v;
483 QPoint pointPropertyValue;
484 QPoint pointProperty() const {
485 return pointPropertyValue;
487 void setPointProperty(const QPoint &v) {
488 pointPropertyValue = v;
491 QPointF pointFPropertyValue;
492 QPointF pointFProperty() const {
493 return pointFPropertyValue;
495 void setPointFProperty(const QPointF &v) {
496 pointFPropertyValue = v;
499 QSize sizePropertyValue;
500 QSize sizeProperty() const {
501 return sizePropertyValue;
503 void setSizeProperty(const QSize &v) {
504 sizePropertyValue = v;
507 QSizeF sizeFPropertyValue;
508 QSizeF sizeFProperty() const {
509 return sizeFPropertyValue;
511 void setSizeFProperty(const QSizeF &v) {
512 sizeFPropertyValue = v;
515 QRect rectPropertyValue;
516 QRect rectProperty() const {
517 return rectPropertyValue;
519 void setRectProperty(const QRect &v) {
520 rectPropertyValue = v;
521 emit rectPropertyChanged();
524 QRect rectPropertyValue2;
525 QRect rectProperty2() const {
526 return rectPropertyValue2;
528 void setRectProperty2(const QRect &v) {
529 rectPropertyValue2 = v;
532 QRectF rectFPropertyValue;
533 QRectF rectFProperty() const {
534 return rectFPropertyValue;
536 void setRectFProperty(const QRectF &v) {
537 rectFPropertyValue = v;
540 bool boolPropertyValue;
541 bool boolProperty() const {
542 return boolPropertyValue;
544 void setBoolProperty(const bool &v) {
545 boolPropertyValue = v;
548 QVariant variantPropertyValue;
549 QVariant variantProperty() const {
550 return variantPropertyValue;
552 void setVariantProperty(const QVariant &v) {
553 variantPropertyValue = v;
556 QVector3D vectorPropertyValue;
557 QVector3D vectorProperty() const {
558 return vectorPropertyValue;
560 void setVectorProperty(const QVector3D &v) {
561 vectorPropertyValue = v;
564 QUrl urlPropertyValue;
565 QUrl urlProperty() const {
566 return urlPropertyValue;
568 void setUrlProperty(const QUrl &v) {
569 urlPropertyValue = v;
572 QDeclarativeScriptString scriptPropertyValue;
573 QDeclarativeScriptString scriptProperty() const {
574 return scriptPropertyValue;
576 void setScriptProperty(const QDeclarativeScriptString &v) {
577 scriptPropertyValue = v;
580 void doAction() { emit action(); }
583 void rectPropertyChanged();
585 Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags)
587 class MyDerivedObject : public MyTypeObject
591 Q_INVOKABLE bool intProperty() const {
596 Q_DECLARE_METATYPE(QJSValue);
597 class MyInvokableBaseObject : public QObject
601 inline ~MyInvokableBaseObject() = 0;
603 Q_INVOKABLE inline void method_inherited(int a);
604 Q_INVOKABLE inline void method_overload();
607 class MyInvokableObject : public MyInvokableBaseObject
612 enum TestEnum { EnumValue1, EnumValue2 };
613 MyInvokableObject() { reset(); }
615 int invoked() const { return m_invoked; }
616 bool error() const { return m_invokedError; }
617 const QVariantList &actuals() const { return m_actuals; }
618 void reset() { m_invoked = -1; m_invokedError = false; m_actuals.clear(); }
620 Q_INVOKABLE QPointF method_get_QPointF() { return QPointF(99.3, -10.2); }
621 Q_INVOKABLE QPoint method_get_QPoint() { return QPoint(9, 12); }
623 Q_INVOKABLE void method_NoArgs() { invoke(0); }
624 Q_INVOKABLE int method_NoArgs_int() { invoke(1); return 6; }
625 Q_INVOKABLE qreal method_NoArgs_real() { invoke(2); return 19.75; }
626 Q_INVOKABLE QPointF method_NoArgs_QPointF() { invoke(3); return QPointF(123, 4.5); }
627 Q_INVOKABLE QObject *method_NoArgs_QObject() { invoke(4); return this; }
628 Q_INVOKABLE MyInvokableObject *method_NoArgs_unknown() { invoke(5); return this; }
629 Q_INVOKABLE QJSValue method_NoArgs_QScriptValue() { invoke(6); return QJSValue("Hello world"); }
630 Q_INVOKABLE QVariant method_NoArgs_QVariant() { invoke(7); return QVariant("QML rocks"); }
632 Q_INVOKABLE void method_int(int a) { invoke(8); m_actuals << a; }
633 Q_INVOKABLE void method_intint(int a, int b) { invoke(9); m_actuals << a << b; }
634 Q_INVOKABLE void method_real(qreal a) { invoke(10); m_actuals << a; }
635 Q_INVOKABLE void method_QString(QString a) { invoke(11); m_actuals << a; }
636 Q_INVOKABLE void method_QPointF(QPointF a) { invoke(12); m_actuals << a; }
637 Q_INVOKABLE void method_QObject(QObject *a) { invoke(13); m_actuals << qVariantFromValue(a); }
638 Q_INVOKABLE void method_QScriptValue(QJSValue a) { invoke(14); m_actuals << qVariantFromValue(a); }
639 Q_INVOKABLE void method_intQScriptValue(int a, QJSValue b) { invoke(15); m_actuals << a << qVariantFromValue(b); }
641 Q_INVOKABLE void method_overload(int a) { invoke(16); m_actuals << a; }
642 Q_INVOKABLE void method_overload(int a, int b) { invoke(17); m_actuals << a << b; }
643 Q_INVOKABLE void method_overload(QString a) { invoke(18); m_actuals << a; }
645 Q_INVOKABLE void method_with_enum(TestEnum e) { invoke(19); m_actuals << (int)e; }
647 Q_INVOKABLE int method_default(int a, int b = 19) { invoke(20); m_actuals << a << b; return b; }
649 Q_INVOKABLE void method_QVariant(QVariant a, QVariant b = QVariant()) { invoke(21); m_actuals << a << b; }
652 friend class MyInvokableBaseObject;
653 void invoke(int idx) { if (m_invoked != -1) m_invokedError = true; m_invoked = idx;}
656 QVariantList m_actuals;
659 MyInvokableBaseObject::~MyInvokableBaseObject() {}
661 void MyInvokableBaseObject::method_inherited(int a)
663 static_cast<MyInvokableObject *>(this)->invoke(-3);
664 static_cast<MyInvokableObject *>(this)->m_actuals << a;
667 // This is a hidden overload of the MyInvokableObject::method_overload() method
668 void MyInvokableBaseObject::method_overload()
670 static_cast<MyInvokableObject *>(this)->invoke(-2);
673 class NumberAssignment : public QObject
677 Q_PROPERTY(qreal test1 READ test1 WRITE setTest1)
679 qreal test1() const { return _test1; }
680 void setTest1(qreal v) { _test1 = v; }
682 Q_PROPERTY(qreal test2 READ test2 WRITE setTest2)
684 qreal test2() const { return _test2; }
685 void setTest2(qreal v) { _test2 = v; }
687 Q_PROPERTY(qreal test3 READ test3 WRITE setTest3)
689 qreal test3() const { return _test3; }
690 void setTest3(qreal v) { _test3 = v; }
692 Q_PROPERTY(qreal test4 READ test4 WRITE setTest4)
694 qreal test4() const { return _test4; }
695 void setTest4(qreal v) { _test4 = v; }
697 Q_PROPERTY(int test5 READ test5 WRITE setTest5)
699 int test5() const { return _test5; }
700 void setTest5(int v) { _test5 = v; }
702 Q_PROPERTY(int test6 READ test6 WRITE setTest6)
704 int test6() const { return _test6; }
705 void setTest6(int v) { _test6 = v; }
707 Q_PROPERTY(int test7 READ test7 WRITE setTest7)
709 int test7() const { return _test7; }
710 void setTest7(int v) { _test7 = v; }
712 Q_PROPERTY(int test8 READ test8 WRITE setTest8)
714 int test8() const { return _test8; }
715 void setTest8(int v) { _test8 = v; }
717 Q_PROPERTY(unsigned int test9 READ test9 WRITE setTest9)
719 unsigned int test9() const { return _test9; }
720 void setTest9(unsigned int v) { _test9 = v; }
722 Q_PROPERTY(unsigned int test10 READ test10 WRITE setTest10)
723 unsigned int _test10;
724 unsigned int test10() const { return _test10; }
725 void setTest10(unsigned int v) { _test10 = v; }
727 Q_PROPERTY(unsigned int test11 READ test11 WRITE setTest11)
728 unsigned int _test11;
729 unsigned int test11() const { return _test11; }
730 void setTest11(unsigned int v) { _test11 = v; }
732 Q_PROPERTY(unsigned int test12 READ test12 WRITE setTest12)
733 unsigned int _test12;
734 unsigned int test12() const { return _test12; }
735 void setTest12(unsigned int v) { _test12 = v; }
738 class DefaultPropertyExtendedObject : public QObject
741 Q_PROPERTY(QObject *firstProperty READ firstProperty WRITE setFirstProperty)
742 Q_PROPERTY(QObject *secondProperty READ secondProperty WRITE setSecondProperty)
744 DefaultPropertyExtendedObject(QObject *parent = 0) : QObject(parent), m_firstProperty(0), m_secondProperty(0) {}
746 QObject *firstProperty() const { return m_firstProperty; }
747 QObject *secondProperty() const { return m_secondProperty; }
748 void setFirstProperty(QObject *property) { m_firstProperty = property; }
749 void setSecondProperty(QObject *property) { m_secondProperty = property; }
751 QObject* m_firstProperty;
752 QObject* m_secondProperty;
755 class OverrideDefaultPropertyObject : public DefaultPropertyExtendedObject
758 Q_CLASSINFO("DefaultProperty", "secondProperty")
760 OverrideDefaultPropertyObject() {}
763 class MyRevisionedBaseClassRegistered : public QObject
766 Q_PROPERTY(qreal propA READ propA WRITE setPropA NOTIFY propAChanged)
767 Q_PROPERTY(qreal propB READ propB WRITE setPropB NOTIFY propBChanged REVISION 1)
770 MyRevisionedBaseClassRegistered() : m_pa(1), m_pb(2) {}
772 qreal propA() const { return m_pa; }
773 void setPropA(qreal p) {
779 qreal propB() const { return m_pb; }
780 void setPropB(qreal p) {
787 Q_INVOKABLE void methodA() { }
788 Q_INVOKABLE Q_REVISION(1) void methodB() { }
795 Q_REVISION(1) void signalB();
802 class MyRevisionedBaseClassUnregistered : public MyRevisionedBaseClassRegistered
805 Q_PROPERTY(qreal propC READ propC WRITE setPropC NOTIFY propCChanged)
806 Q_PROPERTY(qreal propD READ propD WRITE setPropD NOTIFY propDChanged REVISION 1)
809 MyRevisionedBaseClassUnregistered() : m_pc(1), m_pd(2) {}
811 qreal propC() const { return m_pc; }
812 void setPropC(qreal p) {
818 qreal propD() const { return m_pd; }
819 void setPropD(qreal p) {
826 Q_INVOKABLE void methodC() { }
827 Q_INVOKABLE Q_REVISION(1) void methodD() { }
834 Q_REVISION(1) void signalD();
841 class MyRevisionedClass : public MyRevisionedBaseClassUnregistered
844 Q_PROPERTY(qreal prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
845 Q_PROPERTY(qreal prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed REVISION 1)
848 MyRevisionedClass() {}
850 qreal prop1() const { return m_p1; }
851 void setProp1(qreal p) {
857 qreal prop2() const { return m_p2; }
858 void setProp2(qreal p) {
865 Q_INVOKABLE void method1() { }
866 Q_INVOKABLE Q_REVISION(1) void method2() { }
873 Q_REVISION(1) void signal2();
880 class MyRevisionedSubclass : public MyRevisionedClass
883 Q_PROPERTY(qreal prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed)
884 Q_PROPERTY(qreal prop4 READ prop4 WRITE setProp4 NOTIFY prop4Changed REVISION 1)
887 MyRevisionedSubclass() : m_p3(3), m_p4(4) {}
889 qreal prop3() const { return m_p3; }
890 void setProp3(qreal p) {
896 qreal prop4() const { return m_p4; }
897 void setProp4(qreal p) {
904 Q_INVOKABLE void method3() { }
905 Q_INVOKABLE Q_REVISION(1) void method4() { }
912 Q_REVISION(1) void signal4();
919 QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered)
920 QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered)
921 QML_DECLARE_TYPE(MyRevisionedClass)
922 QML_DECLARE_TYPE(MyRevisionedSubclass)
923 Q_DECLARE_METATYPE(MyQmlObject::MyType)
926 class ScarceResourceObject : public QObject
929 Q_PROPERTY(QPixmap scarceResource READ scarceResource WRITE setScarceResource NOTIFY scarceResourceChanged)
931 ScarceResourceObject(QObject *parent = 0) : QObject(parent), m_value(100, 100) { m_value.fill(Qt::blue); }
932 ~ScarceResourceObject() {}
934 QPixmap scarceResource() const { return m_value; }
935 void setScarceResource(QPixmap v) { m_value = v; emit scarceResourceChanged(); }
937 bool scarceResourceIsDetached() const { return m_value.isDetached(); }
939 // this particular one returns a new one each time
940 // this means that every Scarce Resource Copy will
941 // consume resources (so that we can track disposal
942 // of v8 handles with circular references).
943 Q_INVOKABLE QPixmap newScarceResource() const
945 QPixmap retn(800, 600);
946 retn.fill(QColor(100, 110, 120, 45));
951 void scarceResourceChanged();
956 QML_DECLARE_TYPE(ScarceResourceObject)
958 class testQObjectApi : public QObject
962 Q_PROPERTY (int qobjectTestProperty READ qobjectTestProperty NOTIFY qobjectTestPropertyChanged)
963 Q_PROPERTY (int qobjectTestWritableProperty READ qobjectTestWritableProperty WRITE setQObjectTestWritableProperty NOTIFY qobjectTestWritablePropertyChanged)
966 testQObjectApi(QObject* parent = 0)
967 : QObject(parent), m_testProperty(0), m_testWritableProperty(0), m_methodCallCount(0)
973 enum MyEnum { EnumValue1 = 25, EnumValue2 = 42 };
974 Q_INVOKABLE int qobjectEnumTestMethod(MyEnum val) { return (static_cast<int>(val) + 5); }
975 Q_INVOKABLE int qobjectTestMethod(int increment = 1) { m_methodCallCount += increment; return m_methodCallCount; }
977 int qobjectTestProperty() const { return m_testProperty; }
978 void setQObjectTestProperty(int tp) { m_testProperty = tp; emit qobjectTestPropertyChanged(tp); }
980 int qobjectTestWritableProperty() const { return m_testWritableProperty; }
981 void setQObjectTestWritableProperty(int tp) { m_testWritableProperty = tp; emit qobjectTestWritablePropertyChanged(tp); }
984 void qobjectTestPropertyChanged(int testProperty);
985 void qobjectTestWritablePropertyChanged(int testWritableProperty);
989 int m_testWritableProperty;
990 int m_methodCallCount;
993 class CircularReferenceObject : public QObject,
994 public QV8GCCallback::Node
999 CircularReferenceObject(QObject *parent = 0)
1000 : QObject(parent), QV8GCCallback::Node(callback), m_referenced(0), m_dtorCount(0)
1002 QV8GCCallback::addGcCallbackNode(this);
1005 ~CircularReferenceObject()
1007 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1010 Q_INVOKABLE void setDtorCount(int *dtorCount)
1012 m_dtorCount = dtorCount;
1015 Q_INVOKABLE CircularReferenceObject *generate(QObject *parent = 0)
1017 CircularReferenceObject *retn = new CircularReferenceObject(parent);
1018 retn->m_dtorCount = m_dtorCount;
1022 Q_INVOKABLE void addReference(QObject *other)
1024 m_referenced = other;
1027 static void callback(QV8GCCallback::Referencer *r, QV8GCCallback::Node *n)
1029 CircularReferenceObject *cro = static_cast<CircularReferenceObject*>(n);
1030 if (cro->m_referenced) {
1031 r->addRelationship(cro, cro->m_referenced);
1036 QObject *m_referenced;
1039 Q_DECLARE_METATYPE(CircularReferenceObject*)
1041 class CircularReferenceHandle : public QObject,
1042 public QV8GCCallback::Node
1047 CircularReferenceHandle(QObject *parent = 0)
1048 : QObject(parent), QV8GCCallback::Node(gccallback), m_dtorCount(0)
1050 QV8GCCallback::addGcCallbackNode(this);
1053 ~CircularReferenceHandle()
1055 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1058 Q_INVOKABLE void setDtorCount(int *dtorCount)
1060 m_dtorCount = dtorCount;
1063 Q_INVOKABLE CircularReferenceHandle *generate(QObject *parent = 0)
1065 CircularReferenceHandle *retn = new CircularReferenceHandle(parent);
1066 retn->m_dtorCount = m_dtorCount;
1070 Q_INVOKABLE void addReference(v8::Persistent<v8::Value> handle)
1072 m_referenced = qPersistentNew(handle);
1073 m_referenced.MakeWeak(static_cast<void*>(this), wrcallback);
1076 static void wrcallback(v8::Persistent<v8::Value> handle, void *params)
1078 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(params);
1079 qPersistentDispose(handle);
1080 crh->m_referenced.Clear();
1083 static void gccallback(QV8GCCallback::Referencer *r, QV8GCCallback::Node *n)
1085 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(n);
1086 r->addRelationship(crh, crh->m_referenced);
1090 v8::Persistent<v8::Value> m_referenced;
1093 Q_DECLARE_METATYPE(CircularReferenceHandle*)
1095 class MyDynamicCreationDestructionObject : public QObject
1098 Q_PROPERTY (int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged)
1101 MyDynamicCreationDestructionObject(QObject *parent = 0) : QObject(parent), m_intProperty(0), m_dtorCount(0)
1105 ~MyDynamicCreationDestructionObject()
1112 int intProperty() const { return m_intProperty; }
1113 void setIntProperty(int val) { m_intProperty = val; emit intPropertyChanged(); }
1115 Q_INVOKABLE MyDynamicCreationDestructionObject *createNew()
1117 // no parent == ownership transfers to JS; same dtor counter.
1118 MyDynamicCreationDestructionObject *retn = new MyDynamicCreationDestructionObject;
1119 retn->setDtorCount(m_dtorCount);
1123 void setDtorCount(int *dtorCount)
1125 m_dtorCount = dtorCount;
1129 void intPropertyChanged();
1136 class WriteCounter : public QObject
1139 Q_PROPERTY(int value READ value WRITE setValue);
1141 WriteCounter() : m_value(0), m_count(0) {}
1143 int value() const { return m_value; }
1144 void setValue(int v) { m_value = v; ++m_count; }
1146 int count() const { return m_count; }
1153 class MySequenceConversionObject : public QObject
1157 Q_PROPERTY (QList<int> intListProperty READ intListProperty WRITE setIntListProperty NOTIFY intListPropertyChanged)
1158 Q_PROPERTY (QList<int> intListProperty2 READ intListProperty2 WRITE setIntListProperty2 NOTIFY intListProperty2Changed)
1159 Q_PROPERTY (QList<qreal> qrealListProperty READ qrealListProperty WRITE setQrealListProperty NOTIFY qrealListPropertyChanged)
1160 Q_PROPERTY (QList<bool> boolListProperty READ boolListProperty WRITE setBoolListProperty NOTIFY boolListPropertyChanged)
1161 Q_PROPERTY (QList<QString> stringListProperty READ stringListProperty WRITE setStringListProperty NOTIFY stringListPropertyChanged)
1162 Q_PROPERTY (QList<QUrl> urlListProperty READ urlListProperty WRITE setUrlListProperty NOTIFY urlListPropertyChanged)
1164 Q_PROPERTY (QStringList qstringListProperty READ qstringListProperty WRITE setQStringListProperty NOTIFY qstringListPropertyChanged)
1165 Q_PROPERTY (QList<QPoint> pointListProperty READ pointListProperty WRITE setPointListProperty NOTIFY pointListPropertyChanged)
1168 MySequenceConversionObject()
1170 m_intList << 1 << 2 << 3 << 4;
1171 m_intList2 << 1 << 2 << 3 << 4;
1172 m_qrealList << 1.1 << 2.2 << 3.3 << 4.4;
1173 m_boolList << true << false << true << false;
1174 m_stringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1175 m_urlList << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com");
1177 m_qstringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1178 m_pointList << QPoint(1, 2) << QPoint(3, 4) << QPoint(5, 6);
1181 ~MySequenceConversionObject() {}
1183 QList<int> intListProperty() const { return m_intList; }
1184 void setIntListProperty(const QList<int> &list) { m_intList = list; emit intListPropertyChanged(); }
1185 QList<int> intListProperty2() const { return m_intList2; }
1186 void setIntListProperty2(const QList<int> &list) { m_intList2 = list; emit intListProperty2Changed(); }
1187 QList<qreal> qrealListProperty() const { return m_qrealList; }
1188 void setQrealListProperty(const QList<qreal> &list) { m_qrealList = list; emit qrealListPropertyChanged(); }
1189 QList<bool> boolListProperty() const { return m_boolList; }
1190 void setBoolListProperty(const QList<bool> &list) { m_boolList = list; emit boolListPropertyChanged(); }
1191 QList<QString> stringListProperty() const { return m_stringList; }
1192 void setStringListProperty(const QList<QString> &list) { m_stringList = list; emit stringListPropertyChanged(); }
1193 QList<QUrl> urlListProperty() const { return m_urlList; }
1194 void setUrlListProperty(const QList<QUrl> &list) { m_urlList = list; emit urlListPropertyChanged(); }
1195 QStringList qstringListProperty() const { return m_qstringList; }
1196 void setQStringListProperty(const QStringList &list) { m_qstringList = list; emit qstringListPropertyChanged(); }
1197 QList<QPoint> pointListProperty() const { return m_pointList; }
1198 void setPointListProperty(const QList<QPoint> &list) { m_pointList = list; emit pointListPropertyChanged(); }
1200 // now for "copy resource" sequences:
1201 Q_INVOKABLE QList<int> generateIntSequence() const { QList<int> retn; retn << 1 << 2 << 3; return retn; }
1202 Q_INVOKABLE QList<qreal> generateQrealSequence() const { QList<qreal> retn; retn << 1.1 << 2.2 << 3.3; return retn; }
1203 Q_INVOKABLE QList<bool> generateBoolSequence() const { QList<bool> retn; retn << true << false << true; return retn; }
1204 Q_INVOKABLE QList<QString> generateStringSequence() const { QList<QString> retn; retn << "one" << "two" << "three"; return retn; }
1205 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; }
1206 Q_INVOKABLE QStringList generateQStringSequence() const { QStringList retn; retn << "one" << "two" << "three"; return retn; }
1208 // "reference resource" underlying qobject deletion test:
1209 Q_INVOKABLE MySequenceConversionObject *generateTestObject() const { return new MySequenceConversionObject; }
1210 Q_INVOKABLE void deleteTestObject(QObject *object) const { delete object; }
1213 void intListPropertyChanged();
1214 void intListProperty2Changed();
1215 void qrealListPropertyChanged();
1216 void boolListPropertyChanged();
1217 void stringListPropertyChanged();
1218 void urlListPropertyChanged();
1219 void qstringListPropertyChanged();
1220 void pointListPropertyChanged();
1223 QList<int> m_intList;
1224 QList<int> m_intList2;
1225 QList<qreal> m_qrealList;
1226 QList<bool> m_boolList;
1227 QList<QString> m_stringList;
1228 QList<QUrl> m_urlList;
1230 QStringList m_qstringList; // not a supported sequence type, but QStringList support is hardcoded.
1231 QList<QPoint> m_pointList; // not a supported sequence type
1234 class MyDeleteObject : public QObject
1237 Q_PROPERTY(QObject *nestedObject READ nestedObject NOTIFY nestedObjectChanged);
1238 Q_PROPERTY(int deleteNestedObject READ deleteNestedObject NOTIFY deleteNestedObjectChanged);
1241 MyDeleteObject() : m_nestedObject(new MyQmlObject) {}
1243 QObject *nestedObject() const { return m_nestedObject; }
1244 int deleteNestedObject() { delete m_nestedObject; m_nestedObject = 0; return 1; }
1247 void nestedObjectChanged();
1248 void deleteNestedObjectChanged();
1251 MyQmlObject *m_nestedObject;
1254 void registerTypes();
1256 #endif // TESTTYPES_H