8#include "sharedqmlengine.h" 
    9#include "appletcontext_p.h" 
   11#include <KLocalizedContext> 
   15#include <QQmlNetworkAccessManagerFactory> 
   19#include <Plasma/Applet> 
   23#include <KLocalizedQmlContext> 
   28class SharedQmlEnginePrivate
 
   31    SharedQmlEnginePrivate(SharedQmlEngine *parent)
 
   37        executionEndTimer = 
new QTimer(q);
 
   38        executionEndTimer->setInterval(0);
 
   39        executionEndTimer->setSingleShot(
true);
 
   41            scheduleExecutionEnd();
 
   45    ~SharedQmlEnginePrivate() = 
default;
 
   47    void errorPrint(QQmlComponent *component);
 
   48    void beginExecute(
const QUrl &source);
 
   49    void beginExecute(QAnyStringView module, QAnyStringView type);
 
   51    void scheduleExecutionEnd();
 
   52    void minimumWidthChanged();
 
   53    void minimumHeightChanged();
 
   54    void maximumWidthChanged();
 
   55    void maximumHeightChanged();
 
   56    void preferredWidthChanged();
 
   57    void preferredHeightChanged();
 
   61    QPointer<QObject> rootObject;
 
   62    std::unique_ptr<QQmlComponent> component;
 
   63    QTimer *executionEndTimer;
 
   64    KLocalizedQmlContext *context{
nullptr};
 
   65    QQmlContext *rootContext;
 
   67    std::shared_ptr<QQmlEngine> m_engine;
 
   70    static std::shared_ptr<QQmlEngine> engine()
 
   72        if (
auto locked = s_engine.lock()) {
 
   75        auto createdEngine = std::make_shared<QQmlEngine>();
 
   76        s_engine = createdEngine;
 
   80    static std::weak_ptr<QQmlEngine> s_engine;
 
   83std::weak_ptr<QQmlEngine> SharedQmlEnginePrivate::s_engine = {};
 
   85void SharedQmlEnginePrivate::errorPrint(QQmlComponent *component)
 
   87    QString errorStr = QStringLiteral(
"Error loading QML file.\n");
 
   88    if (component->isError()) {
 
   89        const QList<QQmlError> errors = component->errors();
 
   90        for (
const QQmlError &error : errors) {
 
   95    qWarning(LOG_PLASMAQUICK) << component->url().toString() << 
'\n' << errorStr;
 
   98void SharedQmlEnginePrivate::beginExecute(
const QUrl &source)
 
  101        qWarning(LOG_PLASMAQUICK) << 
"File name empty!";
 
  104    component = std::make_unique<QQmlComponent>(m_engine.get());
 
  109    component->loadUrl(source);
 
  114void SharedQmlEnginePrivate::beginExecute(QAnyStringView module, QAnyStringView type)
 
  117        qWarning(LOG_PLASMAQUICK) << 
"No module or type specified";
 
  121    component = std::make_unique<QQmlComponent>(m_engine.get());
 
  126    component->loadFromModule(module, type);
 
  131void SharedQmlEnginePrivate::endExecute()
 
  133    rootObject = component->beginCreate(rootContext);
 
  136        executionEndTimer->start(0);
 
  138        scheduleExecutionEnd();
 
  142void SharedQmlEnginePrivate::scheduleExecutionEnd()
 
  144    if (component->isReady() || component->isError()) {
 
  145        q->completeInitialization();
 
  148            q->completeInitialization();
 
  155    , d(new SharedQmlEnginePrivate(this))
 
  158    d->rootContext->setParent(
this); 
 
  161    d->rootContext->setContextObject(d->context);
 
 
  166    , d(new SharedQmlEnginePrivate(this))
 
  168    d->rootContext = 
new AppletContext(
engine().get(), applet, 
this);
 
  171    d->rootContext->setContextObject(d->context);
 
  174SharedQmlEngine::~SharedQmlEngine()
 
  177        delete d->rootObject;
 
  183    d->context->setTranslationDomain(translationDomain);
 
 
  186QString SharedQmlEngine::translationDomain()
 const 
  188    return d->context->translationDomain();
 
 
  193    d->beginExecute(source);
 
 
  198    d->beginExecute(module, type);
 
 
  201QUrl SharedQmlEngine::source()
 const 
  204        return d->component->url();
 
 
  226    return d->rootObject;
 
 
  231    return d->component.get();
 
 
  236    return d->rootContext;
 
 
  254    d->executionEndTimer->stop();
 
  257        qWarning(LOG_PLASMAQUICK) << 
"No component for" << source();
 
  262        d->errorPrint(d->component.get());
 
  266    for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
 
  267        d->rootObject->setProperty(it.key().toUtf8().data(), it.value());
 
  270    d->component->completeCreate();
 
 
  286    for (
auto it = initialProperties.constBegin(); it != initialProperties.constEnd(); ++it) {
 
  287        object->setProperty(it.key().toUtf8().data(), it.value());
 
  291    if (!component->
isError() && 
object) {
 
  299                object->setParent(rootObject());
 
  306        d->errorPrint(component);
 
 
  313#include "moc_sharedqmlengine.cpp" 
SharedQmlEngine(QObject *parent=nullptr)
Construct a new PlasmaQuick::SharedQmlEngine.
 
QQmlContext * rootContext() const
The components's creation context.
 
void setInitializationDelayed(const bool delay)
Sets whether the execution of the QML file has to be delayed later in the event loop.
 
void setSource(const QUrl &source)
Sets the path of the QML file to parse and execute.
 
void finished()
Emitted when the parsing and execution of the QML file is terminated.
 
QQmlComponent * mainComponent() const
 
void setTranslationDomain(const QString &translationDomain)
Call this method before calling setupBindings to install a translation domain for all i18n global fun...
 
QObject * createObjectFromSource(const QUrl &source, QQmlContext *context=nullptr, const QVariantHash &initialProperties=QVariantHash())
Creates and returns an object based on the provided url to a Qml file with the same QQmlEngine and th...
 
void completeInitialization(const QVariantHash &initialProperties=QVariantHash())
Finishes the process of initialization.
 
void setSourceFromModule(QAnyStringView module, QAnyStringView type)
Sets the QML source to execute from a type in a module.
 
std::shared_ptr< QQmlEngine > engine()
 
QObject * createObjectFromComponent(QQmlComponent *component, QQmlContext *context=nullptr, const QVariantHash &initialProperties=QVariantHash())
Creates and returns an object based on the provided QQmlComponent with the same QQmlEngine and the sa...
 
bool isInitializationDelayed() const
 
Type type(const QSqlDatabase &db)
 
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
 
The EdgeEventForwarder class This class forwards edge events to be replayed within the given margin T...
 
bool isEmpty() const const
 
ObjectOwnership objectOwnership(QObject *object)
 
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QObject * parent() const const
 
T qobject_cast(QObject *object)
 
void setParent(QObject *parent)
 
virtual QObject * beginCreate(QQmlContext *context)
 
virtual void completeCreate()
 
bool isError() const const
 
void loadUrl(const QUrl &url)
 
void statusChanged(QQmlComponent::Status status)
 
QString number(double n, char format, int precision)
 
bool isEmpty() const const
 
QVariant fromValue(T &&value)