summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Palettes/Project/ProjectFileSystemModel.cpp
blob: 832f4ddc7ea619891f6b078a10a18d47a814d15a (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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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 "qtAuthoring-config.h"
#include <QtCore/qset.h>
#include <QtCore/qtimer.h>

#include "PresentationFile.h"
#include "Qt3DSCommonPrecompile.h"
#include "ProjectFileSystemModel.h"
#include "StudioUtils.h"
#include "StudioApp.h"
#include "ClientDataModelBridge.h"
#include "Core.h"
#include "Doc.h"
#include "Qt3DSFileTools.h"
#include "ImportUtils.h"
#include "Dialogs.h"
#include "Qt3DSDMStudioSystem.h"
#include "Qt3DSImportTranslation.h"
#include "Qt3DSMessageBox.h"
#include "IDocumentEditor.h"
#include "IDragable.h"
#include "IObjectReferenceHelper.h"
#include "IDirectoryWatchingSystem.h"

ProjectFileSystemModel::ProjectFileSystemModel(QObject *parent) : QAbstractListModel(parent)
    , m_model(new QFileSystemModel(this))
{
    connect(m_model, &QAbstractItemModel::rowsInserted, this, &ProjectFileSystemModel::modelRowsInserted);
    connect(m_model, &QAbstractItemModel::rowsAboutToBeRemoved, this, &ProjectFileSystemModel::modelRowsRemoved);
    connect(m_model, &QAbstractItemModel::layoutChanged, this, &ProjectFileSystemModel::modelLayoutChanged);
    connect(&g_StudioApp.GetCore()->getProjectFile(), &ProjectFile::presentationIdChanged,
            this, &ProjectFileSystemModel::handlePresentationIdChange);
    connect(&g_StudioApp.GetCore()->getProjectFile(), &ProjectFile::assetNameChanged,
            this, &ProjectFileSystemModel::asyncUpdateReferences);

    m_projectReferencesUpdateTimer.setSingleShot(true);
    m_projectReferencesUpdateTimer.setInterval(0);

    connect(&m_projectReferencesUpdateTimer, &QTimer::timeout,
            this, &ProjectFileSystemModel::updateProjectReferences);
}

QHash<int, QByteArray> ProjectFileSystemModel::roleNames() const
{
    auto modelRoleNames = m_model->roleNames();
    modelRoleNames.insert(IsExpandableRole, "_isExpandable");
    modelRoleNames.insert(IsDraggableRole, "_isDraggable");
    modelRoleNames.insert(IsReferencedRole, "_isReferenced");
    modelRoleNames.insert(IsProjectReferencedRole, "_isProjectReferenced");
    modelRoleNames.insert(DepthRole, "_depth");
    modelRoleNames.insert(ExpandedRole, "_expanded");
    modelRoleNames.insert(FileIdRole, "_fileId");
    modelRoleNames.insert(ExtraIconRole, "_extraIcon");
    return modelRoleNames;
}

int ProjectFileSystemModel::rowCount(const QModelIndex &) const
{
    return m_items.count();
}

QVariant ProjectFileSystemModel::data(const QModelIndex &index, int role) const
{
    const auto &item = m_items.at(index.row());

    switch (role) {
    case Qt::DecorationRole: {
        QString path = item.index.data(QFileSystemModel::FilePathRole).toString();
        return StudioUtils::resourceImageUrl() + getIconName(path);
    }

    case IsExpandableRole: {
        if (item.index == m_rootIndex) {
            return false;
        } else {
            return hasVisibleChildren(item.index);
        }
    }

    case IsDraggableRole:
        return QFileInfo(item.index.data(QFileSystemModel::FilePathRole).toString()).isFile();

    case IsReferencedRole: {
        const QString path = item.index.data(QFileSystemModel::FilePathRole).toString();
        return m_references.contains(path);
    }

    case IsProjectReferencedRole: {
        const QString path = item.index.data(QFileSystemModel::FilePathRole).toString();
        return m_projectReferences.contains(path);
    }

    case DepthRole:
        return item.depth;

    case ExpandedRole:
        return item.expanded;

    case FileIdRole: {
        const QString path = item.index.data(QFileSystemModel::FilePathRole).toString();
        EStudioObjectType iconType = getIconType(path);
        if (iconType == OBJTYPE_PRESENTATION || iconType == OBJTYPE_QML_STREAM)
            return presentationId(path);
        else
            return {};
    }

    case ExtraIconRole: {
        const QString path = item.index.data(QFileSystemModel::FilePathRole).toString();
        EStudioObjectType iconType = getIconType(path);
        if (iconType == OBJTYPE_PRESENTATION || iconType == OBJTYPE_QML_STREAM) {
            if (presentationId(path).isEmpty())
                return QStringLiteral("warning.png");
            else
                return {};
        } else {
            return {};
        }
    }

    default:
        return m_model->data(item.index, role);
    }
}

QMimeData *ProjectFileSystemModel::mimeData(const QModelIndexList &indexes) const
{
    const QString path = filePath(indexes.first().row()); // can only drag one item
    return CDropSourceFactory::Create(QT3DS_FLAVOR_ASSET_UICFILE, path);
}

QString ProjectFileSystemModel::filePath(int row) const
{
    if (row < 0 || row >= m_items.size())
        return QString();
    const auto &item = m_items.at(row);
    return item.index.data(QFileSystemModel::FilePathRole).toString();
}

bool ProjectFileSystemModel::isRefreshable(int row) const
{
    const QString path = filePath(row);
    // Import needs to be refreshable even if it is not referenced, as user may drag just individual
    // meshes into the scene, and not the whole import.
    return path.endsWith(QLatin1String(".import"));
}

void ProjectFileSystemModel::updateReferences()
{
    m_references.clear();
    const auto doc = g_StudioApp.GetCore()->GetDoc();
    const auto bridge = doc->GetStudioSystem()->GetClientDataModelBridge();
    const auto sourcePathList = bridge->GetSourcePathList();
    const auto fontFileList = bridge->GetFontFileList();
    const auto effectTextureList = bridge->GetDynamicObjectTextureList();
    auto renderableList = bridge->getRenderableList();
    auto subpresentationRecord = g_StudioApp.m_subpresentations;

    const QDir projectDir(doc->GetCore()->getProjectFile().getProjectPath());
    const QString projectPath = QDir::cleanPath(projectDir.absolutePath());
    const QString projectPathSlash = projectPath + QLatin1Char('/');

    // Add current presentation to renderables list
    renderableList.insert(doc->getPresentationId());
    subpresentationRecord.push_back(
                SubPresentationRecord({}, doc->getPresentationId(),
                                      projectDir.relativeFilePath(doc->GetDocumentPath())));

    auto addReferencesPresentation = [this, doc, &projectPath](const Q3DStudio::CString &str) {
        addPathsToReferences(m_references, projectPath, doc->GetResolvedPathToDoc(str).toQString());
    };
    auto addReferencesRenderable = [this, &projectPath, &projectPathSlash, &subpresentationRecord]
            (const QString &id) {
        for (SubPresentationRecord r : qAsConst(subpresentationRecord)) {
            if (r.m_id == id)
                addPathsToReferences(m_references, projectPath, projectPathSlash + r.m_argsOrSrc);
        }
    };

    std::for_each(sourcePathList.begin(), sourcePathList.end(), addReferencesPresentation);
    std::for_each(fontFileList.begin(), fontFileList.end(), addReferencesPresentation);
    std::for_each(effectTextureList.begin(), effectTextureList.end(), addReferencesPresentation);
    std::for_each(renderableList.begin(), renderableList.end(), addReferencesRenderable);

    m_references.insert(projectPath);

    updateRoles({IsReferencedRole, Qt::DecorationRole});
}

/**
 * Checks if file is already imported and if not, adds it to outImportedFiles
 *
 * @param importFile The new imported file to check
 * @param outImportedFiles List of already imported files
 * @return true if importFile was added
 */
bool ProjectFileSystemModel::addUniqueImportFile(const QString &importFile,
                                                 QStringList &outImportedFiles) const
{
    const QString cleanPath = QFileInfo(importFile).canonicalFilePath();
    if (outImportedFiles.contains(cleanPath)) {
        return false;
    } else {
        outImportedFiles.append(cleanPath);
        return true;
    }
}

/**
 * Copy a file with option to override an existing file or skip the override.
 *
 * @param srcFile The source file to copy.
 * @param targetFile The destination file path.
 * @param outImportedFiles list of absolute source paths of the dependent assets that are imported
 *                         in the same import context.
 * @param outOverrideChoice The copy skip/override choice used in this import context.
 */
void ProjectFileSystemModel::overridableCopyFile(const QString &srcFile, const QString &targetFile,
                                                 QStringList &outImportedFiles,
                                                 int &outOverrideChoice) const
{
    QFileInfo srcFileInfo(srcFile);
    if (srcFileInfo.exists() && addUniqueImportFile(srcFile, outImportedFiles)) {
        QFileInfo targetFileInfo(targetFile);
        if (srcFileInfo == targetFileInfo)
            return; // Autoskip when source and target is the same
        if (!targetFileInfo.dir().exists())
            targetFileInfo.dir().mkpath(QStringLiteral("."));

        if (targetFileInfo.exists()) { // asset exists, show override / skip box
            if (outOverrideChoice == QMessageBox::YesToAll) {
                QFile::remove(targetFile);
            } else if (outOverrideChoice == QMessageBox::NoToAll) {
                // QFile::copy() does not override files
            } else {
                QString pathFromRoot = QDir(g_StudioApp.GetCore()->getProjectFile()
                                            .getProjectPath())
                                            .relativeFilePath(targetFile);
                outOverrideChoice = g_StudioApp.GetDialogs()
                        ->displayOverrideAssetBox(pathFromRoot);
                if (outOverrideChoice & (QMessageBox::Yes | QMessageBox::YesToAll))
                    QFile::remove(targetFile);
            }
        }
        QFile::copy(srcFile, targetFile);
    }
}

void ProjectFileSystemModel::updateProjectReferences()
{
    m_projectReferences.clear();

    const QDir projectDir(g_StudioApp.GetCore()->getProjectFile().getProjectPath());
    const QString projectPath = QDir::cleanPath(projectDir.absolutePath());

    QHashIterator<QString, bool> updateIt(m_projectReferencesUpdateMap);
    while (updateIt.hasNext()) {
        updateIt.next();
        m_presentationReferences.remove(updateIt.key());
        if (updateIt.value()) {
            QFileInfo fi(updateIt.key());
            QDir fileDir = fi.dir();
            const QString suffix = fi.suffix();
            QHash<QString, QString> importPathMap;
            QSet<QString> newReferences;

            const auto addReferencesFromImportMap = [&]() {
                QHashIterator<QString, QString> pathIter(importPathMap);
                while (pathIter.hasNext()) {
                    pathIter.next();
                    const QString path = pathIter.key();
                    QString targetAssetPath;
                    if (path.startsWith(QLatin1String("./"))) // path from project root
                        targetAssetPath = projectDir.absoluteFilePath(path);
                    else // relative path
                        targetAssetPath = fileDir.absoluteFilePath(path);
                    newReferences.insert(QDir::cleanPath(targetAssetPath));
                }
            };

            if (CDialogs::presentationExtensions().contains(suffix)
                    || CDialogs::qmlStreamExtensions().contains(suffix)) {
                // Presentation file added/modified, check that it is one of the subpresentations,
                // or we don't care about it
                const QString relPath = g_StudioApp.GetCore()->getProjectFile()
                        .getRelativeFilePathTo(updateIt.key());
                for (int i = 0, count = g_StudioApp.m_subpresentations.size(); i < count; ++i) {
                    SubPresentationRecord &rec = g_StudioApp.m_subpresentations[i];
                    if (rec.m_argsOrSrc == relPath) {
                        if (rec.m_type == QLatin1String("presentation")) {
                            // Since this is not actual import, source and target uip is the same,
                            // and we are only interested in the absolute paths of the "imported"
                            // asset files
                            QString dummyStr;
                            QHash<QString, QString> dummyMap;
                            QSet<QString> dummySet;
                            PresentationFile::getSourcePaths(fi, fi, importPathMap,
                                                             dummyStr, dummyMap, dummySet);
                            addReferencesFromImportMap();
                        } else { // qml-stream
                            QQmlApplicationEngine qmlEngine;
                            bool isQmlStream = false;
                            QObject *qmlRoot = getQmlStreamRootNode(qmlEngine, updateIt.key(),
                                                                    isQmlStream);
                            if (qmlRoot && isQmlStream) {
                                QSet<QString> assetPaths;
                                getQmlAssets(qmlRoot, assetPaths);
                                QDir qmlDir = fi.dir();
                                for (auto &assetSrc : qAsConst(assetPaths)) {
                                    QString targetAssetPath;
                                    targetAssetPath = qmlDir.absoluteFilePath(assetSrc);
                                    newReferences.insert(QDir::cleanPath(targetAssetPath));
                                }
                            }
                        }
                        break;
                    }
                }
            } else if (CDialogs::materialExtensions().contains(suffix)
                       || CDialogs::effectExtensions().contains(suffix)) {
                // Use dummy set, as we are only interested in values set in material files
                QSet<QString> dummySet;
                g_StudioApp.GetCore()->GetDoc()->GetDocumentReader()
                    .ParseSourcePathsOutOfEffectFile(
                            updateIt.key(),
                            g_StudioApp.GetCore()->getProjectFile().getProjectPath(),
                            false, // No need to recurse src mats; those get handled individually
                            importPathMap, dummySet);
                addReferencesFromImportMap();
            }
            if (!newReferences.isEmpty())
                m_presentationReferences.insert(updateIt.key(), newReferences);
        }
    }

    // Update reference cache
    QHashIterator<QString, QSet<QString>> presIt(m_presentationReferences);
    while (presIt.hasNext()) {
        presIt.next();
        const auto &refs = presIt.value();
        for (auto &ref : refs)
            addPathsToReferences(m_projectReferences, projectPath, ref);
    }

    m_projectReferencesUpdateMap.clear();
    updateRoles({IsProjectReferencedRole});
}

void ProjectFileSystemModel::getQmlAssets(const QObject *qmlNode,
                                          QSet<QString> &outAssetPaths) const
{
    QString assetSrc = qmlNode->property("source").toString(); // absolute file path

    if (!assetSrc.isEmpty()) {
        // remove file:///
        if (assetSrc.startsWith(QLatin1String("file:///")))
            assetSrc = assetSrc.mid(8);
        else if (assetSrc.startsWith(QLatin1String("file://")))
            assetSrc = assetSrc.mid(7);

#if !defined(Q_OS_WIN)
        // Only windows has drive letter in the path, other platforms need to start with /
        assetSrc.prepend(QLatin1Char('/'));
#endif
        outAssetPaths.insert(assetSrc);
    }

    // recursively load child nodes
    const QObjectList qmlNodeChildren = qmlNode->children();
    for (auto &node : qmlNodeChildren)
        getQmlAssets(node, outAssetPaths);
}

QObject *ProjectFileSystemModel::getQmlStreamRootNode(QQmlApplicationEngine &qmlEngine,
                                                      const QString &filePath,
                                                      bool &outIsQmlStream) const
{
    QObject *qmlRoot = nullptr;
    outIsQmlStream = false;

    qmlEngine.load(filePath);
    if (qmlEngine.rootObjects().size() > 0) {
        qmlRoot = qmlEngine.rootObjects().at(0);
        const char *rootClassName = qmlEngine.rootObjects().at(0)
                                    ->metaObject()->superClass()->className();
        // The assumption here is that any qml that is not a behavior is a qml stream
        if (strcmp(rootClassName, "Q3DStudio::Q3DSQmlBehavior") != 0)
            outIsQmlStream = true;
    }

    return qmlRoot;
}

Q3DStudio::DocumentEditorFileType::Enum ProjectFileSystemModel::assetTypeForRow(int row)
{
    if (row <= 0 || row >= m_items.size())
        return Q3DStudio::DocumentEditorFileType::Unknown;

    const QString rootPath = m_items[0].index.data(QFileSystemModel::FilePathRole).toString();
    QString path = m_items[row].index.data(QFileSystemModel::FilePathRole).toString();
    QFileInfo fi(path);
    if (!fi.isDir())
        path = fi.absolutePath();
    path = path.mid(rootPath.length() + 1);
    const int slash = path.indexOf(QLatin1String("/"));
    if (slash >= 0)
        path = path.left(slash);
    if (path == QLatin1String("effects"))
        return Q3DStudio::DocumentEditorFileType::Effect;
    else if (path == QLatin1String("fonts"))
        return Q3DStudio::DocumentEditorFileType::Font;
    else if (path == QLatin1String("maps"))
        return Q3DStudio::DocumentEditorFileType::Image;
    else if (path == QLatin1String("materials"))
        return Q3DStudio::DocumentEditorFileType::Material;
    else if (path == QLatin1String("models"))
        return Q3DStudio::DocumentEditorFileType::DAE;
    else if (path == QLatin1String("scripts"))
        return Q3DStudio::DocumentEditorFileType::Behavior;
    else if (path == QLatin1String("presentations"))
        return Q3DStudio::DocumentEditorFileType::Presentation;
    else if (path == QLatin1String("qml"))
        return Q3DStudio::DocumentEditorFileType::QmlStream;

    return Q3DStudio::DocumentEditorFileType::Unknown;
}

void ProjectFileSystemModel::setRootPath(const QString &path)
{
    m_projectReferences.clear();
    m_presentationReferences.clear();
    m_projectReferencesUpdateMap.clear();
    m_projectReferencesUpdateTimer.stop();

    // Delete the old model. If the new project is in a totally different directory tree, not
    // doing this will result in unexplicable crashes when trying to parse something that should
    // not be parsed.
    disconnect(m_model, &QAbstractItemModel::rowsInserted,
               this, &ProjectFileSystemModel::modelRowsInserted);
    disconnect(m_model, &QAbstractItemModel::rowsAboutToBeRemoved,
               this, &ProjectFileSystemModel::modelRowsRemoved);
    disconnect(m_model, &QAbstractItemModel::layoutChanged,
               this, &ProjectFileSystemModel::modelLayoutChanged);
    delete m_model;
    m_model = new QFileSystemModel(this);
    connect(m_model, &QAbstractItemModel::rowsInserted,
            this, &ProjectFileSystemModel::modelRowsInserted);
    connect(m_model, &QAbstractItemModel::rowsAboutToBeRemoved,
            this, &ProjectFileSystemModel::modelRowsRemoved);
    connect(m_model, &QAbstractItemModel::layoutChanged,
            this, &ProjectFileSystemModel::modelLayoutChanged);

    setRootIndex(m_model->setRootPath(path));

    // Open the presentations folder by default
    connect(this, &ProjectFileSystemModel::dataChanged,
            this, &ProjectFileSystemModel::asyncExpandPresentations);

    QTimer::singleShot(0, [this]() {
        // Watch the project directory for changes to .uip files.
        // Note that this initial connection will notify creation for all files, so we call it
        // asynchronously to ensure the subpresentations are registered.
        m_directoryConnection = g_StudioApp.getDirectoryWatchingSystem().AddDirectory(
                    g_StudioApp.GetCore()->getProjectFile().getProjectPath(),
                    std::bind(&ProjectFileSystemModel::onFilesChanged, this,
                              std::placeholders::_1));
    });
}

void ProjectFileSystemModel::setRootIndex(const QModelIndex &rootIndex)
{
    if (rootIndex == m_rootIndex)
        return;

    clearModelData();

    m_rootIndex = rootIndex;

    beginInsertRows({}, 0, 0);
    m_items.append({ m_rootIndex, 0, true, nullptr, 0 });
    endInsertRows();

    showModelTopLevelItems();
}

void ProjectFileSystemModel::clearModelData()
{
    beginResetModel();
    m_defaultDirToAbsPathMap.clear();
    m_items.clear();
    endResetModel();
}

void ProjectFileSystemModel::showModelTopLevelItems()
{
    int rowCount = m_model->rowCount(m_rootIndex);

    if (rowCount == 0) {
        if (m_model->hasChildren(m_rootIndex) && m_model->canFetchMore(m_rootIndex))
            m_model->fetchMore(m_rootIndex);
    } else {
        showModelChildItems(m_rootIndex, 0, rowCount - 1);
    }
}

void ProjectFileSystemModel::showModelChildItems(const QModelIndex &parentIndex, int start, int end)
{
    const int parentRow = modelIndexRow(parentIndex);
    if (parentRow == -1)
        return;

    Q_ASSERT(isVisible(parentIndex));

    QVector<QModelIndex> rowsToInsert;
    for (int i = start; i <= end; ++i) {
        const auto &childIndex = m_model->index(i, 0, parentIndex);
        if (isVisible(childIndex))
            rowsToInsert.append(childIndex);
    }

    const int insertCount = rowsToInsert.count();
    if (insertCount == 0)
        return;

    auto parent = &m_items[parentRow];

    const int depth = parent->depth + 1;
    const int startRow = parentRow + parent->childCount + 1;

    beginInsertRows({}, startRow, startRow + insertCount - 1);

    for (auto it = rowsToInsert.rbegin(); it != rowsToInsert.rend(); ++it)
        m_items.insert(startRow, { *it, depth, false, parent, 0 });

    for (; parent != nullptr; parent = parent->parent)
        parent->childCount += insertCount;

    endInsertRows();

    // also fetch children so we're notified when files are added or removed in immediate subdirs
    for (const auto &childIndex : rowsToInsert) {
        if (m_model->hasChildren(childIndex) && m_model->canFetchMore(childIndex))
            m_model->fetchMore(childIndex);
    }
}

void ProjectFileSystemModel::expand(int row)
{
    if (row < 0 || row > m_items.size() - 1 || m_items[row].expanded)
        return;

    auto &item = m_items[row];
    const auto &modelIndex = item.index;

    const int rowCount = m_model->rowCount(modelIndex);
    if (rowCount == 0) {
        if (m_model->hasChildren(modelIndex) && m_model->canFetchMore(modelIndex))
            m_model->fetchMore(modelIndex);
    } else {
        showModelChildItems(modelIndex, 0, rowCount - 1);
    }

    item.expanded = true;
    Q_EMIT dataChanged(index(row), index(row));
}

bool ProjectFileSystemModel::hasValidUrlsForDropping(const QList<QUrl> &urls) const
{
    for (const auto &url : urls) {
        if (url.isLocalFile()) {
            const QString path = url.toLocalFile();
            const QFileInfo fileInfo(path);
            if (fileInfo.isFile()) {
                const QString extension = fileInfo.suffix();
                return extension.compare(QLatin1String(CDialogs::GetDAEFileExtension()),
                                         Qt::CaseInsensitive) == 0
#ifdef QT_3DSTUDIO_FBX
                       || extension.compare(QLatin1String(CDialogs::GetFbxFileExtension()),
                                            Qt::CaseInsensitive) == 0
#endif
                       || getIconType(path) != OBJTYPE_UNKNOWN;
            }
        }
    }

    return false;
}

void ProjectFileSystemModel::showInfo(int row)
{
    if (row < 0 || row >= m_items.size())
        row = 0;

    const TreeItem &item = m_items.at(row);
    QString path = item.index.data(QFileSystemModel::FilePathRole).toString();

    QFileInfo fi(path);

    if (fi.suffix() == QLatin1String("materialdef")) {
        const auto doc = g_StudioApp.GetCore()->GetDoc();
        bool isDocModified = doc->IsModified();
        { // Scope for the ScopedDocumentEditor
            Q3DStudio::ScopedDocumentEditor sceneEditor(
                        Q3DStudio::SCOPED_DOCUMENT_EDITOR(*doc, QString()));
            const auto material = sceneEditor->getOrCreateMaterial(path);
            QString name;
            QMap<QString, QString> values;
            QMap<QString, QMap<QString, QString>> textureValues;
            sceneEditor->getMaterialInfo(fi.absoluteFilePath(), name, values, textureValues);
            sceneEditor->setMaterialValues(fi.absoluteFilePath(), values, textureValues);
            if (material.Valid())
                doc->SelectDataModelObject(material);
        }
        // Several aspects of the editor are not updated correctly
        // if the data core is changed without a transaction
        // The above scope completes the transaction for creating a new material
        // Next the added undo has to be popped from the stack
        // and the modified flag has to be restored
        // TODO: Find a way to update the editor fully without a transaction
        doc->SetModifiedFlag(isDocModified);
        g_StudioApp.GetCore()->GetCmdStack()->RemoveLastUndo();
    }
}

void ProjectFileSystemModel::duplicate(int row)
{
    if (row < 0 || row >= m_items.size())
        row = 0;

    const TreeItem &item = m_items.at(row);
    QString path = item.index.data(QFileSystemModel::FilePathRole).toString();

    QFileInfo srcFile(path);
    const QString destPathStart = srcFile.dir().absolutePath() + QLatin1Char('/')
            + srcFile.completeBaseName() + QStringLiteral(" Copy");
    const QString destPathEnd = QStringLiteral(".") + srcFile.suffix();
    QString destPath = destPathStart + destPathEnd;

    int i = 1;
    while (QFile::exists(destPath)) {
        i++;
        destPath = destPathStart + QString::number(i) + destPathEnd;
    }

    QFile::copy(path, destPath);
}

void ProjectFileSystemModel::importUrls(const QList<QUrl> &urls, int row, bool autoSort)
{
    if (row < 0 || row >= m_items.size())
        row = 0; // Import to root folder row not specified

    // If importing via buttons or doing in-context import to project root,
    // sort imported items to default folders according to their type
    const bool sortToDefaults = autoSort || row == 0;
    if (sortToDefaults)
        updateDefaultDirMap();

    const TreeItem &item = m_items.at(row);
    QString targetPath = item.index.data(QFileSystemModel::FilePathRole).toString();

    QFileInfo fi(targetPath);
    if (!fi.isDir())
        targetPath = fi.absolutePath();
    const QDir targetDir(targetPath);

    QStringList expandPaths;
    QHash<QString, QString> presentationNodes; // <relative path to presentation, presentation id>
    // List of all files that have been copied by this import. Used to avoid duplicate imports
    // due to some of the imported files also being assets used by other imported files.
    QStringList importedFiles;
    QMap<QString, CDataInputDialogItem *> importedDataInputs;
    int overrideChoice = QMessageBox::NoButton;

    for (const auto &url : urls) {
        QString sortedPath = targetPath;
        QDir sortedDir = targetDir;

        if (sortToDefaults) {
            const QString defaultDir = m_defaultDirToAbsPathMap.value(
                        g_StudioApp.GetDialogs()->defaultDirForUrl(url));
            if (!defaultDir.isEmpty()) {
                sortedPath = defaultDir;
                sortedDir.setPath(sortedPath);
            }
        }

        if (sortedDir.exists()) {
            importUrl(sortedDir, url, presentationNodes, importedFiles, importedDataInputs,
                      overrideChoice);
            expandPaths << sortedDir.path();
        }
    }

    // Batch update all imported presentation nodes
    g_StudioApp.GetCore()->getProjectFile().addPresentationNodes(presentationNodes);

    // Add new data inputs that are missing from project's data inputs. Duplicates are ignored,
    // even if they are different type.
    QMapIterator<QString, CDataInputDialogItem *> diIt(importedDataInputs);
    bool addedDi = false;
    while (diIt.hasNext()) {
        diIt.next();
        if (!g_StudioApp.m_dataInputDialogItems.contains(diIt.key())) {
            g_StudioApp.m_dataInputDialogItems.insert(diIt.key(), diIt.value());
            addedDi = true;
        } else {
            delete diIt.value();
        }
    }
    if (addedDi) {
        g_StudioApp.saveDataInputsToProjectFile();
        g_StudioApp.checkDeletedDatainputs();  // Updates externalPresBoundTypes
    }

    for (const QString &expandPath : qAsConst(expandPaths)) {
        int expandRow = rowForPath(expandPath);
        if (expandRow >= 0 && !m_items[expandRow].expanded)
            expand(expandRow);
    }
}

/**
 * Imports a single asset and the assets it depends on.
 *
 * @param targetDir Target path where the asset is imported to
 * @param url Source url where the asset is imported from
 * @param outPresentationNodes Map where presentation node information is stored for later
 *                             registration. The key is relative path to presentation. The value
 *                             is presentation id.
 * @param outImportedFiles List of absolute source paths of the dependent assets that are imported
 *                         in the same import context.
 * @param outDataInputs Map of data inputs that are in use in this import context.
 * @param outOverrideChoice The copy skip/override choice used in this import context.
 */
void ProjectFileSystemModel::importUrl(QDir &targetDir, const QUrl &url,
                                       QHash<QString, QString> &outPresentationNodes,
                                       QStringList &outImportedFiles,
                                       QMap<QString, CDataInputDialogItem *> &outDataInputs,
                                       int &outOverrideChoice) const
{
    using namespace Q3DStudio;
    using namespace qt3dsimp;
    // Drag and Drop - From Explorer window to Project Palette
    // For all valid Project File Types:
    // - This performs a file copy from the source Explorer location to the selected Project Palette
    // Folder
    // - The destination copy must NOT be read-only even if the source is read-only
    // For DAE, it will import the file.

    if (!url.isLocalFile())
        return;

    const QString sourceFile = url.toLocalFile();

    const QFileInfo fileInfo(sourceFile);
    if (!fileInfo.isFile())
        return;

    // Skip importing if the file has already been imported
    if (!addUniqueImportFile(sourceFile, outImportedFiles))
        return;

    const auto doc = g_StudioApp.GetCore()->GetDoc();

    const QString extension = fileInfo.suffix();
    const QString fileStem = fileInfo.baseName();
    const QString outputFileName = QStringLiteral("%1.%2").arg(fileStem).arg(CDialogs::GetImportFileExtension());

    if (extension.compare(QLatin1String(CDialogs::GetDAEFileExtension()), Qt::CaseInsensitive) == 0) {
        SColladaTranslator translator(sourceFile);
        const QDir outputDir = SFileTools::FindUniqueDestDirectory(targetDir, fileStem);
        const QString fullOutputFile = outputDir.filePath(outputFileName);
        const SImportResult importResult =
            CPerformImport::TranslateToImportFile(translator, CFilePath(fullOutputFile));
        bool forceError = QFileInfo(fullOutputFile).isFile() == false;
        IDocumentEditor::DisplayImportErrors(
                sourceFile, importResult.m_Error, doc->GetImportFailedHandler(),
                translator.m_TranslationLog, forceError);
#ifdef QT_3DSTUDIO_FBX
    } else if (extension.compare(QLatin1String(CDialogs::GetFbxFileExtension()), Qt::CaseInsensitive) == 0) {
        SFbxTranslator translator(sourceFile);
        const QDir outputDir = SFileTools::FindUniqueDestDirectory(targetDir, fileStem);
        const QString fullOutputFile = outputDir.filePath(outputFileName);
        const SImportResult importResult =
            CPerformImport::TranslateToImportFile(translator, CFilePath(fullOutputFile));
        bool forceError = QFileInfo(fullOutputFile).isFile() == false;
        IDocumentEditor::DisplayImportErrors(
                sourceFile, importResult.m_Error, doc->GetImportFailedHandler(),
                translator.m_TranslationLog, forceError);
#endif
    } else {
        QQmlApplicationEngine qmlEngine;
        QObject *qmlRoot = nullptr;
        bool isQmlStream = false;
        if (extension == QLatin1String("qml")) {
            qmlRoot = getQmlStreamRootNode(qmlEngine, sourceFile, isQmlStream);
            if (qmlRoot) {
                if (isQmlStream && targetDir.path().endsWith(QLatin1String("/scripts"))) {
                    const QString path(QStringLiteral("../qml"));
                    targetDir.mkpath(path); // create the folder if doesn't exist
                    targetDir.cd(path);
                }
            } else {
                // Invalid qml file, block import
                g_StudioApp.GetDialogs()->DisplayKnownErrorDialog(
                            tr("Failed to parse '%1'\nAborting import.").arg(sourceFile));
                return;
            }
        }
        // Copy the file to target directory
        // FindAndCopyDestFile will make sure the file name is unique and make sure it is
        // not read only.
        QString destPath; // final file path (after copying and renaming)
        bool copyResult = SFileTools::FindAndCopyDestFile(targetDir, sourceFile, destPath);
        Q_ASSERT(copyResult);

        QString presentationPath;
        if (CDialogs::isPresentationFileExtension(extension.toLatin1().data())) {
            presentationPath = doc->GetCore()->getProjectFile().getRelativeFilePathTo(destPath);
            QSet<QString> dataInputs;
            importPresentationAssets(fileInfo, QFileInfo(destPath), outPresentationNodes,
                                     outImportedFiles, dataInputs, outOverrideChoice);
            const QString projFile = PresentationFile::findProjectFile(fileInfo.absoluteFilePath());
            QMap<QString, CDataInputDialogItem *> allDataInputs;
            ProjectFile::loadDataInputs(projFile, allDataInputs);
            for (auto &di : dataInputs) {
                if (allDataInputs.contains(di))
                    outDataInputs.insert(di, allDataInputs[di]);
            }
        } else if (qmlRoot && isQmlStream) { // importing a qml stream
            presentationPath = doc->GetCore()->getProjectFile().getRelativeFilePathTo(destPath);
            importQmlAssets(qmlRoot, fileInfo.dir(), targetDir, outImportedFiles,
                            outOverrideChoice);
        }

        // outPresentationNodes can already contain this presentation in case of multi-importing
        // both a presentation and its subpresentation
        if (!presentationPath.isEmpty() && !outPresentationNodes.contains(presentationPath)) {
            const QString srcProjFile = PresentationFile::findProjectFile(sourceFile);
            QString presId;
            if (!srcProjFile.isEmpty()) {
                QVector<SubPresentationRecord> subpresentations;
                ProjectFile::getPresentations(srcProjFile, subpresentations);
                QDir srcProjDir(QFileInfo(srcProjFile).path());
                const QString relSrcPresFilePath = srcProjDir.relativeFilePath(sourceFile);
                auto *sp = std::find_if(
                            subpresentations.begin(), subpresentations.end(),
                            [&relSrcPresFilePath](const SubPresentationRecord &spr) -> bool {
                    return spr.m_argsOrSrc == relSrcPresFilePath;
                });
                // Make sure we are not adding a duplicate id. In that case presId will be empty
                // which causes autogeneration of an unique id.
                if (sp != subpresentations.end()
                    && g_StudioApp.GetCore()->getProjectFile().isUniquePresentationId(sp->m_id)) {
                    presId = sp->m_id;
                }
            }
            outPresentationNodes.insert(presentationPath, presId);
        }

        // For effect and custom material files, automatically copy related resources
        if (CDialogs::IsEffectFileExtension(extension.toLatin1().data())
                || CDialogs::IsMaterialFileExtension(extension.toLatin1().data())) {
            QHash<QString, QString> effectFileSourcePaths;
            QString absSrcPath = fileInfo.absoluteFilePath();
            QString projectPath
                    = QFileInfo(PresentationFile::findProjectFile(absSrcPath)).absolutePath();
            // Since we are importing a bare material/effect, we don't care about possible dynamic
            // values of texture properties
            QSet<QString> dummyPropertySet;
            g_StudioApp.GetCore()->GetDoc()->GetDocumentReader()
                .ParseSourcePathsOutOfEffectFile(absSrcPath, projectPath, true,
                                                 effectFileSourcePaths, dummyPropertySet);

            QHashIterator<QString, QString> pathIter(effectFileSourcePaths);
            while (pathIter.hasNext()) {
                pathIter.next();
                overridableCopyFile(pathIter.value(),
                                    QDir(g_StudioApp.GetCore()->getProjectFile().getProjectPath())
                                    .absoluteFilePath(pathIter.key()),
                                    outImportedFiles, outOverrideChoice);
            }
        }
    }
}

/**
 * Import all assets used in a uip file, this includes materials and effects (and their assets),
 * images, fonts, subpresentations (and their recursive assets), models and scripts. Assets are
 * imported in the same relative structure in the imported-from folder in order not to break the
 * assets paths.
 *
 * @param uipSrc source file path where the uip is imported from
 * @param uipTarget target path where the uip is imported to
 * @param outPresentationNodes map where presentation node information is stored for later
 *                             registration. The key is relative path to presentation. The value
 *                             is presentation id.
 * @param overrideChoice tracks user choice (yes to all / no to all) to maintain the value through
 *                       recursive calls
 * @param outImportedFiles list of absolute source paths of the dependent assets that are imported
 *                         in the same import context.
 * @param outDataInputs set of data input identifiers that are in use by this presentation and its
 *                      subpresentations.
 * @param outOverrideChoice The copy skip/override choice used in this import context.
 */
void ProjectFileSystemModel::importPresentationAssets(
        const QFileInfo &uipSrc, const QFileInfo &uipTarget,
        QHash<QString, QString> &outPresentationNodes, QStringList &outImportedFiles,
        QSet<QString> &outDataInputs, int &outOverrideChoice) const
{
    QHash<QString, QString> importPathMap;
    QString projPathSrc; // project absolute path for the source uip
    PresentationFile::getSourcePaths(uipSrc, uipTarget, importPathMap, projPathSrc,
                                     outPresentationNodes, outDataInputs);
    const QDir projDir(g_StudioApp.GetCore()->getProjectFile().getProjectPath());
    const QDir uipSrcDir = uipSrc.dir();
    const QDir uipTargetDir = uipTarget.dir();

    QHashIterator<QString, QString> pathIter(importPathMap);
    while (pathIter.hasNext()) {
        pathIter.next();
        QString srcAssetPath = pathIter.value();
        const QString path = pathIter.key();
        QString targetAssetPath;
        if (srcAssetPath.isEmpty())
            srcAssetPath = uipSrcDir.absoluteFilePath(path);
        targetAssetPath = uipTargetDir.absoluteFilePath(path);

        overridableCopyFile(srcAssetPath, targetAssetPath, outImportedFiles, outOverrideChoice);

        if (path.endsWith(QLatin1String(".uip"))) {
            // recursively load any uip asset's assets
            importPresentationAssets(QFileInfo(srcAssetPath), QFileInfo(targetAssetPath),
                                     outPresentationNodes, outImportedFiles, outDataInputs,
                                     outOverrideChoice);

            // update the path in outPresentationNodes to be correctly relative in target project
            const QString subId = outPresentationNodes.take(path);
            if (!subId.isEmpty())
                outPresentationNodes.insert(projDir.relativeFilePath(targetAssetPath), subId);
        } else if (path.endsWith(QLatin1String(".qml"))) {
            // recursively load any qml stream assets
            QQmlApplicationEngine qmlEngine;
            bool isQmlStream = false;
            QObject *qmlRoot = getQmlStreamRootNode(qmlEngine, srcAssetPath, isQmlStream);
            if (qmlRoot && isQmlStream) {
                importQmlAssets(qmlRoot, QFileInfo(srcAssetPath).dir(),
                                QFileInfo(targetAssetPath).dir(), outImportedFiles,
                                outOverrideChoice);
                // update path in outPresentationNodes to be correctly relative in target project
                const QString subId = outPresentationNodes.take(path);
                if (!subId.isEmpty())
                    outPresentationNodes.insert(projDir.relativeFilePath(targetAssetPath), subId);
            }
        }
    }
}

/**
 * Import all assets specified in "source" properties in a qml file.
 *
 * @param qmlNode The qml node to checkfor assets. Recursively checks all child nodes, too.
 * @param srcDir target dir where the assets are imported to
 * @param outImportedFiles list of absolute source paths of the dependent assets that are imported
 *                         in the same import context.
 * @param outOverrideChoice The copy skip/override choice used in this import context.
 */
void ProjectFileSystemModel::importQmlAssets(const QObject *qmlNode, const QDir &srcDir,
                                             const QDir &targetDir,
                                             QStringList &outImportedFiles,
                                             int &outOverrideChoice) const
{
    QSet<QString> assetPaths;
    getQmlAssets(qmlNode, assetPaths);

    for (auto &assetSrc : qAsConst(assetPaths)) {
        overridableCopyFile(srcDir.absoluteFilePath(assetSrc),
                            targetDir.absoluteFilePath(srcDir.relativeFilePath(assetSrc)),
                            outImportedFiles, outOverrideChoice);
    }
}

int ProjectFileSystemModel::rowForPath(const QString &path) const
{
    for (int i = m_items.size() - 1; i >= 0 ; --i) {
        const QString itemPath = m_items[i].index.data(QFileSystemModel::FilePathRole).toString();
        if (path == itemPath)
            return i;
    }
    return -1;
}

void ProjectFileSystemModel::updateRoles(const QVector<int> &roles, int startRow, int endRow)
{
    Q_EMIT dataChanged(index(startRow, 0),
                       index(endRow < 0 ? rowCount() - 1 : endRow, 0), roles);
}

void ProjectFileSystemModel::collapse(int row)
{
    Q_ASSERT(row >= 0 && row < m_items.size());

    auto &item = m_items[row];
    Q_ASSERT(item.expanded == true);

    const int childCount = item.childCount;

    if (childCount > 0) {
        beginRemoveRows({}, row + 1, row + childCount);

        m_items.erase(std::begin(m_items) + row + 1, std::begin(m_items) + row + 1 + childCount);

        for (auto parent = &item; parent != nullptr; parent = parent->parent)
            parent->childCount -= childCount;

        endRemoveRows();
    }

    item.expanded = false;
    Q_EMIT dataChanged(index(row), index(row));
}

int ProjectFileSystemModel::modelIndexRow(const QModelIndex &modelIndex) const
{
    auto it = std::find_if(
                std::begin(m_items),
                std::end(m_items),
                [&modelIndex](const TreeItem &item)
                {
                    return item.index == modelIndex;
                });

    return it != std::end(m_items) ? std::distance(std::begin(m_items), it) : -1;
}

bool ProjectFileSystemModel::isExpanded(const QModelIndex &modelIndex) const
{
    if (modelIndex == m_rootIndex)
        return true;
    const int row = modelIndexRow(modelIndex);
    return row != -1 && m_items.at(row).expanded;
}

EStudioObjectType ProjectFileSystemModel::getIconType(const QString &path) const
{
    return Q3DStudio::ImportUtils::GetObjectFileTypeForFile(path).m_IconType;
}

QString ProjectFileSystemModel::getIconName(const QString &path) const
{
    QString iconName;

    bool referenced = m_references.contains(path);

    QFileInfo fileInfo(path);
    if (fileInfo.isFile()) {
        EStudioObjectType type = getIconType(path);

        if (type == OBJTYPE_PRESENTATION) {
            const bool isCurrent = isCurrentPresentation(path);
            const bool isInitial = isInitialPresentation(path);
            if (isInitial) {
                iconName = isCurrent ? QStringLiteral("initial_used.png")
                                     : QStringLiteral("initial_notUsed.png");
            } else if (isCurrent) {
                iconName = QStringLiteral("presentation_edit.png");
            }
        }

        if (iconName.isEmpty()) {
            if (type != OBJTYPE_UNKNOWN) {
                iconName = referenced ? CStudioObjectTypes::GetNormalIconName(type)
                                      : CStudioObjectTypes::GetDisabledIconName(type);
            } else {
                iconName = referenced ? QStringLiteral("Objects-Layer-Normal.png")
                                      : QStringLiteral("Objects-Layer-Disabled.png");
            }
        }
    } else {
        iconName = referenced ? QStringLiteral("Objects-Folder-Normal.png")
                              : QStringLiteral("Objects-Folder-Disabled.png");
    }

    return iconName;
}

bool ProjectFileSystemModel::hasVisibleChildren(const QModelIndex &modelIndex) const
{
    const QDir dir(modelIndex.data(QFileSystemModel::FilePathRole).toString());
    if (!dir.exists() || dir.isEmpty())
        return false;

    const auto fileInfoList = dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
    for (const auto &fileInfo : fileInfoList) {
        if (fileInfo.isDir() || getIconType(fileInfo.filePath()) != OBJTYPE_UNKNOWN)
            return true;
    }

    return false;
}

bool ProjectFileSystemModel::isVisible(const QModelIndex &modelIndex) const
{
    QString path = modelIndex.data(QFileSystemModel::FilePathRole).toString();

    if (modelIndex == m_rootIndex || QFileInfo(path).isDir())
        return true;

    if (path.endsWith(QLatin1String("_autosave.uip"))
        || path.endsWith(QLatin1String("_@preview@.uip"))
        || path.endsWith(QLatin1String(".uia"))) {
        return false;
    }

    return getIconType(path) != OBJTYPE_UNKNOWN;
}

void ProjectFileSystemModel::modelRowsInserted(const QModelIndex &parent, int start, int end)
{
    if (!m_rootIndex.isValid())
        return;

    if (isExpanded(parent)) {
        showModelChildItems(parent, start, end);
    } else {
        if (hasVisibleChildren(parent)) {
            // show expand arrow
            const int row = modelIndexRow(parent);
            Q_EMIT dataChanged(index(row), index(row));
        }
    }
}

void ProjectFileSystemModel::modelRowsRemoved(const QModelIndex &parent, int start, int end)
{
    if (!m_rootIndex.isValid())
        return;

    if (isExpanded(parent)) {
        for (int i = start; i <= end; ++i) {
            const int row = modelIndexRow(m_model->index(i, 0, parent));

            if (row != -1) {
                const auto &item = m_items.at(row);

                beginRemoveRows({}, row, row + item.childCount);

                for (auto parent = item.parent; parent != nullptr; parent = parent->parent)
                    parent->childCount -= 1 + item.childCount;

                m_items.erase(std::begin(m_items) + row,
                              std::begin(m_items) + row + item.childCount + 1);

                endRemoveRows();
            }
        }
    }

    if (!hasVisibleChildren(parent)) {
        // collapse the now empty folder
        const int row = modelIndexRow(parent);
        if (m_items[row].expanded)
            collapse(row);
        else
            Q_EMIT dataChanged(index(row), index(row));
    }
}

void ProjectFileSystemModel::modelLayoutChanged()
{
    if (!m_rootIndex.isValid())
        return;

    QSet<QPersistentModelIndex> expandedItems;
    for (const auto &item : m_items) {
        if (item.expanded)
            expandedItems.insert(item.index);
    }

    const std::function<int(const QModelIndex &, TreeItem *)> insertChildren = [this, &expandedItems, &insertChildren](const QModelIndex &parentIndex, TreeItem *parent)
    {
        Q_ASSERT(isVisible(parentIndex));

        const int rowCount = m_model->rowCount(parentIndex);
        const int depth = parent->depth + 1;

        int childCount = 0;

        for (int i = 0; i < rowCount; ++i) {
            const auto &childIndex = m_model->index(i, 0, parentIndex);
            if (isVisible(childIndex)) {
                const bool expanded = expandedItems.contains(childIndex);
                m_items.append({ childIndex, depth, expanded, parent, 0 });
                auto &item = m_items.last();
                if (expanded) {
                    item.childCount = insertChildren(childIndex, &item);
                    childCount += item.childCount;
                }
                ++childCount;
            }
        }

        return childCount;
    };

    const int itemCount = m_items.count();

    m_items.erase(std::begin(m_items) + 1, std::end(m_items));
    m_items.reserve(itemCount);
    insertChildren(m_rootIndex, &m_items.first());

    Q_ASSERT(m_items.count() == itemCount);

    Q_EMIT dataChanged(index(0), index(itemCount - 1));
}

void ProjectFileSystemModel::updateDefaultDirMap()
{
    if (m_defaultDirToAbsPathMap.isEmpty()) {
        m_defaultDirToAbsPathMap.insert(QStringLiteral("effects"), QString());
        m_defaultDirToAbsPathMap.insert(QStringLiteral("fonts"), QString());
        m_defaultDirToAbsPathMap.insert(QStringLiteral("maps"), QString());
        m_defaultDirToAbsPathMap.insert(QStringLiteral("materials"), QString());
        m_defaultDirToAbsPathMap.insert(QStringLiteral("models"), QString());
        m_defaultDirToAbsPathMap.insert(QStringLiteral("scripts"), QString());
        m_defaultDirToAbsPathMap.insert(QStringLiteral("presentations"), QString());
        m_defaultDirToAbsPathMap.insert(QStringLiteral("qml"), QString());
    }

    const QString rootPath = m_items[0].index.data(QFileSystemModel::FilePathRole).toString();
    const QStringList keys = m_defaultDirToAbsPathMap.keys();
    for (const QString &key : keys) {
        QString currentValue = m_defaultDirToAbsPathMap[key];
        if (currentValue.isEmpty()) {
            const QString defaultPath = rootPath + QLatin1Char('/') + key;
            const QFileInfo fi(defaultPath);
            if (fi.exists() && fi.isDir())
                m_defaultDirToAbsPathMap.insert(key, defaultPath);
        } else {
            const QFileInfo fi(currentValue);
            if (!fi.exists())
                m_defaultDirToAbsPathMap.insert(key, QString());
        }
    }
}

void ProjectFileSystemModel::addPathsToReferences(QSet<QString> &references,
                                                  const QString &projectPath,
                                                  const QString &origPath)
{
    references.insert(origPath);
    QString path = origPath;
    QString parentPath = QFileInfo(path).path();
    do {
        references.insert(path);
        path = parentPath;
        parentPath = QFileInfo(path).path();
    } while (path != projectPath && parentPath != path);
}

void ProjectFileSystemModel::handlePresentationIdChange(const QString &path, const QString &id)
{
    const QString cleanPath = QDir::cleanPath(
                QDir(g_StudioApp.GetCore()->GetDoc()->GetCore()->getProjectFile()
                     .getProjectPath()).absoluteFilePath(path));
    int row = rowForPath(cleanPath);
    m_projectReferencesUpdateMap.insert(cleanPath, true);
    m_projectReferencesUpdateTimer.start();
    updateRoles({FileIdRole, ExtraIconRole}, row, row);
}

void ProjectFileSystemModel::asyncExpandPresentations()
{
    disconnect(this, &ProjectFileSystemModel::dataChanged,
               this, &ProjectFileSystemModel::asyncExpandPresentations);

    // expand presentation folder by default (if it exists).
    QTimer::singleShot(0, [this]() {
        QString path = g_StudioApp.GetCore()->getProjectFile().getProjectPath()
                       + QStringLiteral("/presentations");
        expand(rowForPath(path));
    });
}

void ProjectFileSystemModel::asyncUpdateReferences()
{
    QTimer::singleShot(0, this, &ProjectFileSystemModel::updateReferences);
}

void ProjectFileSystemModel::onFilesChanged(
        const Q3DStudio::TFileModificationList &inFileModificationList)
{
    // If any presentation file changes, update asset reference caches
    for (size_t idx = 0, end = inFileModificationList.size(); idx < end; ++idx) {
        const Q3DStudio::SFileModificationRecord &record(inFileModificationList[idx]);
        if (record.m_File.isFile()) {
            const QString suffix = record.m_File.suffix();
            const bool isQml = CDialogs::qmlStreamExtensions().contains(suffix);
            if (isQml || CDialogs::presentationExtensions().contains(suffix)
                    || CDialogs::materialExtensions().contains(suffix)
                    || CDialogs::effectExtensions().contains(suffix)) {
                const QString filePath = record.m_File.absoluteFilePath();
                if (record.m_ModificationType == Q3DStudio::FileModificationType::Created
                        || record.m_ModificationType == Q3DStudio::FileModificationType::Modified) {
                    if (isQml && !g_StudioApp.isQmlStream(filePath))
                        continue; // Skip non-stream qml's to match import logic
                    m_projectReferencesUpdateMap.insert(filePath, true);
                } else if (record.m_ModificationType
                           == Q3DStudio::FileModificationType::Destroyed) {
                    m_projectReferencesUpdateMap.insert(filePath, false);
                }
                m_projectReferencesUpdateTimer.start();
            }
        }
    }
}

bool ProjectFileSystemModel::isCurrentPresentation(const QString &path) const
{
    return path == g_StudioApp.GetCore()->GetDoc()->GetDocumentPath();
}

bool ProjectFileSystemModel::isInitialPresentation(const QString &path) const
{
    QString checkId = presentationId(path);

    return !checkId.isEmpty()
            && checkId == g_StudioApp.GetCore()->getProjectFile().initialPresentation();
}

QString ProjectFileSystemModel::presentationId(const QString &path) const
{
    QString presId;
    if (isCurrentPresentation(path))
        presId = g_StudioApp.GetCore()->GetDoc()->getPresentationId();
    else
        presId = g_StudioApp.getRenderableId(QFileInfo(path).absoluteFilePath());

    return presId;
}