summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Client/Code/Core/Core/Core.cpp
blob: 61ff4d3b654e7ca9288e90998b9c74854cd06329 (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
/****************************************************************************
**
** Copyright (C) 2008 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 "Qt3DSCommonPrecompile.h"
#include "Core.h"
#include "Doc.h"
#include "Dispatch.h"
#include "HotKeys.h"
#include "StudioProjectSettings.h"
#include "FileOutputStream.h"
#include "FormattedOutputStream.h"
#include "Cmd.h"
#include "StudioConst.h"
#include "StudioPreferences.h"
#include "StudioApp.h"
#include "Views/Views.h"
#include "MainFrm.h"
#include "IStudioRenderer.h"
#include <QtWidgets/qaction.h>

CCore::CCore()
    : m_Doc(NULL)
    , m_Dispatch(new CDispatch())
    , m_CmdStack(new CCmdStack())
    , m_HotKeys(new CHotKeys())
    , m_JustSaved(false)
{
    m_StudioProjectSettings = new CStudioProjectSettings(this);
    m_Dispatch->AddPresentationChangeListener(this);
    m_CmdStack->SetModificationListener(this);
    m_Doc = new CDoc(this);
}

CCore::~CCore()
{
    m_BuildConfigurations.Clear();
    m_CmdStack->Clear();
    delete m_Doc;
    delete m_Dispatch;
    delete m_CmdStack;
    delete m_HotKeys;
    delete m_StudioProjectSettings;
}

void CCore::Initialize()
{
    LoadBuildConfigurations();
}

CDoc *CCore::GetDoc() const
{
    return m_Doc;
}

CDispatch *CCore::GetDispatch()
{
    return m_Dispatch;
}

CCmdStack *CCore::GetCmdStack()
{
    return m_CmdStack;
}

CHotKeys *CCore::GetHotKeys()
{
    return m_HotKeys;
}

CStudioProjectSettings *CCore::GetStudioProjectSettings()
{
    return m_StudioProjectSettings;
}

Q3DStudio::CBuildConfigurations &CCore::GetBuildConfigurations()
{
    return m_BuildConfigurations;
}

/**
 *	Load all the build configurations
 */
bool CCore::LoadBuildConfigurations()
{
    using namespace Q3DStudio;
    // See if we can find the build configurations where they are located first
    QString configDirPath = QDir::cleanPath(Qt3DSFile::GetApplicationDirectory()
                                        + QStringLiteral("/../../../Studio/Build Configurations"));

    if (!QFileInfo(configDirPath).isDir()) {
        configDirPath = QDir::cleanPath(Qt3DSFile::GetApplicationDirectory()
                                    + QStringLiteral("/Build Configurations"));
    }

    Q3DStudio::CBuildConfigParser theParser(m_BuildConfigurations);
    bool theSuccess = theParser.LoadConfigurations(configDirPath);
    if (!theSuccess) {
        m_Dispatch->FireOnBuildConfigurationFileParseFail(theParser.GetErrorMessage());
    } else {
        InitAndValidateBuildConfiguration();
    }

    return theSuccess;
}

/**
 *	If the build configuration saved doesn't exist, use the first one
 *	also verify all the properties required existed, else use the first option available
 */
void CCore::InitAndValidateBuildConfiguration()
{
    Q3DStudio::CBuildConfiguration *theConfig =
            m_BuildConfigurations.GetConfiguration(CStudioPreferences::GetPreviewConfig());
    if (!theConfig) {
        Q3DStudio::CBuildConfigurations::TBuildConfigurations &theConfigurations =
                m_BuildConfigurations.GetConfigurations();
        if (theConfigurations.size()) {
            CStudioPreferences::SetPreviewConfig(theConfigurations.begin()->first);
            theConfig = theConfigurations.begin()->second;
        }
    }
    if (theConfig) {
        Q3DStudio::CBuildConfiguration::TConfigProperties &theConfigProperties =
                theConfig->GetBuildProperties();
        Q3DStudio::CBuildConfiguration::TConfigProperties::iterator theConfigPropIter;
        for (theConfigPropIter = theConfigProperties.begin();
             theConfigPropIter != theConfigProperties.end(); ++theConfigPropIter) {
            const QString &thePropName = theConfigPropIter->GetName();
            QString theStoredValue = CStudioPreferences::GetPreviewProperty(thePropName);
            if (!theConfigPropIter->HasValue(theStoredValue)) {
                // add this property in
                if (theConfigPropIter->GetAcceptableValues().size())
                    CStudioPreferences::SetPreviewProperty(
                                thePropName, theConfigPropIter->GetValue(0).GetName());
            }
        }
    }
}

/**
 * Call by the mainframe to register the keyboard settings.
 * @param inShortcutHandler the handler for the keyboard actions.
 */
void CCore::RegisterGlobalKeyboardShortcuts(CHotKeys *inShortcutHandler, QWidget *actionParent)
{
    ADD_GLOBAL_SHORTCUT(actionParent,
                        QKeySequence(Qt::ControlModifier | Qt::Key_F6),
                        CCore::DumpCommandQueue);
    ADD_GLOBAL_SHORTCUT(actionParent,
                        QKeySequence(Qt::Key_BracketLeft),
                        CCore::SetTimebarStartAffectsChildren);
    ADD_GLOBAL_SHORTCUT(actionParent,
                        QKeySequence(Qt::Key_BracketRight),
                        CCore::SetTimebarEndAffectsChildren);
    ADD_GLOBAL_SHORTCUT(actionParent,
                        QKeySequence(Qt::ControlModifier | Qt::Key_BracketLeft),
                        CCore::SetTimebarStart);
    ADD_GLOBAL_SHORTCUT(actionParent,
                        QKeySequence(Qt::ControlModifier | Qt::Key_BracketRight),
                        CCore::SetTimebarEnd);
}

void CCore::GetCreateDirectoryFileName(const QString &inDocument,
                                       Q3DStudio::CFilePath &outFinalDir,
                                       Q3DStudio::CFilePath &outFinalDoc)
{
    using namespace Q3DStudio;
    CFilePath theOriginal(inDocument);
    CFilePath theName(theOriginal.GetFileName());
    CString theStem(theOriginal.GetFileStem());
    CFilePath theDir = theOriginal.GetDirectory();
    outFinalDir = CFilePath::CombineBaseAndRelative(theDir, theStem);
    outFinalDoc = CFilePath::CombineBaseAndRelative(outFinalDir, theName);
}

ProjectFile &CCore::getProjectFile()
{
    return m_projectFile;
}

/**
 * Call to create a new document.
 * This will clear out the current doc (if there is one) then create a new one.
 *
 * @param inDocument the document absolute path including the file name
 * @param isNewProject create a new project or create presentation under an existing project
 * @param silent don't show the presentation settings
 *
 * @return bool creation successful
 */
bool CCore::OnNewDocument(const QString &inDocument, bool isNewProject, bool silent)
{
    CDispatchDataModelNotificationScope __dispatchScope(*m_Dispatch);

    QFileInfo info(inDocument);
    if (!isNewProject) {
        // If document with given name exists, bail out
        if (info.exists())
            return false;

        // Ensure project file is created before current presentation is closed to make sure the
        // current presentation is added to the project.
        m_projectFile.ensureProjectFile();
    }

    m_Doc->CloseDocument();

    QString theDocument = inDocument;

    if (isNewProject) {
        QDir dir(info.absoluteDir());
        QString projName = info.completeBaseName(); // project name
        dir.mkdir(projName); // create project directory
        dir.cd(projName);    // go inside project directory

        // create asset folders
        dir.mkdir(QStringLiteral("effects"));
        dir.mkdir(QStringLiteral("fonts"));
        dir.mkdir(QStringLiteral("maps"));
        dir.mkdir(QStringLiteral("materials"));
        dir.mkdir(QStringLiteral("models"));
        dir.mkdir(QStringLiteral("presentations"));
        dir.mkdir(QStringLiteral("qml"));
        dir.mkdir(QStringLiteral("scripts"));

        // create the project .uia file
        QString uiaPath = info.absolutePath() + QStringLiteral("/") + projName
                          + QStringLiteral("/") + projName + QStringLiteral(".uia");
        m_projectFile.create(uiaPath);

        // set the default uip file path to be inside the presentations folder
        theDocument = dir.absolutePath() + QStringLiteral("/presentations/") + info.fileName();
    }

    if (!m_Doc->SetDocumentPath(theDocument)) {
        m_Doc->CreateNewDocument(); // Required to prevent a crash, as the old one is already closed
        return false;
    }

    m_Doc->CreateNewDocument();

    // Serialize the new document.
    m_Doc->SaveDocument(theDocument);

    // write a new presentation node to the uia file
    m_projectFile.addPresentationNode(theDocument);
    m_projectFile.updateDocPresentationId();
    m_projectFile.loadVariants();
    m_projectFile.loadSubpresentationsAndDatainputs(g_StudioApp.m_subpresentations,
                                                    g_StudioApp.m_dataInputDialogItems);
    g_StudioApp.getRenderer().RegisterSubpresentations(g_StudioApp.m_subpresentations);

    // show the presentation settings panel
    if (!silent)
        g_StudioApp.GetViews()->getMainFrame()->EditPreferences(PAGE_STUDIOPROJECTSETTINGS);

    return true;
}

/**
 * Call to save the current document.
 * This will do all the prompting, directory stuff necessary and perform the
 * saving of the document.
 */
void CCore::OnSaveDocument(const QString &inDocument, bool inSaveCopy /*= false*/)
{
    m_JustSaved = true;
    GetDispatch()->FireOnSavingPresentation(inDocument);
    bool isSuccess = false;
    try {
        OnSaveDocumentCatcher(inDocument, inSaveCopy);
        m_Dispatch->FireOnSaveDocument(inDocument, true, inSaveCopy);
        isSuccess = true;
    } catch (...) { /** TODO: implement stacktrace*/
    }

    if (!isSuccess)
        m_Dispatch->FireOnSaveDocument(inDocument, false, inSaveCopy);
}

/**
 * Called by OnSaveDocument, to allow the error reporting to be inserted.
 * Because of the nature of the error reporting, OnSaveDocument has to have
 * a certain structure that limits it (C type variables, no object destructors).
 * If we are saving a copy, then set the m_SaveCopy flag to true.  This will
 * leave the document in a dirty state and not update it to point to the new
 * file path.
*/
void CCore::OnSaveDocumentCatcher(const QString &inDocument, bool inSaveCopy /*= false*/)
{
    QFileInfo fileInfo(inDocument);
    m_Dispatch->FireOnProgressBegin(QObject::tr("Saving "), fileInfo.fileName());

    bool theDisplaySaveFailDialog = false;
    bool theFileExists = fileInfo.exists();
    Qt3DSFile theTempFile(fileInfo);

    // Test for readonly files
    if (theFileExists && !fileInfo.isWritable())
        theDisplaySaveFailDialog = true;
    else {
        try {
            // if file already exists, write to a temp file first, to prevent corrupting the
            // original file.
            if (theFileExists) {
                theTempFile = Qt3DSFile::GetTemporaryFile();
                // sanity check: if we fail to get a temporary file
                if (theTempFile.GetAbsolutePosixPath().IsEmpty()) { // too bad, we'll have to use
                    // the original (which might be
                    // the only writeable file)
                    theTempFile = inDocument;
                    theFileExists = false;
                }
            }

            m_Doc->SaveDocument(theTempFile.GetAbsolutePath().toQString());

            // update the original file
            if (theFileExists)
                theTempFile.CopyTo(inDocument);

            // If we are saving normally and not a copy, then we need to update the current document
            // to make sure it points to the saved file and make it not-dirty.  If we are saving a
            // copy
            // then we will leave the document with the original file path dirty state
            if (!inSaveCopy) {
                m_Doc->SetDocumentPath(inDocument);
                m_Doc->SetModifiedFlag(false);
            }
        } catch (CStudioException &) // one of our exceptions, show the standard save error.
        {
            theDisplaySaveFailDialog = true;
        } catch (...) {
            m_Dispatch->FireOnSaveFail(false);
        }
    }
    // clean up
    if (theFileExists)
        theTempFile.DeleteFile();

    if (theDisplaySaveFailDialog) {
        m_Dispatch->FireOnSaveFail(true);
    }

    m_Dispatch->FireOnProgressEnd();
}

void CCore::SetCommandStackModifier(ICmdStackModifier *inModifier)
{
    m_CmdStack->SetCommandStackModifier(inModifier);
}

/**
 * This is used for do/undo/redo and handles the execution of the command.
 * The command must have been new'd up, this will take responsibility for
 * deleting the command.
 * @param inCommand the command to be executed.
 * @return bool true if inCommand was deleted, false otherwise
 * @see CCmdStack::ExecuteCommand.
 */
bool CCore::ExecuteCommand(CCmd *inCommand, bool inIsSynchronous)
{
    if (!inIsSynchronous) {
        m_Dispatch->FireOnAsynchronousCommand(inCommand);
        return false;
    } else
        return m_CmdStack->ExecuteCommand(inCommand);
}

/**
 * Commit the last command to be executed.
 * @see CCmdStack::CommitLastCommand.
 */
void CCore::CommitCurrentCommand()
{
    m_CmdStack->CommitLastCommand();
}

/**
 * Callback from the CommandStack to change whether or not this is modified.
 * This is used when the execution of commands changes the modified state of
 * the project.
 */
void CCore::SetCommandModifiedFlag(bool inIsModified)
{
    if (inIsModified)
        m_Doc->SetModifiedFlag();
}

/**
 * Callback from the CommandStack for the change flags after a command was
 * executed.
 */
void CCore::CommandUpdate(unsigned long /*inFlags*/)
{
}

/**
 * Notification from the dispatch that a new presentation is being created.
 */
void CCore::OnNewPresentation()
{
    m_CmdStack->Clear();
}

/**
 * Notification from the dispatch that the current presentation is closing.
 */
void CCore::OnClosingPresentation()
{
    m_StudioProjectSettings->reset();
    m_CmdStack->Clear();
}

/**
 *	Helper function to remove invalid characters not accepted by windows when
 *	exporting a component.
 *	Invalid characters include: \/:*?"<>|
 */
void CCore::RemoveInvalidCharsInComponentName(Q3DStudio::CString &ioComponentName)
{
    if (ioComponentName.Find('\\') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace("\\", "");

    if (ioComponentName.Find('/') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace("/", "");

    if (ioComponentName.Find(':') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace(":", "");

    if (ioComponentName.Find('*') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace("*", "");

    if (ioComponentName.Find('?') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace("?", "");

    if (ioComponentName.Find('"') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace("\"", "");

    if (ioComponentName.Find('<') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace("<", "");

    if (ioComponentName.Find('>') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace(">", "");

    if (ioComponentName.Find('|') != Q3DStudio::CString::ENDOFSTRING)
        ioComponentName.Replace("|", "");
}

void CCore::DeselectAllKeyframes()
{
    m_Doc->DeselectAllKeyframes();
}

/**
 * Called by hotkeys to set the start time of the currently selected object.
 */
void CCore::SetTimebarStart()
{
    m_Doc->TruncateTimebar(true, false);
}

/**
 * Called by hotkeys to set the end time of the currently selected object.
 */
void CCore::SetTimebarEnd()
{
    m_Doc->TruncateTimebar(false, false);
}

/**
 * Called by hotkeys to set the start time of the currently selected object.
 * Affects children objects
 */
void CCore::SetTimebarStartAffectsChildren()
{
    m_Doc->TruncateTimebar(true, true);
}

/**
 * Called by hotkeys to set the end time of the currently selected object.
 * Affects children objects
 */
void CCore::SetTimebarEndAffectsChildren()
{
    m_Doc->TruncateTimebar(false, true);
}

/**
 * Debugging utility to dump the command queue to a file.
 */
void CCore::DumpCommandQueue()
{
    CFileOutputStream theOutputStream("CommandStack.txt");
    CCmdStack::TCmdList theUndoCommands = m_CmdStack->GetUndoStack();
    CCmdStack::TCmdList::iterator thePos = theUndoCommands.begin();
    for (; thePos != theUndoCommands.end(); ++thePos) {
        Q3DStudio::CString theText = Q3DStudio::CString::fromQString((*thePos)->ToString());
        theOutputStream.Write(theText.GetCharStar(), theText.Length() + 1);
        theOutputStream.Write("\r\n", 2);
    }
}