1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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/qdeclarativeengine_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(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 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 QDeclarativeListProperty<QObject> objectListProperty() { return QDeclarativeListProperty<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 stringChanged();
185 void objectChanged();
186 void anotherBasicSignal();
187 void thirdBasicSignal();
188 void signalWithUnknownType(const MyQmlObject::MyType &arg);
189 void signalWithVariant(const QVariant &arg);
190 void signalWithQJSValue(const QJSValue &arg);
194 void deleteMe() { delete this; }
195 void methodNoArgs() { m_methodCalled = true; }
196 void method(int a) { if(a == 163) m_methodIntCalled = true; }
197 void setString(const QString &s) { m_string = s; }
198 void myinvokable(MyQmlObject *o) { myinvokableObject = o; }
199 void variantMethod(const QVariant &v) { m_variant = v; }
200 void qjsvalueMethod(const QJSValue &v) { m_qjsvalue = v; }
201 void v8function(QDeclarativeV8Function*);
204 friend class tst_qdeclarativeecmascript;
206 bool m_methodIntCalled;
211 QList<QObject *> m_objectQList;
220 QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
222 class MyQmlContainer : public QObject
225 Q_PROPERTY(QDeclarativeListProperty<MyQmlObject> children READ children CONSTANT)
229 QDeclarativeListProperty<MyQmlObject> children() { return QDeclarativeListProperty<MyQmlObject>(this, m_children); }
232 QList<MyQmlObject*> m_children;
236 class MyExpression : public QDeclarativeExpression
240 MyExpression(QDeclarativeContext *ctxt, const QString &expr)
241 : QDeclarativeExpression(ctxt, 0, expr), changed(false)
243 QObject::connect(this, SIGNAL(valueChanged()), this, SLOT(expressionValueChanged()));
244 setNotifyOnValueChanged(true);
250 void expressionValueChanged() {
256 class MyDefaultObject1 : public QObject
259 Q_PROPERTY(int horseLegs READ horseLegs CONSTANT)
260 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
261 Q_PROPERTY(int emuLegs READ emuLegs CONSTANT)
263 int horseLegs() const { return 4; }
264 int antLegs() const { return 6; }
265 int emuLegs() const { return 2; }
268 class MyDefaultObject3 : public QObject
271 Q_PROPERTY(int antLegs READ antLegs CONSTANT)
272 Q_PROPERTY(int humanLegs READ humanLegs CONSTANT)
274 int antLegs() const { return 7; } // Mutant
275 int humanLegs() const { return 2; }
276 int millipedeLegs() const { return 1000; }
279 class MyDeferredObject : public QObject
282 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
283 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
284 Q_PROPERTY(QObject *objectProperty2 READ objectProperty2 WRITE setObjectProperty2)
285 Q_CLASSINFO("DeferredPropertyNames", "value,objectProperty,objectProperty2")
288 MyDeferredObject() : m_value(0), m_object(0), m_object2(0) {}
290 int value() const { return m_value; }
291 void setValue(int v) { m_value = v; emit valueChanged(); }
293 QObject *objectProperty() const { return m_object; }
294 void setObjectProperty(QObject *obj) { m_object = obj; }
296 QObject *objectProperty2() const { return m_object2; }
297 void setObjectProperty2(QObject *obj) { m_object2 = obj; }
308 class MyBaseExtendedObject : public QObject
311 Q_PROPERTY(int baseProperty READ baseProperty WRITE setBaseProperty)
313 MyBaseExtendedObject() : m_value(0) {}
315 int baseProperty() const { return m_value; }
316 void setBaseProperty(int v) { m_value = v; }
322 class MyExtendedObject : public MyBaseExtendedObject
325 Q_PROPERTY(int coreProperty READ coreProperty WRITE setCoreProperty)
327 MyExtendedObject() : m_value(0) {}
329 int coreProperty() const { return m_value; }
330 void setCoreProperty(int v) { m_value = v; }
336 class MyTypeObject : public QObject
342 Q_PROPERTY(QString id READ id WRITE setId)
343 Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty)
344 Q_PROPERTY(QDeclarativeComponent *componentProperty READ componentProperty WRITE setComponentProperty)
345 Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty)
346 Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty)
347 Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
348 Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty)
349 Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty)
350 Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty)
351 Q_PROPERTY(double doubleProperty READ doubleProperty WRITE setDoubleProperty)
352 Q_PROPERTY(float floatProperty READ floatProperty WRITE setFloatProperty)
353 Q_PROPERTY(QColor colorProperty READ colorProperty WRITE setColorProperty)
354 Q_PROPERTY(QDate dateProperty READ dateProperty WRITE setDateProperty)
355 Q_PROPERTY(QTime timeProperty READ timeProperty WRITE setTimeProperty)
356 Q_PROPERTY(QDateTime dateTimeProperty READ dateTimeProperty WRITE setDateTimeProperty)
357 Q_PROPERTY(QPoint pointProperty READ pointProperty WRITE setPointProperty)
358 Q_PROPERTY(QPointF pointFProperty READ pointFProperty WRITE setPointFProperty)
359 Q_PROPERTY(QSize sizeProperty READ sizeProperty WRITE setSizeProperty)
360 Q_PROPERTY(QSizeF sizeFProperty READ sizeFProperty WRITE setSizeFProperty)
361 Q_PROPERTY(QRect rectProperty READ rectProperty WRITE setRectProperty NOTIFY rectPropertyChanged)
362 Q_PROPERTY(QRect rectProperty2 READ rectProperty2 WRITE setRectProperty2)
363 Q_PROPERTY(QRectF rectFProperty READ rectFProperty WRITE setRectFProperty)
364 Q_PROPERTY(bool boolProperty READ boolProperty WRITE setBoolProperty)
365 Q_PROPERTY(QVariant variantProperty READ variantProperty WRITE setVariantProperty)
366 Q_PROPERTY(QVector3D vectorProperty READ vectorProperty WRITE setVectorProperty)
367 Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty)
369 Q_PROPERTY(QDeclarativeScriptString scriptProperty READ scriptProperty WRITE setScriptProperty)
373 : objectPropertyValue(0), componentPropertyValue(0) {}
379 void setId(const QString &v) {
383 QObject *objectPropertyValue;
384 QObject *objectProperty() const {
385 return objectPropertyValue;
387 void setObjectProperty(QObject *v) {
388 objectPropertyValue = v;
391 QDeclarativeComponent *componentPropertyValue;
392 QDeclarativeComponent *componentProperty() const {
393 return componentPropertyValue;
395 void setComponentProperty(QDeclarativeComponent *v) {
396 componentPropertyValue = v;
399 enum MyFlag { FlagVal1 = 0x01, FlagVal2 = 0x02, FlagVal3 = 0x04 };
400 Q_DECLARE_FLAGS(MyFlags, MyFlag)
401 MyFlags flagPropertyValue;
402 MyFlags flagProperty() const {
403 return flagPropertyValue;
405 void setFlagProperty(MyFlags v) {
406 flagPropertyValue = v;
409 enum MyEnum { EnumVal1, EnumVal2 };
410 MyEnum enumPropertyValue;
411 MyEnum enumProperty() const {
412 return enumPropertyValue;
414 void setEnumProperty(MyEnum v) {
415 enumPropertyValue = v;
418 QString stringPropertyValue;
419 QString stringProperty() const {
420 return stringPropertyValue;
422 void setStringProperty(const QString &v) {
423 stringPropertyValue = v;
426 uint uintPropertyValue;
427 uint uintProperty() const {
428 return uintPropertyValue;
430 void setUintProperty(const uint &v) {
431 uintPropertyValue = v;
434 int intPropertyValue;
435 int intProperty() const {
436 return intPropertyValue;
438 void setIntProperty(const int &v) {
439 intPropertyValue = v;
442 qreal realPropertyValue;
443 qreal realProperty() const {
444 return realPropertyValue;
446 void setRealProperty(const qreal &v) {
447 realPropertyValue = v;
450 double doublePropertyValue;
451 double doubleProperty() const {
452 return doublePropertyValue;
454 void setDoubleProperty(const double &v) {
455 doublePropertyValue = v;
458 float floatPropertyValue;
459 float floatProperty() const {
460 return floatPropertyValue;
462 void setFloatProperty(const float &v) {
463 floatPropertyValue = v;
466 QColor colorPropertyValue;
467 QColor colorProperty() const {
468 return colorPropertyValue;
470 void setColorProperty(const QColor &v) {
471 colorPropertyValue = v;
474 QDate datePropertyValue;
475 QDate dateProperty() const {
476 return datePropertyValue;
478 void setDateProperty(const QDate &v) {
479 datePropertyValue = v;
482 QTime timePropertyValue;
483 QTime timeProperty() const {
484 return timePropertyValue;
486 void setTimeProperty(const QTime &v) {
487 timePropertyValue = v;
490 QDateTime dateTimePropertyValue;
491 QDateTime dateTimeProperty() const {
492 return dateTimePropertyValue;
494 void setDateTimeProperty(const QDateTime &v) {
495 dateTimePropertyValue = v;
498 QPoint pointPropertyValue;
499 QPoint pointProperty() const {
500 return pointPropertyValue;
502 void setPointProperty(const QPoint &v) {
503 pointPropertyValue = v;
506 QPointF pointFPropertyValue;
507 QPointF pointFProperty() const {
508 return pointFPropertyValue;
510 void setPointFProperty(const QPointF &v) {
511 pointFPropertyValue = v;
514 QSize sizePropertyValue;
515 QSize sizeProperty() const {
516 return sizePropertyValue;
518 void setSizeProperty(const QSize &v) {
519 sizePropertyValue = v;
522 QSizeF sizeFPropertyValue;
523 QSizeF sizeFProperty() const {
524 return sizeFPropertyValue;
526 void setSizeFProperty(const QSizeF &v) {
527 sizeFPropertyValue = v;
530 QRect rectPropertyValue;
531 QRect rectProperty() const {
532 return rectPropertyValue;
534 void setRectProperty(const QRect &v) {
535 rectPropertyValue = v;
536 emit rectPropertyChanged();
539 QRect rectPropertyValue2;
540 QRect rectProperty2() const {
541 return rectPropertyValue2;
543 void setRectProperty2(const QRect &v) {
544 rectPropertyValue2 = v;
547 QRectF rectFPropertyValue;
548 QRectF rectFProperty() const {
549 return rectFPropertyValue;
551 void setRectFProperty(const QRectF &v) {
552 rectFPropertyValue = v;
555 bool boolPropertyValue;
556 bool boolProperty() const {
557 return boolPropertyValue;
559 void setBoolProperty(const bool &v) {
560 boolPropertyValue = v;
563 QVariant variantPropertyValue;
564 QVariant variantProperty() const {
565 return variantPropertyValue;
567 void setVariantProperty(const QVariant &v) {
568 variantPropertyValue = v;
571 QVector3D vectorPropertyValue;
572 QVector3D vectorProperty() const {
573 return vectorPropertyValue;
575 void setVectorProperty(const QVector3D &v) {
576 vectorPropertyValue = v;
579 QUrl urlPropertyValue;
580 QUrl urlProperty() const {
581 return urlPropertyValue;
583 void setUrlProperty(const QUrl &v) {
584 urlPropertyValue = v;
587 QDeclarativeScriptString scriptPropertyValue;
588 QDeclarativeScriptString scriptProperty() const {
589 return scriptPropertyValue;
591 void setScriptProperty(const QDeclarativeScriptString &v) {
592 scriptPropertyValue = v;
595 void doAction() { emit action(); }
598 void rectPropertyChanged();
600 Q_DECLARE_OPERATORS_FOR_FLAGS(MyTypeObject::MyFlags)
602 class MyDerivedObject : public MyTypeObject
606 Q_INVOKABLE bool intProperty() const {
611 Q_DECLARE_METATYPE(QJSValue);
612 class MyInvokableBaseObject : public QObject
616 inline ~MyInvokableBaseObject() = 0;
618 Q_INVOKABLE inline void method_inherited(int a);
619 Q_INVOKABLE inline void method_overload();
622 class MyInvokableObject : public MyInvokableBaseObject
627 enum TestEnum { EnumValue1, EnumValue2 };
628 MyInvokableObject() { reset(); }
630 int invoked() const { return m_invoked; }
631 bool error() const { return m_invokedError; }
632 const QVariantList &actuals() const { return m_actuals; }
633 void reset() { m_invoked = -1; m_invokedError = false; m_actuals.clear(); }
635 Q_INVOKABLE QPointF method_get_QPointF() { return QPointF(99.3, -10.2); }
636 Q_INVOKABLE QPoint method_get_QPoint() { return QPoint(9, 12); }
638 Q_INVOKABLE void method_NoArgs() { invoke(0); }
639 Q_INVOKABLE int method_NoArgs_int() { invoke(1); return 6; }
640 Q_INVOKABLE qreal method_NoArgs_real() { invoke(2); return 19.75; }
641 Q_INVOKABLE QPointF method_NoArgs_QPointF() { invoke(3); return QPointF(123, 4.5); }
642 Q_INVOKABLE QObject *method_NoArgs_QObject() { invoke(4); return this; }
643 Q_INVOKABLE MyInvokableObject *method_NoArgs_unknown() { invoke(5); return this; }
644 Q_INVOKABLE QJSValue method_NoArgs_QScriptValue() { invoke(6); return QJSValue("Hello world"); }
645 Q_INVOKABLE QVariant method_NoArgs_QVariant() { invoke(7); return QVariant("QML rocks"); }
647 Q_INVOKABLE void method_int(int a) { invoke(8); m_actuals << a; }
648 Q_INVOKABLE void method_intint(int a, int b) { invoke(9); m_actuals << a << b; }
649 Q_INVOKABLE void method_real(qreal a) { invoke(10); m_actuals << a; }
650 Q_INVOKABLE void method_QString(QString a) { invoke(11); m_actuals << a; }
651 Q_INVOKABLE void method_QPointF(QPointF a) { invoke(12); m_actuals << a; }
652 Q_INVOKABLE void method_QObject(QObject *a) { invoke(13); m_actuals << qVariantFromValue(a); }
653 Q_INVOKABLE void method_QScriptValue(QJSValue a) { invoke(14); m_actuals << qVariantFromValue(a); }
654 Q_INVOKABLE void method_intQScriptValue(int a, QJSValue b) { invoke(15); m_actuals << a << qVariantFromValue(b); }
656 Q_INVOKABLE void method_overload(int a) { invoke(16); m_actuals << a; }
657 Q_INVOKABLE void method_overload(int a, int b) { invoke(17); m_actuals << a << b; }
658 Q_INVOKABLE void method_overload(QString a) { invoke(18); m_actuals << a; }
660 Q_INVOKABLE void method_with_enum(TestEnum e) { invoke(19); m_actuals << (int)e; }
662 Q_INVOKABLE int method_default(int a, int b = 19) { invoke(20); m_actuals << a << b; return b; }
664 Q_INVOKABLE void method_QVariant(QVariant a, QVariant b = QVariant()) { invoke(21); m_actuals << a << b; }
667 friend class MyInvokableBaseObject;
668 void invoke(int idx) { if (m_invoked != -1) m_invokedError = true; m_invoked = idx;}
671 QVariantList m_actuals;
674 MyInvokableBaseObject::~MyInvokableBaseObject() {}
676 void MyInvokableBaseObject::method_inherited(int a)
678 static_cast<MyInvokableObject *>(this)->invoke(-3);
679 static_cast<MyInvokableObject *>(this)->m_actuals << a;
682 // This is a hidden overload of the MyInvokableObject::method_overload() method
683 void MyInvokableBaseObject::method_overload()
685 static_cast<MyInvokableObject *>(this)->invoke(-2);
688 class NumberAssignment : public QObject
692 Q_PROPERTY(qreal test1 READ test1 WRITE setTest1)
694 qreal test1() const { return _test1; }
695 void setTest1(qreal v) { _test1 = v; }
697 Q_PROPERTY(qreal test2 READ test2 WRITE setTest2)
699 qreal test2() const { return _test2; }
700 void setTest2(qreal v) { _test2 = v; }
702 Q_PROPERTY(qreal test3 READ test3 WRITE setTest3)
704 qreal test3() const { return _test3; }
705 void setTest3(qreal v) { _test3 = v; }
707 Q_PROPERTY(qreal test4 READ test4 WRITE setTest4)
709 qreal test4() const { return _test4; }
710 void setTest4(qreal v) { _test4 = v; }
712 Q_PROPERTY(int test5 READ test5 WRITE setTest5)
714 int test5() const { return _test5; }
715 void setTest5(int v) { _test5 = v; }
717 Q_PROPERTY(int test6 READ test6 WRITE setTest6)
719 int test6() const { return _test6; }
720 void setTest6(int v) { _test6 = v; }
722 Q_PROPERTY(int test7 READ test7 WRITE setTest7)
724 int test7() const { return _test7; }
725 void setTest7(int v) { _test7 = v; }
727 Q_PROPERTY(int test8 READ test8 WRITE setTest8)
729 int test8() const { return _test8; }
730 void setTest8(int v) { _test8 = v; }
732 Q_PROPERTY(unsigned int test9 READ test9 WRITE setTest9)
734 unsigned int test9() const { return _test9; }
735 void setTest9(unsigned int v) { _test9 = v; }
737 Q_PROPERTY(unsigned int test10 READ test10 WRITE setTest10)
738 unsigned int _test10;
739 unsigned int test10() const { return _test10; }
740 void setTest10(unsigned int v) { _test10 = v; }
742 Q_PROPERTY(unsigned int test11 READ test11 WRITE setTest11)
743 unsigned int _test11;
744 unsigned int test11() const { return _test11; }
745 void setTest11(unsigned int v) { _test11 = v; }
747 Q_PROPERTY(unsigned int test12 READ test12 WRITE setTest12)
748 unsigned int _test12;
749 unsigned int test12() const { return _test12; }
750 void setTest12(unsigned int v) { _test12 = v; }
753 class DefaultPropertyExtendedObject : public QObject
756 Q_PROPERTY(QObject *firstProperty READ firstProperty WRITE setFirstProperty)
757 Q_PROPERTY(QObject *secondProperty READ secondProperty WRITE setSecondProperty)
759 DefaultPropertyExtendedObject(QObject *parent = 0) : QObject(parent), m_firstProperty(0), m_secondProperty(0) {}
761 QObject *firstProperty() const { return m_firstProperty; }
762 QObject *secondProperty() const { return m_secondProperty; }
763 void setFirstProperty(QObject *property) { m_firstProperty = property; }
764 void setSecondProperty(QObject *property) { m_secondProperty = property; }
766 QObject* m_firstProperty;
767 QObject* m_secondProperty;
770 class OverrideDefaultPropertyObject : public DefaultPropertyExtendedObject
773 Q_CLASSINFO("DefaultProperty", "secondProperty")
775 OverrideDefaultPropertyObject() {}
778 class MyRevisionedBaseClassRegistered : public QObject
781 Q_PROPERTY(qreal propA READ propA WRITE setPropA NOTIFY propAChanged)
782 Q_PROPERTY(qreal propB READ propB WRITE setPropB NOTIFY propBChanged REVISION 1)
785 MyRevisionedBaseClassRegistered() : m_pa(1), m_pb(2) {}
787 qreal propA() const { return m_pa; }
788 void setPropA(qreal p) {
794 qreal propB() const { return m_pb; }
795 void setPropB(qreal p) {
802 Q_INVOKABLE void methodA() { }
803 Q_INVOKABLE Q_REVISION(1) void methodB() { }
810 Q_REVISION(1) void signalB();
817 class MyRevisionedBaseClassUnregistered : public MyRevisionedBaseClassRegistered
820 Q_PROPERTY(qreal propC READ propC WRITE setPropC NOTIFY propCChanged)
821 Q_PROPERTY(qreal propD READ propD WRITE setPropD NOTIFY propDChanged REVISION 1)
824 MyRevisionedBaseClassUnregistered() : m_pc(1), m_pd(2) {}
826 qreal propC() const { return m_pc; }
827 void setPropC(qreal p) {
833 qreal propD() const { return m_pd; }
834 void setPropD(qreal p) {
841 Q_INVOKABLE void methodC() { }
842 Q_INVOKABLE Q_REVISION(1) void methodD() { }
849 Q_REVISION(1) void signalD();
856 class MyRevisionedClass : public MyRevisionedBaseClassUnregistered
859 Q_PROPERTY(qreal prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed)
860 Q_PROPERTY(qreal prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed REVISION 1)
863 MyRevisionedClass() {}
865 qreal prop1() const { return m_p1; }
866 void setProp1(qreal p) {
872 qreal prop2() const { return m_p2; }
873 void setProp2(qreal p) {
880 Q_INVOKABLE void method1() { }
881 Q_INVOKABLE Q_REVISION(1) void method2() { }
888 Q_REVISION(1) void signal2();
895 class MyRevisionedSubclass : public MyRevisionedClass
898 Q_PROPERTY(qreal prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed)
899 Q_PROPERTY(qreal prop4 READ prop4 WRITE setProp4 NOTIFY prop4Changed REVISION 1)
902 MyRevisionedSubclass() : m_p3(3), m_p4(4) {}
904 qreal prop3() const { return m_p3; }
905 void setProp3(qreal p) {
911 qreal prop4() const { return m_p4; }
912 void setProp4(qreal p) {
919 Q_INVOKABLE void method3() { }
920 Q_INVOKABLE Q_REVISION(1) void method4() { }
927 Q_REVISION(1) void signal4();
934 QML_DECLARE_TYPE(MyRevisionedBaseClassRegistered)
935 QML_DECLARE_TYPE(MyRevisionedBaseClassUnregistered)
936 QML_DECLARE_TYPE(MyRevisionedClass)
937 QML_DECLARE_TYPE(MyRevisionedSubclass)
938 Q_DECLARE_METATYPE(MyQmlObject::MyType)
941 class ScarceResourceObject : public QObject
944 Q_PROPERTY(QPixmap scarceResource READ scarceResource WRITE setScarceResource NOTIFY scarceResourceChanged)
946 ScarceResourceObject(QObject *parent = 0) : QObject(parent), m_value(100, 100) { m_value.fill(Qt::blue); }
947 ~ScarceResourceObject() {}
949 QPixmap scarceResource() const { return m_value; }
950 void setScarceResource(QPixmap v) { m_value = v; emit scarceResourceChanged(); }
952 bool scarceResourceIsDetached() const { return m_value.isDetached(); }
954 // this particular one returns a new one each time
955 // this means that every Scarce Resource Copy will
956 // consume resources (so that we can track disposal
957 // of v8 handles with circular references).
958 Q_INVOKABLE QPixmap newScarceResource() const
960 QPixmap retn(800, 600);
961 retn.fill(QColor(100, 110, 120, 45));
966 void scarceResourceChanged();
971 QML_DECLARE_TYPE(ScarceResourceObject)
973 class testQObjectApi : public QObject
977 Q_PROPERTY (int qobjectTestProperty READ qobjectTestProperty NOTIFY qobjectTestPropertyChanged)
978 Q_PROPERTY (int qobjectTestWritableProperty READ qobjectTestWritableProperty WRITE setQObjectTestWritableProperty NOTIFY qobjectTestWritablePropertyChanged)
981 testQObjectApi(QObject* parent = 0)
982 : QObject(parent), m_testProperty(0), m_testWritableProperty(0), m_methodCallCount(0)
988 enum MyEnum { EnumValue1 = 25, EnumValue2 = 42 };
989 Q_INVOKABLE int qobjectEnumTestMethod(MyEnum val) { return (static_cast<int>(val) + 5); }
990 Q_INVOKABLE int qobjectTestMethod(int increment = 1) { m_methodCallCount += increment; return m_methodCallCount; }
992 int qobjectTestProperty() const { return m_testProperty; }
993 void setQObjectTestProperty(int tp) { m_testProperty = tp; emit qobjectTestPropertyChanged(tp); }
995 int qobjectTestWritableProperty() const { return m_testWritableProperty; }
996 void setQObjectTestWritableProperty(int tp) { m_testWritableProperty = tp; emit qobjectTestWritablePropertyChanged(tp); }
999 void qobjectTestPropertyChanged(int testProperty);
1000 void qobjectTestWritablePropertyChanged(int testWritableProperty);
1004 int m_testWritableProperty;
1005 int m_methodCallCount;
1008 class CircularReferenceObject : public QObject,
1009 public QV8GCCallback::Node
1014 CircularReferenceObject(QObject *parent = 0)
1015 : QObject(parent), QV8GCCallback::Node(callback), m_referenced(0), m_dtorCount(0)
1017 QV8GCCallback::addGcCallbackNode(this);
1020 ~CircularReferenceObject()
1022 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1025 Q_INVOKABLE void setDtorCount(int *dtorCount)
1027 m_dtorCount = dtorCount;
1030 Q_INVOKABLE CircularReferenceObject *generate(QObject *parent = 0)
1032 CircularReferenceObject *retn = new CircularReferenceObject(parent);
1033 retn->m_dtorCount = m_dtorCount;
1034 retn->m_engine = m_engine;
1038 Q_INVOKABLE void addReference(QObject *other)
1040 m_referenced = other;
1043 static void callback(QV8GCCallback::Node *n)
1045 CircularReferenceObject *cro = static_cast<CircularReferenceObject*>(n);
1046 if (cro->m_referenced) {
1047 cro->m_engine->addRelationshipForGC(cro, cro->m_referenced);
1051 void setEngine(QDeclarativeEngine* declarativeEngine)
1053 m_engine = QDeclarativeEnginePrivate::get(declarativeEngine)->v8engine();
1057 QObject *m_referenced;
1059 QV8Engine* m_engine;
1061 Q_DECLARE_METATYPE(CircularReferenceObject*)
1063 class CircularReferenceHandle : public QObject,
1064 public QV8GCCallback::Node
1069 CircularReferenceHandle(QObject *parent = 0)
1070 : QObject(parent), QV8GCCallback::Node(gccallback), m_dtorCount(0), m_engine(0)
1072 QV8GCCallback::addGcCallbackNode(this);
1075 ~CircularReferenceHandle()
1077 if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
1080 Q_INVOKABLE void setDtorCount(int *dtorCount)
1082 m_dtorCount = dtorCount;
1085 Q_INVOKABLE CircularReferenceHandle *generate(QObject *parent = 0)
1087 CircularReferenceHandle *retn = new CircularReferenceHandle(parent);
1088 retn->m_dtorCount = m_dtorCount;
1089 retn->m_engine = m_engine;
1093 Q_INVOKABLE void addReference(v8::Persistent<v8::Value> handle)
1095 m_referenced = qPersistentNew(handle);
1096 m_referenced.MakeWeak(static_cast<void*>(this), wrcallback);
1099 static void wrcallback(v8::Persistent<v8::Value> handle, void *params)
1101 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(params);
1102 qPersistentDispose(handle);
1103 crh->m_referenced.Clear();
1106 static void gccallback(QV8GCCallback::Node *n)
1108 CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(n);
1109 crh->m_engine->addRelationshipForGC(crh, crh->m_referenced);
1112 void setEngine(QDeclarativeEngine* declarativeEngine)
1114 m_engine = QDeclarativeEnginePrivate::get(declarativeEngine)->v8engine();
1118 v8::Persistent<v8::Value> m_referenced;
1120 QV8Engine* m_engine;
1122 Q_DECLARE_METATYPE(CircularReferenceHandle*)
1124 class MyDynamicCreationDestructionObject : public QObject
1127 Q_PROPERTY (int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged)
1130 MyDynamicCreationDestructionObject(QObject *parent = 0) : QObject(parent), m_intProperty(0), m_dtorCount(0)
1134 ~MyDynamicCreationDestructionObject()
1141 int intProperty() const { return m_intProperty; }
1142 void setIntProperty(int val) { m_intProperty = val; emit intPropertyChanged(); }
1144 Q_INVOKABLE MyDynamicCreationDestructionObject *createNew()
1146 // no parent == ownership transfers to JS; same dtor counter.
1147 MyDynamicCreationDestructionObject *retn = new MyDynamicCreationDestructionObject;
1148 retn->setDtorCount(m_dtorCount);
1152 void setDtorCount(int *dtorCount)
1154 m_dtorCount = dtorCount;
1158 void intPropertyChanged();
1165 class WriteCounter : public QObject
1168 Q_PROPERTY(int value READ value WRITE setValue);
1170 WriteCounter() : m_value(0), m_count(0) {}
1172 int value() const { return m_value; }
1173 void setValue(int v) { m_value = v; ++m_count; }
1175 int count() const { return m_count; }
1182 class MySequenceConversionObject : public QObject
1186 Q_PROPERTY (QList<int> intListProperty READ intListProperty WRITE setIntListProperty NOTIFY intListPropertyChanged)
1187 Q_PROPERTY (QList<int> intListProperty2 READ intListProperty2 WRITE setIntListProperty2 NOTIFY intListProperty2Changed)
1188 Q_PROPERTY (QList<qreal> qrealListProperty READ qrealListProperty WRITE setQrealListProperty NOTIFY qrealListPropertyChanged)
1189 Q_PROPERTY (QList<bool> boolListProperty READ boolListProperty WRITE setBoolListProperty NOTIFY boolListPropertyChanged)
1190 Q_PROPERTY (QList<QString> stringListProperty READ stringListProperty WRITE setStringListProperty NOTIFY stringListPropertyChanged)
1191 Q_PROPERTY (QList<QUrl> urlListProperty READ urlListProperty WRITE setUrlListProperty NOTIFY urlListPropertyChanged)
1192 Q_PROPERTY (QStringList qstringListProperty READ qstringListProperty WRITE setQStringListProperty NOTIFY qstringListPropertyChanged)
1194 Q_PROPERTY (QList<QPoint> pointListProperty READ pointListProperty WRITE setPointListProperty NOTIFY pointListPropertyChanged)
1195 Q_PROPERTY (QList<QVariant> variantListProperty READ variantListProperty WRITE setVariantListProperty NOTIFY variantListPropertyChanged)
1198 MySequenceConversionObject()
1200 m_intList << 1 << 2 << 3 << 4;
1201 m_intList2 << 1 << 2 << 3 << 4;
1202 m_qrealList << 1.1 << 2.2 << 3.3 << 4.4;
1203 m_boolList << true << false << true << false;
1204 m_stringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1205 m_urlList << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com");
1206 m_qstringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
1208 m_pointList << QPoint(1, 2) << QPoint(3, 4) << QPoint(5, 6);
1209 m_variantList << QVariant(QLatin1String("one")) << QVariant(true) << QVariant(3);
1212 ~MySequenceConversionObject() {}
1214 QList<int> intListProperty() const { return m_intList; }
1215 void setIntListProperty(const QList<int> &list) { m_intList = list; emit intListPropertyChanged(); }
1216 QList<int> intListProperty2() const { return m_intList2; }
1217 void setIntListProperty2(const QList<int> &list) { m_intList2 = list; emit intListProperty2Changed(); }
1218 QList<qreal> qrealListProperty() const { return m_qrealList; }
1219 void setQrealListProperty(const QList<qreal> &list) { m_qrealList = list; emit qrealListPropertyChanged(); }
1220 QList<bool> boolListProperty() const { return m_boolList; }
1221 void setBoolListProperty(const QList<bool> &list) { m_boolList = list; emit boolListPropertyChanged(); }
1222 QList<QString> stringListProperty() const { return m_stringList; }
1223 void setStringListProperty(const QList<QString> &list) { m_stringList = list; emit stringListPropertyChanged(); }
1224 QList<QUrl> urlListProperty() const { return m_urlList; }
1225 void setUrlListProperty(const QList<QUrl> &list) { m_urlList = list; emit urlListPropertyChanged(); }
1226 QStringList qstringListProperty() const { return m_qstringList; }
1227 void setQStringListProperty(const QStringList &list) { m_qstringList = list; emit qstringListPropertyChanged(); }
1228 QList<QPoint> pointListProperty() const { return m_pointList; }
1229 void setPointListProperty(const QList<QPoint> &list) { m_pointList = list; emit pointListPropertyChanged(); }
1230 QList<QVariant> variantListProperty() const { return m_variantList; }
1231 void setVariantListProperty(const QList<QVariant> &list) { m_variantList = list; emit variantListPropertyChanged(); }
1233 // now for "copy resource" sequences:
1234 Q_INVOKABLE QList<int> generateIntSequence() const { QList<int> retn; retn << 1 << 2 << 3; return retn; }
1235 Q_INVOKABLE QList<qreal> generateQrealSequence() const { QList<qreal> retn; retn << 1.1 << 2.2 << 3.3; return retn; }
1236 Q_INVOKABLE QList<bool> generateBoolSequence() const { QList<bool> retn; retn << true << false << true; return retn; }
1237 Q_INVOKABLE QList<QString> generateStringSequence() const { QList<QString> retn; retn << "one" << "two" << "three"; return retn; }
1238 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; }
1239 Q_INVOKABLE QStringList generateQStringSequence() const { QStringList retn; retn << "one" << "two" << "three"; return retn; }
1241 // "reference resource" underlying qobject deletion test:
1242 Q_INVOKABLE MySequenceConversionObject *generateTestObject() const { return new MySequenceConversionObject; }
1243 Q_INVOKABLE void deleteTestObject(QObject *object) const { delete object; }
1246 void intListPropertyChanged();
1247 void intListProperty2Changed();
1248 void qrealListPropertyChanged();
1249 void boolListPropertyChanged();
1250 void stringListPropertyChanged();
1251 void urlListPropertyChanged();
1252 void qstringListPropertyChanged();
1253 void pointListPropertyChanged();
1254 void variantListPropertyChanged();
1257 QList<int> m_intList;
1258 QList<int> m_intList2;
1259 QList<qreal> m_qrealList;
1260 QList<bool> m_boolList;
1261 QList<QString> m_stringList;
1262 QList<QUrl> m_urlList;
1263 QStringList m_qstringList;
1265 QList<QPoint> m_pointList; // not a supported sequence type
1266 QList<QVariant> m_variantList; // not a supported sequence type, but QVariantList support is hardcoded.
1269 class MyDeleteObject : public QObject
1272 Q_PROPERTY(QObject *nestedObject READ nestedObject NOTIFY nestedObjectChanged);
1273 Q_PROPERTY(int deleteNestedObject READ deleteNestedObject NOTIFY deleteNestedObjectChanged);
1276 MyDeleteObject() : m_nestedObject(new MyQmlObject) {}
1278 QObject *nestedObject() const { return m_nestedObject; }
1279 int deleteNestedObject() { delete m_nestedObject; m_nestedObject = 0; return 1; }
1282 void nestedObjectChanged();
1283 void deleteNestedObjectChanged();
1286 MyQmlObject *m_nestedObject;
1289 void registerTypes();
1291 #endif // TESTTYPES_H