summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Workspace/Dialogs.cpp
blob: 2a5a6780ff6a39f6401b0871a9e47c3db2620821 (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
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** 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 "Dialogs.h"
#include "StudioApp.h"
#include "Core.h"
#include "Doc.h"
#include "MainFrm.h"
#include "InterpolationDlg.h"
#include "Qt3DSMessageBox.h"
#include "ProgressView.h"
#include "TimeEditDlg.h"
#include "DurationEditDlg.h"
#include "StudioPreferences.h"
#include "ResetKeyframeValuesDlg.h"
#include "GLVersionDlg.h"
#include "Qt3DSMacros.h"
#include "ImportUtils.h"

#include <QtWidgets/qcolordialog.h>
#include <QtWidgets/qfiledialog.h>
#include <QtWidgets/qmessagebox.h>
#include <QtWidgets/qcheckbox.h>
#include <QtCore/qstandardpaths.h>
#include <QtWidgets/qdesktopwidget.h>
#include <QtWidgets/qpushbutton.h>
#include <QtGui/qscreen.h>
#include <QtWidgets/qlayout.h>
#include <QtWidgets/qdialogbuttonbox.h>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qstyle.h>

namespace {

inline Q3DStudio::CString CreateExtensionsList(const char **extList)
{
    Q3DStudio::CString retval;
    for (const char **ext = extList; *ext != nullptr; ++ext) {
        if (retval.Length())
            retval += " ";
        retval += Q3DStudio::CString("*.") + *ext;
    }
    return retval;
}

struct SAllowedTypesEntry
{
    Q3DStudio::DocumentEditorFileType::Enum m_FileType;
    QString m_ResourceString; // Model Files, Image Files, etc
    const char **m_FileExtensions;
};

const char *imgExts[] = {
    "png", "jpg", "jpeg", "dds", "bmp", "gif", "hdr", "ktx", nullptr,
};

const wchar_t *wideImgExts[] = {
    L"png", L"jpg", L"jpeg", L"dds", L"bmp", L"gif", L"hdr", L"ktx", nullptr,
};

const char *modelExts[] = {
    CDialogs::GetDAEFileExtension(),
    #ifdef QT_3DSTUDIO_FBX
    CDialogs::GetFbxFileExtension(),
    #endif
    nullptr,
};

const char *meshExts[] = {
    CDialogs::GetMeshFileExtension(), nullptr,
};

const char *importExts[] = {
    CDialogs::GetImportFileExtension(), nullptr,
};

const char *behaviorExts[] = {
    CDialogs::GetQmlFileExtension(), nullptr,
};

const char *presentationExts[] = {
    "uip", nullptr,
};

const wchar_t *widePresentationExts[] = {
    L"uip", nullptr,
};

const char *qmlStreamExts[] = {
    "qml", nullptr,
};

const wchar_t *wideQmlStreamExts[] = {
    L"qml", nullptr,
};

const char *projectExts[] = {
    "uia", nullptr,
};

const wchar_t *wideProjectExts[] = {
    L"uia", nullptr,
};

const char *fontExts[] = {
    "ttf", "otf", nullptr,
};
const wchar_t *wideFontExts[] = {
    L"ttf", L"otf", nullptr,
};

const char *effectExts[] = {
    "effect", nullptr,
};

const wchar_t *wideEffectExts[] = {
    L"effect", nullptr,
};

const char *materialExts[] = {
    "material", "shader", "materialdef", nullptr,
};

const wchar_t *wideMaterialExts[] = {
    L"material", L"shader", L"materialdef", nullptr,
};

const char *soundExts[] = {
    "wav", nullptr,
};

const wchar_t *wideSoundExts[] = {
    L"wav", nullptr,
};

// List of file types allowed during import
// Note: Despite its name, Q3DStudio::DocumentEditorFileType::DAE type includes
// all supported model types
SAllowedTypesEntry g_AllowedImportTypes[] = {
    { Q3DStudio::DocumentEditorFileType::Presentation, QObject::tr("Presentations"),
      presentationExts },
    { Q3DStudio::DocumentEditorFileType::QmlStream, QObject::tr("Qml streams"), qmlStreamExts },
    { Q3DStudio::DocumentEditorFileType::DAE, QObject::tr("Model Files"), modelExts },
    { Q3DStudio::DocumentEditorFileType::Image, QObject::tr("Image Files"), imgExts },
    { Q3DStudio::DocumentEditorFileType::Behavior, QObject::tr("Behavior Scripts"), behaviorExts },
    { Q3DStudio::DocumentEditorFileType::Effect, QObject::tr("Effect Files"), effectExts },
    { Q3DStudio::DocumentEditorFileType::Font, QObject::tr("Font Files"), fontExts },
    { Q3DStudio::DocumentEditorFileType::Material, QObject::tr("Material Files"), materialExts },
};
int g_NumAllowedImportTypes = sizeof(g_AllowedImportTypes) / sizeof(*g_AllowedImportTypes);

// List of file types allowed for file references
SAllowedTypesEntry g_AllowedFileReferencesTypes[] = {
    { Q3DStudio::DocumentEditorFileType::Image, QObject::tr("Image Files"), imgExts },
    { Q3DStudio::DocumentEditorFileType::Behavior, QObject::tr("Behavior Scripts"), behaviorExts },
    { Q3DStudio::DocumentEditorFileType::Mesh, QObject::tr("Mesh Files"), meshExts },
    { Q3DStudio::DocumentEditorFileType::Import, QObject::tr("Import Files"), importExts },
    { Q3DStudio::DocumentEditorFileType::Effect, QObject::tr("Effect Files"), effectExts },
};
int g_NumAllowedFileReferencesTypes =
        sizeof(g_AllowedFileReferencesTypes) / sizeof(*g_AllowedFileReferencesTypes);
}

/**
 * @param inShowGUI true if dialogs should be displayed or piped to std:cout instead
 */
CDialogs::CDialogs(bool inShowGUI /*= true*/)
    : m_ProgressPalette(nullptr)
    , m_ShowGUI(inShowGUI)
    , m_LastSaveFile(QStringLiteral("./"))
{
    const auto effectExt = effectExtensions();
    const auto fontExt = fontExtensions();
    const auto mapExt = mapExtensions();
    const auto materialExt = materialExtensions();
    const auto modelExt = modelExtensions();
    const auto behaviorExt = behaviorExtensions();
    const auto presentationExt = presentationExtensions();

    for (const auto ext : effectExt)
        m_defaultDirForSuffixMap.insert(ext, QStringLiteral("effects"));
    for (const auto ext : fontExt)
        m_defaultDirForSuffixMap.insert(ext, QStringLiteral("fonts"));
    for (const auto ext : mapExt)
        m_defaultDirForSuffixMap.insert(ext, QStringLiteral("maps"));
    for (const auto ext : materialExt)
        m_defaultDirForSuffixMap.insert(ext, QStringLiteral("materials"));
    for (const auto ext : modelExt)
        m_defaultDirForSuffixMap.insert(ext, QStringLiteral("models"));
    for (const auto ext : behaviorExt)
        m_defaultDirForSuffixMap.insert(ext, QStringLiteral("scripts"));
    for (const auto ext : presentationExt)
        m_defaultDirForSuffixMap.insert(ext, QStringLiteral("presentations"));
}

CDialogs::~CDialogs()
{
}

/**
 * Displays a dialog asking the user to choose the keyframe interpolation.
 *
 * @param ioEaseIn value to be set as the ease in default - passes back the value chosen by the user
 * @param ioEaseOut value to be set as the ease out default - passes back the value chosen by the
 * user
 * @return true if the user clicked OK on the dialog (indicating that the values should be updated
 * on the track)
 */
bool CDialogs::PromptForKeyframeInterpolation(float &ioEaseIn, float &ioEaseOut)
{
    bool theReturnValue = false;

    CInterpolationDlg theInterpolationDialog;
    theInterpolationDialog.setEaseIn(ioEaseIn);
    theInterpolationDialog.setEaseOut(ioEaseOut);

    // If the user presses the OK button
    if (theInterpolationDialog.exec() ==  QDialog::Accepted) {
        // Retrieve the new interpolation values
        ioEaseIn = theInterpolationDialog.easeIn();
        ioEaseOut = theInterpolationDialog.easeOut();
        theReturnValue = true;
    }

    return theReturnValue;
}

/**
 *	Notify the user that the deletion of an asset has failed.
 */
void CDialogs::DisplayAssetDeleteFailed()
{
    QString theMessage = QObject::tr("Studio was unable to save your project data. Please close "
                                     "down Studio and try again.");
    QString theTitle = QObject::tr("General Error");

    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theTitle, theMessage, Qt3DSMessageBox::ICON_ERROR, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << theMessage;
    }
}

