summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Palettes/Project/ProjectView.cpp
blob: 840295bd8e70a0a473c0a0d6f9bdc14b5ed027b8 (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
/****************************************************************************
**
** 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 "ProjectView.h"
#include "ProjectFileSystemModel.h"
#include "Core.h"
#include "Dispatch.h"
#include "Doc.h"
#include "Literals.h"
#include "StudioUtils.h"
#include "ImportUtils.h"
#include "StudioApp.h"
#include "StudioClipboard.h"
#include "StudioPreferences.h"
#include "Qt3DSImport.h"
#include "Dialogs.h"
#include "IDocumentEditor.h"
#include "ProjectContextMenu.h"

#include <QtCore/qprocess.h>
#include <QtCore/qtimer.h>
#include <QtGui/qdrag.h>
#include <QtGui/qdesktopservices.h>
#include <QtQml/qqmlcontext.h>
#include <QtQml/qqmlengine.h>
#include <QtQuick/qquickitem.h>

ProjectView::ProjectView(const QSize &preferredSize, QWidget *parent) : QQuickWidget(parent)
  , m_ProjectModel(new ProjectFileSystemModel(this))
  , m_preferredSize(preferredSize)
{
    const QString theApplicationPath =
            Qt3DSFile::GetApplicationDirectory().GetPath().toQString();

    m_defaultBehaviorDir = theApplicationPath + QStringLiteral("/Content/Behavior Library");
    m_defaultEffectDir = theApplicationPath + QStringLiteral("/Content/Effect Library");
    m_defaultFontDir = theApplicationPath + QStringLiteral("/Content/Font Library");
    m_defaultImageDir = theApplicationPath + QStringLiteral("/Content/Maps Library");
    m_defaultMaterialDir = theApplicationPath + QStringLiteral("/Content/Material Library");
    m_defaultModelDir = theApplicationPath + QStringLiteral("/Content/Models Library");

    m_BehaviorDir = m_defaultBehaviorDir;
    m_EffectDir = m_defaultEffectDir;
    m_FontDir = m_defaultFontDir;
    m_ImageDir = m_defaultImageDir;
    m_MaterialDir = m_defaultMaterialDir;
    m_ModelDir = m_defaultModelDir;

    m_assetImportDir = theApplicationPath + QStringLiteral("/Content");

    setResizeMode(QQuickWidget::SizeRootObjectToView);
    QTimer::singleShot(0, this, &ProjectView::initialize);

    auto dispatch = g_StudioApp.GetCore()->GetDispatch();
    dispatch->AddPresentationChangeListener(this);
    dispatch->AddDataModelListener(this);
}

ProjectView::~ProjectView()
{
}

QAbstractItemModel *ProjectView::projectModel() const
{
    return m_ProjectModel;
}

QSize ProjectView::sizeHint() const
{
    return m_preferredSize;
}

void ProjectView::initialize()
{
    CStudioPreferences::setQmlContextProperties(rootContext());
    rootContext()->setContextProperty("_resDir"_L1, resourceImageUrl());
    rootContext()->setContextProperty("_projectView"_L1, this);

    engine()->addImportPath(qmlImportPath());
    setSource(QUrl("qrc:/Palettes/Project/ProjectView.qml"_L1));
}

void ProjectView::effectAction(int row)
{
    m_EffectDir = m_defaultEffectDir;
    QList<QUrl> urls = g_StudioApp.GetDialogs()->SelectAssets(
                m_EffectDir, Q3DStudio::DocumentEditorFileType::Effect);
    m_ProjectModel->importUrls(urls, row);
}

void ProjectView::fontAction(int row)
{
    m_FontDir = m_defaultFontDir;
    QList<QUrl> urls = g_StudioApp.GetDialogs()->SelectAssets(
                m_FontDir, Q3DStudio::DocumentEditorFileType::Font);
    m_ProjectModel->importUrls(urls, row);
}

void ProjectView::imageAction(int row)
{
    m_ImageDir = m_defaultImageDir;
    QList<QUrl> urls = g_StudioApp.GetDialogs()->SelectAssets(
                m_ImageDir, Q3DStudio::DocumentEditorFileType::Image);
    m_ProjectModel->importUrls(urls, row);
}

void ProjectView::materialAction(int row)
{
    m_MaterialDir = m_defaultMaterialDir;
    QList<QUrl> urls = g_StudioApp.GetDialogs()->SelectAssets(
                m_MaterialDir, Q3DStudio::DocumentEditorFileType::Material);
    m_ProjectModel->importUrls(urls, row);
}

void ProjectView::modelAction(int row)
{
    m_ModelDir = m_defaultModelDir;
    QList<QUrl> urls = g_StudioApp.GetDialogs()->SelectAssets(
                m_ModelDir, Q3DStudio::DocumentEditorFileType::DAE);
    m_ProjectModel->importUrls(urls, row);
}

void ProjectView::behaviorAction(int row)
{
    m_BehaviorDir = m_defaultBehaviorDir;
    QList<QUrl> urls = g_StudioApp.GetDialogs()->SelectAssets(
                m_BehaviorDir, Q3DStudio::DocumentEditorFileType::Behavior);
    m_ProjectModel->importUrls(urls, row);
}

void ProjectView::assetImportAction(int row)
{
    QList<QUrl> urls = g_StudioApp.GetDialogs()->SelectAssets(
                m_assetImportDir, Q3DStudio::DocumentEditorFileType::Unknown);
    m_ProjectModel->importUrls(urls, row);
}

void ProjectView::assetImportInContext(int row)
{
    // If the context is a default directory, select the correct directory
    Q3DStudio::DocumentEditorFileType::Enum assetType = m_ProjectModel->assetTypeForRow(row);
    QString *assetDir = &m_assetImportDir;
    switch (assetType) {
    case Q3DStudio::DocumentEditorFileType::Effect:
        assetDir = &m_EffectDir;
        break;
    case Q3DStudio::DocumentEditorFileType::Font:
        assetDir = &m_FontDir;
        break;
    case Q3DStudio::DocumentEditorFileType::Image:
        assetDir = &m_ImageDir;
        break;
    case Q3DStudio::DocumentEditorFileType::Material:
        assetDir = &m_MaterialDir;
        break;
    case Q3DStudio::DocumentEditorFileType::DAE:
        assetDir = &m_ModelDir;
        break;
    case Q3DStudio::DocumentEditorFileType::Behavior:
        assetDir = &m_BehaviorDir;
        break;
    default:
        break;
    }

    QList<QUrl> urls;
    urls = g_StudioApp.GetDialogs()->SelectAssets(*assetDir, assetType);
    m_ProjectModel->importUrls(urls, row, false);
}

void ProjectView::OnNewPresentation()
{
    rebuild();
}

void ProjectView::OnBeginDataModelNotifications()
{
}

void ProjectView::OnEndDataModelNotifications()
{
    m_ProjectModel->updateReferences(true);
}

void ProjectView::OnImmediateRefreshInstanceSingle(qt3dsdm::Qt3DSDMInstanceHandle inInstance)
{
    Q_UNUSED(inInstance);
}

void ProjectView::OnImmediateRefreshInstanceMultiple(qt3dsdm::Qt3DSDMInstanceHandle *inInstance, long inInstanceCount)
{
    Q_UNUSED(inInstance);
    Q_UNUSED(inInstanceCount);
}

void ProjectView::startDrag(QQuickItem *item, int row)
{
    const auto index = m_ProjectModel->index(row);

    QDrag drag(this);
    drag.setMimeData(m_ProjectModel->mimeData({index}));
    drag.exec(Qt::CopyAction);
    QTimer::singleShot(0, item, &QQuickItem::ungrabMouse);
}

void ProjectView::showInExplorer(int row) const
{
    if (row == -1)
        return;
    const auto path = m_ProjectModel->filePath(row);
#if defined(Q_OS_WIN)
    QString param = QStringLiteral("explorer ");
    if (!QFileInfo(path).isDir())
        param += QLatin1String("/select,");
    param += QDir::toNativeSeparators(path).replace(QStringLiteral(" "), QStringLiteral("\ "));
    QProcess::startDetached(param);
#elif defined(Q_OS_MACOS)
    QProcess::startDetached("/usr/bin/osascript", {"-e",
        QStringLiteral("tell application \"Finder\" to reveal POSIX file \"%1\"").arg(path)});
    QProcess::startDetached("/usr/bin/osascript", {"-e",
        QStringLiteral("tell application \"Finder\" to activate")});
#else
    // we cannot select a file here, because no file browser really supports it...
    QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(path).absolutePath()));
#endif
}

void ProjectView::copyPath(int row) const
{
    if (row == -1)
        return;
    const auto path = m_ProjectModel->filePath(row);
    const auto doc = g_StudioApp.GetCore()->GetDoc();
    const auto relativePath = doc->GetRelativePathToDoc(
                Q3DStudio::CFilePath(Q3DStudio::CString::fromQString(path)));
    CStudioClipboard::CopyTextToClipboard(relativePath.toQString());
}

void ProjectView::copyFullPath(int row) const
{
    if (row == -1)
        return;
    const auto path = m_ProjectModel->filePath(row);
    CStudioClipboard::CopyTextToClipboard(path);
}

bool ProjectView::isGroup(int row) const
{
    if (row == -1)
        return false;
    Q3DStudio::CFilePath path(Q3DStudio::CString::fromQString(m_ProjectModel->filePath(row)));
    return Q3DStudio::ImportUtils::GetObjectFileTypeForFile(path).m_ObjectType == OBJTYPE_GROUP;
}

bool ProjectView::isRefreshable(int row) const
{
    return m_ProjectModel->isRefreshable(row);
}

void ProjectView::showContextMenu(int x, int y, int index)
{
    ProjectContextMenu contextMenu(this, index);
    contextMenu.exec(mapToGlobal({x, y}));
}

void ProjectView::refreshImport(int row) const
{
    if (row == -1)
        return;
    using namespace Q3DStudio;
    const auto path = m_ProjectModel->filePath(row);
    qt3dsimp::ImportPtrOrError importPtr = qt3dsimp::Import::Load(path.toStdWString().c_str());
    if (importPtr.m_Value) {
        const auto destDir = importPtr.m_Value->GetDestDir();
        const auto srcFile = importPtr.m_Value->GetSrcFile();
        const QString fullSrcPath(QDir(destDir).filePath(srcFile));
        const QFileInfo oldFile(fullSrcPath);
        const QFileInfo newFile(g_StudioApp.GetDialogs()->ConfirmRefreshModelFile(fullSrcPath));
        if (newFile.exists() && newFile.isFile()){
            const auto doc = g_StudioApp.GetCore()->GetDoc();
            Q3DStudio::SCOPED_DOCUMENT_EDITOR(*doc, tr("Refresh Import..."))
                    ->RefreshImport(oldFile.canonicalFilePath(), newFile.canonicalFilePath());
        }
    }
}

void ProjectView::rebuild()
{
    const auto theDoc = g_StudioApp.GetCore()->GetDoc();
    const Q3DStudio::CFilePath thePath(theDoc->GetDocumentPath().GetAbsolutePath());
    const Q3DStudio::CFilePath theRootDirPath = thePath.GetDirectory();

    m_ProjectModel->setRootPath(theRootDirPath.toQString());
}