summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qmlmetaproperty.cpp
blob: efc4a2b779b4fc98dea97c0bda0129acd937fc30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmlmetaproperty.h"
#include "qmlmetaproperty_p.h"
#include "qmlcompositetypedata_p.h"
#include <qml.h>
#include <private/qfxperf_p.h>
#include <QStringList>
#include "qmlbinding.h"
#include <qmlcontext.h>
#include "qmlboundsignal_p.h"
#include <math.h>
#include <QtCore/qdebug.h>
#include <QtDeclarative/qmlengine.h>
#include <private/qmlengine_p.h>
#include <private/qmldeclarativedata_p.h>

Q_DECLARE_METATYPE(QList<QObject *>);

QT_BEGIN_NAMESPACE

QmlMetaObjectCache::QmlMetaObjectCache()
: propertyCache(0)
{
}

void QmlMetaObjectCache::init(const QMetaObject *metaObject)
{
    if (propertyCache || !metaObject)
        return;

    int propCount = metaObject->propertyCount();

    propertyCache = new Data[propCount];
    for (int ii = 0; ii < propCount; ++ii) {
        QMetaProperty p = metaObject->property(ii);
        propertyCache[ii].propType = p.userType();
        propertyCache[ii].coreIndex = ii;
        propertyCache[ii].name = QLatin1String(p.name());

        propertyNameCache.insert(propertyCache[ii].name, ii);
    }
}

QmlMetaObjectCache::~QmlMetaObjectCache()
{
    delete [] propertyCache;
}

QmlMetaObjectCache::Data *
QmlMetaObjectCache::property(int index, const QMetaObject *metaObject)
{
    init(metaObject);

    return propertyCache + index;
}

QmlMetaObjectCache::Data *
QmlMetaObjectCache::property(const QString &name, const QMetaObject *metaObject)
{
    init(metaObject);

    QHash<QString, int>::ConstIterator iter = propertyNameCache.find(name);

    if (iter != propertyNameCache.end()) {
        return propertyCache + *iter;
    } else {
        return 0;
    }
}

/*!
    \class QmlMetaProperty
    \brief The QmlMetaProperty class abstracts accessing QML properties.
 */

/*!
    Create an invalid QmlMetaProperty.
*/
QmlMetaProperty::QmlMetaProperty()
: d(new QmlMetaPropertyPrivate)
{
    d->q = this;
}

/*!
  The destructor deletes its heap data.
 */
QmlMetaProperty::~QmlMetaProperty()
{
    delete d; d = 0;
}

/*!
    Creates a QmlMetaProperty for the default property of \a obj. If there is no
    default property, an invalid QmlMetaProperty will be created.
 */
QmlMetaProperty::QmlMetaProperty(QObject *obj)
: d(new QmlMetaPropertyPrivate)
{
    d->q = this;
    d->initDefault(obj);
}

/*!
    \internal
    Creates a QmlMetaProperty for the default property of \a obj. If there is no
    default property, an invalid QmlMetaProperty will be created.
 */
QmlMetaProperty::QmlMetaProperty(QObject *obj, QmlContext *ctxt)
: d(new QmlMetaPropertyPrivate)
{
    d->q = this;
    d->context = ctxt;
    d->initDefault(obj);
}

/*!
    Initialize from the default property of \a obj
*/
void QmlMetaPropertyPrivate::initDefault(QObject *obj)
{
    if (!obj)
        return;

    object = obj;
    QMetaProperty p = QmlMetaType::defaultProperty(obj);
    name = QLatin1String(p.name());
    propType = p.userType();;
    coreIdx = p.propertyIndex();
    if (!name.isEmpty()) 
        type = QmlMetaProperty::Property | QmlMetaProperty::Default;
}

/*!
    \internal

    Creates a QmlMetaProperty for the property at index \a idx of \a obj.
 */
QmlMetaProperty::QmlMetaProperty(QObject *obj, int idx, QmlContext *ctxt)
: d(new QmlMetaPropertyPrivate)
{
    d->q = this;
    d->context = ctxt;
    d->object = obj;
    d->type = Property;
    QMetaProperty p = obj->metaObject()->property(idx);
    d->propType = p.userType();
    d->coreIdx = idx;
    if (p.name() != 0)
        d->name = QLatin1String(p.name());
}

/*!
    Creates a QmlMetaProperty for the property \a name of \a obj.
 */