// Get the export choice.
Qt3DSFile CDialogs::GetExportChoice(const Q3DStudio::CString &, const Q3DStudio::CString &)
{
    // Need to fix this for windows if we decide to use it
    return Qt3DSFile("", false, false);
}

/**
 *	Notify that we are unable to refresh the resource.
 */
void CDialogs::DisplayRefreshResourceFailed(const QString &inResourceName,
                                            const QString &inDescription)
{
    QString theTitle = QObject::tr("Refresh File Error");
    QString theText = QObject::tr("Studio was unable to refresh the resource '%1'.\n")
            .arg(inResourceName);

    if (!inDescription.isEmpty())
        theText += inDescription;

    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theTitle, theText, Qt3DSMessageBox::ICON_WARNING, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << theText;
    }
}

/**
 *	Notify the user that the loading of the requested resource failed.
 *
 *  @param inURL				the URL for the asset that was to have been imported
 *	@param inDescription        description for the failure, if any
 *	@param inWarningsOnly		not a failure, just warnings
 */
void CDialogs::DisplayImportFailed(const QUrl &inURL, const QString &inDescription,
                                   bool inWarningsOnly)
{
    // Notify the user we couldn't load the resource.
    QString theTitle;
    QString theText;
    QString theMsgText;

    theTitle = QObject::tr("Studio Import Resource ");
    theTitle += !inWarningsOnly ? QObject::tr("Error") : QObject::tr("Warning");

    // Determine the asset type
    EStudioObjectType theAssetType =
            Q3DStudio::ImportUtils::GetObjectFileTypeForFile(inURL.path(), false)
            .m_ObjectType;

    bool theIsStudioObject = theAssetType != OBJTYPE_UNKNOWN;

    // Is this a behavior file, but perhaps incorrectly formatted?
    if (theAssetType == OBJTYPE_BEHAVIOR) {
        // Load the message about the behavior format
        if (inWarningsOnly) {
            theText = QObject::tr("Warnings were detected during import of the behavior script."
                                  "\nPlease check the file.\n");
        } else {
            theText = QObject::tr("Studio was unable to import the behavior script.\nPlease check "
                                  "the file and try again.\nNote that behavior files must be "
                                  "syntactically correct to be importable.");
        }
        if (!inDescription.isEmpty())
            theText += QStringLiteral("\n") + inDescription;
    } else if (theAssetType != OBJTYPE_UNKNOWN || theIsStudioObject) {
        // Valid registered file type, but invalid file

        bool theNoDescription = inDescription.isEmpty();
        // Load default text stating that the import resource failed.
        // descriptions if present are presented as "reasons" for failure.
        if (!inWarningsOnly || theNoDescription) {
            theText = QObject::tr("Studio was unable to import the resource%1")
                    .arg(theNoDescription ? QStringLiteral(".\n")
                                          : QObject::tr(" due to the following reason(s):\n"));
        }
        if (!theNoDescription)
            theText += inDescription;
        else
            theText += QObject::tr("Please check the above file and try again.");
    } else {
        // Display the warning messsage if we have one
        // instead of a meaningless message. This provides more feed back
        if (!inDescription.isEmpty()) {
            theText += inDescription;
        } else {
            theText = QObject::tr("Studio was unable to import the resource file.\nThis "
                                  "resource file type is not currently supported.\n");
        }
    }

    theMsgText = !inWarningsOnly ? QObject::tr("Import resource failed:")
                                 : QObject::tr("Import resource succeeded with warning(s):");
    theMsgText += QStringLiteral("\n%1\n\n").arg(inURL.toDisplayString()) + theText;

    // Display the failed import resource message.
    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theTitle, theMsgText, Qt3DSMessageBox::ICON_WARNING, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << theMsgText;
    }
}

