| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickitemgenerator_p.h" |
| 5 | #include "utils_p.h" |
| 6 | #include "qquicknodeinfo_p.h" |
| 7 | |
| 8 | #include <private/qsgcurveprocessor_p.h> |
| 9 | #include <private/qquickshape_p.h> |
| 10 | #include <private/qquadpath_p.h> |
| 11 | #include <private/qquickitem_p.h> |
| 12 | #include <private/qquickimagebase_p_p.h> |
| 13 | #include <private/qquickanimation_p.h> |
| 14 | #include <private/qquicktext_p.h> |
| 15 | #include <private/qquicktranslate_p.h> |
| 16 | #include <private/qquickimage_p.h> |
| 17 | |
| 18 | #include <QtCore/qloggingcategory.h> |
| 19 | #include <QtCore/qstandardpaths.h> |
| 20 | #include <QtQml/qqmlcomponent.h> |
| 21 | |
| 22 | QT_BEGIN_NAMESPACE |
| 23 | |
| 24 | QQuickItemGenerator::QQuickItemGenerator(const QString fileName, |
| 25 | QQuickVectorImageGenerator::GeneratorFlags flags, |
| 26 | QQuickItem *parentItem, |
| 27 | QQmlContext *ctx) |
| 28 | : QQuickQmlGenerator(fileName, flags, QString{}) |
| 29 | , m_parentItem(parentItem) |
| 30 | , m_qmlContext(ctx) |
| 31 | { |
| 32 | setRetainFilePaths(true); |
| 33 | |
| 34 | QString tmpDir = QStandardPaths::writableLocation(type: QStandardPaths::TempLocation); |
| 35 | setAssetFileDirectory(tmpDir); |
| 36 | setAssetFilePrefix(QStringLiteral("_qt_vectorimage_")); |
| 37 | setUrlPrefix(QStringLiteral("file:")); |
| 38 | } |
| 39 | |
| 40 | QQuickItemGenerator::~QQuickItemGenerator() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | bool QQuickItemGenerator::generateRootNode(const StructureNodeInfo &info) |
| 45 | { |
| 46 | bool cont = QQuickQmlGenerator::generateRootNode(info); |
| 47 | if (info.stage == StructureNodeStage::End) { |
| 48 | if (m_qmlContext == nullptr) { |
| 49 | qCWarning(lcQuickVectorImage) << "QQuickItemGenerator::generateRootItem: Requires QML context"; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | QQmlEngine *engine = m_qmlContext->engine(); |
| 54 | if (engine == nullptr) { |
| 55 | qCWarning(lcQuickVectorImage) << "QQuickItemGenerator::generateRootItem: Requires QML engine"; |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | QQmlComponent component(engine); |
| 60 | component.setData(m_result.data(), baseUrl: QUrl{}); |
| 61 | QObject *obj = component.create(context: m_qmlContext); |
| 62 | QQuickItem *rootItem = qobject_cast<QQuickItem *>(o: obj); |
| 63 | if (rootItem == nullptr) { |
| 64 | delete obj; |
| 65 | |
| 66 | qCWarning(lcQuickVectorImage) << "QQuickItemGenerator::generateRootItem: Root item not a QQuickItem: "<< component.errorString(); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | m_parentItem->setImplicitWidth(rootItem->width()); |
| 71 | m_parentItem->setImplicitHeight(rootItem->height()); |
| 72 | |
| 73 | rootItem->setParent(m_parentItem); |
| 74 | rootItem->setParentItem(m_parentItem); |
| 75 | } |
| 76 | |
| 77 | return cont; |
| 78 | } |
| 79 | |
| 80 | QT_END_NAMESPACE |
| 81 |
