summaryrefslogtreecommitdiffstats
path: root/src/runtime/q3dsuipparser.cpp
blob: ecb79f9082839cef7c3fcd1f102fbc0cd8377807 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/****************************************************************************
**
** Copyright (C) 2017 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 "q3dsuipparser_p.h"
#include "q3dspresentationcommon_p.h"
#include "q3dsutils_p.h"
#include "q3dslogging_p.h"
#include "q3dsenummaps_p.h"
#include <QLoggingCategory>

QT_BEGIN_NAMESPACE

/*
 Basic structure:

<?xml version="1.0" encoding="UTF-8" ?>
<UIP version="3" >
    <Project >
        <ProjectSettings author="" company="" presentationWidth="800" presentationHeight="480"
            presentationRotation="None" maintainAspect="False" />
        <Classes >
            <CustomMaterial id="aluminum" name="aluminum" sourcepath=".\aluminum.material" />
            <Effect id="dof" name="Depth Of Field HQ Blur" sourcepath=".\effects\Depth Of Field HQ Blur.effect" />
            ...
        </Classes>
        <BufferData>
            <ImageBuffer sourcepath="..." hasTransparency="true" />
        </BufferData>
        <Graph>
            <Scene id="Scene">
                <Layer id="layer">
                    <Camera id="Camera" />
                    <Light id="Light" />
                    ...
                    <Model id="Cube">
                        <Material id="Material" />
                    </Model>
                </Layer>
                ... more layers
            </Scene>
        </Graph>
        <Logic>
            <State name="Master" component="#Scene"> master slide
                <Add ref="#Layer />
                <Add ref="#Camera" />
                <Add ref="#Light" />
                <State id=... name=... playmode="..."> normal slides
                    <Add ref="#Cube" name="Cube" rotation="-52.4901 -10.7851 -20.7213" scale="2 2 2" sourcepath="#Cube">
                      ...
                    </Add>
                    <Add ref="#Material" />
                </State>
                ... more States (slides)
            </State>
        </Logic>
    </Project>
</UIP>
 */

// Returns a Presentation and does not maintain ownership
Q3DSUipPresentation *Q3DSUipParser::parse(const QString &filename,
                                          const QString &presentationName,
                                          const QHash<QStringRef,
                                              QVector<QStringRef>> &variantMap)
{
    if (!setSource(filename))
        return nullptr;

    m_variantFilter = variantMap;
    return createPresentation(presentationName);
}

Q3DSUipPresentation *Q3DSUipParser::parseData(const QByteArray &data,
                                              const QString &presentationName,
                                              const QHash<QStringRef,
                                                  QVector<QStringRef>> &variantMap)
{
    if (!setSourceData(data))
        return nullptr;

    m_variantFilter = variantMap;
    return createPresentation(presentationName);
}

Q3DSUipPresentation *Q3DSUipParser::createPresentation(const QString &presentationName)
{
    // reset (not owned by Q3DSUipParser)
    m_presentation.reset(new Q3DSUipPresentation);

    m_presentation->setSourceFile(sourceInfo()->absoluteFilePath());
    m_presentation->setName(presentationName.isEmpty() ? QLatin1String("main") : presentationName);

    QXmlStreamReader *r = reader();
    if (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("UIP")
                && (r->attributes().value(QLatin1String("version")) == QStringLiteral("5")
                    || r->attributes().value(QLatin1String("version")) == QStringLiteral("4")
                    || r->attributes().value(QLatin1String("version")) == QStringLiteral("3"))) {
            parseUIP();
        } else {
            r->raiseError(QObject::tr("UIP version is too low, and is no longer supported."));
        }
    }

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

    resolveReferences(m_presentation->scene());
    resolveReferences(m_presentation->masterSlide());

    m_presentation->resolveAliases();
    m_presentation->updateObjectStateForSubTrees();
    m_presentation->addImplicitPropertyChanges();

    qint64 loadTime = elapsedSinceSetSource();
    qCDebug(lcPerf, "Presentation %s loaded in %lld ms", qPrintable(m_presentation->sourceFile()), loadTime);
    m_presentation->setLoadTime(loadTime);

    return m_presentation.take();
}