// Inform user that UIP file contained datainput bindings for datainput names not found
// from UIA file. Returns true if user wants to delete invalid datainput bindings
// automatically
bool CDialogs::DisplayUndefinedDatainputDlg(
        const QMultiMap<QString,
                        QPair<qt3dsdm::Qt3DSDMInstanceHandle,
                              qt3dsdm::Qt3DSDMPropertyHandle>> *map)
{
    const auto keys = map->uniqueKeys();
    QString theTitle = QObject::tr("Missing Data Input");
    QLabel *theText = new QLabel;
    int keysSize = keys.size();
    if (keysSize > 1) {
        theText->setText(QObject::tr("Could not find Data Inputs. "
                                     "%1 Data Inputs used as controllers are undefined.")
                         .arg(keysSize));
    } else {
        theText->setText(QObject::tr("Could not find Data Input. "
                                     "%1 Data Input used as controller is undefined.")
                         .arg(keysSize));
    }

    QString theSmallText;
    for (auto it : keys)
        theSmallText.append(QStringLiteral("\n") + it);

    theSmallText.append(QStringLiteral("\n"));

    QLabel *diList = new QLabel(theSmallText);

    QLabel *noUndoText = new QLabel;
    noUndoText->setText(QObject::tr("Clear cannot be undone.\n"));
    theText->setIndent(10);
    diList->setIndent(20);
    noUndoText->setIndent(10);


    QIcon warn(QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning));
    QLabel *warnLab = new QLabel();
    const int size = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
    warnLab->setPixmap(warn.pixmap(QSize(size, size)));

    QDialog msgBox(g_StudioApp.m_pMainWnd, Qt::WindowCloseButtonHint | Qt::WindowTitleHint
                   | Qt::MSWindowsFixedSizeDialogHint);
    QGridLayout *layout = new QGridLayout();

    layout->addWidget(warnLab, 1, 1);
    layout->addWidget(theText, 1, 2);

    QPushButton *ok = new QPushButton(QObject::tr("Clear invalid controllers now"));
    QPushButton *cancel = new QPushButton(QObject::tr("I'll fix controllers manually"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(ok, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancel, QDialogButtonBox::RejectRole);
    layout->addWidget(buttonBox, 4, 1, 1, 3, Qt::AlignCenter);
    layout->addWidget(diList, 2, 2, Qt::AlignVCenter);
    layout->addWidget(noUndoText, 3, 2);

    msgBox.setLayout(layout);

    msgBox.setWindowTitle(theTitle);

    QObject::connect(buttonBox, &QDialogButtonBox::accepted, &msgBox, &QDialog::accept);
    QObject::connect(buttonBox, &QDialogButtonBox::rejected, &msgBox, &QDialog::reject);

    auto res = msgBox.exec();

    return res == QDialog::Accepted ? true : false;
}

QString CDialogs::ConfirmRefreshModelFile(const QString &inFile)
{
    // this produces an extension string which contains all allowed formats specified in
    // g_AllowedImportTypes
    // currently DAE and FBX
    QString initialFilter;
    QString theFileFilter =
            CreateAllowedTypesString(Q3DStudio::DocumentEditorFileType::DAE, initialFilter,
                                     true, true);

    return QFileDialog::getOpenFileName(g_StudioApp.m_pMainWnd, QObject::tr("Open"),
                                        inFile, theFileFilter, nullptr);
}

QList<QUrl> CDialogs::SelectAssets(QString &outPath,
                                   Q3DStudio::DocumentEditorFileType::Enum assetType)
{
    QFileDialog fd(g_StudioApp.m_pMainWnd);
    fd.setDirectory(outPath);
    fd.setFileMode(QFileDialog::ExistingFiles);
    QString initialFilter;
    fd.setNameFilter(CreateAllowedTypesString(
                         assetType, initialFilter, true,
                         assetType != Q3DStudio::DocumentEditorFileType::Unknown));
    fd.selectNameFilter(initialFilter);
    fd.setWindowTitle(QObject::tr("Import Assets"));

    QList<QUrl> files;
    if (fd.exec()) {
        files = fd.selectedUrls();
        QString newOutPath = fd.directory().absolutePath();
        QString contentPath = QDir::fromNativeSeparators(
                    Qt3DSFile::GetApplicationDirectory() + QStringLiteral("/Content"));

        if (assetType != Q3DStudio::DocumentEditorFileType::Unknown
                || (assetType == Q3DStudio::DocumentEditorFileType::Unknown
                    && !newOutPath.startsWith(contentPath))) {
            // Return the new path if we are browsing a specific asset type, or we are browsing
            // outside the Content folder.
            outPath = newOutPath;
        }
    }

    return files;
}

QString CDialogs::defaultDirForUrl(const QUrl &url)
{
    QString defaultDir;
    if (!url.isLocalFile())
        return defaultDir;

    const QFileInfo fi(url.toLocalFile());
    const QString suffix = fi.suffix();

    defaultDir = m_defaultDirForSuffixMap.value(suffix.toLower());

    return defaultDir;
}

/**
 * Notify the user that the presentation we tried to load has failed.
 * @param loadFileInfo QFileInfo for the failing file
 * @param errrorText error message
 */
void CDialogs::DisplayLoadingPresentationFailed(const QFileInfo &loadFileInfo,
                                                const QString &loadFileName,
                                                const QString &errorText)
{
    QString theErrorMessage = loadFileInfo.isFile() ? loadFileInfo.fileName() : loadFileName;

    if (errorText.isEmpty())
        theErrorMessage +=  QObject::tr(" failed to load.");
    else
        theErrorMessage += errorText;

    QString theErrorTitle = QObject::tr("Open File Error");

    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theErrorTitle, theErrorMessage, Qt3DSMessageBox::ICON_WARNING, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theErrorTitle << ": " << theErrorMessage;
    }
}

/**
 *	Notify the user that the presentation we tried to save has failed.
 *
 *	@param	inSavedLocation	The AKFile that we failed to save.
 */
void CDialogs::DisplaySavingPresentationFailed()
{
    QString theErrorMessage = QObject::tr("Unable to save presentation. Please ensure that the "
                                          "file is not set as read-only.");
    QString theErrorTitle = QObject::tr("Qt 3D Studio");

    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theErrorTitle, theErrorMessage, Qt3DSMessageBox::ICON_WARNING, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theErrorTitle << ": " << theErrorMessage;
    }
}

