summaryrefslogtreecommitdiffstats
path: root/src/runtime/q3dsbehavior.cpp
blob: e480ac9ee2bbcf34a78725115f4df32633905eaf (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
174
175
176
177
178
179
180
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "q3dsbehavior_p.h"
#include "q3dsutils_p.h"
#include "q3dslogging_p.h"
#include "q3dsabstractxmlparser_p.h"
#include <QFile>

QT_BEGIN_NAMESPACE

class BehaviorMetaDataParser : public Q3DSAbstractXmlParser
{
public:
    BehaviorMetaDataParser(const QString &data);

    bool parse();

    const QMap<QString, Q3DSBehavior::Property> &properties() const { return m_properties; }
    const QMap<QString, Q3DSBehavior::Handler> &handlers() const { return m_handlers; }

private:
    void registerProperty(const QXmlStreamAttributes &attrs);
    void registerHandler(const QXmlStreamAttributes &attrs);

    QString m_data;
    QMap<QString, Q3DSBehavior::Property> m_properties;
    QMap<QString, Q3DSBehavior::Handler> m_handlers;
};

BehaviorMetaDataParser::BehaviorMetaDataParser(const QString &data)
{
    setSourceData(data.toUtf8());
}

bool BehaviorMetaDataParser::parse()
{
    QXmlStreamReader *r = reader();
    if (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("BehaviorMeta")) {
            while (r->readNextStartElement()) {
                if (r->name() == QStringLiteral("Property"))
                    registerProperty(r->attributes());
                else if (r->name() == QStringLiteral("Handler"))
                    registerHandler(r->attributes());
                r->skipCurrentElement();
            }
        } else {
            r->raiseError(QObject::tr("Not a BehaviorMeta document"));
        }
    }

    if (r->hasError()) {
        Q3DSUtils::showMessage(readerErrorString());
        return false;
    }

    return true;
}

void BehaviorMetaDataParser::registerProperty(const QXmlStreamAttributes &attrs)
{
    Q3DSBehavior::Property p;
    p.name = attrs.value(QLatin1String("name")).toString();
    p.formalName = attrs.value(QLatin1String("formalName")).toString();
    Q3DS::convertToPropertyType(attrs.value(QLatin1String("type")), &p.type, nullptr, "property type", reader());
    p.defaultValue = attrs.value(QLatin1String("default")).toString();
    p.publishLevel = attrs.value(QLatin1String("publishLevel")).toString();
    p.description = attrs.value(QLatin1String("description")).toString();
    m_properties.insert(p.name, p);
}

void BehaviorMetaDataParser::registerHandler(const QXmlStreamAttributes &attrs)
{
    Q3DSBehavior::Handler h;
    h.name = attrs.value(QLatin1String("name")).toString();
    h.formalName = attrs.value(QLatin1String("formalName")).toString();
    h.category = attrs.value(QLatin1String("category")).toString();
    h.description = attrs.value(QLatin1String("description")).toString();
    m_handlers.insert(h.name, h);
}

Q3DSBehavior::Q3DSBehavior()
{
}

bool Q3DSBehavior::isNull() const
{
    return m_qmlCode.isEmpty();
}

Q3DSBehavior Q3DSBehaviorParser::parse(const QString &filename, bool *ok)
{
    if (ok)
        *ok = false;

    QFile f(filename);
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
        Q3DSUtils::showMessage(QObject::tr("Failed to open %1").arg(filename));
        return Q3DSBehavior();
    }

    QString contents = QString::fromUtf8(f.readAll());
    if (contents.isEmpty())
        return Q3DSBehavior();

    Q3DSBehavior behavior;
    behavior.m_qmlSourceUrl = QUrl::fromLocalFile(filename);

    // Find the metadata looking like this:
    // /*[[
    //     <Property name="cameraTarget" formalName="Camera Target" type="ObjectRef" default="Scene.Layer.Camera" description="Object in scene the camera should look at" />
    //     <Property name="startImmediately" formalName="Start Immediately?" type="Boolean" default="True" publishLevel="Advanced" description="Start immediately, or wait for the Enable action to be called?" />
    //
    //     <Handler name="start" formalName="Start" category="CameraLookAt" description="Begin looking the target" />
    //     <Handler name="stop" formalName="Stop" category="CameraLookAt" description="Stop looking the target" />
    // ]]*/

    const int metaStartPos = contents.indexOf(QLatin1String("/*[["));
    if (metaStartPos >= 0) {
        const int metaEndPos = contents.indexOf(QLatin1String("]]*/"), metaStartPos);
        if (metaEndPos > metaStartPos) {
            const QString prefix = QLatin1String("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<BehaviorMeta>\n");
            const QString suffix = QLatin1String("</BehaviorMeta>");
            const QString metadata = prefix + contents.mid(metaStartPos + 4, metaEndPos - metaStartPos - 4) + suffix;
            BehaviorMetaDataParser parser(metadata);
            if (parser.parse()) {
                behavior.m_properties = parser.properties();
                behavior.m_handlers = parser.handlers();
            }
        }
    }

    if (contents.indexOf(QLatin1String("\nimport QtQuick 2.")) < 0) {
        // As a workaround for not getting QtQuick imported implicitly (unlike in
        // 3DS1), insert "import QtQuick 2.0" before the first import (which is likely
        // to be QtStudio3D.Behavior)
        const QString quickImport = QLatin1String("import QtQuick 2.0\n");
        const int firstImportIdx = contents.indexOf(QLatin1String("\nimport "));
        if (firstImportIdx >= 0) {
            qCDebug(lcUip, "Inserting import QtQuick 2.0 statement into %s", qPrintable(filename));
            contents.insert(firstImportIdx + 1, quickImport);
        }
    }

    behavior.m_qmlCode = contents;

    if (ok)
        *ok = true;

    return behavior;
}

QT_END_NAMESPACE