QmlMetaProperty::QmlMetaProperty(QObject *obj, const QString &name)
: d(new QmlMetaPropertyPrivate)
{
    d->q = this;
    d->initProperty(obj, name);
}

/*!
    \internal
    Creates a QmlMetaProperty for the property \a name of \a obj.
 */
QmlMetaProperty::QmlMetaProperty(QObject *obj, const QString &name, QmlContext *ctxt)
: d(new QmlMetaPropertyPrivate)
{
    d->q = this;
    d->context = ctxt;
    d->initProperty(obj, name);
}

void QmlMetaPropertyPrivate::initProperty(QObject *obj, const QString &name)
{
    QmlEnginePrivate *enginePrivate = 0;
    if (context && context->engine())
        enginePrivate = QmlEnginePrivate::get(context->engine());

    this->name = name;
    object = obj;

    if (name.isEmpty() || !obj)
        return;

    if (enginePrivate && name.at(0).isUpper()) {
        // Attached property
        //### needs to be done in a better way
        QmlCompositeTypeData *typeData =
            enginePrivate->typeManager.get(context->baseUrl());

        if (typeData) {
            QmlType *t = 0;
            enginePrivate->resolveType(typeData->imports, name.toLatin1(), &t, 0, 0, 0, 0);
            if (t && t->attachedPropertiesFunction()) {
                attachedFunc = t->index();
                if (attachedFunc != -1)
                    type  = QmlMetaProperty::Property | QmlMetaProperty::Attached;
            }
            typeData->release();
        }
        return;

    } else if (name.count() >= 3 && 
               name.at(0) == QChar(QLatin1Char('o')) && 
               name.at(1) == QChar(QLatin1Char('n')) && 
               name.at(2).isUpper()) {
        // Signal
        QString signalName = name.mid(2);
        signalName[0] = signalName.at(0).toLower();

        findSignalInt(obj, signalName);
        if (signal.signature() != 0) {
            type = QmlMetaProperty::SignalProperty;
            return;
        }
    } 

    // Property
    QmlMetaObjectCache *cache = QmlEnginePrivate::cache(enginePrivate, obj);
    if (cache) {
        QmlMetaObjectCache::Data *data = 
            cache->property(name, obj->metaObject());
        if (data) {
            type = QmlMetaProperty::Property;
            propType = data->propType;
            coreIdx = data->coreIndex;
        }
    } else {
        // Can't cache
        QMetaProperty p = QmlMetaType::property(obj, name.toUtf8().constData());
        propType = p.userType();
        coreIdx = p.propertyIndex();
        if (p.name())
            type = QmlMetaProperty::Property;
    }
}

/*!
    Create a copy of \a other.
*/
QmlMetaProperty::QmlMetaProperty(const QmlMetaProperty &other)
: d(new QmlMetaPropertyPrivate(*other.d))
{
    d->q = this;
}

/*!
  \enum QmlMetaProperty::PropertyCategory

  This enum specifies a category of QML property.

  \value Unknown The category is unknown.  This will never be returned from propertyCategory()
  \value InvalidProperty The property is invalid.
  \value Bindable The property is a QmlBinding.
  \value List The property is a QList pointer
  \value QmlList The property is a QmlList pointer
  \value Object The property is a QObject derived type pointer
  \value Normal The property is none of the above.
 */

/*!
  \enum QmlMetaProperty::Type

  This enum specifies a type of QML property.

  \value Invalid The property is invalid.
  \value Property The property is a regular Qt property.
  \value SignalProperty The property is a signal property.
  \value Default The property is the default property.
  \value Attached The property is an attached property.
*/

/*!
    Returns the property category.
*/
QmlMetaProperty::PropertyCategory QmlMetaProperty::propertyCategory() const
{
    return d->propertyCategory();
}

QmlMetaProperty::PropertyCategory 
QmlMetaPropertyPrivate::propertyCategory() const
{
    if (category == QmlMetaProperty::Unknown) {
        int type = propertyType();
        if (type == QmlMetaProperty::Invalid)
            category = QmlMetaProperty::InvalidProperty;
        else if (type < QVariant::UserType)
            category = QmlMetaProperty::Normal;
        else if (type == qMetaTypeId<QmlBinding *>())
            category = QmlMetaProperty::Bindable;
        else {
            QmlMetaType::TypeCategory tc = QmlMetaType::typeCategory(type);
            switch(tc) {
            case QmlMetaType::Object:
                category = QmlMetaProperty::Object;
                break;
            case QmlMetaType::QmlList:
                category = QmlMetaProperty::QmlList;
                break;
            case QmlMetaType::List:
                category = QmlMetaProperty::List;
                break;
            case QmlMetaType::Unknown:
                category = QmlMetaProperty::Normal;
                break;
            }
        }
    }
    return category;
}

