1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QGuiApplication>
5#include <QQmlApplicationEngine>
6
7#include <QtQuick3D>
8
9#include <QtCore/qcommandlineparser.h>
10#include <QtCore/qcommandlineoption.h>
11
12// For component loading case
13#include <QtQuick3D/private/qquick3dcustommaterial_p.h>
14
15int main(int argc, char *argv[])
16{
17 qputenv(varName: "QT_QUICK3D_EDITORMODE", value: "1");
18#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
19 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
20#endif
21
22 QGuiApplication app(argc, argv);
23 app.setApplicationName("Material Editor");
24 app.setOrganizationName("QtProject");
25 app.setOrganizationDomain("qt-project.org");
26 app.setApplicationVersion(QLatin1String(QT_VERSION_STR));
27
28 QSurfaceFormat::setDefaultFormat(QQuick3D::idealSurfaceFormat(samples: 4));
29
30 QCommandLineParser cmdLineparser;
31 cmdLineparser.setApplicationDescription("Editor for creating custom materials for use with Qt Quick 3D");
32 cmdLineparser.addHelpOption();
33
34 QCommandLineOption modeOption({QChar(u'm'), QLatin1String("mode")}, QLatin1String("sets editor mode"), QLatin1String("c"));
35 cmdLineparser.addOption(commandLineOption: modeOption);
36
37 QCommandLineOption changeDirOption({QChar(u'C'), QLatin1String("directory")}, QLatin1String("Change the working directory"), QLatin1String("dir"));
38 cmdLineparser.addOption(commandLineOption: changeDirOption);
39
40 QCommandLineOption projectDirOption({QChar(u'p'), QLatin1String("project-dir")}, QLatin1String("Project directory"), QLatin1String("dir"));
41 cmdLineparser.addOption(commandLineOption: projectDirOption);
42
43 QCommandLineOption verboseOutputOption({QChar(u'v'), QLatin1String("verbose")}, QLatin1String("Turn on verbose output."));
44 cmdLineparser.addOption(commandLineOption: verboseOutputOption);
45
46 cmdLineparser.process(app);
47
48 bool workingDirSet = false;
49 if (cmdLineparser.isSet(option: changeDirOption)) {
50 const auto value = cmdLineparser.value(option: changeDirOption);
51 QFileInfo fi(value);
52 workingDirSet = fi.isDir();
53 if (workingDirSet)
54 QDir::setCurrent(value);
55 else
56 qWarning(msg: "%s : %s - Not a directory", qPrintable(app.applicationName()), qPrintable(value));
57 }
58
59 QString resourceFile;
60 auto args = cmdLineparser.positionalArguments();
61 for (const auto &arg : std::as_const(t&: args)) {
62 if (arg.endsWith(s: ".qml"))
63 resourceFile = arg;
64 }
65
66 if (!resourceFile.isEmpty() && QFileInfo::exists(file: resourceFile)) {
67 QFileInfo resourceFileInfo(resourceFile);
68 if (resourceFileInfo.isFile()) {
69 if (!workingDirSet)
70 QDir::setCurrent(resourceFileInfo.dir().path());
71
72 resourceFile = resourceFileInfo.canonicalFilePath();
73 }
74 }
75
76 QString projectPath;
77 if (cmdLineparser.isSet(option: projectDirOption)) {
78 const auto value = cmdLineparser.value(option: projectDirOption);
79 QFileInfo fi(value);
80 if (fi.isDir())
81 projectPath = fi.canonicalFilePath();
82 else
83 qWarning(msg: "%s : %s - Not a directory", qPrintable(app.applicationName()), qPrintable(value));
84 }
85
86 QQmlApplicationEngine engine;
87 if (auto ctx = engine.rootContext())
88 ctx->setContextProperty("_qtProjectDir", QUrl::fromLocalFile(localfile: projectPath));
89 const QUrl url(QStringLiteral("qrc:/qt-project.org/imports/QtQuick3D/MaterialEditor/main.qml"));
90 engine.load(url);
91 if (engine.rootObjects().isEmpty())
92 return -1;
93
94 return app.exec();
95}
96

source code of qtquick3d/tools/materialeditor/main.cpp