File indexing completed on 2025-02-09 06:41:29

0001 /*
0002     SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText:
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef PLASMA_SHAREDQMLENGINE_H
0009 #define PLASMA_SHAREDQMLENGINE_H
0010 
0011 #include <plasmaquick/plasmaquick_export.h>
0012 
0013 #include <QObject>
0014 #include <QQmlComponent>
0015 #include <QQmlContext>
0016 
0017 #include <memory>
0018 
0019 class QQmlComponent;
0020 class QQmlEngine;
0021 class KLocalizedContext;
0022 
0023 namespace Plasma
0024 {
0025 class Applet;
0026 }
0027 
0028 namespace PlasmaQuick
0029 {
0030 class SharedQmlEnginePrivate;
0031 
0032 /**
0033  * @short An object that instantiates an entire QML context, with its own declarative engine
0034  *
0035  * PlasmaQuick::SharedQmlEngine provides a class to conveniently use QML based
0036  * declarative user interfaces.
0037  * A SharedQmlEngine corresponds to one QML file (which can include others).
0038  * It will a shared QQmlEngine with a single root object, described in the QML file.
0039  *
0040  * @since 6.0
0041  */
0042 class PLASMAQUICK_EXPORT SharedQmlEngine : public QObject
0043 {
0044     Q_OBJECT
0045 
0046     Q_PROPERTY(QUrl source READ source WRITE setSource)
0047     Q_PROPERTY(QString translationDomain READ translationDomain WRITE setTranslationDomain)
0048     Q_PROPERTY(bool initializationDelayed READ isInitializationDelayed WRITE setInitializationDelayed)
0049     Q_PROPERTY(QObject *rootObject READ rootObject)
0050     Q_PROPERTY(QQmlComponent::Status status READ status NOTIFY statusChanged)
0051 
0052 public:
0053     /**
0054      * Construct a new PlasmaQuick::SharedQmlEngine
0055      *
0056      * @param parent The QObject parent for this object.
0057      */
0058     explicit SharedQmlEngine(QObject *parent = nullptr);
0059     explicit SharedQmlEngine(Plasma::Applet *applet, QObject *parent = nullptr);
0060 
0061     ~SharedQmlEngine() override;
0062 
0063     /**
0064      * Call this method before calling setupBindings to install a translation domain for all
0065      * i18n global functions. If a translation domain is set all i18n calls delegate to the
0066      * matching i18nd calls with the provided translation domain.
0067      *
0068      * The translationDomain affects all i18n calls including those from imports. Because of
0069      * that modules intended to be used as imports should prefer the i18nd variants and set
0070      * the translation domain explicitly in each call.
0071      *
0072      * This method is only required if your declarative usage is inside a library. If it's
0073      * in an application there is no need to set the translation domain as the application's
0074      * domain can be used.
0075      *
0076      * @param translationDomain The translation domain to be used for i18n calls.
0077      */
0078     void setTranslationDomain(const QString &translationDomain);
0079 
0080     /**
0081      * @return the translation domain for the i18n calls done in this QML engine
0082      */
0083     QString translationDomain() const;
0084 
0085     /**
0086      * Sets the path of the QML file to parse and execute
0087      *
0088      * @param path the absolute path of a QML file
0089      */
0090     void setSource(const QUrl &source);
0091 
0092     /**
0093      * @return the absolute path of the current QML file
0094      */
0095     QUrl source() const;
0096 
0097     /**
0098      * Sets whether the execution of the QML file has to be delayed later in the event loop. It has to be called before setQmlPath().
0099      * In this case it will be possible to assign new objects in the main engine context
0100      * before the main component gets initialized.
0101      * In that case it will be possible to access it immediately from the QML code.
0102      * The initialization will either be completed automatically asynchronously
0103      * or explicitly by calling completeInitialization()
0104      *
0105      * @param delay if true the initialization of the QML file will be delayed
0106      *              at the end of the event loop
0107      */
0108     void setInitializationDelayed(const bool delay);
0109 
0110     /**
0111      * @return true if the initialization of the QML file will be delayed
0112      *              at the end of the event loop
0113      */
0114     bool isInitializationDelayed() const;
0115 
0116     /**
0117      * @return the declarative engine that runs the qml file assigned to this widget.
0118      */
0119     std::shared_ptr<QQmlEngine> engine();
0120 
0121     /**
0122      * @return the root object of the declarative object tree
0123      */
0124     QObject *rootObject() const;
0125 
0126     /**
0127      * @return the main QQmlComponent of the engine
0128      */
0129     QQmlComponent *mainComponent() const;
0130 
0131     /**
0132      * The components's creation context.
0133      */
0134     QQmlContext *rootContext() const;
0135 
0136     /**
0137      * The component's current status.
0138      */
0139     QQmlComponent::Status status() const;
0140 
0141     /**
0142      * Creates and returns an object based on the provided url to a Qml file
0143      * with the same QQmlEngine and the same root context as the main object,
0144      * that will be the parent of the newly created object
0145      * @param source url where the QML file is located
0146      * @param context The QQmlContext in which we will create the object,
0147      *             if 0 it will use the engine's root context
0148      * @param initialProperties optional properties that will be set on
0149      *             the object when created (and before Component.onCompleted
0150      *             gets emitted
0151      */
0152     QObject *createObjectFromSource(const QUrl &source, QQmlContext *context = nullptr, const QVariantHash &initialProperties = QVariantHash());
0153 
0154     /**
0155      * Creates and returns an object based on the provided QQmlComponent
0156      * with the same QQmlEngine and the same root context as the admin object,
0157      * that will be the parent of the newly created object
0158      * @param component the component we want to instantiate
0159      * @param context The QQmlContext in which we will create the object,
0160      *             if 0 it will use the engine's root context
0161      * @param initialProperties optional properties that will be set on
0162      *             the object when created (and before Component.onCompleted
0163      *             gets emitted
0164      */
0165     QObject *createObjectFromComponent(QQmlComponent *component, QQmlContext *context = nullptr, const QVariantHash &initialProperties = QVariantHash());
0166 
0167 public Q_SLOTS:
0168     /**
0169      * Finishes the process of initialization.
0170      * If isInitializationDelayed() is false, calling this will have no effect.
0171      * @param initialProperties optional properties that will be set on
0172      *             the object when created (and before Component.onCompleted
0173      *             gets emitted
0174      */
0175     void completeInitialization(const QVariantHash &initialProperties = QVariantHash());
0176 
0177 Q_SIGNALS:
0178     /**
0179      * Emitted when the parsing and execution of the QML file is terminated
0180      */
0181     void finished();
0182 
0183     void statusChanged(QQmlComponent::Status);
0184 
0185 private:
0186     const std::unique_ptr<SharedQmlEnginePrivate> d;
0187 
0188     Q_PRIVATE_SLOT(d, void scheduleExecutionEnd())
0189 };
0190 
0191 }
0192 
0193 #endif // multiple inclusion guard