void Q3DSUipParser::parseUIP()
{
    QXmlStreamReader *r = reader();
    int projectCount = 0;
    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("Project")) {
            ++projectCount;
            if (projectCount == 1)
                parseProject();
            else
                r->raiseError(QObject::tr("Multiple Project elements found."));
        } else {
            r->skipCurrentElement();
        }
    }
}

void Q3DSUipParser::parseProject()
{
    QXmlStreamReader *r = reader();
    bool foundGraph = false;
    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("ProjectSettings"))
            parseProjectSettings();
        else if (r->name() == QStringLiteral("Classes"))
            parseClasses();
        else if (r->name() == QStringLiteral("BufferData"))
            parseBufferData();
        else if (r->name() == QStringLiteral("Graph")) {
            if (!foundGraph) {
                foundGraph = true;
                parseGraph();
            } else {
                r->raiseError(QObject::tr("Multiple Graph elements found."));
            }
        } else if (r->name() == QStringLiteral("Logic")) {
            if (foundGraph)
                parseLogic();
            else
                r->raiseError(QObject::tr("Encountered Logic element before Graph."));
        } else {
            r->skipCurrentElement();
        }
    }
}

void Q3DSUipParser::parseProjectSettings()
{
    QXmlStreamReader *r = reader();
    for (const QXmlStreamAttribute &attr : r->attributes()) {
        if (attr.name() == QStringLiteral("author")) {
            m_presentation->setAuthor(attr.value().toString());
        } else if (attr.name() == QStringLiteral("company")) {
            m_presentation->setCompany(attr.value().toString());
        } else if (attr.name() == QStringLiteral("presentationWidth")) {
            int w;
            if (Q3DS::convertToInt(attr.value(), &w, "presentation width", r))
                m_presentation->setPresentationWidth(w);
        } else if (attr.name() == QStringLiteral("presentationHeight")) {
            int h;
            if (Q3DS::convertToInt(attr.value(), &h, "presentation height", r))
                m_presentation->setPresentationHeight(h);
        } else if (attr.name() == QStringLiteral("presentationRotation")) {
            Q3DSUipPresentation::Rotation v;
            if (Q3DSEnumMap::enumFromStr(attr.value(), &v))
                m_presentation->setPresentationRotation(v);
            else
                r->raiseError(QObject::tr("Invalid presentation rotation \"%1\"").arg(attr.value().toString()));
        } else if (attr.name() == QStringLiteral("maintainAspect")) {
            bool v;
            if (Q3DS::convertToBool(attr.value(), &v, "maintainAspect value", r))
                m_presentation->setMaintainAspectRatio(v);
        } else if (attr.name() == QStringLiteral("preferKtx")) {
            bool v;
            if (Q3DS::convertToBool(attr.value(), &v, "preferKtx value", r))
                m_presentation->setPreferKtx(v);
        }
    }
    r->skipCurrentElement();
}

void Q3DSUipParser::parseClasses()
{
    QXmlStreamReader *r = reader();
    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("CustomMaterial")) {
            parseExternalFileRef(std::bind(&Q3DSUipPresentation::loadCustomMaterial, m_presentation.data(),
                                           std::placeholders::_1, std::placeholders::_2));
        } else if (r->name() == QStringLiteral("Effect")) {
            parseExternalFileRef(std::bind(&Q3DSUipPresentation::loadEffect, m_presentation.data(),
                                           std::placeholders::_1, std::placeholders::_2));
        } else if (r->name() == QStringLiteral("Behavior")) {
            parseExternalFileRef(std::bind(&Q3DSUipPresentation::loadBehavior, m_presentation.data(),
                                           std::placeholders::_1, std::placeholders::_2));
        } else if (r->name() == QStringLiteral("RenderPlugin")) {
            r->raiseError(QObject::tr("RenderPlugin not supported"));
        } else {
            r->skipCurrentElement();
        }
    }
}

void Q3DSUipParser::parseExternalFileRef(ExternalFileLoadCallback callback)
{
    QXmlStreamReader *r = reader();
    auto a = r->attributes();

    const QStringRef id = a.value(QStringLiteral("id"));
    const QStringRef sourcePath = a.value(QStringLiteral("sourcepath"));

    // custommaterial/effect/behavior all expect ids to be prefixed with #
    const QByteArray decoratedId = QByteArrayLiteral("#") + id.toUtf8();
    const QString src = m_presentation->assetFileName(sourcePath.toString(), nullptr);
    if (!callback(decoratedId, src))
        r->raiseError(QObject::tr("Failed to load external file %1").arg(src));

    r->skipCurrentElement();
}