/*!
    Returns the type name of the property, or 0 if the property has no type
    name.
*/
const char *QmlMetaProperty::propertyTypeName() const
{
    if (!d->name.isEmpty() && d->object) {
        return d->object->metaObject()->property(d->coreIdx).typeName();
    } else {
        return 0;
    }
}

/*!
    Returns true if \a other and this QmlMetaProperty represent the same 
    property.
*/
bool QmlMetaProperty::operator==(const QmlMetaProperty &other) const
{
    return d->name == other.d->name &&
           d->signal.signature() == other.d->signal.signature() &&
           d->type == other.d->type &&
           d->object == other.d->object;
}

/*!
    Returns the QVariant type of the property, or QVariant::Invalid if the 
    property has no QVariant type.
*/
int QmlMetaProperty::propertyType() const
{
    return d->propertyType();
}

int QmlMetaPropertyPrivate::propertyType() const
{
    int rv = QVariant::Invalid;

    if (!name.isEmpty()) {
        if (propType == (int)QVariant::LastType)
            rv = qMetaTypeId<QVariant>();
        else
            rv = propType;
    } else if (attachedFunc) {
        rv = qMetaTypeId<QObject *>();
    } 

    return rv;
}

/*!
    Returns the type of the property.
*/
QmlMetaProperty::Type QmlMetaProperty::type() const
{
    return (Type)d->type;
}

/*!
    Returns true if this QmlMetaProperty represents a regular Qt property.
*/
bool QmlMetaProperty::isProperty() const
{
    return type() & Property;
}

/*!
    Returns true if this QmlMetaProperty represents a default property.
*/
bool QmlMetaProperty::isDefault() const
{
    return type() & Default;
}

/*!
    Returns the QmlMetaProperty's QObject.
*/
QObject *QmlMetaProperty::object() const
{
    return d->object;
}

/*!
    Assign \a other to this QmlMetaProperty.
*/
QmlMetaProperty &QmlMetaProperty::operator=(const QmlMetaProperty &other)
{
    d->name = other.d->name;
    d->signal = other.d->signal;
    d->context = other.d->context;
    d->coreIdx = other.d->coreIdx;
    d->valueTypeIdx = other.d->valueTypeIdx;
    d->valueTypeId = other.d->valueTypeId;
    d->type = other.d->type;
    d->attachedFunc = other.d->attachedFunc;
    d->object = other.d->object;
    d->propType = other.d->propType;
    d->category = other.d->category;
    return *this;
}

/*!
    Returns true if the property is writable, otherwise false.
*/
bool QmlMetaProperty::isWritable() const
{
    if (propertyCategory() == List || propertyCategory() == QmlList)
        return true;
    else if (type() & SignalProperty)
        return true;
    else if (!d->name.isEmpty() && d->object)
        return d->object->metaObject()->property(d->coreIdx).isWritable();
    else
        return false;
}

/*!
    Returns true if the property is designable, otherwise false.
*/
bool QmlMetaProperty::isDesignable() const
{
    if (!d->name.isEmpty() && d->object)
        return d->object->metaObject()->property(d->coreIdx).isDesignable();
    else
        return false;
}

/*!
    Returns true if the QmlMetaProperty refers to a valid property, otherwise
    false.
*/
bool QmlMetaProperty::isValid() const
{
    return type() != Invalid;
}

/*!
    Returns all of \a obj's Qt properties.
*/
QStringList QmlMetaProperty::properties(QObject *obj)
{
    if (!obj)
        return QStringList();

    QStringList rv;
    const QMetaObject *mo = obj->metaObject();
    for (int ii = 0; ii < mo->propertyCount(); ++ii) {
        QMetaProperty prop = mo->property(ii);
        rv << QLatin1String(prop.name());
    }

    return rv;
}

