2023-11-15 13:21:37 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
2024-02-22 14:38:20 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2023-11-15 13:21:37 +00:00
|
|
|
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QQmlApplicationEngine>
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QQuickWindow>
|
2024-01-18 11:30:56 +00:00
|
|
|
#include <QQuickItem>
|
2024-02-12 11:10:20 +00:00
|
|
|
#include <QtQuickVectorImageGenerator/private/qquickitemgenerator_p.h>
|
|
|
|
#include <QtQuickVectorImageGenerator/private/qquickqmlgenerator_p.h>
|
|
|
|
#include <QtQuickVectorImageGenerator/private/qquickvectorimageglobal_p.h>
|
2023-11-15 13:21:37 +00:00
|
|
|
|
|
|
|
#define ENABLE_GUI
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_GUI
|
|
|
|
QGuiApplication app(argc, argv);
|
|
|
|
#else
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription("SVG to QML converter [tech preview]");
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addPositionalArgument("input", QCoreApplication::translate("main", "SVG file to read."));
|
|
|
|
parser.addPositionalArgument("output", QCoreApplication::translate("main", "QML file to write."));
|
|
|
|
|
2023-11-23 12:00:42 +00:00
|
|
|
QCommandLineOption optimizeOption("optimize-paths",
|
|
|
|
QCoreApplication::translate("main", "Optimize paths for the curve renderer."));
|
|
|
|
parser.addOption(optimizeOption);
|
|
|
|
|
|
|
|
QCommandLineOption curveRendererOption("curve-renderer",
|
|
|
|
QCoreApplication::translate("main", "Use the curve renderer in generated QML."));
|
|
|
|
parser.addOption(curveRendererOption);
|
|
|
|
|
2023-11-15 13:21:37 +00:00
|
|
|
QCommandLineOption typeNameOption(QStringList() << "t" << "type-name",
|
|
|
|
QCoreApplication::translate("main", "Use <typename> for Shape."),
|
|
|
|
QCoreApplication::translate("main", "typename"));
|
|
|
|
parser.addOption(typeNameOption);
|
|
|
|
|
2024-01-18 11:30:56 +00:00
|
|
|
QCommandLineOption copyrightOption("copyright-statement",
|
|
|
|
QCoreApplication::translate("main", "Add <string> as a comment at the start of the generated file."),
|
|
|
|
QCoreApplication::translate("main", "string"));
|
|
|
|
parser.addOption(copyrightOption);
|
|
|
|
|
|
|
|
QCommandLineOption outlineModeOption("outline-stroke-mode",
|
|
|
|
QCoreApplication::translate("main", "Stroke the outside of the filled shape instead of "
|
|
|
|
"the original path. Also sets optimize-paths."));
|
|
|
|
parser.addOption(outlineModeOption);
|
|
|
|
|
2023-11-15 13:21:37 +00:00
|
|
|
#ifdef ENABLE_GUI
|
|
|
|
QCommandLineOption guiOption(QStringList() << "v" << "view",
|
2023-11-23 12:00:42 +00:00
|
|
|
QCoreApplication::translate("main", "Display the SVG in a window."));
|
2023-11-15 13:21:37 +00:00
|
|
|
parser.addOption(guiOption);
|
|
|
|
#endif
|
|
|
|
parser.process(app);
|
|
|
|
const QStringList args = parser.positionalArguments();
|
|
|
|
if (args.size() < 1) {
|
|
|
|
parser.showHelp(1);
|
|
|
|
}
|
|
|
|
|
2023-11-23 12:00:42 +00:00
|
|
|
const QString inFileName = args.at(0);
|
|
|
|
|
2024-01-18 11:30:56 +00:00
|
|
|
QString commentString = QLatin1String("Generated from SVG file %1").arg(inFileName);
|
2023-11-23 12:00:42 +00:00
|
|
|
|
|
|
|
const auto outFileName = args.size() > 1 ? args.at(1) : QString{};
|
|
|
|
const auto typeName = parser.value(typeNameOption);
|
2024-01-18 11:30:56 +00:00
|
|
|
auto copyrightString = parser.value(copyrightOption);
|
2023-11-23 12:00:42 +00:00
|
|
|
|
2024-01-18 11:30:56 +00:00
|
|
|
if (!copyrightString.isEmpty()) {
|
|
|
|
copyrightString = copyrightString.replace("\\n", "\n");
|
|
|
|
commentString = copyrightString + u"\n" + commentString;
|
|
|
|
}
|
|
|
|
|
2024-02-12 11:10:20 +00:00
|
|
|
QQuickVectorImageGenerator::GeneratorFlags flags;
|
2023-11-23 12:00:42 +00:00
|
|
|
if (parser.isSet(curveRendererOption))
|
2024-02-12 11:10:20 +00:00
|
|
|
flags |= QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer;
|
2023-11-23 12:00:42 +00:00
|
|
|
if (parser.isSet(optimizeOption))
|
2024-02-12 11:10:20 +00:00
|
|
|
flags |= QQuickVectorImageGenerator::GeneratorFlag::OptimizePaths;
|
2024-01-18 11:30:56 +00:00
|
|
|
if (parser.isSet(outlineModeOption))
|
2024-02-12 11:10:20 +00:00
|
|
|
flags |= (QQuickVectorImageGenerator::GeneratorFlag::OutlineStrokeMode
|
|
|
|
| QQuickVectorImageGenerator::GeneratorFlag::OptimizePaths);
|
2024-01-18 11:30:56 +00:00
|
|
|
|
|
|
|
QQuickQmlGenerator generator(inFileName, flags, outFileName);
|
|
|
|
generator.setShapeTypeName(typeName);
|
|
|
|
generator.setCommentString(commentString);
|
|
|
|
generator.generate();
|
2023-11-15 13:21:37 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_GUI
|
|
|
|
if (parser.isSet(guiOption)) {
|
|
|
|
app.setOrganizationName("QtProject");
|
|
|
|
const QUrl url(QStringLiteral("qrc:/main.qml"));
|
|
|
|
QQmlApplicationEngine engine;
|
|
|
|
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
|
2023-11-23 12:00:42 +00:00
|
|
|
&app, [&](QObject *obj, const QUrl &objUrl){
|
2023-11-15 13:21:37 +00:00
|
|
|
if (!obj && url == objUrl)
|
|
|
|
QCoreApplication::exit(-1);
|
|
|
|
if (obj) {
|
|
|
|
auto *containerItem = obj->findChild<QQuickItem*>(QStringLiteral("svg_item"));
|
2024-01-18 11:30:56 +00:00
|
|
|
QQuickItemGenerator generator(inFileName, flags, containerItem);
|
|
|
|
generator.generate();
|
2023-11-15 13:21:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
engine.load(url);
|
|
|
|
return app.exec();
|
|
|
|
}
|
2024-01-18 11:30:56 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
2023-11-15 13:21:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|