Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / util / qdeclarativelistmodelworkeragent_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVELISTMODELWORKERAGENT_P_H
43 #define QDECLARATIVELISTMODELWORKERAGENT_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qdeclarative.h"
57
58 #include <QtScript/qscriptvalue.h>
59 #include <QtGui/qevent.h>
60 #include <QMutex>
61 #include <QWaitCondition>
62
63 QT_BEGIN_HEADER
64
65 QT_BEGIN_NAMESPACE
66
67 QT_MODULE(Declarative)
68
69 class QDeclarativeListModel;
70 class FlatListScriptClass;
71
72 class QDeclarativeListModelWorkerAgent : public QObject
73 {
74     Q_OBJECT
75     Q_PROPERTY(int count READ count)
76
77 public:
78     QDeclarativeListModelWorkerAgent(QDeclarativeListModel *);
79     ~QDeclarativeListModelWorkerAgent();
80
81     void setScriptEngine(QScriptEngine *eng);
82     QScriptEngine *scriptEngine() const;
83
84     void addref();
85     void release();
86
87     int count() const;
88
89     Q_INVOKABLE void clear();
90     Q_INVOKABLE void remove(int index);
91     Q_INVOKABLE void append(const QScriptValue &);
92     Q_INVOKABLE void insert(int index, const QScriptValue&);
93     Q_INVOKABLE QScriptValue get(int index) const;
94     Q_INVOKABLE void set(int index, const QScriptValue &);
95     Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value);
96     Q_INVOKABLE void move(int from, int to, int count);
97     Q_INVOKABLE void sync();
98
99     struct VariantRef
100     {
101         VariantRef() : a(0) {}
102         VariantRef(const VariantRef &r) : a(r.a) { if (a) a->addref(); }
103         VariantRef(QDeclarativeListModelWorkerAgent *_a) : a(_a) { if (a) a->addref(); }
104         ~VariantRef() { if (a) a->release(); }
105
106         VariantRef &operator=(const VariantRef &o) { 
107             if (o.a) o.a->addref(); 
108             if (a) a->release(); a = o.a; 
109             return *this; 
110         }
111
112         QDeclarativeListModelWorkerAgent *a;
113     };
114 protected:
115     virtual bool event(QEvent *);
116
117 private:
118     friend class QDeclarativeWorkerScriptEnginePrivate;
119     friend class FlatListScriptClass;
120     QScriptEngine *m_engine;
121
122     struct Change {
123         enum { Inserted, Removed, Moved, Changed } type;
124         int index; // Inserted/Removed/Moved/Changed
125         int count; // Inserted/Removed/Moved/Changed
126         int to;    // Moved
127         QList<int> roles;
128     };
129
130     struct Data {
131         QList<Change> changes;
132
133         void clearChange();
134         void insertChange(int index, int count);
135         void removeChange(int index, int count);
136         void moveChange(int index, int count, int to);
137         void changedChange(int index, int count, const QList<int> &roles);
138     };
139     Data data;
140
141     struct Sync : public QEvent {
142         Sync() : QEvent(QEvent::User) {}
143         Data data;
144         QDeclarativeListModel *list;
145     };
146
147     void changedData(int index, int count, const QList<int> &roles);
148
149     QAtomicInt m_ref;
150     QDeclarativeListModel *m_orig;
151     QDeclarativeListModel *m_copy;
152     QMutex mutex;
153     QWaitCondition syncDone;
154 };
155
156 QT_END_NAMESPACE
157
158 Q_DECLARE_METATYPE(QDeclarativeListModelWorkerAgent::VariantRef)
159
160 QT_END_HEADER
161
162 #endif // QDECLARATIVEWORKERSCRIPT_P_H
163