1 /*
2 SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "appletcontainer.h"
8
9 #include <QQmlContext>
10 #include <QQmlEngine>
11
12 #include <Plasma/Applet>
13 #include <PlasmaQuick/AppletQuickItem>
14
AppletContainer(QQuickItem * parent)15 AppletContainer::AppletContainer(QQuickItem *parent)
16 : ItemContainer(parent)
17 {
18 connect(this, &AppletContainer::contentItemChanged, this, [this]() {
19 if (m_appletItem) {
20 disconnect(m_appletItem->applet(), &Plasma::Applet::busyChanged, this, nullptr);
21 }
22 m_appletItem = qobject_cast<PlasmaQuick::AppletQuickItem *>(contentItem());
23
24 connectBusyIndicator();
25 connectConfigurationRequired();
26
27 emit appletChanged();
28 });
29 }
30
~AppletContainer()31 AppletContainer::~AppletContainer()
32 {
33 }
34
componentComplete()35 void AppletContainer::componentComplete()
36 {
37 connectBusyIndicator();
38 connectConfigurationRequired();
39 ItemContainer::componentComplete();
40 }
41
applet()42 PlasmaQuick::AppletQuickItem *AppletContainer::applet()
43 {
44 return m_appletItem;
45 }
46
busyIndicatorComponent() const47 QQmlComponent *AppletContainer::busyIndicatorComponent() const
48 {
49 return m_busyIndicatorComponent;
50 }
51
setBusyIndicatorComponent(QQmlComponent * component)52 void AppletContainer::setBusyIndicatorComponent(QQmlComponent *component)
53 {
54 if (m_busyIndicatorComponent == component) {
55 return;
56 }
57
58 m_busyIndicatorComponent = component;
59
60 if (m_busyIndicatorItem) {
61 m_busyIndicatorItem->deleteLater();
62 m_busyIndicatorItem = nullptr;
63 }
64
65 emit busyIndicatorComponentChanged();
66 }
67
connectBusyIndicator()68 void AppletContainer::connectBusyIndicator()
69 {
70 if (m_appletItem && !m_busyIndicatorItem) {
71 Q_ASSERT(m_appletItem->applet());
72 connect(m_appletItem->applet(), &Plasma::Applet::busyChanged, this, [this]() {
73 if (!m_busyIndicatorComponent || !m_appletItem->applet()->isBusy() || m_busyIndicatorItem) {
74 return;
75 }
76
77 QQmlContext *context = QQmlEngine::contextForObject(this);
78 Q_ASSERT(context);
79 QObject *instance = m_busyIndicatorComponent->beginCreate(context);
80 m_busyIndicatorItem = qobject_cast<QQuickItem *>(instance);
81
82 if (!m_busyIndicatorItem) {
83 qWarning() << "Error: busyIndicatorComponent not of Item type";
84 if (instance) {
85 instance->deleteLater();
86 }
87 return;
88 }
89
90 m_busyIndicatorItem->setParentItem(this);
91 m_busyIndicatorItem->setZ(999);
92 m_busyIndicatorComponent->completeCreate();
93 });
94 }
95 }
96
configurationRequiredComponent() const97 QQmlComponent *AppletContainer::configurationRequiredComponent() const
98 {
99 return m_configurationRequiredComponent;
100 }
101
setConfigurationRequiredComponent(QQmlComponent * component)102 void AppletContainer::setConfigurationRequiredComponent(QQmlComponent *component)
103 {
104 if (m_configurationRequiredComponent == component) {
105 return;
106 }
107
108 m_configurationRequiredComponent = component;
109
110 if (m_configurationRequiredItem) {
111 m_configurationRequiredItem->deleteLater();
112 m_configurationRequiredItem = nullptr;
113 }
114
115 emit configurationRequiredComponentChanged();
116 }
117
connectConfigurationRequired()118 void AppletContainer::connectConfigurationRequired()
119 {
120 if (m_appletItem && !m_configurationRequiredItem) {
121 Q_ASSERT(m_appletItem->applet());
122
123 auto syncConfigRequired = [this]() {
124 if (!m_configurationRequiredComponent || !m_appletItem->applet()->configurationRequired() || m_configurationRequiredItem) {
125 return;
126 }
127
128 QQmlContext *context = QQmlEngine::contextForObject(this);
129 Q_ASSERT(context);
130 QObject *instance = m_configurationRequiredComponent->beginCreate(context);
131 m_configurationRequiredItem = qobject_cast<QQuickItem *>(instance);
132
133 if (!m_configurationRequiredItem) {
134 qWarning() << "Error: configurationRequiredComponent not of Item type";
135 if (instance) {
136 instance->deleteLater();
137 }
138 return;
139 }
140
141 m_configurationRequiredItem->setParentItem(this);
142 m_configurationRequiredItem->setZ(998);
143 m_configurationRequiredComponent->completeCreate();
144 };
145
146 connect(m_appletItem->applet(), &Plasma::Applet::configurationRequiredChanged, this, syncConfigRequired);
147
148 if (m_appletItem->applet()->configurationRequired()) {
149 syncConfigRequired();
150 }
151 }
152 }
153
154 #include "moc_appletcontainer.cpp"
155