/*!
    Return the name of this QML property.
*/
QString QmlMetaProperty::name() const
{
    return d->name;
}

/*!
  Returns the \l{QMetaProperty} {Qt property} associated with
  this QML property.
 */
QMetaProperty QmlMetaProperty::property() const
{
    if (d->object)
        return d->object->metaObject()->property(d->coreIdx);
    else
        return QMetaProperty();
}

/*!
    Returns the binding associated with this property, or 0 if no binding 
    exists.
*/
QmlAbstractBinding *QmlMetaProperty::binding() const
{
    if (!isProperty() || (type() & Attached) || !d->object)
        return 0;

    QmlDeclarativeData *data = QmlDeclarativeData::get(d->object);
    if (!data) 
        return 0;

    if (!data->hasBindingBit(d->coreIdx))
        return 0;

    QmlAbstractBinding *binding = data->bindings;
    while (binding) {
        // ### This wont work for value types
        if (binding->propertyIndex() == d->coreIdx)
            return binding; 
        binding = binding->m_nextBinding;
    }
    return 0;
}

/*!
    Set the binding associated with this property to \a newBinding.  Returns
    the existing binding (if any), otherwise 0.

    \a newBinding will be enabled, and the returned binding (if any) will be
    disabled.

    Ownership of \a newBinding transfers to QML.  Ownership of the return value
    is assumed by the caller.
*/
QmlAbstractBinding *
QmlMetaProperty::setBinding(QmlAbstractBinding *newBinding) const
{
    if (!isProperty() || (type() & Attached) || !d->object)
        return 0;

    QmlDeclarativeData *data = 
        QmlDeclarativeData::get(d->object, 0 != newBinding);

    if (data && data->hasBindingBit(d->coreIdx)) {
        QmlAbstractBinding *binding = data->bindings;
        while (binding) {
            // ### This wont work for value types
            if (binding->propertyIndex() == d->coreIdx) {
                binding->setEnabled(false);

                if (newBinding) 
                    newBinding->setEnabled(true);

                return binding; // ### QmlAbstractBinding;
            }

            binding = binding->m_nextBinding;
        }
    } 

    if (newBinding)
        newBinding->setEnabled(true);

    return 0;
}
/*!
    Returns the expression associated with this signal property, or 0 if no 
    signal expression exists.
*/
QmlExpression *QmlMetaProperty::signalExpression() const
{
    if (!(type() & SignalProperty))
        return 0;

    const QObjectList &children = d->object->children();
    
    for (int ii = 0; ii < children.count(); ++ii) {
        QObject *child = children.at(ii);

        QmlBoundSignal *signal = QmlBoundSignal::cast(child);
        if (signal && signal->index() == coreIndex()) 
            return signal->expression();
    }

    return 0;
}

/*!
    Set the signal expression associated with this signal property to \a expr.
    Returns the existing signal expression (if any), otherwise 0.

    Ownership of \a expr transfers to QML.  Ownership of the return value is
    assumed by the caller.
*/
QmlExpression *QmlMetaProperty::setSignalExpression(QmlExpression *expr) const
{
    if (!(type() & SignalProperty))
        return 0;

    const QObjectList &children = d->object->children();
    
    for (int ii = 0; ii < children.count(); ++ii) {
        QObject *child = children.at(ii);

        QmlBoundSignal *signal = QmlBoundSignal::cast(child);
        if (signal && signal->index() == coreIndex()) 
            return signal->setExpression(expr);
    }

    if (expr) {
        QmlBoundSignal *signal = new QmlBoundSignal(d->object, d->signal, 
                                                    d->object);
        return signal->setExpression(expr);
    } else {
        return 0;
    }
}

void QmlMetaPropertyPrivate::findSignalInt(QObject *obj, const QString &name)
{
    const QMetaObject *mo = obj->metaObject();

    int methods = mo->methodCount();
    for (int ii = methods - 1; ii >= 0; --ii) {
        QMetaMethod method = mo->method(ii);
        QString methodName = QLatin1String(method.signature());
        int idx = methodName.indexOf(QLatin1Char('('));
        methodName = methodName.left(idx);

        if (methodName == name) {
            signal = method;
            coreIdx = ii;
            return;
        }
    }
}

QObject *QmlMetaPropertyPrivate::attachedObject() const
{
    if (attachedFunc == -1)
        return 0;
    else
        return qmlAttachedPropertiesObjectById(attachedFunc, object);
}

