File indexing completed on 2024-04-28 05:35:57

0001 /*
0002     SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "alternativeshelper.h"
0008 
0009 #include <QQmlContext>
0010 #include <QQmlEngine>
0011 
0012 #include <KConfigLoader>
0013 
0014 #include <Plasma/Containment>
0015 #include <Plasma/PluginLoader>
0016 #include <PlasmaQuick/AppletQuickItem>
0017 
0018 AlternativesHelper::AlternativesHelper(Plasma::Applet *applet, QObject *parent)
0019     : QObject(parent)
0020     , m_applet(applet)
0021 {
0022 }
0023 
0024 AlternativesHelper::~AlternativesHelper()
0025 {
0026 }
0027 
0028 QStringList AlternativesHelper::appletProvides() const
0029 {
0030     return m_applet->pluginMetaData().value(QStringLiteral("X-Plasma-Provides"), QStringList());
0031 }
0032 
0033 QString AlternativesHelper::currentPlugin() const
0034 {
0035     return m_applet->pluginMetaData().pluginId();
0036 }
0037 
0038 QQuickItem *AlternativesHelper::applet() const
0039 {
0040     return PlasmaQuick::AppletQuickItem::itemForApplet(m_applet);
0041 }
0042 
0043 void AlternativesHelper::loadAlternative(const QString &plugin)
0044 {
0045     if (plugin == m_applet->pluginMetaData().pluginId() || m_applet->isContainment()) {
0046         return;
0047     }
0048 
0049     Plasma::Containment *cont = m_applet->containment();
0050     if (!cont) {
0051         return;
0052     }
0053 
0054     QQuickItem *appletItem = PlasmaQuick::AppletQuickItem::itemForApplet(m_applet);
0055     QQuickItem *contItem = PlasmaQuick::AppletQuickItem::itemForApplet(cont);
0056     if (!appletItem || !contItem) {
0057         return;
0058     }
0059 
0060     // ensure the global shortcut is moved to the new applet
0061     const QKeySequence &shortcut = m_applet->globalShortcut();
0062     m_applet->setGlobalShortcut(QKeySequence()); // need to unmap the old one first
0063 
0064     const QPoint newPos = appletItem->mapToItem(contItem, QPointF(0, 0)).toPoint();
0065 
0066     Plasma::Applet *newApplet = cont->createApplet(plugin, {}, QRectF(newPos, QSizeF()));
0067 
0068     if (newApplet) {
0069         newApplet->setGlobalShortcut(shortcut);
0070         KConfigGroup newCg(newApplet->config());
0071         m_applet->config().copyTo(&newCg);
0072         // To let ConfigPropertyMap reload its config
0073         Q_EMIT newApplet->configScheme()->configChanged();
0074     }
0075 
0076     m_applet->destroy();
0077 }
0078 
0079 #include "moc_alternativeshelper.cpp"