1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-09-28 15:56:33 -04:00

Support touchscreen tap and hold to be used instead of right mouse click.

This commit is contained in:
Jon Beniston 2022-12-20 10:01:16 +00:00
parent e3f9d1032d
commit 60a933771d
2 changed files with 95 additions and 29 deletions

View File

@ -1,13 +1,93 @@
/* ///////////////////////////////////////////////////////////////////////////////////
* crightclickenabler.cpp // Copyright (C) 2018 F4EXB //
* // written by Edouard Griffiths //
* Created on: Mar 26, 2018 // //
* Author: f4exb // This program 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 as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QMouseEvent>
#include <QGestureEvent>
#include <QGesture>
#include <QTapAndHoldGesture>
#include "crightclickenabler.h" #include "crightclickenabler.h"
CRightClickEnabler::CRightClickEnabler(QAbstractButton *button): QObject(button), _button(button) { CRightClickEnabler::CRightClickEnabler(QWidget *widget) :
button->installEventFilter(this); QObject(widget),
}; m_widget(widget),
m_mousePressed(false)
{
m_widget->installEventFilter(this);
m_widget->grabGesture(Qt::TapAndHoldGesture);
}
bool CRightClickEnabler::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress)
{
auto mouseEvent = (QMouseEvent*) event;
if (mouseEvent->button() == Qt::RightButton)
{
emit rightClick(mouseEvent->globalPos());
mouseEvent->setAccepted(true);
return true;
}
if (mouseEvent->button() == Qt::LeftButton)
{
if (mouseEvent->source() == Qt::MouseEventNotSynthesized) {
m_mousePressed = true;
} else {
m_mousePressed = false; // Mouse event generated from touch event
}
}
}
else if (event->type() == QEvent::MouseButtonRelease)
{
auto mouseEvent = (QMouseEvent*) event;
if (mouseEvent->button() == Qt::RightButton)
{
mouseEvent->setAccepted(true);
return true;
}
if (mouseEvent->button() == Qt::LeftButton) {
m_mousePressed = false;
}
}
else if (event->type() == QEvent::Gesture)
{
// Use tap and hold as right click on touch screens
if (!m_mousePressed)
{
QGestureEvent *gestureEvent = static_cast<QGestureEvent *>(event);
if (QTapAndHoldGesture *tapAndHold = static_cast<QTapAndHoldGesture *>(gestureEvent->gesture(Qt::TapAndHoldGesture)))
{
if (tapAndHold->state() == Qt::GestureFinished) {
emit rightClick(tapAndHold->position().toPoint());
}
return true;
}
}
}
else if (event->type() == QEvent::ContextMenu)
{
// Filter ContextMenu events, so we don't get popup menus as well
return true;
}
return QObject::eventFilter(obj, event);
}

View File

@ -19,38 +19,24 @@
#ifndef SDRGUI_GUI_CRIGHTCLICKENABLER_H_ #ifndef SDRGUI_GUI_CRIGHTCLICKENABLER_H_
#define SDRGUI_GUI_CRIGHTCLICKENABLER_H_ #define SDRGUI_GUI_CRIGHTCLICKENABLER_H_
#include <QAbstractButton> #include <QWidget>
#include <QMouseEvent>
#include "export.h" #include "export.h"
class SDRGUI_API CRightClickEnabler : public QObject { class SDRGUI_API CRightClickEnabler : public QObject {
Q_OBJECT Q_OBJECT
public: public:
CRightClickEnabler(QAbstractButton *button); CRightClickEnabler(QWidget *widget);
signals: signals:
void rightClick(const QPoint&); void rightClick(const QPoint&); // Emitted for right mouse click and touch tap and hold
protected: protected:
inline bool eventFilter(QObject *watched, QEvent *event) override bool eventFilter(QObject *watched, QEvent *event) override;
{
(void) watched;
if (event->type() == QEvent::MouseButtonPress)
{
auto mouseEvent = (QMouseEvent*) event;
if (mouseEvent->button() == Qt::RightButton) {
emit rightClick(mouseEvent->globalPos());
}
}
return false;
}
private: private:
QAbstractButton* _button; QWidget* m_widget;
bool m_mousePressed;
}; };
#endif /* SDRGUI_GUI_CRIGHTCLICKENABLER_H_ */ #endif /* SDRGUI_GUI_CRIGHTCLICKENABLER_H_ */