/*!
    Returns the property value.
*/
QVariant QmlMetaProperty::read() const
{
    if (!d->object)
        return QVariant();

    if (type() & SignalProperty) {

        const QObjectList &children = object()->children();

        for (int ii = 0; ii < children.count(); ++ii) {
            QmlBoundSignal *sig = QmlBoundSignal::cast(children.at(ii));
            if (sig && sig->index() == d->coreIdx) 
                return sig->expression()->expression();
        }
    } else if (type() & Property) {
        if (type() & Attached) {
            return QVariant::fromValue(d->attachedObject());
        } else if(type() & ValueTypeProperty) {
            QmlEnginePrivate *ep = d->context?static_cast<QmlEnginePrivate *>(QObjectPrivate::get(d->context->engine())):0;
            QmlValueType *valueType = 0;
            if (ep)
                valueType = ep->valueTypes[d->valueTypeId];
            else
                valueType = QmlValueTypeFactory::valueType(d->valueTypeId);
            Q_ASSERT(valueType);

            valueType->read(object(), d->coreIdx);
            QVariant rv =
                valueType->metaObject()->property(d->valueTypeIdx).read(valueType);
            if (!ep)
                delete valueType;
            return rv;

        } else {
            return d->object->metaObject()->property(d->coreIdx).read(object());
        }
    }
    return QVariant();
}

void QmlMetaPropertyPrivate::writeSignalProperty(const QVariant &value)
{
    QString expr = value.toString();
    const QObjectList &children = object->children();

    for (int ii = 0; ii < children.count(); ++ii) {
        QmlBoundSignal *sig = QmlBoundSignal::cast(children.at(ii));
        if (sig && sig->index() == coreIdx) {
            if (expr.isEmpty()) {
                sig->disconnect();
                sig->deleteLater();
            } else {
                sig->expression()->setExpression(expr);
            }
            return;
        }
    }

    if (!expr.isEmpty()) {
        // XXX scope
        (void *)new QmlBoundSignal(qmlContext(object), expr, object, signal, object);
    }
}

