2017-02-14 15:33:45 -05:00
/*
2021-05-27 15:01:00 +00:00
SPDX - FileCopyrightText : 2014 Marco Martin < mart @ kde . org >
SPDX - License - Identifier : LGPL - 2.0 - or - later
*/
2017-02-14 15:33:45 -05:00
# include "alternativeshelper.h"
2018-12-02 02:05:52 +02:00
// Qt
2017-02-14 15:33:45 -05:00
# include <QQmlEngine>
# include <QQmlContext>
2018-12-02 02:05:52 +02:00
// KDE
2017-02-14 15:33:45 -05:00
# include <KPackage/Package>
# include <kconfig_version.h>
2018-12-02 02:05:52 +02:00
// Plasma
# include <Plasma/Containment>
# include <Plasma/PluginLoader>
2017-02-14 15:33:45 -05:00
AlternativesHelper : : AlternativesHelper ( Plasma : : Applet * applet , QObject * parent )
: QObject ( parent ) ,
m_applet ( applet )
{
}
AlternativesHelper : : ~ AlternativesHelper ( )
{
}
QStringList AlternativesHelper : : appletProvides ( ) const
{
# if KCONFIG_VERSION_MINOR >= 27
return KPluginMetaData : : readStringList ( m_applet - > pluginMetaData ( ) . rawData ( ) , QStringLiteral ( " X-Plasma-Provides " ) ) ;
# else
return KPluginMetaData : : readStringList ( m_applet - > pluginInfo ( ) . toMetaData ( ) . rawData ( ) , QStringLiteral ( " X-Plasma-Provides " ) ) ;
# endif
}
QString AlternativesHelper : : currentPlugin ( ) const
{
# if KCONFIG_VERSION_MINOR >= 27
return m_applet - > pluginMetaData ( ) . pluginId ( ) ;
# else
return m_applet - > pluginInfo ( ) . toMetaData ( ) . pluginId ( ) ;
# endif
}
QQuickItem * AlternativesHelper : : applet ( ) const
{
return m_applet - > property ( " _plasma_graphicObject " ) . value < QQuickItem * > ( ) ;
}
void AlternativesHelper : : loadAlternative ( const QString & plugin )
{
if ( plugin = = currentPlugin ( ) | | m_applet - > isContainment ( ) ) {
return ;
}
Plasma : : Containment * cont = m_applet - > containment ( ) ;
2017-03-12 15:14:30 -05:00
2017-02-14 15:33:45 -05:00
if ( ! cont ) {
return ;
}
QQuickItem * appletItem = m_applet - > property ( " _plasma_graphicObject " ) . value < QQuickItem * > ( ) ;
QQuickItem * contItem = cont - > property ( " _plasma_graphicObject " ) . value < QQuickItem * > ( ) ;
2017-03-12 15:14:30 -05:00
2017-02-14 15:33:45 -05:00
if ( ! appletItem | | ! contItem ) {
return ;
}
// ensure the global shortcut is moved to the new applet
const QKeySequence & shortcut = m_applet - > globalShortcut ( ) ;
m_applet - > setGlobalShortcut ( QKeySequence ( ) ) ; // need to unmap the old one first
2017-03-12 15:14:30 -05:00
const QPoint newPos = appletItem - > mapToItem ( contItem , QPointF ( 0 , 0 ) ) . toPoint ( ) ;
2017-02-14 15:33:45 -05:00
m_applet - > destroy ( ) ;
2017-03-12 15:14:30 -05:00
connect ( m_applet , & QObject : : destroyed , [ = ] ( ) {
2017-02-14 15:33:45 -05:00
Plasma : : Applet * newApplet = Q_NULLPTR ;
QMetaObject : : invokeMethod ( contItem , " createApplet " , Q_RETURN_ARG ( Plasma : : Applet * , newApplet ) , Q_ARG ( QString , plugin ) , Q_ARG ( QVariantList , QVariantList ( ) ) , Q_ARG ( QPoint , newPos ) ) ;
if ( newApplet ) {
newApplet - > setGlobalShortcut ( shortcut ) ;
}
} ) ;
}
# include "moc_alternativeshelper.cpp"