void Q3DSUipParser::parseBufferData()
{
    QXmlStreamReader *r = reader();
    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("ImageBuffer"))
            parseImageBuffer();
        else
            r->skipCurrentElement();
    }
}

void Q3DSUipParser::parseImageBuffer()
{
    QXmlStreamReader *r = reader();
    auto a = r->attributes();
    const QStringRef &sourcePath = a.value(QStringLiteral("sourcepath"));
    const QStringRef &hasTransparency = a.value(QStringLiteral("hasTransparency"));

    if (!sourcePath.isEmpty() && !hasTransparency.isEmpty())
        m_presentation->registerImageBuffer(sourcePath.toString(), hasTransparency.compare(QStringLiteral("True")) == 0);

    r->skipCurrentElement();
}

void Q3DSUipParser::parseGraph()
{
    QXmlStreamReader *r = reader();
    int sceneCount = 0;
    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("Scene")) {
            ++sceneCount;
            if (sceneCount == 1)
                parseScene();
            else
                r->raiseError(QObject::tr("Multiple Scene elements found."));
        } else {
            r->skipCurrentElement();
        }
    }
}

void Q3DSUipParser::parseScene()
{
    QXmlStreamReader *r = reader();
    QByteArray id = getId(r->name());
    if (id.isEmpty())
        return;

    auto scene = new Q3DSScene;
    scene->setProperties(r->attributes(), Q3DSGraphObject::PropSetDefaults);
    scene->addDataInputControlledProperties(getDataInputControlledProperties());
    m_presentation->registerObject(id, scene);
    m_presentation->setScene(scene);

    // Parse elements below Layer element
    while (r->readNextStartElement())
        parseObjects(scene);
}

void Q3DSUipParser::parseObjects(Q3DSGraphObject *parent)
{
    QXmlStreamReader *r = reader();
    Q_ASSERT(parent);
    QByteArray id = getId(r->name());
    if (id.isEmpty())
        return;

    Q3DSGraphObject *obj = nullptr;

    if (isIncludedInVariant(r->attributes())) {
        if (r->name() == QStringLiteral("Layer"))
            obj = new Q3DSLayerNode;
        else if (r->name() == QStringLiteral("Camera"))
            obj = new Q3DSCameraNode;
        else if (r->name() == QStringLiteral("Light"))
            obj = new Q3DSLightNode;
        else if (r->name() == QStringLiteral("Model"))
            obj = new Q3DSModelNode;
        else if (r->name() == QStringLiteral("Group"))
            obj = new Q3DSGroupNode;
        else if (r->name() == QStringLiteral("Component"))
            obj = new Q3DSComponentNode;
        else if (r->name() == QStringLiteral("Text"))
            obj = new Q3DSTextNode;
        else if (r->name() == QStringLiteral("Material"))
            obj = new Q3DSDefaultMaterial;
        else if (r->name() == QStringLiteral("ReferencedMaterial"))
            obj = new Q3DSReferencedMaterial;
        else if (r->name() == QStringLiteral("CustomMaterial"))
            obj = new Q3DSCustomMaterialInstance;
        else if (r->name() == QStringLiteral("Effect"))
            obj = new Q3DSEffectInstance;
        else if (r->name() == QStringLiteral("Behavior"))
            obj = new Q3DSBehaviorInstance;
        else if (r->name() == QStringLiteral("Image"))
            obj = new Q3DSImage;
        else if (r->name() == QStringLiteral("Alias"))
            obj = new Q3DSAliasNode;

        // Skip unknown elements in the XML
        if (!obj) {
            r->skipCurrentElement();
            return;
        }
    } else {
        // Element failed variant filtering, skip it and it's children
        ignoreCurrentElement();
        return;
    }

    obj->setProperties(r->attributes(), Q3DSGraphObject::PropSetDefaults);
    obj->addDataInputControlledProperties(getDataInputControlledProperties());
    m_presentation->registerObject(id, obj);
    parent->appendChildNode(obj);

    while (r->readNextStartElement())
        parseObjects(obj);
}

