aboutsummaryrefslogtreecommitdiffstats
path: root/tools/svgtoqml/main.cpp
diff options
context:
space:
mode:
authorHatem ElKharashy <hatem.elkharashy@qt.io>2024-01-18 13:30:56 +0200
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>2024-02-03 00:13:49 +0100
commit718ed149327c03bec33e8befcefaadf2033270d6 (patch)
tree1d093a5530112cb4b7305feb20436078d95f6b8d /tools/svgtoqml/main.cpp
parent2462d9d8c60be46b5a929c46a5e9f5a97220b3d7 (diff)
Refactor svgtoqml tool
Move the generation part of the tool to a separate private module, which can generate either a QML file or a QQuickItem representation of an SVG File. The generator relies mainly on QtQuickShapes for rendering, in addition to using other QQuickItems. The generator in this module can be used by 'svgtoqml' tool. The plan succeeding this commit will be to add a QML module that will expose a QML component for loading an SVG file at runtime. Task-number: QTBUG-121659 Change-Id: I4014eb696a7d39f05f8b3035bdcee50d2acaf2b6 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools/svgtoqml/main.cpp')
-rw-r--r--tools/svgtoqml/main.cpp56
1 files changed, 36 insertions, 20 deletions
diff --git a/tools/svgtoqml/main.cpp b/tools/svgtoqml/main.cpp
index 816f4808c0..91d55b73eb 100644
--- a/tools/svgtoqml/main.cpp
+++ b/tools/svgtoqml/main.cpp
@@ -5,18 +5,17 @@
#include <QQmlApplicationEngine>
#include <QCommandLineParser>
#include <QFile>
-#include <private/qquickrectangle_p.h>
-#include <private/qsvgtinydocument_p.h>
#include <QQuickWindow>
-
-#include "qsvgloader_p.h"
+#include <QQuickItem>
+#include <QtQuickVectorGraphicsGenerator/private/qquickitemgenerator_p.h>
+#include <QtQuickVectorGraphicsGenerator/private/qquickqmlgenerator_p.h>
+#include <QtQuickVectorGraphicsGenerator/private/qquickvectorgraphicsglobal_p.h>
#define ENABLE_GUI
int main(int argc, char *argv[])
{
#ifdef ENABLE_GUI
- qputenv("QT_QUICKSHAPES_BACKEND", "curverenderer");
QGuiApplication app(argc, argv);
#else
QCoreApplication app(argc, argv);
@@ -41,6 +40,16 @@ int main(int argc, char *argv[])
QCoreApplication::translate("main", "typename"));
parser.addOption(typeNameOption);
+ 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);
+
#ifdef ENABLE_GUI
QCommandLineOption guiOption(QStringList() << "v" << "view",
QCoreApplication::translate("main", "Display the SVG in a window."));
@@ -54,22 +63,30 @@ int main(int argc, char *argv[])
const QString inFileName = args.at(0);
- auto *doc = QSvgTinyDocument::load(inFileName);
- if (!doc) {
- fprintf(stderr, "%s is not a valid SVG\n", qPrintable(inFileName));
- return 2;
- }
-
- const QString commentString = QLatin1String("Generated from SVG file %1").arg(inFileName);
+ QString commentString = QLatin1String("Generated from SVG file %1").arg(inFileName);
const auto outFileName = args.size() > 1 ? args.at(1) : QString{};
const auto typeName = parser.value(typeNameOption);
+ auto copyrightString = parser.value(copyrightOption);
- QSvgQmlWriter::GeneratorFlags flags;
+ if (!copyrightString.isEmpty()) {
+ copyrightString = copyrightString.replace("\\n", "\n");
+ commentString = copyrightString + u"\n" + commentString;
+ }
+
+ QQuickVectorGraphics::GeneratorFlags flags;
if (parser.isSet(curveRendererOption))
- flags |= QSvgQmlWriter::CurveRenderer;
+ flags |= QQuickVectorGraphics::GeneratorFlag::CurveRenderer;
if (parser.isSet(optimizeOption))
- flags |= QSvgQmlWriter::OptimizePaths;
+ flags |= QQuickVectorGraphics::GeneratorFlag::OptimizePaths;
+ if (parser.isSet(outlineModeOption))
+ flags |= (QQuickVectorGraphics::GeneratorFlag::OutlineStrokeMode
+ | QQuickVectorGraphics::GeneratorFlag::OptimizePaths);
+
+ QQuickQmlGenerator generator(inFileName, flags, outFileName);
+ generator.setShapeTypeName(typeName);
+ generator.setCommentString(commentString);
+ generator.generate();
#ifdef ENABLE_GUI
if (parser.isSet(guiOption)) {
@@ -82,16 +99,15 @@ int main(int argc, char *argv[])
QCoreApplication::exit(-1);
if (obj) {
auto *containerItem = obj->findChild<QQuickItem*>(QStringLiteral("svg_item"));
- auto *contents = QSvgQmlWriter::loadSVG(doc, outFileName, flags, typeName, containerItem, commentString);
- contents->setWidth(containerItem->implicitWidth()); // Workaround for runtime loader viewbox size logic. TODO: fix
- contents->setHeight(containerItem->implicitHeight());
+ QQuickItemGenerator generator(inFileName, flags, containerItem);
+ generator.generate();
}
});
engine.load(url);
return app.exec();
}
+#else
+ return 0;
#endif
- QSvgQmlWriter::loadSVG(doc, outFileName, flags, typeName, nullptr, commentString);
- return 0;
}