/**
 *	Display a message box to indicate failure to overwrite a read-only file
 *
 *	@param	inSavedLocation
 *			the file location to be saved
 *
 *	@return	void
 */
void CDialogs::DisplaySaveReadOnlyFailed(const QString &inSavedLocation)
{
    QString theMsg = QObject::tr("Studio cannot save the file '%1'. The file is marked Read-Only."
                                 "\nSave the file with another file name or to a different "
                                 "location.").arg(inSavedLocation);
    QString theTitle = QObject::tr("Qt 3D Studio");

    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theTitle, theMsg, Qt3DSMessageBox::ICON_WARNING, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << theMsg;
    }
}

void CDialogs::DisplayObjectRenamed(const QString &origName, const QString &newName, bool async)
{
    QString title = QObject::tr("Warning");
    QString message = QObject::tr("Object %1 was renamed to %2 because "
                                  "original name was duplicated "
                                  "under its parent.").arg(origName).arg(newName);
    if (async) {
        QTimer::singleShot(0, [this, title, message]() {
            DisplayMessageBox(title, message, Qt3DSMessageBox::ICON_WARNING, false);
        });
    } else {
        DisplayMessageBox(title, message, Qt3DSMessageBox::ICON_WARNING, false);
    }
}

/**
 *	Displays a Qt3DSMessageBox using the specified parameters.  The message box
 *	is modal to the main frame.  This provides an easy way to place modal dialogs
 * 	to the user, without requiring your class to know about the main frame or
 *	window refs.
 *	@param inTitle Title of the message box (not used on Mac)
 *	@param inText Text of the message
 *	@param inIcon Icon to be displayed next to the text
 *	@param inShowCancel true to show a Cancel button, false only show an OK button
 *	@return Indication of which button was pressed to dismiss the dialog
 */
Qt3DSMessageBox::EMessageBoxReturn
CDialogs::DisplayMessageBox(const QString &inTitle, const QString &inText,
                            Qt3DSMessageBox::EMessageBoxIcon inIcon, bool inShowCancel,
                            QWidget *parent)
{
    Qt3DSMessageBox::EMessageBoxReturn theUserChoice;

    if (m_ShowGUI) {
        if (parent == nullptr)
            parent = g_StudioApp.m_pMainWnd;
        theUserChoice =
                Qt3DSMessageBox::Show(inTitle, inText, inIcon,
                                      inShowCancel, parent);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << inTitle << ": " << inText;
        theUserChoice = Qt3DSMessageBox::MSGBX_OK;
    }

    return theUserChoice;
}

void CDialogs::asyncDisplayMessageBox(const QString &title, const QString &text,
                                      Qt3DSMessageBox::EMessageBoxIcon icon, QWidget *parent)
{
    if (m_ShowGUI) {
        if (parent == nullptr)
            parent = g_StudioApp.m_pMainWnd;
        QTimer::singleShot(0, [title, text, icon, parent]() {
            Qt3DSMessageBox::Show(title, text, icon, false, parent);
        });
    } else {
        qCDebug(qt3ds::TRACE_INFO) << title << ": " << text;
    }
}

int CDialogs::DisplayChoiceBox(const QString &inTitle, const QString &inText, int inIcon)
{
    if (m_ShowGUI) {
        QMessageBox box;
        box.setWindowTitle(inTitle);
        box.setText(inText);
        switch (inIcon) {
        case Qt3DSMessageBox::ICON_WARNING:
            box.setIcon(QMessageBox::Warning);
            break;
        case Qt3DSMessageBox::ICON_ERROR:
            box.setIcon(QMessageBox::Critical);
            break;
        case Qt3DSMessageBox::ICON_INFO:
            box.setIcon(QMessageBox::Information);
            break;
        default:
            break;
        }
        box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        switch (box.exec()) {
        case QMessageBox::Yes:
            return IDYES;
        case QMessageBox::No:
            return IDNO;
        default:
            Q_UNREACHABLE();
        }
    } else {
        qCDebug(qt3ds::TRACE_INFO) << inTitle << ": " << inText;
        return IDYES;
    }
}

/**
 * Display a box to choose whether to override or skip an existing asset during import
 *
 * @return  user choice (Yes, No, YesToAll, NoToAll)
 */
int CDialogs::displayOverrideAssetBox(const QString &assetPath)
{
    if (m_ShowGUI) {
        QMessageBox box;
        box.setWindowTitle(QObject::tr("Asset Exists"));
        box.setText(QObject::tr("The following asset already exists, do you want to override it?"));
        box.setInformativeText(assetPath);
        box.setIcon(QMessageBox::Warning);
        box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        box.setButtonText(QMessageBox::Yes, QObject::tr("Override"));
        box.setButtonText(QMessageBox::No, QObject::tr("Skip"));
        box.setDefaultButton(QMessageBox::No);
        box.setCheckBox(new QCheckBox(QObject::tr("Do this action for all and don't ask again."),
                                      &box));
        int choice = box.exec();
        if (box.checkBox()->isChecked())
            choice = choice == QMessageBox::Yes ? QMessageBox::YesToAll : QMessageBox::NoToAll;
        return choice;
    } else {
        qCDebug(qt3ds::TRACE_INFO) << "Asset Override: " << assetPath;
        return QMessageBox::No;
    }
}

const char *CDialogs::GetDAEFileExtension()
{
    return "dae";
}

const char *CDialogs::GetFbxFileExtension()
{
    return "fbx";
}

// Null terminated list
const char **CDialogs::GetImgFileExtensions()
{
    return imgExts;
}

const char *CDialogs::GetImportFileExtension()
{
    return "import";
}

const char *CDialogs::GetMeshFileExtension()
{
    return "mesh";
}

const char *CDialogs::GetQmlFileExtension()
{
    return "qml";
}

const char *CDialogs::GetMaterialDataFileExtension()
{
    return "materialdef";
}

const char **CDialogs::GetFontFileExtensions()
{
    return fontExts;
}

const char **CDialogs::GetEffectFileExtensions()
{
    return effectExts;
}

const char **CDialogs::GetMaterialFileExtensions()
{
    return materialExts;
}
const char **CDialogs::GetSoundFileExtensions()
{
    return soundExts;
}