void Q3DSUipParser::ignoreCurrentElement()
{
    QXmlStreamReader *r = reader();
    QByteArray id = getId(r->name());
    if (!id.isEmpty()) {
        m_ignoredNodes[id] = true;

        // Recursively ignore children
        while (r->readNextStartElement())
            ignoreCurrentElement();
    }
}

bool Q3DSUipParser::isIgnoredVariant(const QByteArray &id)
{
    return !m_ignoredNodes.isEmpty() && m_ignoredNodes.contains(id);
}

void Q3DSUipParser::parseLogic()
{
    QXmlStreamReader *r = reader();
    int masterCount = 0;
    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("State")) {
            QStringRef compRef = r->attributes().value(QLatin1String("component"));
            if (!compRef.startsWith('#')) {
                r->raiseError(QObject::tr("Invalid ref '%1' in State").arg(compRef.toString()));
                return;
            }

            QByteArray refId = compRef.mid(1).toUtf8();
            Q3DSGraphObject *slideTarget = m_presentation->object(refId);
            if (!slideTarget) {
                r->raiseError(
                            QObject::tr(
                                "State references unknown object '%1'").arg(
                                compRef.mid(1).toString()));
                return;
            }
            const QByteArray idPrefix = compRef.mid(1).toUtf8();
            if (slideTarget->type() == Q3DSGraphObject::Scene) {
                if (++masterCount == 1) {
                    auto masterSlide = parseSlide(nullptr, idPrefix);
                    Q_ASSERT(masterSlide);
                    m_presentation->setMasterSlide(masterSlide);
                } else {
                    r->raiseError(QObject::tr("Multiple State (master slide) elements found."));
                }
            } else {
                Q3DSSlide *componentMasterSlide = parseSlide(nullptr, idPrefix);
                Q_ASSERT(componentMasterSlide);
                static_cast<Q3DSComponentNode *>(slideTarget)->m_masterSlide = componentMasterSlide; // transfer ownership
            }
        } else {
            r->raiseError(QObject::tr("Logic can only have State children."));
        }
    }
}

Q3DSSlide *Q3DSUipParser::parseSlide(Q3DSSlide *parent, const QByteArray &idPrefix)
{
    QXmlStreamReader *r = reader();
    QByteArray id = getId(r->name(), false);
    const bool isMaster = !parent;
    if (isMaster) {
        // The master slide may not have an id.
        if (id.isEmpty())
            id = idPrefix + QByteArrayLiteral("-Master");
    }
    if (id.isEmpty())
        return nullptr;

    Q3DSSlide *slide = new Q3DSSlide;
    slide->setProperties(r->attributes(), Q3DSGraphObject::PropSetDefaults);
    slide->addDataInputControlledProperties(getDataInputControlledProperties());
    m_presentation->registerObject(id, slide);
    if (parent)
        parent->appendChildNode(slide);

    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("State")) {
            if (isMaster)
                parseSlide(slide);
            else
                r->raiseError(QObject::tr("Encountered sub-slide in sub-slide."));
        } else if (r->name() == QStringLiteral("Add")) {
            parseAddSet(slide, false, isMaster);
        } else if (r->name() == QStringLiteral("Set")) {
            parseAddSet(slide, true, isMaster);
        } else {
            r->skipCurrentElement();
        }
    }

    return slide;
}