void QmlMetaPropertyPrivate::writeValueProperty(const QVariant &value,
                                                QmlMetaProperty::WriteSource source)
{
    QObject *object = this->object;
    int coreIdx = this->coreIdx;
    
    QmlValueType *writeBack = 0;
    QObject *writeBackObj = 0;
    int writeBackIdx = -1;
    bool deleteWriteBack = false;

    // Remove any existing bindings on this property
    if (source != QmlMetaProperty::Binding) 
        delete q->setBinding(0);

    if (type & QmlMetaProperty::ValueTypeProperty) {
        QmlEnginePrivate *ep = context?static_cast<QmlEnginePrivate *>(QObjectPrivate::get(context->engine())):0;

        if (ep) {
            writeBack = ep->valueTypes[valueTypeId];
        } else {
            writeBack = QmlValueTypeFactory::valueType(valueTypeId);
            deleteWriteBack = true;
        }

        writeBackObj = this->object;
        writeBackIdx = this->coreIdx;
        writeBack->read(writeBackObj, writeBackIdx);
        object = writeBack;
        coreIdx = valueTypeIdx;
    }

    QMetaProperty prop = object->metaObject()->property(coreIdx);

    if (prop.isEnumType()) {
        QVariant v = value;
        if (value.type() == QVariant::Double) {   //enum values come through the script engine as doubles
            double integral;
            double fractional = modf(value.toDouble(), &integral);
            if (qFuzzyCompare(fractional, (double)0.0))
                v.convert(QVariant::Int);
        }
        prop.write(object, v);

        if (writeBack) {
            writeBack->write(writeBackObj, writeBackIdx);
            if (deleteWriteBack) delete writeBack;
        }
        return;
    }

    int t = propertyType();
    int vt = value.userType();
    int category = propertyCategory();

    if (vt == t
        && t != QVariant::Url) { // always resolve relative urls

        void *a[1];
        a[0] = (void *)value.constData();
        QMetaObject::metacall(object, QMetaObject::WriteProperty, coreIdx, a);

    } else if (qMetaTypeId<QVariant>() == t) {

        prop.write(object, value);

    } else if (category == QmlMetaProperty::Object) {

        QObject *o = QmlMetaType::toQObject(value);

        const QMetaObject *valMo = 0;

        if (o) {

            valMo = o->metaObject();
            const QMetaObject *propMo = QmlMetaType::rawMetaObjectForType(t);

            while (valMo) {
                if (valMo == propMo)
                    break;
                valMo = valMo->superClass();
            }

        }

        if (valMo || !o) {

            void *args[] = { &o, 0 };
            QMetaObject::metacall(object, QMetaObject::WriteProperty, coreIdx, 
                                  args);

        }

    } else if (category == QmlMetaProperty::List) {

        int listType = QmlMetaType::listType(t);

        if (value.userType() == qMetaTypeId<QList<QObject *> >()) {
            const QList<QObject *> &list =
                qvariant_cast<QList<QObject *> >(value);
            QVariant listVar = prop.read(object);
            QmlMetaType::clear(listVar);
            for (int ii = 0; ii < list.count(); ++ii) {
                QVariant v = QmlMetaType::fromObject(list.at(ii), listType);
                QmlMetaType::append(listVar, v);
            }

        } else if (vt == listType ||
                  value.userType() == listType) {
            QVariant listVar = prop.read(object);
            QmlMetaType::clear(listVar);
            QmlMetaType::append(listVar, value);
        }
    } else if (category == QmlMetaProperty::QmlList) {

        // XXX - optimize!
        QVariant list = prop.read(object);
        QmlPrivate::ListInterface *li =
            *(QmlPrivate::ListInterface **)list.constData();

        int type = li->type();

        if (QObject *obj = QmlMetaType::toQObject(value)) {
            const QMetaObject *mo =
                QmlMetaType::rawMetaObjectForType(type);

            const QMetaObject *objMo = obj->metaObject();
            bool found = false;
            while(!found && objMo) {
                if (objMo == mo)
                    found = true;
                else
                    objMo = objMo->superClass();
            }

            if (!found) {
                qWarning() << "Unable to assign object to list";
                return;
            }

            // NOTE: This assumes a cast to QObject does not alter
            // the object pointer
            void *d = (void *)&obj;
            li->append(d);
        }
    } else if (category == QmlMetaProperty::Normal) {

        bool found = false;
        switch(t) {
        case QVariant::Double:
            {
                double d;
                if (vt == QVariant::Int) {
                    d = value.toInt();
                    found = true;
                } else if (vt == QVariant::UInt) {
                    d = value.toUInt();
                    found = true;
                } 

                if (found) {
                    void *a[1];
                    a[0] = &d;
                    QMetaObject::metacall(object, 
                                          QMetaObject::WriteProperty,
                                          coreIdx, a);
                }
            }
            break;

        case QVariant::Int:
            {
                int i;
                if (vt == QVariant::Double) {
                    i = (int)value.toDouble();
                    found = true;
                } else if (vt == QVariant::UInt) {
                    i = (int)value.toUInt();
                    found = true;
                } 

                if (found) {
                    void *a[1];
                    a[0] = &i;
                    QMetaObject::metacall(object, 
                                          QMetaObject::WriteProperty,
                                          coreIdx, a);
                }
            }
            break;

        case QVariant::String:
            {
                QString s;
                if (vt == QVariant::ByteArray) {
                    s = QLatin1String(value.toByteArray());
                    found = true;
                } 

                if (found) {
                    void *a[1];
                    a[0] = &s;
                    QMetaObject::metacall(object, 
                                          QMetaObject::WriteProperty,
                                          coreIdx, a);
                }
            }
            break;

        case QVariant::Url:
            {
                QUrl u;
                if (vt == QVariant::Url) {
                    u = value.toUrl();
                    found = true;
                } else if (vt == QVariant::ByteArray) {
                    u = QUrl(QLatin1String(value.toByteArray()));
                    found = true;
                } else if (vt == QVariant::String) {
                    u = QUrl(value.toString());
                    found = true;
                }

                if (found) {
                    if (context && u.isRelative() && !u.isEmpty())
                        u = context->baseUrl().resolved(u);
                    void *a[1];
                    a[0] = &u;
                    QMetaObject::metacall(object, 
                                          QMetaObject::WriteProperty,
                                          coreIdx, a);
                }

            }
            break;


        default:
            {
                if ((uint)t >= QVariant::UserType && vt == QVariant::String) {
                    QmlMetaType::StringConverter con = QmlMetaType::customStringConverter(t);
                    if (con) {
                        QVariant v = con(value.toString());
                        prop.write(object, v);
                        found = true;
                    }
                }
            }
            break;
        }
        if (!found)
            prop.write(object, value);
    }

    if (writeBack) {
        writeBack->write(writeBackObj, writeBackIdx);
        if (deleteWriteBack) delete writeBack;
    }
}

