summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/PreviewHelper.cpp
blob: 271daf4d0e5aaf4b786d4cd23822d825099ce5b6 (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
/****************************************************************************
**
** Copyright (C) 2006 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$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================
#include "stdafx.h"
#include "Strings.h"

//==============================================================================
//	Includes
//==============================================================================
#include "PreviewHelper.h"
#include "StudioApp.h"
#include "Dialogs.h"
#include "Dispatch.h"
#include "Doc.h"
#include "StudioPreferences.h"
#include "StudioProjectSettings.h"
#include "StudioProjectVariables.h"
#include "Core.h"
#include "UICFileTools.h"

#include <QInputDialog>
#include <QMessageBox>
#include <QProcess>

#include "remoteproject.h"

//=============================================================================
/**
 *	PKC : Yes, we're duplicating functionality found in UICStateApplication
 *  but we want to eliminate as many dependencies on UICState as possible in Studio.
 */

Q3DStudio::CString CPreviewHelper::GetLaunchFile(const Q3DStudio::CString &inUipPath)
{
    Q3DStudio::CFilePath theUipPath(inUipPath);

    Q3DStudio::CString theDir = theUipPath.GetDirectory();
    Q3DStudio::CString theStem = theUipPath.GetFileStem();

    // Check if a corresponding .UIA file actually exists
    theDir.append('/');
    Q3DStudio::CString idealPath = theDir + theStem + Q3DStudio::CString(".uia");

    Q3DStudio::CFilePath theUiaPath(idealPath);

    return (theUiaPath.IsFile()) ? idealPath : inUipPath;
}

//=============================================================================
/**
 *	Callback for previewing a presentation.
 */
void CPreviewHelper::OnPreview()
{
    Q3DStudio::CBuildConfigurations &theConfigurations =
        g_StudioApp.GetCore()->GetBuildConfigurations();
    Q3DStudio::CBuildConfiguration *theBuildConfiguration =
        theConfigurations.GetConfiguration(CStudioPreferences::GetPreviewConfig());
    if (theBuildConfiguration)
        PreviewViaConfig(theBuildConfiguration, EXECMODE_PREVIEW);
}

//=============================================================================
/**
 *	Callback for deploying a presentation.
 */
void CPreviewHelper::OnDeploy(RemoteProject &project)
{
    Q3DStudio::CBuildConfigurations &theConfigurations =
        g_StudioApp.GetCore()->GetBuildConfigurations();
    Q3DStudio::CBuildConfiguration *theBuildConfiguration =
        theConfigurations.GetConfiguration(CStudioPreferences::GetPreviewConfig());
    if (theBuildConfiguration) {
        // ItemDataPtr != nullptr ==> Build configurations specified NANT pipeline exporter
        PreviewViaConfig(theBuildConfiguration, EXECMODE_DEPLOY, &project);
    }
}

//=============================================================================
/**
 *	Previewing a presentation using the build configurations loaded.
 *	This involves 2 steps:
 *	1	Export the presentation using the specified exporter.
 *	2	Viewing the exported content following the command specified in the configuration.
 */
void CPreviewHelper::PreviewViaConfig(Q3DStudio::CBuildConfiguration *inSelectedConfig,
                                      EExecMode inMode, RemoteProject *project)
{
    bool theUsingTempFile;
    CUICFile theDocument = GetDocumentFile(theUsingTempFile);
    CCore *theCore = g_StudioApp.GetCore();
    try {
        if (theCore->GetDoc()->IsModified()) {
            theCore->OnSaveDocument(theDocument);
        }

        DoPreviewViaConfig(inSelectedConfig, theDocument.GetAbsolutePath(),
            inMode, project);
    } catch (...) {
        theCore->GetDispatch()->FireOnProgressEnd();
        g_StudioApp.GetDialogs()->DisplaySaveReadOnlyFailed(theDocument);
    }
}

//=============================================================================
/**
 *	Launch a viewing app to preview a file.
 *	@param	inDocumentFile		File to be previewed
 */
void CPreviewHelper::DoPreviewViaConfig(Q3DStudio::CBuildConfiguration * /*inSelectedConfig*/,
                                        const Q3DStudio::CString &inDocumentFile,
                                        EExecMode inMode,
                                        RemoteProject *project)
{
    using namespace Q3DStudio;
    Q3DStudio::CString theCommandStr;

    if (inMode == EXECMODE_DEPLOY) {
        Q_ASSERT(project);
        project->streamProject(inDocumentFile.GetCharStar());
    } else if (inMode == EXECMODE_PREVIEW
        && CStudioPreferences::GetPreviewProperty("PLATFORM") == "PC") {
        // Quick Preview on PC without going via NANT
        CFilePath theCurrentPath(CUICFile::GetApplicationDirectory().GetAbsolutePath());
        CFilePath theViewerDir = CFilePath::fromQString(QApplication::applicationDirPath());
        if (!theViewerDir.IsDirectory()) {
            // theMainDir = theCurrentPath.GetDirectory().GetDirectory();
            // theViewerDir = CFilePath::CombineBaseAndRelative( theMainDir, CFilePath(
            // L"Runtime/Build/Bin/Win32" ) );
            theViewerDir = theCurrentPath.GetDirectory(); // Developing directory
        }

        Q3DStudio::CString theDocumentFile = CPreviewHelper::GetLaunchFile(inDocumentFile);
#ifdef Q_OS_WIN
        Q3DStudio::CString theViewerFile = "Qt3DViewer.exe";
        theCommandStr = theViewerDir + "\\" + theViewerFile;
#else
#ifdef Q_OS_MACOS
        Q3DStudio::CString theViewerFile = "../../../Qt3DViewer.app/Contents/MacOS/Qt3DViewer";
#else
        Q3DStudio::CString theViewerFile = "Qt3DViewer";
#endif
        theCommandStr = theViewerDir + "/" + theViewerFile;
#endif

        QProcess *p = new QProcess;
        auto finished = static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished);
        QObject::connect(p, finished, p, &QObject::deleteLater);
        p->start(theCommandStr.toQString(), { theDocumentFile.toQString() });

        if (!p->waitForStarted()) {
            QMessageBox::critical(nullptr, QObject::tr("Error Launching Viewer"), QObject::tr("%1 failed with error: %2")
                                  .arg(theViewerFile.toQString()).arg(p->errorString()));
            delete p;
            return;
        }
    }
}