bool IsFileExtension(const char *inExt, const char **inExts)
{
    if (inExt == nullptr)
        return false;
    for (const char **ext = inExts; *ext != nullptr; ++ext) {
        if (QString::compare(inExt, *ext, Qt::CaseInsensitive) == 0)
            return true;
    }
    return false;
}

bool CDialogs::IsImageFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, imgExts);
}

bool CDialogs::IsFontFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, fontExts);
}

bool CDialogs::IsEffectFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, effectExts);
}

bool CDialogs::IsMaterialFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, materialExts);
}

bool CDialogs::IsSoundFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, soundExts);
}

bool CDialogs::isMeshFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, meshExts);
}

bool CDialogs::isImportFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, importExts);
}

bool CDialogs::isPresentationFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, presentationExts);
}

bool CDialogs::isProjectFileExtension(const char *inExt)
{
    return IsFileExtension(inExt, projectExts);
}

const wchar_t **CDialogs::GetWideImgFileExtensions()
{
    return wideImgExts;
}

const wchar_t *CDialogs::GetWideDAEFileExtension()
{
    return L"dae";
}

const wchar_t *CDialogs::GetWideFbxFileExtension()
{
    return L"fbx";
}

const wchar_t *CDialogs::GetWideImportFileExtension()
{
    return L"import";
}

const wchar_t *CDialogs::GetWideMeshFileExtension()
{
    return L"mesh";
}

const wchar_t **CDialogs::GetWideFontFileExtensions()
{
    return wideFontExts;
}

const wchar_t **CDialogs::GetWideEffectFileExtensions()
{
    return wideEffectExts;
}

const wchar_t **CDialogs::GetWideMaterialFileExtensions()
{
    return wideMaterialExts;
}

bool IsFileExtension(const wchar_t *inExt, const wchar_t **inExts)
{
    if (inExt == nullptr)
        return false;
    for (const wchar_t **ext = inExts; *ext != nullptr; ++ext) {
        if (QString::compare(QString::fromWCharArray(inExt),
                             QString::fromWCharArray(*ext), Qt::CaseInsensitive) == 0) {
            return true;
        }
    }
    return false;
}

bool CDialogs::IsImageFileExtension(const wchar_t *inExt)
{
    return IsFileExtension(inExt, wideImgExts);
}

bool CDialogs::IsFontFileExtension(const wchar_t *inExt)
{
    return IsFileExtension(inExt, wideFontExts);
}

bool CDialogs::IsEffectFileExtension(const wchar_t *inExt)
{
    return IsFileExtension(inExt, wideEffectExts);
}

bool CDialogs::IsMaterialFileExtension(const wchar_t *inExt)
{
    return IsFileExtension(inExt, wideMaterialExts);
}

bool CDialogs::IsPathFileExtension(const wchar_t *inExt)
{
    return QString::compare(QString::fromWCharArray(inExt), "svg", Qt::CaseInsensitive) == 0;
}

bool CDialogs::IsPathBufferExtension(const wchar_t *inExt)
{
    return QString::compare(QString::fromWCharArray(inExt), "path", Qt::CaseInsensitive) == 0;
}

bool CDialogs::IsSoundFileExtension(const wchar_t *inExt)
{
    return IsFileExtension(inExt, wideSoundExts);
}

bool CDialogs::isPresentationFileExtension(const wchar_t *inExt)
{
    return IsFileExtension(inExt, widePresentationExts);
}

bool CDialogs::isProjectFileExtension(const wchar_t *inExt)
{
    return IsFileExtension(inExt, wideProjectExts);
}

/**
 *  CreateAllowedTypesString: Creates the string used to determine allowable types
 *  for import or for filereferences
 *  @return the string that dynamically created with the extensions supported.
 */
QString CDialogs::CreateAllowedTypesString(Q3DStudio::DocumentEditorFileType::Enum fileTypeFilter,
                                           QString &outInitialFilter, bool forImport,
                                           bool exclusive)
{
    QString theReturnString;
    QString combinedFilter;
    int theCount = forImport ? g_NumAllowedImportTypes : g_NumAllowedFileReferencesTypes;
    for (int idx = 0; idx < theCount; ++idx) {
        const SAllowedTypesEntry &entry =
                forImport ? g_AllowedImportTypes[idx] : g_AllowedFileReferencesTypes[idx];
        if (!exclusive || fileTypeFilter == entry.m_FileType) {
            QString theTypeString(entry.m_ResourceString);
            QString theExtensions(CreateExtensionsList(entry.m_FileExtensions).toQString());
            const QString filterString = theTypeString + " (" + theExtensions + ");;";
            theReturnString += filterString;
            if (exclusive)
                outInitialFilter = filterString;
            else
                combinedFilter += theExtensions + " ";
        }
    }
    if (!combinedFilter.isEmpty()) {
        combinedFilter.chop(1); // Remove last separator
        theReturnString.prepend(QObject::tr("All Supported Asset types")
                                + " (" + combinedFilter + ");;");
        outInitialFilter = QObject::tr("All Supported Asset types")
                + " (" + combinedFilter + ")";
    }
    theReturnString.chop(2);
    if (exclusive)
        outInitialFilter.chop(2);

    return theReturnString;
}

/**
 *	Display a error dialog box with the given text string that describes the error.
 */
void CDialogs::DisplayKnownErrorDialog(const QString &inErrorText)
{
    // make sure this is valid
    if (!inErrorText.isEmpty()) {
        QString theTitle = QObject::tr("Qt 3D Studio");
        if (m_ShowGUI) {
            Qt3DSMessageBox::Show(theTitle, inErrorText, Qt3DSMessageBox::ICON_ERROR,
                                  false, g_StudioApp.m_pMainWnd);
        } else {
            qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << inErrorText;
        }
    }
}

/**
 *	Prompt the user to save the document before losing their changes.
 *	This is used when closing, loading or newing up a document when the current
 *	one has modifications.
 *	@return	the user's choice.
 */