/*!
    Set the property value to \a value.
*/
void QmlMetaProperty::write(const QVariant &value) const
{
    write(value, Other);
}

void QmlMetaProperty::write(const QVariant &value, WriteSource source) const
{
    if (!d->object)
        return;

    if (type() & SignalProperty) {

        d->writeSignalProperty(value);

    } else if (d->coreIdx != -1) {

        d->writeValueProperty(value, source);

    }
}

/*!
    Returns true if the property has a change notifier signal, otherwise false.
*/
bool QmlMetaProperty::hasChangedNotifier() const
{
    if (type() & Property && !(type() & Attached) && d->object) {
        return d->object->metaObject()->property(d->coreIdx).hasNotifySignal();
    }
    return false;
}

/*!
    Returns true if the property needs a change notifier signal for bindings
    to remain upto date, false otherwise.

    Some properties, such as attached properties or those whose value never 
    changes, do not require a change notifier.
*/
bool QmlMetaProperty::needsChangedNotifier() const
{
    return type() & Property && !(type() & Attached) && 
           !property().isConstant();
}

/*!
    Connects the property's change notifier signal to the
    specified \a method of the \a dest object and returns
    true. Returns false if this metaproperty does not
    represent a regular Qt property or if it has no
    change notifier signal, or if the \a dest object does
    not have the specified \a method.
*/
bool QmlMetaProperty::connectNotifier(QObject *dest, int method) const
{
    if (!(type() & Property) || (type() & Attached) || !d->object)
        return false;

    QMetaProperty prop = d->object->metaObject()->property(d->coreIdx);
    if (prop.hasNotifySignal()) {
        return QMetaObject::connect(d->object, prop.notifySignalIndex(), dest, method, Qt::DirectConnection);
    } else {
        return false;
    }
}

/*!
    Connects the property's change notifier signal to the
    specified \a slot of the \a dest object and returns
    true. Returns false if this metaproperty does not
    represent a regular Qt property or if it has no
    change notifier signal, or if the \a dest object does
    not have the specified \a slot.
*/
bool QmlMetaProperty::connectNotifier(QObject *dest, const char *slot) const
{
    if (!(type() & Property) || (type() & Attached) || !d->object)
        return false;

    QMetaProperty prop = d->object->metaObject()->property(d->coreIdx);
    if (prop.hasNotifySignal()) {
        QByteArray signal(QByteArray("2") + prop.notifySignal().signature());
        return QObject::connect(d->object, signal.constData(), dest, slot);
    } else  {
        return false;
    }
}

/*!
    Return the Qt metaobject index of the property.
*/
int QmlMetaProperty::coreIndex() const
{
    return d->coreIdx;
}

Q_GLOBAL_STATIC(QmlValueTypeFactory, qmlValueTypes);

/*!
    Returns the property information serialized into a single integer.  
    QmlMetaProperty uses the bottom 24 bits only.
*/
quint32 QmlMetaProperty::save() const
{
    quint32 rv = 0;
    if (type() & Attached) {
        rv = d->attachedFunc;
    } else if (type() != Invalid) {
        rv = d->coreIdx;
    }

    Q_ASSERT(rv <= 0x7FF);
    Q_ASSERT(type() <= 0x3F);
    Q_ASSERT(d->valueTypeIdx <= 0x7F);

    rv |= (type() << 18);

    if (type() & ValueTypeProperty)
        rv |= (d->valueTypeIdx << 11);

    return rv;
}

quint32 QmlMetaPropertyPrivate::saveValueType(int core, int valueType)
{
    Q_ASSERT(core <= 0x7FF);
    Q_ASSERT(valueType <= 0x7F);
    quint32 rv = 0;
    rv = (QmlMetaProperty::ValueTypeProperty | QmlMetaProperty::Property) << 18;
    rv |= core;
    rv |= valueType << 11;
    return rv;
}

