1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qqmllistmodelworkeragent_p.h"
5#include "qqmllistmodel_p_p.h"
6#include <private/qqmldata_p.h>
7#include <private/qqmlengine_p.h>
8#include <qqmlinfo.h>
9
10#include <QtCore/qcoreevent.h>
11#include <QtCore/qcoreapplication.h>
12#include <QtCore/qdebug.h>
13
14
15QT_BEGIN_NAMESPACE
16
17QQmlListModelWorkerAgent::Sync::~Sync()
18{
19}
20
21QQmlListModelWorkerAgent::QQmlListModelWorkerAgent(QQmlListModel *model)
22: m_ref(1), m_orig(model), m_copy(new QQmlListModel(model, this))
23{
24}
25
26QQmlListModelWorkerAgent::~QQmlListModelWorkerAgent()
27{
28 mutex.lock();
29 syncDone.wakeAll();
30 mutex.unlock();
31}
32
33QV4::ExecutionEngine *QQmlListModelWorkerAgent::engine() const
34{
35 return m_copy->m_engine;
36}
37
38void QQmlListModelWorkerAgent::setEngine(QV4::ExecutionEngine *eng)
39{
40 if (eng != m_copy->m_engine) {
41 m_copy->m_engine = eng;
42 emit engineChanged(engine: eng);
43 }
44}
45
46void QQmlListModelWorkerAgent::addref()
47{
48 m_ref.ref();
49}
50
51void QQmlListModelWorkerAgent::release()
52{
53 bool del = !m_ref.deref();
54
55 if (del)
56 deleteLater();
57}
58
59void QQmlListModelWorkerAgent::modelDestroyed()
60{
61 m_orig = nullptr;
62}
63
64int QQmlListModelWorkerAgent::count() const
65{
66 return m_copy->count();
67}
68
69void QQmlListModelWorkerAgent::clear()
70{
71 m_copy->clear();
72}
73
74void QQmlListModelWorkerAgent::remove(QQmlV4Function *args)
75{
76 m_copy->remove(args);
77}
78
79void QQmlListModelWorkerAgent::append(QQmlV4Function *args)
80{
81 m_copy->append(args);
82}
83
84void QQmlListModelWorkerAgent::insert(QQmlV4Function *args)
85{
86 m_copy->insert(args);
87}
88
89QJSValue QQmlListModelWorkerAgent::get(int index) const
90{
91 return m_copy->get(index);
92}
93
94void QQmlListModelWorkerAgent::set(int index, const QJSValue &value)
95{
96 m_copy->set(index, value);
97}
98
99void QQmlListModelWorkerAgent::setProperty(int index, const QString& property, const QVariant& value)
100{
101 m_copy->setProperty(index, property, value);
102}
103
104void QQmlListModelWorkerAgent::move(int from, int to, int count)
105{
106 m_copy->move(from, to, count);
107}
108
109void QQmlListModelWorkerAgent::sync()
110{
111 Sync *s = new Sync(m_copy);
112
113 mutex.lock();
114 QCoreApplication::postEvent(receiver: this, event: s);
115 syncDone.wait(lockedMutex: &mutex);
116 mutex.unlock();
117}
118
119bool QQmlListModelWorkerAgent::event(QEvent *e)
120{
121 if (e->type() == QEvent::User) {
122 bool cc = false;
123 QMutexLocker locker(&mutex);
124 if (m_orig) {
125 Sync *s = static_cast<Sync *>(e);
126
127 cc = (m_orig->count() != s->list->count());
128
129 Q_ASSERT(m_orig->m_dynamicRoles == s->list->m_dynamicRoles);
130 if (m_orig->m_dynamicRoles)
131 QQmlListModel::sync(src: s->list, target: m_orig);
132 else
133 ListModel::sync(src: s->list->m_listModel, target: m_orig->m_listModel);
134 }
135
136 syncDone.wakeAll();
137 locker.unlock();
138
139 if (cc)
140 emit m_orig->countChanged();
141 return true;
142 }
143
144 return QObject::event(event: e);
145}
146
147QT_END_NAMESPACE
148
149#include "moc_qqmllistmodelworkeragent_p.cpp"
150

source code of qtdeclarative/src/qmlmodels/qqmllistmodelworkeragent.cpp