summaryrefslogtreecommitdiffstats
path: root/src/core/qpostman.cpp
blob: 6b93353d120207cb6f19977edf54cea560324cb5 (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
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qpostman_p.h"
#include "qpostman_p_p.h"

#include <Qt3DCore/qnode.h>
#include <Qt3DCore/qpropertyupdatedchange.h>

#include <Qt3DCore/private/qlockableobserverinterface_p.h>
#include <Qt3DCore/private/qnode_p.h>
#include <Qt3DCore/private/qpropertyupdatedchangebase_p.h>
#include <Qt3DCore/private/qscene_p.h>
#include <QtCore/private/qobject_p.h>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {

QPostmanPrivate *QPostmanPrivate::get(QPostman *q)
{
    return q->d_func();
}

QPostman::QPostman(QObject *parent)
    : QObject(*new QPostmanPrivate, parent)
{
    qRegisterMetaType<QSceneChangePtr >("QSceneChangePtr");
}

QPostman::~QPostman()
{
}

void QPostman::setScene(QScene *scene)
{
    Q_D(QPostman);
    d->m_scene = scene;
}

static inline QMetaMethod notifyFrontendNodeMethod()
{
    int idx = QPostman::staticMetaObject.indexOfMethod("notifyFrontendNode(QSceneChangePtr)");
    Q_ASSERT(idx != -1);
    return QPostman::staticMetaObject.method(idx);
}

void QPostman::sceneChangeEvent(const QSceneChangePtr &e)
{
    static const QMetaMethod notifyFrontendNode = notifyFrontendNodeMethod();
    notifyFrontendNode.invoke(this, Q_ARG(QSceneChangePtr, e));
}

static inline QMetaMethod submitChangeBatchMethod()
{
    int idx = QPostman::staticMetaObject.indexOfMethod("submitChangeBatch()");
    Q_ASSERT(idx != -1);
    return QPostman::staticMetaObject.method(idx);
}

/*
 * \internal
 * This will start or append \a change to a batch of changes from frontend
 * nodes. Once the batch is complete, when the event loop returns, the batch is
 * sent to the QChangeArbiter to notify the backend aspects.
 */
void QPostman::notifyBackend(const QSceneChangePtr &change)
{
    // If batch in progress
    // add change
    // otherwise start batch
    // by calling a queued slot
    Q_D(QPostman);
    if (d->m_batch.empty()) {
        static const QMetaMethod submitChangeBatch = submitChangeBatchMethod();
        submitChangeBatch.invoke(this, Qt::QueuedConnection);
    }
    d->m_batch.push_back(change);
}

// AspectThread
bool QPostman::shouldNotifyFrontend(const QSceneChangePtr &e)
{
    Q_D(QPostman);
    const QPropertyUpdatedChangePtr propertyChange = qSharedPointerDynamicCast<QPropertyUpdatedChange>(e);
    if (Q_LIKELY(d->m_scene != nullptr) && !propertyChange.isNull()) {
        const QScene::NodePropertyTrackData propertyTrackData
                = d->m_scene->lookupNodePropertyTrackData(e->subjectId());

        const QNode::PropertyTrackingMode trackMode = propertyTrackData.trackedPropertiesOverrides.value(QLatin1String(propertyChange->propertyName()),
                                                                                                      propertyTrackData.defaultTrackMode);

        switch (trackMode) {
        case QNode::TrackAllValues:
            return true;

        case QNode::DontTrackValues:
            return false;

        case QNode::TrackFinalValues: {
            const bool isIntermediate
                = QPropertyUpdatedChangeBasePrivate::get(propertyChange.data())->m_isIntermediate;
            return !isIntermediate;
        }

        default:
            Q_UNREACHABLE();
            return false;
        }
    }
    return true;
}

// Main Thread
void QPostman::notifyFrontendNode(const QSceneChangePtr &e)
{
    Q_D(QPostman);
    if (!e.isNull() && d->m_scene != nullptr) {
        QNode *n = d->m_scene->lookupNode(e->subjectId());
        if (n != nullptr)
            n->sceneChangeEvent(e);
    }
}

void QPostman::submitChangeBatch()
{
    Q_D(QPostman);
    QLockableObserverInterface *arbiter = nullptr;
    if (d->m_scene && (arbiter = d->m_scene->arbiter()) != nullptr) {
        arbiter->sceneChangeEventWithLock(d->m_batch);
        d->m_batch.clear();
    }
}

} //Qt3D

QT_END_NAMESPACE