CDialogs::ESavePromptResult CDialogs::PromptForSave()
{
    QString theDocTitle;

    Qt3DSFile theCurrentDoc = g_StudioApp.GetCore()->GetDoc()->GetDocumentPath();
    if (theCurrentDoc.IsFile())
        theDocTitle = theCurrentDoc.GetName().toQString();
    else // if the current doc has not been saved then use the default title.
        theDocTitle = QObject::tr("Untitled");

    QString thePrompt = QObject::tr("Save changes to %1?").arg(theDocTitle);

    int theChoice = QMessageBox::warning(nullptr, QObject::tr("Qt 3D Studio"),
                                         thePrompt, QMessageBox::Yes | QMessageBox::No
                                         | QMessageBox::Cancel);

    ESavePromptResult theResult = CANCEL_OPERATION;

    switch (theChoice) {
    case QMessageBox::Yes:
        theResult = SAVE_FIRST;
        break;
    case QMessageBox::No:
        theResult = CONTINUE_NO_SAVE;
        break;
    case QMessageBox::Cancel:
        theResult = CANCEL_OPERATION;
        break;
    default:
        break;
    }

    return theResult;
}

/**
 * Prompt the user for a file to save to.
 * If browsing for location for presentation (isProject == false), then
 * allow browsing only inside current project.
 * If browsing for a location for a project, then disallow browsing inside current project.
 * If isCopy is true, then create a copy of the existing project rather than a new one.
 * isCopy is ignored if isProject is false;
 * Return an invalid file if the user cancels the save dialog.
 */
QString CDialogs::GetSaveAsChoice(const QString &inDialogTitle, bool isProject, bool isCopy)
{
    QString projPath(QDir::cleanPath(g_StudioApp.GetCore()->getProjectFile().getProjectPath()));
    QString previousFolder;
    QString theFilename = g_StudioApp.GetCore()->GetDoc()->GetDocumentPath();

    if (theFilename.isEmpty() || isProject)
        theFilename = QObject::tr("Untitled");

    QString theFileExt = QStringLiteral(".uip");

    QFileDialog theFileDlg;
    theFileDlg.setOption(QFileDialog::DontConfirmOverwrite);

    const QFileInfo fi(m_LastSaveFile);
    // TODO: introduce a workspace concept?
    theFileDlg.setDirectory(
                fi.path() == QLatin1String(".")
                ? QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
                : fi.path());
    theFileDlg.setAcceptMode(QFileDialog::AcceptSave);
    theFileDlg.setDefaultSuffix(theFileExt);
    if (!inDialogTitle.isEmpty())
        theFileDlg.setWindowTitle(inDialogTitle);

    if (isProject) {
        theFileDlg.setLabelText(QFileDialog::FileName, QObject::tr("Project Name"));
        if (!isCopy)
            theFileDlg.setLabelText(QFileDialog::Accept, QObject::tr("Create"));
        // Limit browsing to outside project directory
        if (!projPath.isEmpty()) {
            QDir projDir(projPath);
            projDir.cdUp();
            previousFolder = projDir.absolutePath();
            if (isCopy)
                theFileDlg.setDirectory(previousFolder);
            connect(&theFileDlg, &QFileDialog::directoryEntered,
                    [&](const QString &dir) {
                QFileInfo fi(QDir::cleanPath(dir));
                const QString absPath = fi.absoluteFilePath();
                if (absPath.startsWith(projPath))
                    theFileDlg.setDirectory(previousFolder);
                else
                    previousFolder = absPath;
            });
        }
    } else {
        // Limit browsing to project directory
        connect(&theFileDlg, &QFileDialog::directoryEntered,
                [&theFileDlg, &projPath](const QString &dir) {
            QFileInfo fi(QDir::cleanPath(dir));
            const QString absPath = fi.absoluteFilePath();
            if (!absPath.startsWith(projPath))
                theFileDlg.setDirectory(projPath);
        });
        // Note that since we are using native file dialog, we cannot change the sidebar or
        // "look in" combo contents of the file dialog, which may cause bit of confusion.
    }

    bool theShowDialog = true;
    QString theFile;
    while (theShowDialog && theFileDlg.exec()) {
        theShowDialog = false;
        QString selectedName = theFileDlg.selectedFiles().front();

        // Make sure file name has correct extension
        if (isProject && isCopy && selectedName.endsWith(theFileExt))
            selectedName.chop(theFileExt.length());
        else if (!selectedName.endsWith(theFileExt))
            selectedName.append(theFileExt);

        if (!isProject) {
            // If user somehow manages to select a file path outside project directory, save to
            // default presentations directory
            QFileInfo fi(QDir::cleanPath(selectedName));
            const QString absPath = fi.absoluteFilePath();
            if (!absPath.startsWith(projPath))
                selectedName = projPath + QStringLiteral("/presentations/") + fi.fileName();
        }

        theFile = selectedName;
        m_LastSaveFile = selectedName;
        // New directory is only created when creating a new project.
        if (isProject) {
            Q3DStudio::CFilePath theFinalDir;
            Q3DStudio::CFilePath theFinalDoc;
            g_StudioApp.GetCore()->GetCreateDirectoryFileName(selectedName,
                                                              theFinalDir, theFinalDoc);

            // Update last save file to final doc
            m_LastSaveFile = theFinalDoc.absoluteFilePath();

            // File dialog shouldn't allow choosing existing folder for project, but since we
            // are using native dialog, let's play it safe and check.
            if (theFinalDir.Exists()) {
                const QString title(QObject::tr("Existing directory"));
                const QString warning = QObject::tr("%1 already exists.")
                        .arg(theFinalDir.GetFileName().toQString());
                QMessageBox::warning(nullptr, title, warning);
                // Reset the file and show the file dialog again
                theFile.clear();
                theShowDialog = true;
                continue;
            }
        }
    }

    return theFile;
}

QString CDialogs::getImportVariantsDlg()
{
    QString docDir = QFileInfo(g_StudioApp.GetCore()->GetDoc()->GetDocumentPath()).absolutePath();

    QFileDialog dlg;
    dlg.setDirectory(docDir);
    dlg.setWindowTitle(tr("Import variants"));
    dlg.setDefaultSuffix(QStringLiteral(".variants"));
    dlg.setNameFilters({tr("All supported files (*.variants *.uia)"),
                        tr("Variants files (*.variants)"), tr("Project files (*.uia)")});
    auto result = dlg.exec();

    if (result == QDialog::Accepted && !dlg.selectedFiles().empty())
        return dlg.selectedFiles().front();

    return {};
}