quint32 QmlMetaPropertyPrivate::saveProperty(int core)
{
    Q_ASSERT(core <= 0x7FF);
    quint32 rv = 0;
    rv = (QmlMetaProperty::Property) << 18;
    rv |= core;
    return rv;
}

/*!
  Restore a QmlMetaProperty from a previously saved \a id.
  \a obj must be the same object as used in the previous call
  to QmlMetaProperty::save().  Only the bottom 24-bits are
  used, the high bits can be set to any value.
*/
void QmlMetaProperty::restore(quint32 id, QObject *obj, QmlContext *ctxt)
{
    QmlEnginePrivate *enginePrivate = 0;
    if (ctxt && ctxt->engine())
        enginePrivate = QmlEnginePrivate::get(ctxt->engine());

    d->object = obj;
    d->context = ctxt;

    id &= 0xFFFFFF;
    d->type = id >> 18;
    id &= 0xFFFF;

    if (d->type & Attached) {
        d->attachedFunc = id;
    } else if (d->type & ValueTypeProperty) {
        int coreIdx = id & 0x7FF;
        int valueTypeIdx = id >> 11;

        QMetaProperty p(obj->metaObject()->property(coreIdx));
        Q_ASSERT(p.type() < QVariant::UserType);

        QmlValueType *valueType = qmlValueTypes()->valueTypes[p.type()];

        QMetaProperty p2(valueType->metaObject()->property(valueTypeIdx));

        d->name = QLatin1String(p2.name());
        d->propType = p2.userType();
        d->coreIdx = coreIdx;
        d->valueTypeIdx = valueTypeIdx;
        d->valueTypeId = p.type();

    } else if (d->type & Property) {

        QmlMetaObjectCache *cache = QmlEnginePrivate::cache(enginePrivate, obj);

        d->coreIdx = id;

        if (cache) {
            QmlMetaObjectCache::Data *data = 
                cache->property(id, obj->metaObject());
            d->propType = data->propType;
            d->name = data->name;
        } else {
            QMetaProperty p(obj->metaObject()->property(id));
            d->name = QLatin1String(p.name());
            d->propType = p.userType();
        }

    } else if (d->type & SignalProperty) {
        d->signal = obj->metaObject()->method(id);
        d->coreIdx = id;
    } else {
        *this = QmlMetaProperty();
    }
}

/*!
    Return the QMetaMethod for this property if it is a SignalProperty, 
    otherwise returns an invalid QMetaMethod.
*/
QMetaMethod QmlMetaProperty::method() const
{
    return d->signal;
}

/*!
    \internal

    Creates a QmlMetaProperty for the property \a name of \a obj. Unlike
    the QmlMetaProperty(QObject*, QString) constructor, this static function
    will correctly handle dot properties.
*/
QmlMetaProperty QmlMetaProperty::createProperty(QObject *obj, 
                                                const QString &name)
{
    QStringList path = name.split(QLatin1Char('.'));

    QObject *object = obj;

    for (int jj = 0; jj < path.count() - 1; ++jj) {
        const QString &pathName = path.at(jj);
        QmlMetaProperty prop(object, pathName);

        if (jj == path.count() - 2 && 
            prop.propertyType() < (int)QVariant::UserType &&
            qmlValueTypes()->valueTypes[prop.propertyType()]) {
            // We're now at a value type property
            QObject *typeObject = 
                qmlValueTypes()->valueTypes[prop.propertyType()];
            int idx = typeObject->metaObject()->indexOfProperty(path.last().toUtf8().constData());
            if (idx == -1)
                return QmlMetaProperty();

            QmlMetaProperty p;
            p.d->name = pathName + QLatin1String(".") + path.last();
            p.d->context = 0;
            p.d->coreIdx = prop.coreIndex();
            p.d->valueTypeIdx = idx;
            p.d->valueTypeId = prop.propertyType();
            p.d->type = QmlMetaProperty::ValueTypeProperty | 
                        QmlMetaProperty::Property;
            p.d->object = obj;
            p.d->propType = typeObject->metaObject()->property(idx).userType();
            return p;
        }

        QObject *objVal = QmlMetaType::toQObject(prop.read());
        if (!objVal)
            return QmlMetaProperty();
        object = objVal;
    }

    const QString &propName = path.last();
    QmlMetaProperty prop(object, propName);
    if (!prop.isValid())
        return QmlMetaProperty();
    else
        return prop;
}

QT_END_NAMESPACE