summaryrefslogtreecommitdiffstats
path: root/tools/q3dsviewer/q3dsmainwindow.cpp
blob: e89c7b822b06bec67d520f24a0e143115b29979b (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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** 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 "q3dsmainwindow.h"
#include "q3dsremotedeploymentmanager.h"
#include <private/q3dswindow_p.h>
#include <private/q3dsengine_p.h>
#include <private/q3dsutils_p.h>
#include <private/q3dsslideplayer_p.h>
#include <QApplication>
#include <QMenuBar>
#include <QMenu>
#include <QMessageBox>
#include <QFileDialog>
#include <QFileInfo>

QT_BEGIN_NAMESPACE

QString Q3DStudioMainWindow::fileFilter()
{
    return tr("All Supported Formats (*.uia *.uip);;Studio UI Presentation (*.uip);;Application File (*.uia);;All Files (*)");
}

Q3DStudioMainWindow::Q3DStudioMainWindow(Q3DSWindow *view, Q3DSRemoteDeploymentManager *remote, QWidget *parent)
    : QMainWindow(parent)
{
    static const bool enableDebugMenu = qEnvironmentVariableIntValue("Q3DS_DEBUG") >= 1;

    QWidget *wrapper = QWidget::createWindowContainer(view);
    setCentralWidget(wrapper);

    const QSize designSize = view->size(); // because Q3DSWindow::setSource() set this already

    QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    auto open = [=]() {
        QString dir;
        QString prevFilename = view->engine()->source();
        if (!prevFilename.isEmpty())
            dir = QFileInfo(prevFilename).canonicalPath();
        QString fn = QFileDialog::getOpenFileName(this, tr("Open"), dir, fileFilter());
        if (!fn.isEmpty())
            view->engine()->setSource(fn);
        if (remote)
            remote->setState(Q3DSRemoteDeploymentManager::LocalProject);
    };
    fileMenu->addAction(tr("&Open..."), this, [=] {
        view->engine()->setFlag(Q3DSEngine::EnableProfiling, true);
        open();
    } , QKeySequence::Open);
    fileMenu->addAction(tr("Open Without &Profiling..."), this, [=] {
        view->engine()->setFlag(Q3DSEngine::EnableProfiling, false);
        open();
    });
    if (remote)
        fileMenu->addAction(tr("Remote Setup"), this, [remote] {
            remote->showConnectionSetup();
        });
    fileMenu->addAction(tr("&Reload"), this, [=] {
        // Don't reload if on the ConnectionInfo screen
        if (remote &&
            remote->state() != Q3DSRemoteDeploymentManager::LocalProject &&
            remote->state() != Q3DSRemoteDeploymentManager::RemoteProject)
            return;
        view->engine()->setSource(view->engine()->source());
    }, QKeySequence::Refresh);
    fileMenu->addAction(tr("E&xit"), this, &QWidget::close, QKeySequence::Quit);

    QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
    if (enableDebugMenu) {
        QAction *forcePresSize = viewMenu->addAction(tr("&Force design size"));
        forcePresSize->setCheckable(true);
        forcePresSize->setChecked(false);
        connect(forcePresSize, &QAction::toggled, [=]() {
            if (forcePresSize->isChecked()) {
                wrapper->setMinimumSize(designSize);
                wrapper->setMaximumSize(designSize);
            } else {
                wrapper->setMinimumSize(1, 1);
                wrapper->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
            }
        });
    }

    viewMenu->addAction(tr("Toggle Full Scree&n"), this, [this] {
        Qt::WindowStates s = windowState();
        s.setFlag(Qt::WindowFullScreen, !s.testFlag(Qt::WindowFullScreen));
        setWindowState(s);
    }, QKeySequence::FullScreen);

    QMenu *profileSubMenu = new QMenu(tr("&Profile and Debug"));
    profileSubMenu->addAction(tr("Toggle in-scene &debug view"), this, [view] {
        Q3DSEngine *engine = view->engine();
        engine->setProfileUiVisible(!engine->isProfileUiVisible());
    }, Qt::Key_F10);
    profileSubMenu->addAction(tr("Scale in-scene debug view up"), this, [view] {
        Q3DSEngine *engine = view->engine();
        engine->configureProfileUi(engine->profileUiScaleFactor() + 0.2f);
    }, QKeySequence(QLatin1String("Ctrl+F10")));
    profileSubMenu->addAction(tr("Scale in-scene debug view down"), this, [view] {
        Q3DSEngine *engine = view->engine();
        engine->configureProfileUi(engine->profileUiScaleFactor() - 0.2f);
    }, QKeySequence(QLatin1String("Alt+F10")));
    viewMenu->addMenu(profileSubMenu);

    if (enableDebugMenu) {
        QMenu *debugMenu = menuBar()->addMenu(tr("&Debug"));
        debugMenu->addAction(tr("&Object graph..."), [=]() {
            Q3DSUtils::showObjectGraph(view->engine()->presentation()->scene());
        });
        debugMenu->addAction(tr("&Scene slide graph..."), [=]() {
            Q3DSUtils::showObjectGraph(view->engine()->presentation()->masterSlide());
        });
        QAction *depthTexAction = debugMenu->addAction(tr("&Force depth texture"));
        depthTexAction->setCheckable(true);
        depthTexAction->setChecked(false);
        connect(depthTexAction, &QAction::toggled, [=]() {
            Q3DSUipPresentation::forAllLayers(view->engine()->presentation()->scene(),
                                              [=](Q3DSLayerNode *layer3DS) {
                view->engine()->sceneManager()->setDepthTextureEnabled(
                            layer3DS, depthTexAction->isChecked());
            });
        });
        QAction *ssaoAction = debugMenu->addAction(tr("Force SS&AO"));
        ssaoAction->setCheckable(true);
        ssaoAction->setChecked(false);
        connect(ssaoAction, &QAction::toggled, [=]() {
            Q3DSUipPresentation::forAllLayers(view->engine()->presentation()->scene(),
                                              [=](Q3DSLayerNode *layer3DS) {
                Q3DSPropertyChangeList changeList;
                const QString value = ssaoAction->isChecked() ? QLatin1String("50") : QLatin1String("0");
                changeList.append(Q3DSPropertyChange(QLatin1String("aostrength"), value));
                layer3DS->applyPropertyChanges(changeList);
                layer3DS->notifyPropertyChanges(changeList);
            });
        });
        QAction *rebuildMatAction = debugMenu->addAction(tr("&Rebuild model materials"));
        connect(rebuildMatAction, &QAction::triggered, [=]() {
            Q3DSUipPresentation::forAllModels(view->engine()->presentation()->scene(),
                                              [=](Q3DSModelNode *model3DS) {
                view->engine()->sceneManager()->rebuildModelMaterial(model3DS);
            });
        });
        QAction *toggleShadowAction = debugMenu->addAction(tr("&Toggle shadow casting for point lights"));
        connect(toggleShadowAction, &QAction::triggered, [=]() {
            Q3DSUipPresentation::forAllObjectsOfType(view->engine()->presentation()->scene(),
                                                     Q3DSGraphObject::Light, [=](Q3DSGraphObject *obj) {
                Q3DSLightNode *light3DS = static_cast<Q3DSLightNode *>(obj);
                if (light3DS->flags().testFlag(Q3DSNode::Active) &&
                        light3DS->lightType() == Q3DSLightNode::Point) {
                    Q3DSPropertyChangeList changeList;
                    const QString value = light3DS->castShadow() ? QLatin1String("false") : QLatin1String("true");
                    changeList.append(Q3DSPropertyChange(QLatin1String("castshadow"), value));
                    light3DS->applyPropertyChanges(changeList);
                    light3DS->notifyPropertyChanges(changeList);
                }
            });
        });
        QAction *shadowResChangeAction = debugMenu->addAction(tr("&Maximize shadow map resolution for lights"));
        connect(shadowResChangeAction, &QAction::triggered, [=]() {
            Q3DSUipPresentation::forAllObjectsOfType(view->engine()->presentation()->scene(),
                                                     Q3DSGraphObject::Light, [=](Q3DSGraphObject *obj) {
                Q3DSLightNode *light3DS = static_cast<Q3DSLightNode *>(obj);
                if (light3DS->flags().testFlag(Q3DSNode::Active)) {
                    Q3DSPropertyChangeList changeList;
                    const QString value = QLatin1String("11"); // 8..11
                    changeList.append(Q3DSPropertyChange(QLatin1String("shdwmapres"), value));
                    light3DS->applyPropertyChanges(changeList);
                    light3DS->notifyPropertyChanges(changeList);
                }
            });
        });
        QAction *pauseAnims = debugMenu->addAction(tr("&Pause animations"));
        pauseAnims->setCheckable(true);
        pauseAnims->setChecked(false);
        connect(pauseAnims, &QAction::toggled, [=]() {
            Q3DSSceneManager *sb = view->engine()->sceneManager();
            Q3DSSlidePlayer *player = sb->slidePlayer();
            if (player) {
                if (pauseAnims->isChecked())
                    player->pause();
                else
                    player->play();
            }
        });
        QAction *renderOnDemand = debugMenu->addAction(tr("Render on &demand only"));
        renderOnDemand->setCheckable(true);
        renderOnDemand->setChecked(false);
        connect(renderOnDemand, &QAction::toggled, [=]() {
            view->engine()->setOnDemandRendering(renderOnDemand->isChecked());
        });
    }

    QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(tr("&About"), this, [this]() {
        QMessageBox::about(this, tr("About q3dsviewer"), tr("Qt 3D Studio Viewer 2.0"));
    });
    helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);

    // Initial size for the main window. We are sizing the main window, not the
    // embedded one (view), so add some extra height to be closer to the design
    // size. ### This should be made more accurate at some point. Note that by
    // default we are not required to strictly follow the design size, that's
    // what Force Design Size is for.
    resize(designSize + QSize(0, 40));
}

QT_END_NAMESPACE