QString CDialogs::getExportVariantsDlg()
{
    QString docDir = QFileInfo(g_StudioApp.GetCore()->GetDoc()->GetDocumentPath()).absolutePath();

    QFileDialog dlg;
    dlg.setDirectory(docDir);
    dlg.setAcceptMode(QFileDialog::AcceptSave);
    dlg.setWindowTitle(tr("Export variants"));
    dlg.setDefaultSuffix(QStringLiteral(".variants"));
    dlg.setNameFilters({QObject::tr("Variants files (*.variants)")});
    dlg.exec();

    if (!dlg.selectedFiles().empty())
        return dlg.selectedFiles().front();

    return {};
}

/**
 * Prompt the user for a file to create.
 * @param  isProject   true: new project, false: new presentation
 * @return an invalid file if the user cancels the save dialog.
 */
QString CDialogs::GetNewDocumentChoice(const QString &inInitialDirectory, bool isProject)
{
    if (inInitialDirectory.size())
        m_LastSaveFile = inInitialDirectory + QStringLiteral("/");
    QString title = isProject ? QObject::tr("Create New Project")
                              : QObject::tr("Create New Presentation");
    return GetSaveAsChoice(title, isProject);
}

/**
 * Prompt the user for a file to open.
 * This will return an invalid file if the user cancels the save dialog.
 */
QString CDialogs::GetFileOpenChoice(const QString &inInitialDirectory)
{
    QFileInfo theFile;
    QString theImportFilter = QObject::tr("Studio UI Presentation (*.uip *.uia)");

    QFileDialog theFileDlg(g_StudioApp.m_pMainWnd, QString(),
                           (inInitialDirectory == QLatin1String("."))
                           ? QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
                           : inInitialDirectory,
                           theImportFilter);
    theFileDlg.setAcceptMode(QFileDialog::AcceptOpen);

    if (theFileDlg.exec() == QDialog::Accepted) {
        theFile.setFile(theFileDlg.selectedFiles().first());
        m_LastSaveFile = theFile.absoluteFilePath();
    }

    return theFile.absoluteFilePath();
}

/**
 *	Prompt the user to make sure they want to revert the current project.
 *	@return true if they do want to continue with the revert.
 */
bool CDialogs::ConfirmRevert()
{
    bool theConfirmation = false;
    QString thePrompt = QObject::tr("All changes that have been made to your project since your "
                                    "last save will be lost.\n\nDo you want to continue?");
    QString theTitle = QObject::tr("Qt 3D Studio");

    Qt3DSMessageBox::EMessageBoxReturn theChoice;

    if (m_ShowGUI) {
        theChoice = Qt3DSMessageBox::Show(theTitle, thePrompt, Qt3DSMessageBox::ICON_WARNING, true,
                                          g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << thePrompt;
        theChoice = Qt3DSMessageBox::MSGBX_OK;
    }

    // user decided to go ahead and delete all unused resources
    if (theChoice == Qt3DSMessageBox::MSGBX_OK)
        theConfirmation = true;

    return theConfirmation;
}

/**
 * Displays a progress screen, if there is not one aleady being shown.  The
 * progress screen doesn't get dismissed until you call
 * CDialogs::DestroyProgressScreen().
 * @param inActionText text to be displayed as the action
 * @param inAdditionalText additional text, for example a file name
 */
void CDialogs::DisplayProgressScreen(const QString &inActionText,
                                     const QString &inAdditionalText)
{
    if (m_ShowGUI && !m_ProgressPalette) {
        m_ProgressPalette = new CProgressView(g_StudioApp.m_pMainWnd);
        m_ProgressPalette->SetActionText(inActionText);
        m_ProgressPalette->SetAdditionalText(inAdditionalText);
        m_ProgressPalette->show();
        qApp->processEvents();
    }
}

/**
 * If a loading screen is currently being shown, this function destroys it.  You
 * can show the loading screen again with another call to
 * CDialogs::DisplayLoadingScreen().
 */
void CDialogs::DestroyProgressScreen()
{
    if (m_ShowGUI && m_ProgressPalette) {
        delete m_ProgressPalette;
        m_ProgressPalette = nullptr;
    }
}

/**
 *	Inform the user that the environment variables entered does not match the format
 *	expected, listing down all those settings that are wrong.
 *	@param	inErrorMessage the listing of all those errors.
 */
void CDialogs::DisplayEnvironmentVariablesError(const Q3DStudio::CString &inErrorMessage)
{
    QString theTitle = QObject::tr("Unable to accept all Environment Variables");
    QString theMessage = QObject::tr("The following variables will not be saved:\n")
            + inErrorMessage.toQString()
            + QObject::tr("\nVariables must be listed in the following format:"
                          "\n{Variable} = Value\n");
    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theTitle, theMessage, Qt3DSMessageBox::ICON_ERROR, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << theMessage;
    }
}

/**
 *	Reset settings.
 *	Typically inCurrentDocPath is only set when Studio is first launched.
 *	@param inCurrentDocPath	the current document path, if any. Application directory if
 *there is none.
 */
void CDialogs::ResetSettings(const QString &inCurrentDocPath)
{
    // Initialize the default dir/paths to the current document path if specified, otherwise leave
    // everything as it is.
    if (!inCurrentDocPath.isEmpty())
        m_LastSaveFile = inCurrentDocPath;
}

bool CDialogs::DisplayResetKeyframeValuesDlg()
{
    CResetKeyframeValuesDlg theDialog;
    return theDialog.exec() == QDialog::Accepted;
}

/**
 *	User trying to do a pathological paste, such as pasting a component copied from a different
 *instance
 *	of Studio into an instance of the same component that already exists in the current instance
 *of Studio, and
 *	is potentially replaced and deleted.
 */
void CDialogs::DisplayPasteFailed()
{
    QString theTitle = QObject::tr("Paste Error");
    QString theMessage = QObject::tr("Sorry, the attempted paste operation cannot be completed,"
                                     " the destination is invalid.");

    if (m_ShowGUI) {
        Qt3DSMessageBox::Show(theTitle, theMessage, Qt3DSMessageBox::ICON_ERROR, false,
                              g_StudioApp.m_pMainWnd);
    } else {
        qCDebug(qt3ds::TRACE_INFO) << theTitle << ": " << theMessage;
    }
}

/**
 *	Video card OpenGL version is too low to be supported.
 */
void CDialogs::DisplayGLVersionError(const Q3DStudio::CString &inGLVersion,
                                     const Q3DStudio::CString &inMinVersion)
{
    DisplayGLVersionDialog(inGLVersion, inMinVersion, true);
}