//=============================================================================
/**
 *	Interpret the string by resolving the variables.
 *	@param	inSourceString		String to be interpreted
 */
Q3DStudio::CString CPreviewHelper::InterpretString(Q3DStudio::CBuildConfiguration *inSelectedConfig,
                                                   const Q3DStudio::CString &inDocumentFile,
                                                   const Q3DStudio::CString &inSourceString)
{
    Q3DStudio::CString theReturnString;
    long theStart = 0; // start index of string
    long theBeginIndex = 0; // index of '%'
    long theEndIndex = 0; // index of '%'
    while (Q3DStudio::CString::ENDOFSTRING != theEndIndex) {
        theBeginIndex = inSourceString.Find('%', theStart);
        if (Q3DStudio::CString::ENDOFSTRING != theBeginIndex) {
            theReturnString += inSourceString.Extract(theStart, theBeginIndex - theStart);
            // find the corresponding '%'
            theEndIndex = inSourceString.Find('%', theBeginIndex + 1);
            if (Q3DStudio::CString::ENDOFSTRING != theEndIndex) {
                // first, resolve the variable by the toolbar selection
                Q3DStudio::CString theVariable =
                    inSourceString.Extract(theBeginIndex + 1, theEndIndex - theBeginIndex - 1);
                Q3DStudio::CString theResolvedVariable;
                bool theHasResolved = ResolveVariable(inSelectedConfig, inDocumentFile, theVariable,
                                                      theResolvedVariable);

                if (theHasResolved) {
                    theReturnString += theResolvedVariable;
                } else {
                    theReturnString += "_NULL_";
                }
                theStart = theEndIndex + 1;
            } else
                theReturnString += inSourceString.Extract(theBeginIndex);
        } else {
            theEndIndex = theBeginIndex;
            theReturnString += inSourceString.Extract(theStart);
        }
    }

    return theReturnString;
}

//==============================================================================
/**
 *	Resolves the passed in variable and write out the resolved value if it exists
 *	in the current selected build configuration.
 *	@param inVariable	the environment to be resolved
 *	@param outValue		the string to receive the resolved value
 *	@return true if the variable exists, else false
 */
//==============================================================================
bool CPreviewHelper::ResolveVariable(Q3DStudio::CBuildConfiguration *inSelectedConfig,
                                     const Q3DStudio::CString &inDocumentFile,
                                     const Q3DStudio::CString &inVariable,
                                     Q3DStudio::CString &outValue)
{
    Q3DStudio::CString theReturnStr;
    bool theHasResolved = false;

    // Handle special variable
    if (inVariable == "BUILDFILE") {
        if (inSelectedConfig) {
            theReturnStr = inSelectedConfig->GetPath();
            theHasResolved = true;
        }
    } else if (inVariable == "UIPFILE") {
        theReturnStr = inDocumentFile;
        theHasResolved = true;
    }

    if (!theHasResolved) {
        Q3DStudio::CString theValue = CStudioPreferences::GetPreviewProperty(inVariable);
        if (theValue != "") {
            theReturnStr = theValue;
            theHasResolved = true;
        }
    }

    if (theHasResolved)
        outValue = InterpretString(inSelectedConfig, inDocumentFile, theReturnStr);

    return theHasResolved;
}

//=============================================================================
/**
 *	Gets a file to be previewed or conditioned based on the current document.
 *	If the document has not been saved yet (and thus does not have a name), a temp file is used.
 *	If the document has been saved but it's dirty, a temp file based on the current document is
 *used (same path but different extension).
 *	@param	outUsingTempFile	if temp file if used
 *	@return the document file to be previewed or conditioned
 */
CUICFile CPreviewHelper::GetDocumentFile(bool &outUsingTempFile)
{
    CUICFile theDocument = g_StudioApp.GetCore()->GetDoc()->GetDocumentPath();
    CUICFile theResult("");
    theResult = theDocument;
    outUsingTempFile = false;
    return theResult;
}