2018-02-03 16:00:44 +03:00
/*
* Copyright 2018 Michail Vourlakos < mvourlakos @ gmail . com >
*
* This file is part of Latte - Dock
*
* Latte - Dock is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License , or ( at your option ) any later version .
*
* Latte - Dock is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2018-12-02 01:53:40 +03:00
# include "contextmenu.h"
2018-02-03 16:00:44 +03:00
2018-12-02 03:05:52 +03:00
// local
2018-12-06 13:15:58 +03:00
# include "view.h"
2018-02-03 16:00:44 +03:00
# include "visibilitymanager.h"
2018-12-06 15:35:34 +03:00
# include "../lattecorona.h"
2018-02-03 16:00:44 +03:00
2018-12-02 03:05:52 +03:00
// Qt
2018-02-03 16:00:44 +03:00
# include <QMouseEvent>
# include <QVersionNumber>
2018-12-02 03:05:52 +03:00
// KDE
2018-02-03 16:00:44 +03:00
# include <KActionCollection>
# include <KAuthorized>
# include <KLocalizedString>
2018-12-02 03:05:52 +03:00
// Plasma
2018-02-03 16:00:44 +03:00
# include <Plasma/Applet>
# include <Plasma/Containment>
# include <Plasma/ContainmentActions>
# include <Plasma/Corona>
# include <PlasmaQuick/AppletQuickItem>
2020-04-05 14:40:32 +03:00
# define BLOCKHIDINGTYPE "View::contextMenu()"
2018-02-03 16:00:44 +03:00
namespace Latte {
2018-12-06 12:37:14 +03:00
namespace ViewPart {
2018-02-03 16:00:44 +03:00
2018-12-06 13:15:58 +03:00
ContextMenu : : ContextMenu ( Latte : : View * view ) :
2018-02-03 16:00:44 +03:00
QObject ( view ) ,
2018-12-06 13:51:15 +03:00
m_latteView ( view )
2018-02-03 16:00:44 +03:00
{
}
2018-12-02 01:53:40 +03:00
ContextMenu : : ~ ContextMenu ( )
2018-02-03 16:00:44 +03:00
{
}
2018-12-02 01:53:40 +03:00
QMenu * ContextMenu : : menu ( )
2018-02-03 16:00:44 +03:00
{
return m_contextMenu ;
}
2018-12-02 01:53:40 +03:00
void ContextMenu : : menuAboutToHide ( )
2018-02-03 16:00:44 +03:00
{
2018-12-06 13:51:15 +03:00
if ( ! m_latteView ) {
2018-02-03 16:00:44 +03:00
return ;
}
m_contextMenu = 0 ;
2018-03-05 18:43:39 +03:00
2018-12-02 01:53:40 +03:00
emit menuChanged ( ) ;
2018-02-03 16:00:44 +03:00
}
2020-04-11 00:41:29 +03:00
QPoint ContextMenu : : popUpRelevantToParent ( const QRect & parentItem , const QRect popUpRect )
2020-04-08 20:31:29 +03:00
{
QPoint resultPoint ;
2020-04-11 00:41:29 +03:00
if ( m_latteView - > location ( ) = = Plasma : : Types : : TopEdge ) {
resultPoint . setX ( parentItem . left ( ) ) ;
resultPoint . setY ( parentItem . bottom ( ) ) ;
} else if ( m_latteView - > location ( ) = = Plasma : : Types : : BottomEdge ) {
resultPoint . setX ( parentItem . left ( ) ) ;
resultPoint . setY ( parentItem . top ( ) - popUpRect . height ( ) - 1 ) ;
} else if ( m_latteView - > location ( ) = = Plasma : : Types : : LeftEdge ) {
resultPoint . setX ( parentItem . right ( ) ) ;
resultPoint . setY ( parentItem . top ( ) ) ;
} else if ( m_latteView - > location ( ) = = Plasma : : Types : : RightEdge ) {
resultPoint . setX ( parentItem . left ( ) - popUpRect . width ( ) ) ;
resultPoint . setY ( parentItem . top ( ) ) ;
2020-04-08 20:31:29 +03:00
}
return resultPoint ;
}
2020-04-11 00:41:29 +03:00
QPoint ContextMenu : : popUpRelevantToGlobalPoint ( const QRect & parentItem , const QRect popUpRect )
{
QPoint resultPoint ;
if ( m_latteView - > location ( ) = = Plasma : : Types : : TopEdge ) {
resultPoint . setX ( popUpRect . x ( ) ) ;
resultPoint . setY ( popUpRect . y ( ) + 1 ) ;
} else if ( m_latteView - > location ( ) = = Plasma : : Types : : BottomEdge ) {
resultPoint . setX ( popUpRect . x ( ) ) ;
resultPoint . setY ( popUpRect . y ( ) - popUpRect . height ( ) - 1 ) ;
} else if ( m_latteView - > location ( ) = = Plasma : : Types : : LeftEdge ) {
resultPoint . setX ( popUpRect . x ( ) + 1 ) ;
resultPoint . setY ( popUpRect . y ( ) ) ;
} else if ( m_latteView - > location ( ) = = Plasma : : Types : : RightEdge ) {
resultPoint . setX ( popUpRect . x ( ) - popUpRect . width ( ) - 1 ) ;
resultPoint . setY ( popUpRect . y ( ) ) ;
}
return resultPoint ;
}
QPoint ContextMenu : : popUpTopLeft ( Plasma : : Applet * applet , const QRect popUpRect )
2020-04-08 20:31:29 +03:00
{
PlasmaQuick : : AppletQuickItem * ai = applet - > property ( " _plasma_graphicObject " ) . value < PlasmaQuick : : AppletQuickItem * > ( ) ;
2020-04-11 00:41:29 +03:00
QRect globalItemRect = m_latteView - > absoluteGeometry ( ) ;
2020-04-08 20:31:29 +03:00
2020-04-11 00:41:29 +03:00
if ( ai & & applet ! = m_latteView - > containment ( ) ) {
2020-04-08 20:31:29 +03:00
QPointF appletGlobalTopLeft = ai - > mapToGlobal ( QPointF ( ai - > x ( ) , ai - > y ( ) ) ) ;
globalItemRect = QRect ( appletGlobalTopLeft . x ( ) , appletGlobalTopLeft . y ( ) , ai - > width ( ) , ai - > height ( ) ) ;
}
2020-04-11 18:31:36 +03:00
int itemLength = ( m_latteView - > formFactor ( ) = = Plasma : : Types : : Horizontal ? globalItemRect . width ( ) : globalItemRect . height ( ) ) ;
int menuLength = ( m_latteView - > formFactor ( ) = = Plasma : : Types : : Horizontal ? popUpRect . width ( ) : popUpRect . height ( ) ) ;
if ( ( itemLength > menuLength )
2020-04-11 00:41:29 +03:00
| | ( applet = = m_latteView - > containment ( ) )
| | ( m_latteView & & m_latteView - > layout ( ) & & m_latteView - > layout ( ) - > isInternalContainment ( applet ) ) ) {
return popUpRelevantToGlobalPoint ( globalItemRect , popUpRect ) ;
} else {
return popUpRelevantToParent ( globalItemRect , popUpRect ) ;
}
2020-04-08 20:31:29 +03:00
}
2020-07-11 20:13:19 +03:00
bool ContextMenu : : mousePressEventForContainmentMenu ( QQuickView * view , QMouseEvent * event )
{
if ( ! event | | ! view | | ! m_latteView - > containment ( ) ) {
return false ;
}
if ( m_contextMenu ) {
m_contextMenu - > close ( ) ;
m_contextMenu = 0 ;
return false ;
}
QString trigger = Plasma : : ContainmentActions : : eventToString ( event ) ;
if ( trigger = = " RightButton;NoModifier " ) {
Plasma : : ContainmentActions * plugin = m_latteView - > containment ( ) - > containmentActions ( ) . value ( trigger ) ;
if ( ! plugin | | plugin - > contextualActions ( ) . isEmpty ( ) ) {
event - > setAccepted ( false ) ;
return false ;
}
2020-07-11 20:57:54 +03:00
if ( m_latteView - > containment ( ) ) {
QMenu * desktopMenu = new QMenu ;
desktopMenu - > setAttribute ( Qt : : WA_TranslucentBackground ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
if ( desktopMenu - > winId ( ) ) {
desktopMenu - > windowHandle ( ) - > setTransientParent ( m_latteView ) ;
}
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
desktopMenu - > setAttribute ( Qt : : WA_DeleteOnClose ) ;
m_contextMenu = desktopMenu ;
emit menuChanged ( ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
auto ungrabMouseHack = [ this ] ( ) {
if ( m_latteView - > mouseGrabberItem ( ) ) {
m_latteView - > mouseGrabberItem ( ) - > ungrabMouse ( ) ;
}
} ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
if ( QVersionNumber : : fromString ( qVersion ( ) ) > QVersionNumber ( 5 , 8 , 0 ) ) {
QTimer : : singleShot ( 0 , this , ungrabMouseHack ) ;
} else {
ungrabMouseHack ( ) ;
2020-07-11 20:13:19 +03:00
}
2020-07-11 20:57:54 +03:00
emit m_latteView - > containment ( ) - > contextualActionsAboutToShow ( ) ;
addContainmentActions ( desktopMenu , event ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
desktopMenu - > setAttribute ( Qt : : WA_TranslucentBackground ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
QPoint globalPos = event - > globalPos ( ) ;
desktopMenu - > adjustSize ( ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
QRect popUpRect ( globalPos . x ( ) , globalPos . y ( ) , desktopMenu - > width ( ) , desktopMenu - > height ( ) ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
globalPos = popUpRelevantToGlobalPoint ( QRect ( 0 , 0 , 0 , 0 ) , popUpRect ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
if ( desktopMenu - > isEmpty ( ) ) {
delete desktopMenu ;
event - > accept ( ) ;
return false ;
}
connect ( desktopMenu , SIGNAL ( aboutToHide ( ) ) , this , SLOT ( menuAboutToHide ( ) ) ) ;
2020-07-11 20:13:19 +03:00
2020-07-11 20:57:54 +03:00
desktopMenu - > popup ( globalPos ) ;
event - > setAccepted ( true ) ;
2020-07-11 20:13:19 +03:00
return false ;
}
}
return true ;
}
2020-04-08 20:31:29 +03:00
2018-12-02 01:53:40 +03:00
bool ContextMenu : : mousePressEvent ( QMouseEvent * event )
2018-02-03 16:00:44 +03:00
{
//qDebug() << "Step -1 ...";
2018-02-03 19:44:08 +03:00
2018-12-06 13:51:15 +03:00
if ( ! event | | ! m_latteView - > containment ( ) ) {
2018-02-03 16:00:44 +03:00
return false ;
}
//qDebug() << "Step 0...";
//even if the menu is executed synchronously, other events may be processed
//by the qml incubator when plasma is loading, so we need to guard there
if ( m_contextMenu ) {
//qDebug() << "Step 0.5 ...";
m_contextMenu - > close ( ) ;
m_contextMenu = 0 ;
2018-03-02 21:21:29 +03:00
return false ;
2018-02-03 16:00:44 +03:00
}
//qDebug() << "1 ...";
2018-02-03 19:44:08 +03:00
QString trigger = Plasma : : ContainmentActions : : eventToString ( event ) ;
2018-02-03 16:00:44 +03:00
if ( trigger = = " RightButton;NoModifier " ) {
2018-12-06 13:51:15 +03:00
Plasma : : ContainmentActions * plugin = m_latteView - > containment ( ) - > containmentActions ( ) . value ( trigger ) ;
2018-02-03 16:00:44 +03:00
if ( ! plugin | | plugin - > contextualActions ( ) . isEmpty ( ) ) {
event - > setAccepted ( false ) ;
return false ;
}
//qDebug() << "2 ...";
//the plugin can be a single action or a context menu
//Don't have an action list? execute as single action
//and set the event position as action data
/*if (plugin->contextualActions().length() == 1) {
QAction * action = plugin - > contextualActions ( ) . at ( 0 ) ;
action - > setData ( event - > pos ( ) ) ;
action - > trigger ( ) ;
event - > accept ( ) ;
return ;
} */
//FIXME: very inefficient appletAt() implementation
Plasma : : Applet * applet = 0 ;
bool inSystray = false ;
//! initialize the appletContainsMethod on the first right click
if ( ! m_appletContainsMethod . isValid ( ) ) {
updateAppletContainsMethod ( ) ;
}
2019-04-04 23:55:44 +03:00
for ( const Plasma : : Applet * appletTemp : m_latteView - > containment ( ) - > applets ( ) ) {
2018-02-03 16:00:44 +03:00
PlasmaQuick : : AppletQuickItem * ai = appletTemp - > property ( " _plasma_graphicObject " ) . value < PlasmaQuick : : AppletQuickItem * > ( ) ;
bool appletContainsMouse = false ;
if ( m_appletContainsMethod . isValid ( ) ) {
QVariant retVal ;
m_appletContainsMethod . invoke ( m_appletContainsMethodItem , Qt : : DirectConnection , Q_RETURN_ARG ( QVariant , retVal )
, Q_ARG ( QVariant , appletTemp - > id ( ) ) , Q_ARG ( QVariant , event - > pos ( ) ) ) ;
appletContainsMouse = retVal . toBool ( ) ;
} else {
2018-12-06 13:51:15 +03:00
appletContainsMouse = ai - > contains ( ai - > mapFromItem ( m_latteView - > contentItem ( ) , event - > pos ( ) ) ) ;
2018-02-03 16:00:44 +03:00
}
if ( ai & & ai - > isVisible ( ) & & appletContainsMouse ) {
applet = ai - > applet ( ) ;
2020-03-24 21:29:10 +03:00
2020-03-24 21:07:14 +03:00
if ( m_latteView & & m_latteView - > layout ( ) & & m_latteView - > layout ( ) - > isInternalContainment ( applet ) ) {
Plasma : : Containment * internalC = m_latteView - > layout ( ) - > internalContainmentOf ( applet ) ;
if ( internalC ) {
2020-03-26 15:40:00 +03:00
Plasma : : Applet * internalApplet { nullptr } ;
2020-03-24 21:07:14 +03:00
for ( const Plasma : : Applet * appletCont : internalC - > applets ( ) ) {
2018-02-03 16:00:44 +03:00
PlasmaQuick : : AppletQuickItem * ai2 = appletCont - > property ( " _plasma_graphicObject " ) . value < PlasmaQuick : : AppletQuickItem * > ( ) ;
2018-12-06 13:51:15 +03:00
if ( ai2 & & ai2 - > isVisible ( ) & & ai2 - > contains ( ai2 - > mapFromItem ( m_latteView - > contentItem ( ) , event - > pos ( ) ) ) ) {
2020-03-26 15:40:00 +03:00
internalApplet = ai2 - > applet ( ) ;
2018-02-03 16:00:44 +03:00
break ;
}
}
2020-03-26 15:40:00 +03:00
if ( ! internalApplet ) {
return true ;
} else {
applet = internalApplet ;
}
2018-02-03 16:00:44 +03:00
}
break ;
} else {
ai = 0 ;
}
}
}
if ( ! applet & & ! inSystray ) {
2018-12-06 13:51:15 +03:00
applet = m_latteView - > containment ( ) ;
2018-02-03 16:00:44 +03:00
}
//qDebug() << "3 ...";
if ( applet ) {
const auto & provides = KPluginMetaData : : readStringList ( applet - > pluginMetaData ( ) . rawData ( ) , QStringLiteral ( " X-Plasma-Provides " ) ) ;
//qDebug() << "3.5 ...";
if ( ! provides . contains ( QLatin1String ( " org.kde.plasma.multitasking " ) ) ) {
//qDebug() << "4...";
QMenu * desktopMenu = new QMenu ;
2018-03-02 21:21:29 +03:00
//this is a workaround where Qt now creates the menu widget
//in .exec before oxygen can polish it and set the following attribute
desktopMenu - > setAttribute ( Qt : : WA_TranslucentBackground ) ;
//end workaround
if ( desktopMenu - > winId ( ) ) {
2018-12-06 13:51:15 +03:00
desktopMenu - > windowHandle ( ) - > setTransientParent ( m_latteView ) ;
2018-03-02 21:21:29 +03:00
}
2018-02-03 16:00:44 +03:00
desktopMenu - > setAttribute ( Qt : : WA_DeleteOnClose ) ;
m_contextMenu = desktopMenu ;
2020-05-04 18:56:34 +03:00
emit menuChanged ( ) ;
2018-02-03 16:00:44 +03:00
2018-08-30 08:47:53 +03:00
//! deprecated old code that can be removed if the following plasma approach doesn't
2018-02-03 16:00:44 +03:00
//! create any issues with context menu creation in Latte
2018-12-06 13:51:15 +03:00
/*if (m_latteView->mouseGrabberItem()) {
2018-02-03 16:00:44 +03:00
//workaround, this fixes for me most of the right click menu behavior
2018-12-06 13:51:15 +03:00
m_latteView - > mouseGrabberItem ( ) - > ungrabMouse ( ) ;
2018-02-03 16:00:44 +03:00
return ;
} */
//!plasma official code
2019-08-18 19:59:09 +03:00
//this is a workaround where Qt will fail to realize a mouse has been released
2018-02-03 16:00:44 +03:00
// this happens if a window which does not accept focus spawns a new window that takes focus and X grab
// whilst the mouse is depressed
// https://bugreports.qt.io/browse/QTBUG-59044
// this causes the next click to go missing
//by releasing manually we avoid that situation
auto ungrabMouseHack = [ this ] ( ) {
2018-12-06 13:51:15 +03:00
if ( m_latteView - > mouseGrabberItem ( ) ) {
m_latteView - > mouseGrabberItem ( ) - > ungrabMouse ( ) ;
2018-02-03 16:00:44 +03:00
}
} ;
//pre 5.8.0 QQuickWindow code is "item->grabMouse(); sendEvent(item, mouseEvent)"
//post 5.8.0 QQuickWindow code is sendEvent(item, mouseEvent); item->grabMouse()
if ( QVersionNumber : : fromString ( qVersion ( ) ) > QVersionNumber ( 5 , 8 , 0 ) ) {
QTimer : : singleShot ( 0 , this , ungrabMouseHack ) ;
} else {
ungrabMouseHack ( ) ;
}
//end workaround
//!end of plasma official code(workaround)
//qDebug() << "5 ...";
2018-12-06 13:51:15 +03:00
if ( applet & & applet ! = m_latteView - > containment ( ) ) {
2018-02-03 16:00:44 +03:00
//qDebug() << "5.3 ...";
emit applet - > contextualActionsAboutToShow ( ) ;
addAppletActions ( desktopMenu , applet , event ) ;
} else {
//qDebug() << "5.6 ...";
2018-12-06 13:51:15 +03:00
emit m_latteView - > containment ( ) - > contextualActionsAboutToShow ( ) ;
2018-02-03 16:00:44 +03:00
addContainmentActions ( desktopMenu , event ) ;
}
//this is a workaround where Qt now creates the menu widget
//in .exec before oxygen can polish it and set the following attribute
desktopMenu - > setAttribute ( Qt : : WA_TranslucentBackground ) ;
//end workaround
2020-04-08 20:31:29 +03:00
QPoint globalPos = event - > globalPos ( ) ;
desktopMenu - > adjustSize ( ) ;
2018-02-03 16:00:44 +03:00
2020-04-08 20:31:29 +03:00
QRect popUpRect ( globalPos . x ( ) , globalPos . y ( ) , desktopMenu - > width ( ) , desktopMenu - > height ( ) ) ;
2018-02-03 16:00:44 +03:00
2020-04-08 20:31:29 +03:00
if ( applet ) {
globalPos = popUpTopLeft ( applet , popUpRect ) ;
} else {
2020-04-11 00:41:29 +03:00
globalPos = popUpRelevantToGlobalPoint ( QRect ( 0 , 0 , 0 , 0 ) , popUpRect ) ;
2018-02-03 16:00:44 +03:00
}
//qDebug() << "7...";
if ( desktopMenu - > isEmpty ( ) ) {
//qDebug() << "7.5 ...";
delete desktopMenu ;
event - > accept ( ) ;
return false ;
}
connect ( desktopMenu , SIGNAL ( aboutToHide ( ) ) , this , SLOT ( menuAboutToHide ( ) ) ) ;
2020-04-05 14:40:32 +03:00
2020-04-08 20:31:29 +03:00
desktopMenu - > popup ( globalPos ) ;
2018-02-03 16:00:44 +03:00
event - > setAccepted ( true ) ;
return false ;
}
//qDebug() << "8 ...";
}
//qDebug() << "9 ...";
}
//qDebug() << "10 ...";
return true ;
// PlasmaQuick::ContainmentView::mousePressEvent(event);
}
//! update the appletContainsPos method from Panel view
2018-12-02 01:53:40 +03:00
void ContextMenu : : updateAppletContainsMethod ( )
2018-02-03 16:00:44 +03:00
{
2018-12-06 13:51:15 +03:00
for ( QQuickItem * item : m_latteView - > contentItem ( ) - > childItems ( ) ) {
2018-02-03 16:00:44 +03:00
if ( auto * metaObject = item - > metaObject ( ) ) {
// not using QMetaObject::invokeMethod to avoid warnings when calling
// this on applets that don't have it or other child items since this
// is pretty much trial and error.
// Also, "var" arguments are treated as QVariant in QMetaObject
int methodIndex = metaObject - > indexOfMethod ( " appletContainsPos(QVariant,QVariant) " ) ;
if ( methodIndex = = - 1 ) {
continue ;
}
m_appletContainsMethod = metaObject - > method ( methodIndex ) ;
m_appletContainsMethodItem = item ;
}
}
}
2018-12-02 01:53:40 +03:00
void ContextMenu : : addAppletActions ( QMenu * desktopMenu , Plasma : : Applet * applet , QEvent * event )
2018-02-03 16:00:44 +03:00
{
2018-12-06 13:51:15 +03:00
if ( ! m_latteView - > containment ( ) ) {
2018-02-03 16:00:44 +03:00
return ;
}
2019-04-04 23:55:44 +03:00
for ( QAction * action : applet - > contextualActions ( ) ) {
2018-02-03 16:00:44 +03:00
if ( action ) {
desktopMenu - > addAction ( action ) ;
}
}
if ( ! applet - > failedToLaunch ( ) ) {
QAction * runAssociatedApplication = applet - > actions ( ) - > action ( QStringLiteral ( " run associated application " ) ) ;
if ( runAssociatedApplication & & runAssociatedApplication - > isEnabled ( ) ) {
desktopMenu - > addAction ( runAssociatedApplication ) ;
}
QAction * configureApplet = applet - > actions ( ) - > action ( QStringLiteral ( " configure " ) ) ;
if ( configureApplet & & configureApplet - > isEnabled ( ) ) {
desktopMenu - > addAction ( configureApplet ) ;
}
QAction * appletAlternatives = applet - > actions ( ) - > action ( QStringLiteral ( " alternatives " ) ) ;
2018-12-06 13:51:15 +03:00
if ( appletAlternatives & & appletAlternatives - > isEnabled ( ) & & m_latteView - > containment ( ) - > isUserConfiguring ( ) ) {
2018-02-03 16:00:44 +03:00
desktopMenu - > addAction ( appletAlternatives ) ;
}
}
2018-03-02 21:21:29 +03:00
QAction * containmentAction = desktopMenu - > menuAction ( ) ;
2018-12-06 13:51:15 +03:00
containmentAction - > setText ( i18nc ( " %1 is the name of the containment " , " %1 Options " , m_latteView - > containment ( ) - > title ( ) ) ) ;
2018-03-02 21:21:29 +03:00
addContainmentActions ( containmentAction - > menu ( ) , event ) ;
2018-02-03 16:00:44 +03:00
2018-03-02 21:21:29 +03:00
if ( ! containmentAction - > menu ( ) - > isEmpty ( ) ) {
2018-02-03 16:00:44 +03:00
int enabled = 0 ;
//count number of real actions
2018-03-02 21:21:29 +03:00
QListIterator < QAction * > actionsIt ( containmentAction - > menu ( ) - > actions ( ) ) ;
2018-02-03 16:00:44 +03:00
while ( enabled < 3 & & actionsIt . hasNext ( ) ) {
QAction * action = actionsIt . next ( ) ;
if ( action - > isVisible ( ) & & ! action - > isSeparator ( ) ) {
+ + enabled ;
}
}
desktopMenu - > addSeparator ( ) ;
if ( enabled ) {
//if there is only one, don't create a submenu
// if (enabled < 2) {
2019-04-04 23:55:44 +03:00
for ( QAction * action : containmentAction - > menu ( ) - > actions ( ) ) {
if ( action & & action - > isVisible ( ) ) {
2018-02-03 16:00:44 +03:00
desktopMenu - > addAction ( action ) ;
}
}
// } else {
// desktopMenu->addMenu(containmentMenu);
// }
}
}
2018-12-06 13:51:15 +03:00
if ( m_latteView - > containment ( ) - > immutability ( ) = = Plasma : : Types : : Mutable & &
2020-04-11 00:41:29 +03:00
( m_latteView - > containment ( ) - > containmentType ( ) ! = Plasma : : Types : : PanelContainment | | m_latteView - > containment ( ) - > isUserConfiguring ( ) ) ) {
2018-02-03 16:00:44 +03:00
QAction * closeApplet = applet - > actions ( ) - > action ( QStringLiteral ( " remove " ) ) ;
//qDebug() << "checking for removal" << closeApplet;
if ( closeApplet ) {
if ( ! desktopMenu - > isEmpty ( ) ) {
desktopMenu - > addSeparator ( ) ;
}
//qDebug() << "adding close action" << closeApplet->isEnabled() << closeApplet->isVisible();
desktopMenu - > addAction ( closeApplet ) ;
}
}
}
2018-12-02 01:53:40 +03:00
void ContextMenu : : addContainmentActions ( QMenu * desktopMenu , QEvent * event )
2018-02-03 16:00:44 +03:00
{
2018-12-06 13:51:15 +03:00
if ( ! m_latteView - > containment ( ) ) {
2018-02-03 16:00:44 +03:00
return ;
}
2018-12-06 13:51:15 +03:00
if ( m_latteView - > containment ( ) - > corona ( ) - > immutability ( ) ! = Plasma : : Types : : Mutable & &
2020-04-11 00:41:29 +03:00
! KAuthorized : : authorizeAction ( QStringLiteral ( " plasma/containment_actions " ) ) ) {
2018-02-03 16:00:44 +03:00
//qDebug() << "immutability";
return ;
}
//this is what ContainmentPrivate::prepareContainmentActions was
const QString trigger = Plasma : : ContainmentActions : : eventToString ( event ) ;
//"RightButton;NoModifier"
2018-12-06 13:51:15 +03:00
Plasma : : ContainmentActions * plugin = m_latteView - > containment ( ) - > containmentActions ( ) . value ( trigger ) ;
2018-02-03 16:00:44 +03:00
if ( ! plugin ) {
return ;
}
2018-12-06 13:51:15 +03:00
if ( plugin - > containment ( ) ! = m_latteView - > containment ( ) ) {
plugin - > setContainment ( m_latteView - > containment ( ) ) ;
2018-02-03 16:00:44 +03:00
// now configure it
2018-12-06 13:51:15 +03:00
KConfigGroup cfg ( m_latteView - > containment ( ) - > corona ( ) - > config ( ) , " ActionPlugins " ) ;
cfg = KConfigGroup ( & cfg , QString : : number ( m_latteView - > containment ( ) - > containmentType ( ) ) ) ;
2018-02-03 16:00:44 +03:00
KConfigGroup pluginConfig = KConfigGroup ( & cfg , trigger ) ;
plugin - > restore ( pluginConfig ) ;
}
QList < QAction * > actions = plugin - > contextualActions ( ) ;
2019-04-04 23:55:44 +03:00
for ( const QAction * act : actions ) {
2018-03-02 21:21:29 +03:00
if ( act - > menu ( ) ) {
//this is a workaround where Qt now creates the menu widget
//in .exec before oxygen can polish it and set the following attribute
act - > menu ( ) - > setAttribute ( Qt : : WA_TranslucentBackground ) ;
//end workaround
if ( act - > menu ( ) - > winId ( ) ) {
2018-12-06 13:51:15 +03:00
act - > menu ( ) - > windowHandle ( ) - > setTransientParent ( m_latteView ) ;
2018-03-02 21:21:29 +03:00
}
}
}
2018-02-04 00:29:02 +03:00
desktopMenu - > addActions ( actions ) ;
2018-02-03 16:00:44 +03:00
return ;
}
2018-12-02 01:53:40 +03:00
Plasma : : Containment * ContextMenu : : containmentById ( uint id )
2018-02-03 16:00:44 +03:00
{
2019-04-04 23:55:44 +03:00
for ( const auto containment : m_latteView - > corona ( ) - > containments ( ) ) {
2018-02-03 16:00:44 +03:00
if ( id = = containment - > id ( ) ) {
return containment ;
}
}
return 0 ;
}
}
2018-12-02 01:53:40 +03:00
}