/**
 *	Video card OpenGL version is outdated, but could be usable.
 */
void CDialogs::DisplayGLVersionWarning(const Q3DStudio::CString &inGLVersion,
                                       const Q3DStudio::CString &inRecommendedVersion)
{
    DisplayGLVersionDialog(inGLVersion, inRecommendedVersion, false);
}

void CDialogs::asyncDisplayTimeEditDialog(long time, IDoc *doc, long objectAssociation,
                                          KeyframeManager *keyframesManager) const
{
    QTimer::singleShot(0, [time, doc, objectAssociation, keyframesManager]() {
        CTimeEditDlg timeEditDlg(keyframesManager);
        timeEditDlg.showDialog(time, doc, objectAssociation);
    });
}

void CDialogs::asyncDisplayDurationEditDialog(long startTime, long endTime,
                                              ITimeChangeCallback *callback) const
{
    QTimer::singleShot(0, [startTime, endTime, callback]() {
        CDurationEditDlg durationEditDlg;
        durationEditDlg.showDialog(startTime, endTime, callback);
    });
}

void CDialogs::showWidgetBrowser(QWidget *screenWidget, QWidget *browser, const QPoint &point,
                                 WidgetBrowserAlign align, QSize customSize)
{
    QSize popupSize = customSize.isEmpty() ? CStudioPreferences::browserPopupSize() : customSize;
    browser->resize(popupSize);
    QPoint newPos = point;

    // Make sure the popup doesn't go outside the screen
    int screenNum = QApplication::desktop()->screenNumber(screenWidget);
    QScreen *screen = nullptr;

    // If we are somehow not on any screen, just show the browser at upper left corner of the
    // primary screen.
    if (screenNum < 0) {
        screen = QGuiApplication::primaryScreen();
        newPos = QPoint(25, 25) + QPoint(popupSize.width(), popupSize.height());
    } else {
        screen = QGuiApplication::screens().at(screenNum);
    }
    QRect screenRect = screen->availableGeometry();

    const int controlH = CStudioPreferences::controlBaseHeight();
    if (align == WidgetBrowserAlign::ComboBox) {
        // position the popup below the combobox
        newPos -= QPoint(popupSize.width(), -controlH) + screenRect.topLeft();
        // if no space below the combobox, move it above it
        if (newPos.y() + popupSize.height() > screenRect.height())
            newPos.setY(newPos.y() - popupSize.height() - controlH);
    } else if (align == WidgetBrowserAlign::ToolButton) {
        // The point is assumed to be the lower right corner of the button
        newPos -= QPoint(popupSize.width(), popupSize.height()) + screenRect.topLeft();
        if (newPos.y() < 0)
            newPos.setY(newPos.y() + popupSize.height() - controlH);
    } else { // WidgetBrowserAlign::Center
        newPos -= QPoint(popupSize.width() / 2, popupSize.height() / 2) + screenRect.topLeft();
    }

    if (newPos.y() < 0)
        newPos.setY(0);

    if (newPos.x() + popupSize.width() > screenRect.width())
        newPos.setX(screenRect.width() - popupSize.width());
    else if (newPos.x() < 0)
        newPos.setX(0);

    newPos += screenRect.topLeft();

    browser->move(newPos);

    // Show asynchronously to avoid flashing blank window on first show
    QTimer::singleShot(0, screenWidget, [browser, popupSize, screenWidget] {
        browser->show();
        browser->activateWindow();
        browser->setFocus();
        // Make sure we are the desired size after show, as showing on a different screen
        // can cause incorrectly sized popup.
        QTimer::singleShot(0, screenWidget, [browser, popupSize] {
            browser->resize(popupSize);
        });
    });
}

/**
 *	Display the error dialog or warning dialog that OpenGL version is lower than what is
 *expected
 */
void CDialogs::DisplayGLVersionDialog(const Q3DStudio::CString &inGLVersion,
                                      const Q3DStudio::CString &inRecommendedVersion, bool inError)
{
    QString theTitle;
    QString theMessage;

    if (inError) {
        theTitle = QObject::tr("Error");
        theMessage = QObject::tr("OpenGL version %1 is unsupported.\nPlease use a video card and "
                                 "driver that supports at least OpenGL %2 or higher.").arg(
                    inGLVersion.toQString()).arg(inRecommendedVersion.toQString());
    } else {
        theTitle = QObject::tr("Warning");
        theMessage = QObject::tr("OpenGL version %1 detected.\nA video card with an updated driver "
                                 "capable of OpenGL %2 is recommended or there may be rendering "
                                 "errors.").arg(inGLVersion.toQString()).arg(
                    inRecommendedVersion.toQString());
    }

    CGLVersionDlg theGLVersionDlg;
    theGLVersionDlg.Initialize(theTitle, theMessage, inError);
    theGLVersionDlg.exec();

    if (theGLVersionDlg.GetDontShowAgain())
        CStudioPreferences::SetDontShowGLVersionDialog(true);
}

QStringList CDialogs::effectExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : effectExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QStringList CDialogs::fontExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : fontExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QStringList CDialogs::mapExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : imgExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QStringList CDialogs::materialExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : materialExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QStringList CDialogs::modelExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : modelExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QStringList CDialogs::behaviorExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : behaviorExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QStringList CDialogs::presentationExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : presentationExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QStringList CDialogs::qmlStreamExtensions()
{
    static QStringList exts;
    if (exts.isEmpty()) {
        for (const char *ext : qmlStreamExts) {
            if (ext)
                exts << QString::fromLatin1(ext);
        }
    }
    return exts;
}

QColor CDialogs::displayColorDialog(const QColor &color, bool showAlpha) const
{
    QColorDialog theColorDlg;
    theColorDlg.setOption(QColorDialog::DontUseNativeDialog);

    if (showAlpha)
        theColorDlg.setOption(QColorDialog::ShowAlphaChannel);

    theColorDlg.setCurrentColor(color);
    connect(&theColorDlg, &QColorDialog::currentColorChanged, this, &CDialogs::onColorChanged);
    int result = theColorDlg.exec();
    disconnect(&theColorDlg, &QColorDialog::currentColorChanged, this, &CDialogs::onColorChanged);
    if (result == QDialog::Accepted)
        return theColorDlg.selectedColor();
    else
        return color;
}