File indexing completed on 2024-05-12 05:37:12

0001 /*
0002     SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "appletcontainer.h"
0008 #include "containmentlayoutmanager_debug.h"
0009 
0010 #include <QQmlContext>
0011 #include <QQmlEngine>
0012 
0013 #include <Plasma/Applet>
0014 #include <Plasma/Containment>
0015 #include <PlasmaQuick/AppletQuickItem>
0016 #include <plasmaquick/appletquickitem.h>
0017 
0018 AppletContainer::AppletContainer(QQuickItem *parent)
0019     : ItemContainer(parent)
0020 {
0021     connect(this, &AppletContainer::contentItemChanged, this, [this]() {
0022         if (m_appletItem) {
0023             disconnect(m_appletItem->applet(), nullptr, this, nullptr);
0024         }
0025         m_appletItem = qobject_cast<PlasmaQuick::AppletQuickItem *>(contentItem());
0026 
0027         connectBusyIndicator();
0028         connectConfigurationRequired();
0029 
0030         Q_EMIT appletChanged();
0031     });
0032 }
0033 
0034 AppletContainer::~AppletContainer()
0035 {
0036 }
0037 
0038 void AppletContainer::componentComplete()
0039 {
0040     connectBusyIndicator();
0041     connectConfigurationRequired();
0042     ItemContainer::componentComplete();
0043 }
0044 
0045 PlasmaQuick::AppletQuickItem *AppletContainer::applet()
0046 {
0047     return m_appletItem;
0048 }
0049 
0050 QQmlComponent *AppletContainer::busyIndicatorComponent() const
0051 {
0052     return m_busyIndicatorComponent;
0053 }
0054 
0055 void AppletContainer::setBusyIndicatorComponent(QQmlComponent *component)
0056 {
0057     if (m_busyIndicatorComponent == component) {
0058         return;
0059     }
0060 
0061     m_busyIndicatorComponent = component;
0062 
0063     if (m_busyIndicatorItem) {
0064         m_busyIndicatorItem->deleteLater();
0065         m_busyIndicatorItem = nullptr;
0066     }
0067 
0068     Q_EMIT busyIndicatorComponentChanged();
0069 }
0070 
0071 void AppletContainer::connectBusyIndicator()
0072 {
0073     if (m_appletItem && !m_busyIndicatorItem) {
0074         Q_ASSERT(m_appletItem->applet());
0075         connect(m_appletItem->applet(), &Plasma::Applet::busyChanged, this, [this]() {
0076             if (!m_busyIndicatorComponent || !m_appletItem->applet()->isBusy() || m_busyIndicatorItem) {
0077                 return;
0078             }
0079 
0080             QQmlContext *context = QQmlEngine::contextForObject(this);
0081             Q_ASSERT(context);
0082             QObject *instance = m_busyIndicatorComponent->beginCreate(context);
0083             m_busyIndicatorItem = qobject_cast<QQuickItem *>(instance);
0084 
0085             if (!m_busyIndicatorItem) {
0086                 qCWarning(CONTAINMENTLAYOUTMANAGER_DEBUG) << "Error: busyIndicatorComponent not of Item type";
0087                 if (instance) {
0088                     instance->deleteLater();
0089                 }
0090                 return;
0091             }
0092 
0093             m_busyIndicatorItem->setParentItem(this);
0094             m_busyIndicatorItem->setZ(999);
0095             m_busyIndicatorComponent->completeCreate();
0096         });
0097     }
0098 }
0099 
0100 QQmlComponent *AppletContainer::configurationRequiredComponent() const
0101 {
0102     return m_configurationRequiredComponent;
0103 }
0104 
0105 void AppletContainer::setConfigurationRequiredComponent(QQmlComponent *component)
0106 {
0107     if (m_configurationRequiredComponent == component) {
0108         return;
0109     }
0110 
0111     m_configurationRequiredComponent = component;
0112 
0113     if (m_configurationRequiredItem) {
0114         m_configurationRequiredItem->deleteLater();
0115         m_configurationRequiredItem = nullptr;
0116     }
0117 
0118     Q_EMIT configurationRequiredComponentChanged();
0119 }
0120 
0121 void AppletContainer::connectConfigurationRequired()
0122 {
0123     if (m_appletItem && !m_configurationRequiredItem) {
0124         Q_ASSERT(m_appletItem->applet());
0125 
0126         auto syncConfigRequired = [this]() {
0127             if (!m_configurationRequiredComponent || !m_appletItem->applet()->configurationRequired() || m_configurationRequiredItem) {
0128                 return;
0129             }
0130 
0131             QQmlContext *context = QQmlEngine::contextForObject(this);
0132             Q_ASSERT(context);
0133             QObject *instance = m_configurationRequiredComponent->beginCreate(context);
0134             m_configurationRequiredItem = qobject_cast<QQuickItem *>(instance);
0135 
0136             if (!m_configurationRequiredItem) {
0137                 qCWarning(CONTAINMENTLAYOUTMANAGER_DEBUG) << "Error: configurationRequiredComponent not of Item type";
0138                 if (instance) {
0139                     instance->deleteLater();
0140                 }
0141                 return;
0142             }
0143 
0144             m_configurationRequiredItem->setParentItem(this);
0145             m_configurationRequiredItem->setZ(998);
0146             m_configurationRequiredComponent->completeCreate();
0147         };
0148 
0149         connect(m_appletItem->applet(), &Plasma::Applet::configurationRequiredChanged, this, syncConfigRequired);
0150 
0151         if (m_appletItem->applet()->configurationRequired()) {
0152             syncConfigRequired();
0153         }
0154     }
0155 }
0156 
0157 #include "moc_appletcontainer.cpp"