bool Q3DSUipParser::isIncludedInVariant(const QXmlStreamAttributes &attrs) const
{
    QStringRef variantAttributes = attrs.value(QLatin1String("variants"));

    // Variant filter is ignored when it's not defined
    // or if the object has no variant attributes
    if (m_variantFilter.isEmpty() || variantAttributes.isEmpty())
        return true;

    // Collect all variant tags per group from the element
    QHash<QStringRef, QVector<QStringRef>> groupToVariants;
    const QVector<QStringRef> variantTags = variantAttributes.split(
                QLatin1Char(','),
                QString::SkipEmptyParts);

    for (auto tag : variantTags) {
        // Break each variantGroup:value to group and value strings
        int groupSeparatorIdx = tag.indexOf(QLatin1Char(':'));
        QStringRef group = tag.left(groupSeparatorIdx);

        // Only collect variant tags that are relevant to variant filtering
        if (m_variantFilter.contains(group)) {
            QStringRef variant = tag.mid(groupSeparatorIdx + 1);
            groupToVariants[group].append(variant);
        }
    }

    // If no relevant variant tags found in element, load the element
    if (groupToVariants.isEmpty())
        return true;

    // Verify that the element matches the variant filtering per group.
    // To match the element must either:
    // - Have no variant tag defined for the group
    // - Have a matching tag for the group
    bool isLoaded = true;
    const auto filteredGroups = m_variantFilter.keys();
    for (auto group : filteredGroups) {
        const QVector<QStringRef> variants = groupToVariants[group];
        const QVector<QStringRef> filteredVariants = m_variantFilter[group];

        if (variants.size() > 0) {
            // Check if ANY of the variant values of the element matches ANY
            // of the included variants in the filter for this variant group
            bool matchesFilter = false;
            for (auto variant : variants)
                matchesFilter |= filteredVariants.contains(variant);

            isLoaded &= matchesFilter;
        }
    }

    return isLoaded;
}

