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

pull/1539/head
Jon Beniston 2022-12-20 10:01:16 +00:00
rodzic e3f9d1032d
commit 60a933771d
2 zmienionych plików z 95 dodań i 29 usunięć

Wyświetl plik

@ -1,13 +1,93 @@
/*
* crightclickenabler.cpp
*
* Created on: Mar 26, 2018
* Author: f4exb
*/
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 F4EXB //
// written by Edouard Griffiths //
// //
// 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"
CRightClickEnabler::CRightClickEnabler(QAbstractButton *button): QObject(button), _button(button) {
button->installEventFilter(this);
};
CRightClickEnabler::CRightClickEnabler(QWidget *widget) :
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);
}

Wyświetl plik

@ -19,38 +19,24 @@
#ifndef SDRGUI_GUI_CRIGHTCLICKENABLER_H_
#define SDRGUI_GUI_CRIGHTCLICKENABLER_H_
#include <QAbstractButton>
#include <QMouseEvent>
#include <QWidget>
#include "export.h"
class SDRGUI_API CRightClickEnabler : public QObject {
Q_OBJECT
public:
CRightClickEnabler(QAbstractButton *button);
CRightClickEnabler(QWidget *widget);
signals:
void rightClick(const QPoint&);
void rightClick(const QPoint&); // Emitted for right mouse click and touch tap and hold
protected:
inline 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;
}
bool eventFilter(QObject *watched, QEvent *event) override;
private:
QAbstractButton* _button;
QWidget* m_widget;
bool m_mousePressed;
};
#endif /* SDRGUI_GUI_CRIGHTCLICKENABLER_H_ */