diff --git a/src/scriptengines/qml/plasmoid/appletinterface.h b/src/scriptengines/qml/plasmoid/appletinterface.h --- a/src/scriptengines/qml/plasmoid/appletinterface.h +++ b/src/scriptengines/qml/plasmoid/appletinterface.h @@ -312,6 +312,11 @@ */ Q_INVOKABLE QStringList downloadedFiles() const; + /** + * Shows the associated context menu by the applet + */ + Q_INVOKABLE void showContextMenu(); + QVariantList availableScreenRegion() const; QRect availableScreenRect() const; @@ -444,14 +449,16 @@ void init() override; protected: + void touchEvent(QTouchEvent * event) override; + bool event(QEvent *event) override; bool eventFilter(QObject *watched, QEvent *event) override; private Q_SLOTS: void destroyedChanged(bool destroyed); private: - + void showContextMenuTap(); QStringList m_actions; KDeclarative::ConfigPropertyMap *m_configuration; @@ -473,6 +480,8 @@ friend class ContainmentInterface; //This is used by ContainmentInterface QPointF m_positionBeforeRemoval; + QTimer* const m_longTap; + QTouchEvent::TouchPoint m_longTapPos; }; QML_DECLARE_TYPEINFO(AppletInterface, QML_HAS_ATTACHED_PROPERTIES) diff --git a/src/scriptengines/qml/plasmoid/appletinterface.cpp b/src/scriptengines/qml/plasmoid/appletinterface.cpp --- a/src/scriptengines/qml/plasmoid/appletinterface.cpp +++ b/src/scriptengines/qml/plasmoid/appletinterface.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -57,7 +58,8 @@ m_hideOnDeactivate(true), m_oldKeyboardShortcut(0), m_dummyNativeInterface(nullptr), - m_positionBeforeRemoval(QPointF(-1, -1)) + m_positionBeforeRemoval(QPointF(-1, -1)), + m_longTap(new QTimer(this)) { qmlRegisterType(); @@ -141,6 +143,10 @@ } } }); + + m_longTap->setSingleShot(true); + m_longTap->setInterval(QGuiApplication::styleHints()->mousePressAndHoldInterval()); + connect(m_longTap, &QTimer::timeout, this, &AppletInterface::showContextMenuTap); } AppletInterface::~AppletInterface() @@ -720,6 +726,21 @@ return rect; } +void AppletInterface::touchEvent(QTouchEvent* event) +{ + if (event->touchPoints().count() != 1) + return; + + if (event->touchPointStates() & Qt::TouchPointPressed) { + m_longTap->start(); + m_longTapPos = event->touchPoints().constFirst(); + } + if (event->touchPointStates() & Qt::TouchPointReleased) { + m_longTap->stop(); + } + AppletQuickItem::touchEvent(event); +} + bool AppletInterface::event(QEvent *event) { // QAction keyboard shortcuts cannot work with QML2 (and probably newver will @@ -842,5 +863,21 @@ return AppletQuickItem::eventFilter(watched, event); } +void AppletInterface::showContextMenuTap() +{ + QMouseEvent ev(QEvent::MouseButtonPress, m_longTapPos.pos(), m_longTapPos.scenePos(), m_longTapPos.screenPos(), Qt::RightButton, Qt::RightButton, nullptr); + mousePressEvent(&ev); +} + +void AppletInterface::showContextMenu() +{ + ContainmentInterface *ci = applet()->containment()->property("_plasma_graphicObject").value(); + const auto pos = mapToItem(ci, boundingRect().center()); + QMouseEvent ev(QEvent::MouseButtonPress, pos, mapToScene(pos), mapToGlobal(pos), Qt::RightButton, nullptr, nullptr); + if (!ci) { + return; + } + ci->mousePressEvent(&ev); +} #include "moc_appletinterface.cpp"