1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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     {
VariantRefVariantRef101         VariantRef() : a(0) {}
VariantRefVariantRef102         VariantRef(const VariantRef &r) : a(r.a) { if (a) a->addref(); }
VariantRefVariantRef103         VariantRef(QDeclarativeListModelWorkerAgent *_a) : a(_a) { if (a) a->addref(); }
~VariantRefVariantRef104         ~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 {
SyncSync142         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 
164