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