void Q3DSUipParser::parseAddSet(Q3DSSlide *slide, bool isSet, bool isMaster)
{
    QXmlStreamReader *r = reader();
    QStringRef ref = r->attributes().value(QLatin1String("ref"));
    if (!ref.startsWith('#')) {
        r->raiseError(QObject::tr("Invalid ref '%1' in Add/Set").arg(ref.toString()));
        return;
    }

    QByteArray refId = ref.mid(1).toUtf8();
    Q3DSGraphObject *obj = m_presentation->object(refId);
    if (!obj) {
        // Raise error only if the slide target
        // wasn't ignored due to variant mechanism
        if (isIgnoredVariant(refId)) {
            r->skipCurrentElement();
            return;
        } else {
            r->raiseError(
                        QObject::tr("Add/Set references unknown object '%1'").arg(
                            ref.mid(1).toString()));
            return;
        }
    }

    if (!isSet) {
        // Add: register the object for this slide
        slide->addObject(obj);
        // and set the properties on the object right away.
        Q3DSGraphObject::PropSetFlags flags = 0;
        if (isMaster)
            flags |= Q3DSGraphObject::PropSetOnMaster;
        obj->setProperties(r->attributes(), flags);
    } else {
        // Set: store the property changes
        QScopedPointer<Q3DSPropertyChangeList> changeList(new Q3DSPropertyChangeList);
        for (const QXmlStreamAttribute &attr : r->attributes()) {
            if (attr.name() == QStringLiteral("ref"))
                continue;
            if (attr.name() == QStringLiteral("sourcepath")) {
                QString absoluteFileName =
                        m_presentation->assetFileName(attr.value().toString(), nullptr);
                changeList->append(Q3DSPropertyChange(attr.name().toString(), absoluteFileName));
            } else {
                changeList->append(Q3DSPropertyChange(attr.name().toString(), attr.value().toString()));
            }
        }
        if (!changeList->isEmpty())
            slide->addPropertyChanges(obj, changeList.take());
    }

    // controlledproperty attributes may be present in the Logic section as well.
    obj->addDataInputControlledProperties(getDataInputControlledProperties());

    // Store animations and actions.
    while (r->readNextStartElement()) {
        if (r->name() == QStringLiteral("AnimationTrack")) {
            Q3DSAnimationTrack animTrack;
            animTrack.m_target = obj;
            for (const QXmlStreamAttribute &attr : r->attributes()) {
                if (attr.name() == QStringLiteral("property")) {
                    animTrack.m_property = attr.value().toString().trimmed();
                } else if (attr.name() == QStringLiteral("type")) {
                    if (!Q3DSEnumMap::enumFromStr(attr.value(), &animTrack.m_type))
                        r->raiseError(QObject::tr("Unknown animation type %1").arg(attr.value().toString()));
                } else if (attr.name() == QStringLiteral("dynamic")) {
                    Q3DS::convertToBool(attr.value(), &animTrack.m_dynamic, "'dynamic' attribute value", r);
                }
            }
            parseAnimationKeyFrames(r->readElementText(QXmlStreamReader::SkipChildElements).trimmed(), &animTrack);
            if (!animTrack.m_keyFrames.isEmpty())
                slide->addAnimation(animTrack);
        } else if (r->name() == QStringLiteral("Action")) {
            Q3DSAction action;
            action.owner = obj;
            action.id = getId(QStringRef(), false);
            // we only support simple, static action definitions; no changes afterwards
            if (!action.id.isEmpty()) {
                for (const QXmlStreamAttribute &attr : r->attributes()) {
                    if (attr.name() == QStringLiteral("eyeball")) {
                        Q3DS::convertToBool(attr.value(), &action.eyeball, "'eyeball' attribute value", r);
                    } else if (attr.name() == QStringLiteral("triggerObject")) {
                        action.triggerObject_unresolved = attr.value().trimmed().toString();
                    } else if (attr.name() == QStringLiteral("event")) {
                        action.event = attr.value().trimmed().toString();
                    } else if (attr.name() == QStringLiteral("targetObject")) {
                        action.targetObject_unresolved = attr.value().trimmed().toString();
                    } else if (attr.name() == QStringLiteral("handler")) {
                        if (!Q3DSEnumMap::enumFromStr(attr.value(), &action.handler)) {
                            action.handler = Q3DSAction::BehaviorHandler;
                            action.behaviorHandler = attr.value().trimmed().toString();
                        }
                    }
                }
                // Parse the HandlerArgument children.
                while (r->readNextStartElement()) {
                    if (r->name() == QStringLiteral("HandlerArgument")) {
                        Q3DSAction::HandlerArgument ha;
                        for (const QXmlStreamAttribute &attr : r->attributes()) {
                            if (attr.name() == QStringLiteral("name")) {
                                ha.name = attr.value().trimmed().toString();
                            } else if (attr.name() == QStringLiteral("type")) {
                                if (!Q3DS::convertToPropertyType(attr.value(), &ha.type, nullptr, "handler type", r))
                                    r->raiseError(QObject::tr("Invalid action handler type %1").arg(attr.value().toString()));
                            } else if (attr.name() == QStringLiteral("argtype")) {
                                if (!Q3DSEnumMap::enumFromStr(attr.value(), &ha.argType))
                                    r->raiseError(QObject::tr("Invalid action handler argtype %1").arg(attr.value().toString()));
                            } else if (attr.name() == QStringLiteral("value")) {
                                ha.value = attr.value().trimmed().toString();
                            }
                        }
                        if (ha.value.isEmpty() && ha.type == Q3DS::Vector) {
                            // With all-zero vector values (0,0,0) Studio doesn't currently
                            // store "value" parameter at all into uip. So empty vector
                            // type values are handled as "0 0 0", see QT3DS-3143.
                            ha.value = QStringLiteral("0 0 0");
                        }
                        action.handlerArgs.append(ha);
                    }
                    r->skipCurrentElement();
                }
                slide->addAction(action);
            } else {
                r->skipCurrentElement();
            }
        } else {
            r->skipCurrentElement();
        }
    }
}

