port plasma1 popupapplet event filter logic

use an event filter to open popup menus inside popups.
requires a recent 5.3 revision and a small change in the shell package
This commit is contained in:
Marco Martin 2014-08-06 19:45:31 +02:00
parent 120132879a
commit 1baddcc004
4 changed files with 51 additions and 4 deletions

View File

@ -44,6 +44,7 @@
#include <Plasma/Corona> #include <Plasma/Corona>
#include <Plasma/Package> #include <Plasma/Package>
#include <Plasma/PluginLoader> #include <Plasma/PluginLoader>
#include <Plasma/ContainmentActions>
#include "containmentinterface.h" #include "containmentinterface.h"
#include <kdeclarative/configpropertymap.h> #include <kdeclarative/configpropertymap.h>
@ -85,6 +86,15 @@ AppletInterface::AppletInterface(DeclarativeAppletScript *script, const QVariant
connect(applet()->containment(), &Plasma::Containment::screenChanged, connect(applet()->containment(), &Plasma::Containment::screenChanged,
this, &ContainmentInterface::screenChanged); this, &ContainmentInterface::screenChanged);
} }
connect(this, &AppletInterface::expandedChanged, [=](bool expanded) {
//FIXME: this is obviously wrong: the filter should be installed when the
//applet goes popup and removed when it goes normal,
//this is a not really working heuristic
if (fullRepresentationItem() && fullRepresentationItem()->parentItem()) {
fullRepresentationItem()->parentItem()->installEventFilter(this);
}
});
} }
AppletInterface::AppletInterface(Plasma::Applet *a, const QVariantList &args, QQuickItem *parent) AppletInterface::AppletInterface(Plasma::Applet *a, const QVariantList &args, QQuickItem *parent)
@ -523,4 +533,35 @@ void AppletInterface::executeAction(const QString &name)
} }
} }
bool AppletInterface::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
//pass it up to the applet
//well, actually we have to pass it to the *containment*
//because all the code for showing an applet's contextmenu is actually in Containment.
Plasma::Containment *c = applet()->containment();
if (c) {
const QString trigger = Plasma::ContainmentActions::eventToString(event);
Plasma::ContainmentActions *plugin = c->containmentActions().value(trigger);
if (!plugin) {
return false;
}
ContainmentInterface *ci = c->property("_plasma_graphicObject").value<ContainmentInterface *>();
QMenu desktopMenu;
ci->addAppletActions(desktopMenu, applet(), event);
if (!desktopMenu.isEmpty()) {
desktopMenu.exec(static_cast<QMouseEvent*>(event)->globalPos());
return true;
}
return false;
}
}
return AppletQuickItem::eventFilter(watched, event);
}
#include "moc_appletinterface.cpp" #include "moc_appletinterface.cpp"

View File

@ -334,6 +334,9 @@ Q_SIGNALS:
protected Q_SLOTS: protected Q_SLOTS:
virtual void init(); virtual void init();
protected:
bool eventFilter(QObject *watched, QEvent *event);
private: private:
QStringList m_actions; QStringList m_actions;

View File

@ -656,12 +656,14 @@ QList<QObject *> ContainmentInterface::actions() const
//PROTECTED-------------------- //PROTECTED--------------------
void ContainmentInterface::mousePressEvent(QMouseEvent *event) void ContainmentInterface::mouseReleaseEvent(QMouseEvent *event)
{ {
event->setAccepted(m_containment->containmentActions().contains(Plasma::ContainmentActions::eventToString(event))); //event->setAccepted(m_containment->containmentActions().contains(Plasma::ContainmentActions::eventToString(event)));
event->setAccepted(false);
} }
void ContainmentInterface::mouseReleaseEvent(QMouseEvent *event) void ContainmentInterface::mousePressEvent(QMouseEvent *event)
{ {
//even if the menu is executed synchronously, other events may be processed //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 //by the qml incubator when plasma is loading, so we need to guard there
@ -721,7 +723,7 @@ void ContainmentInterface::mouseReleaseEvent(QMouseEvent *event)
//end workaround //end workaround
desktopMenu.exec(event->globalPos()); desktopMenu.exec(event->globalPos());
event->accept(); event->setAccepted(false);
} }
void ContainmentInterface::wheelEvent(QWheelEvent *event) void ContainmentInterface::wheelEvent(QWheelEvent *event)

View File

@ -198,6 +198,7 @@ private:
KActivities::Info *m_activityInfo; KActivities::Info *m_activityInfo;
QPointer<Plasma::Containment> m_containment; QPointer<Plasma::Containment> m_containment;
QWeakPointer<QMenu> m_contextMenu; QWeakPointer<QMenu> m_contextMenu;
friend class AppletInterface;
}; };
QML_DECLARE_TYPEINFO(ContainmentInterface, QML_HAS_ATTACHED_PROPERTIES) QML_DECLARE_TYPEINFO(ContainmentInterface, QML_HAS_ATTACHED_PROPERTIES)