1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml 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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qqmllistmodelworkeragent_p.h" |
41 | #include "qqmllistmodel_p_p.h" |
42 | #include <private/qqmldata_p.h> |
43 | #include <private/qqmlengine_p.h> |
44 | #include <qqmlinfo.h> |
45 | |
46 | #include <QtCore/qcoreevent.h> |
47 | #include <QtCore/qcoreapplication.h> |
48 | #include <QtCore/qdebug.h> |
49 | |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | QQmlListModelWorkerAgent::Sync::~Sync() |
54 | { |
55 | } |
56 | |
57 | QQmlListModelWorkerAgent::QQmlListModelWorkerAgent(QQmlListModel *model) |
58 | : m_ref(1), m_orig(model), m_copy(new QQmlListModel(model, this)) |
59 | { |
60 | } |
61 | |
62 | QQmlListModelWorkerAgent::~QQmlListModelWorkerAgent() |
63 | { |
64 | mutex.lock(); |
65 | syncDone.wakeAll(); |
66 | mutex.unlock(); |
67 | } |
68 | |
69 | QV4::ExecutionEngine *QQmlListModelWorkerAgent::engine() const |
70 | { |
71 | return m_copy->m_engine; |
72 | } |
73 | |
74 | void QQmlListModelWorkerAgent::setEngine(QV4::ExecutionEngine *eng) |
75 | { |
76 | if (eng != m_copy->m_engine) { |
77 | m_copy->m_engine = eng; |
78 | emit engineChanged(engine: eng); |
79 | } |
80 | } |
81 | |
82 | void QQmlListModelWorkerAgent::addref() |
83 | { |
84 | m_ref.ref(); |
85 | } |
86 | |
87 | void QQmlListModelWorkerAgent::release() |
88 | { |
89 | bool del = !m_ref.deref(); |
90 | |
91 | if (del) |
92 | deleteLater(); |
93 | } |
94 | |
95 | void QQmlListModelWorkerAgent::modelDestroyed() |
96 | { |
97 | m_orig = nullptr; |
98 | } |
99 | |
100 | int QQmlListModelWorkerAgent::count() const |
101 | { |
102 | return m_copy->count(); |
103 | } |
104 | |
105 | void QQmlListModelWorkerAgent::clear() |
106 | { |
107 | m_copy->clear(); |
108 | } |
109 | |
110 | void QQmlListModelWorkerAgent::remove(QQmlV4Function *args) |
111 | { |
112 | m_copy->remove(args); |
113 | } |
114 | |
115 | void QQmlListModelWorkerAgent::append(QQmlV4Function *args) |
116 | { |
117 | m_copy->append(args); |
118 | } |
119 | |
120 | void QQmlListModelWorkerAgent::insert(QQmlV4Function *args) |
121 | { |
122 | m_copy->insert(args); |
123 | } |
124 | |
125 | QJSValue QQmlListModelWorkerAgent::get(int index) const |
126 | { |
127 | return m_copy->get(index); |
128 | } |
129 | |
130 | void QQmlListModelWorkerAgent::set(int index, const QJSValue &value) |
131 | { |
132 | m_copy->set(index, value); |
133 | } |
134 | |
135 | void QQmlListModelWorkerAgent::setProperty(int index, const QString& property, const QVariant& value) |
136 | { |
137 | m_copy->setProperty(index, property, value); |
138 | } |
139 | |
140 | void QQmlListModelWorkerAgent::move(int from, int to, int count) |
141 | { |
142 | m_copy->move(from, to, count); |
143 | } |
144 | |
145 | void QQmlListModelWorkerAgent::sync() |
146 | { |
147 | Sync *s = new Sync(m_copy); |
148 | |
149 | mutex.lock(); |
150 | QCoreApplication::postEvent(receiver: this, event: s); |
151 | syncDone.wait(lockedMutex: &mutex); |
152 | mutex.unlock(); |
153 | } |
154 | |
155 | bool QQmlListModelWorkerAgent::event(QEvent *e) |
156 | { |
157 | if (e->type() == QEvent::User) { |
158 | bool cc = false; |
159 | QMutexLocker locker(&mutex); |
160 | if (m_orig) { |
161 | Sync *s = static_cast<Sync *>(e); |
162 | |
163 | cc = (m_orig->count() != s->list->count()); |
164 | |
165 | Q_ASSERT(m_orig->m_dynamicRoles == s->list->m_dynamicRoles); |
166 | if (m_orig->m_dynamicRoles) |
167 | QQmlListModel::sync(src: s->list, target: m_orig); |
168 | else |
169 | ListModel::sync(src: s->list->m_listModel, target: m_orig->m_listModel); |
170 | } |
171 | |
172 | syncDone.wakeAll(); |
173 | locker.unlock(); |
174 | |
175 | if (cc) |
176 | emit m_orig->countChanged(); |
177 | return true; |
178 | } |
179 | |
180 | return QObject::event(event: e); |
181 | } |
182 | |
183 | QT_END_NAMESPACE |
184 | |
185 | #include "moc_qqmllistmodelworkeragent_p.cpp" |
186 | |