void Q3DSUipParser::parseAnimationKeyFrames(const QString &data, Q3DSAnimationTrack *animTrack)
{
    QXmlStreamReader *r = reader();
    QString spaceOnlyData = data;
    spaceOnlyData.replace('\n', ' ');
    const QStringList values = spaceOnlyData.split(' ', QString::SkipEmptyParts);
    if (values.isEmpty() || values.first().isEmpty())
        return;

    switch (animTrack->type()) {
    case Q3DSAnimationTrack::Linear:
        if (!(values.count() % 2)) {
            for (int i = 0; i < values.count() / 2; ++i) {
                Q3DSAnimationTrack::KeyFrame kf;
                if (!Q3DS::convertToFloat(&values[i * 2], &kf.time, "keyframe time", r))
                    continue;
                if (!Q3DS::convertToFloat(&values[i * 2 + 1], &kf.value, "keyframe value", r))
                    continue;
                animTrack->m_keyFrames.append(kf);
            }
        } else {
            r->raiseError(QObject::tr("Invalid Linear animation track: %1").arg(spaceOnlyData));
            return;
        }
        break;
    case Q3DSAnimationTrack::EaseInOut:
        if (!(values.count() % 4)) {
            for (int i = 0; i < values.count() / 4; ++i) {
                Q3DSAnimationTrack::KeyFrame kf;
                if (!Q3DS::convertToFloat(&values[i * 4], &kf.time, "keyframe time", r))
                    continue;
                if (!Q3DS::convertToFloat(&values[i * 4 + 1], &kf.value, "keyframe value", r))
                    continue;
                if (!Q3DS::convertToFloat(&values[i * 4 + 2], &kf.easeIn, "keyframe EaseIn", r))
                    continue;
                if (!Q3DS::convertToFloat(&values[i * 4 + 3], &kf.easeOut, "keyframe EaseOut", r))
                    continue;
                animTrack->m_keyFrames.append(kf);
            }
        } else {
            r->raiseError(QObject::tr("Invalid EaseInOut animation track: %1").arg(spaceOnlyData));
            return;
        }
        break;
    case Q3DSAnimationTrack::Bezier:
        if (!(values.count() % 6)) {
            for (int i = 0; i < values.count() / 6; ++i) {
                Q3DSAnimationTrack::KeyFrame kf;
                if (!Q3DS::convertToFloat(&values[i * 6], &kf.time, "keyframe time", r))
                    continue;
                kf.time *= 1000.0f;
                if (!Q3DS::convertToFloat(&values[i * 6 + 1], &kf.value, "keyframe value", r))
                    continue;
                if (i < values.count() / 6 - 1) {
                    if (!Q3DS::convertToFloat(&values[i * 6 + 2], &kf.c2time, "keyframe C2 time", r))
                        continue;
                    kf.c2time *= 1000.0f;
                    if (!Q3DS::convertToFloat(&values[i * 6 + 3], &kf.c2value, "keyframe C2 value", r))
                        continue;
                } else { // last keyframe
                    kf.c2time = kf.c2value = 0.0f;
                }
                if (!Q3DS::convertToFloat(&values[i * 6 + 4], &kf.c1time, "keyframe C1 time", r))
                    continue;
                kf.c1time *= 1000.0f;
                if (!Q3DS::convertToFloat(&values[i * 6 + 5], &kf.c1value, "keyframe C1 value", r))
                    continue;
                animTrack->m_keyFrames.append(kf);
            }
        } else {
            r->raiseError(QObject::tr("Invalid Bezier animation track: %1").arg(spaceOnlyData));
            return;
        }
        break;
    default:
        break;
    }
}

QByteArray Q3DSUipParser::getId(const QStringRef &desc, bool required)
{
    QByteArray id = reader()->attributes().value(QLatin1String("id")).toUtf8();
    if (id.isEmpty() && required)
        reader()->raiseError(QObject::tr("Missing %1 id.").arg(desc.toString()));
    return id;
}

Q3DSGraphObject::DataInputControlledProperties Q3DSUipParser::getDataInputControlledProperties()
{
    Q3DSGraphObject::DataInputControlledProperties dataInputControlledProperties;
    const QStringRef cp = reader()->attributes().value(QLatin1String("controlledproperty"));
    if (!cp.isEmpty()) {
        QVector<QStringRef> nameTargetPairs = cp.trimmed().split(' ', QString::SkipEmptyParts);
        for (int i = 0; i < nameTargetPairs.count(); i += 2) {
            if (i + 1 == nameTargetPairs.count()) {
                reader()->raiseError(QObject::tr("Malformed controlledproperty attribute: %1").arg(cp.toString()));
                return dataInputControlledProperties;
            }
            // remove prefix '$' from datainput controller name
            QString controller = nameTargetPairs[i].toString();
            if (controller.at(0) == QLatin1Char('$'))
                controller.remove(0, 1);

            dataInputControlledProperties.insert(controller, nameTargetPairs[i + 1].toString());
        }
    }
    return dataInputControlledProperties;
}

void Q3DSUipParser::resolveReferences(Q3DSGraphObject *obj)
{
    while (obj) {
        obj->resolveReferences(*m_presentation);
        m_presentation->registerDataInputTarget(obj);

        resolveReferences(obj->firstChild());
        obj = obj->nextSibling();
    }
}

QT_END_NAMESPACE