Commit f509d41b authored by Steffen Hartleib's avatar Steffen Hartleib Committed by Nate Graham
Browse files

Using the gesture recognizer from KWidgetsAddons

Using the gesture recognizer from KWidgetsAddons, so that the source code for the gestures is only in one place
parent 45af4bc0
Pipeline #154263 passed with stage
in 2 minutes and 21 seconds
......@@ -67,6 +67,7 @@ find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
Notifications
Crash
WindowSystem
WidgetsAddons
)
find_package(KUserFeedback 1.2.0)
......
......@@ -92,8 +92,6 @@ target_sources(dolphinprivate PRIVATE
kitemviews/private/kitemlistviewanimation.cpp
kitemviews/private/kitemlistviewlayouter.cpp
kitemviews/private/kpixmapmodifier.cpp
kitemviews/private/ktwofingerswipe.cpp
kitemviews/private/ktwofingertap.cpp
settings/applyviewpropsjob.cpp
settings/viewmodes/viewmodesettings.cpp
settings/viewpropertiesdialog.cpp
......@@ -159,6 +157,7 @@ target_link_libraries(
KF5::NewStuff
KF5::Parts
KF5::WindowSystem
KF5::WidgetsAddons
)
if(HAVE_BALOO)
......
......@@ -13,10 +13,11 @@
#include "kitemlistview.h"
#include "private/kitemlistkeyboardsearchmanager.h"
#include "private/kitemlistrubberband.h"
#include "private/ktwofingerswipe.h"
#include "private/ktwofingertap.h"
#include "views/draganddrophelper.h"
#include <KTwoFingerSwipe>
#include <KTwoFingerTap>
#include <QAccessible>
#include <QApplication>
#include <QDrag>
......
/*
* SPDX-FileCopyrightText: 2020 Steffen Hartleib <steffenhartleib@t-online.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
// Self
#include "ktwofingerswipe.h"
// Qt
#include <QTouchEvent>
#include <QLineF>
KTwoFingerSwipeRecognizer::KTwoFingerSwipeRecognizer() :
QGestureRecognizer(),
m_touchBeginnTimestamp(0),
m_gestureAlreadyTriggered(false)
{
}
KTwoFingerSwipeRecognizer::~KTwoFingerSwipeRecognizer()
{
}
QGesture* KTwoFingerSwipeRecognizer::create(QObject*)
{
return static_cast<QGesture*>(new KTwoFingerSwipe());
}
QGestureRecognizer::Result KTwoFingerSwipeRecognizer::recognize(QGesture* gesture, QObject* watched, QEvent* event)
{
Q_UNUSED(watched)
KTwoFingerSwipe* const kTwoFingerSwipe = static_cast<KTwoFingerSwipe*>(gesture);
const QTouchEvent* touchEvent = static_cast<const QTouchEvent*>(event);
const int maxTimeFrameForSwipe = 90;
const int minDistanceForSwipe = 30;
switch (event->type()) {
case QEvent::TouchBegin: {
m_touchBeginnTimestamp = touchEvent->timestamp();
m_gestureAlreadyTriggered = false;
kTwoFingerSwipe->setHotSpot(touchEvent->touchPoints().first().startScreenPos());
kTwoFingerSwipe->setPos(touchEvent->touchPoints().first().startPos());
kTwoFingerSwipe->setScreenPos(touchEvent->touchPoints().first().startScreenPos());
kTwoFingerSwipe->setScenePos(touchEvent->touchPoints().first().startScenePos());
return MayBeGesture;
}
case QEvent::TouchUpdate: {
const qint64 now = touchEvent->timestamp();
const qint64 elapsedTime = now - m_touchBeginnTimestamp;
const QPointF distance = touchEvent->touchPoints().first().startPos() - touchEvent->touchPoints().first().pos();
kTwoFingerSwipe->setHotSpot(touchEvent->touchPoints().first().startScreenPos());
kTwoFingerSwipe->setPos(touchEvent->touchPoints().first().startPos());
kTwoFingerSwipe->setScreenPos(touchEvent->touchPoints().first().startScreenPos());
kTwoFingerSwipe->setScenePos(touchEvent->touchPoints().first().startScenePos());
const QLineF ql = QLineF(touchEvent->touchPoints().first().startPos(), touchEvent->touchPoints().first().pos());
kTwoFingerSwipe->setSwipeAngle(ql.angle());
if (touchEvent->touchPoints().size() > 2) {
return CancelGesture;
}
if (touchEvent->touchPoints().size() == 2) {
if ((elapsedTime) > maxTimeFrameForSwipe) {
return CancelGesture;
}
if (distance.manhattanLength() >= minDistanceForSwipe &&
(elapsedTime) <= maxTimeFrameForSwipe && !m_gestureAlreadyTriggered) {
m_gestureAlreadyTriggered = true;
return FinishGesture;
} else if ((elapsedTime) <= maxTimeFrameForSwipe && !m_gestureAlreadyTriggered) {
return TriggerGesture;
}
}
break;
}
default:
return Ignore;
}
return Ignore;
}
KTwoFingerSwipe::KTwoFingerSwipe(QObject* parent) :
QGesture(parent),
m_pos(QPointF(-1, -1)),
m_screenPos(QPointF(-1, -1)),
m_scenePos(QPointF(-1, -1)),
m_swipeAngle(0.0)
{
}
KTwoFingerSwipe::~KTwoFingerSwipe()
{
}
QPointF KTwoFingerSwipe::pos() const
{
return m_pos;
}
void KTwoFingerSwipe::setPos(QPointF _pos)
{
m_pos = _pos;
}
QPointF KTwoFingerSwipe::screenPos() const
{
return m_screenPos;
}
void KTwoFingerSwipe::setScreenPos(QPointF _screenPos)
{
m_screenPos = _screenPos;
}
QPointF KTwoFingerSwipe::scenePos() const
{
return m_scenePos;
}
void KTwoFingerSwipe::setScenePos(QPointF _scenePos)
{
m_scenePos = _scenePos;
}
qreal KTwoFingerSwipe::swipeAngle() const
{
return m_swipeAngle;
}
void KTwoFingerSwipe::setSwipeAngle(qreal _swipeAngle)
{
m_swipeAngle = _swipeAngle;
}
/*
* SPDX-FileCopyrightText: 2020 Steffen Hartleib <steffenhartleib@t-online.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef KTWOFINGERSWIPE_H
#define KTWOFINGERSWIPE_H
#include "dolphin_export.h"
// Qt
#include <QGesture>
#include <QGestureRecognizer>
class DOLPHIN_EXPORT KTwoFingerSwipe : public QGesture
{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
Q_PROPERTY(QPointF screenPos READ screenPos WRITE setScreenPos)
Q_PROPERTY(QPointF scenePos READ scenePos WRITE setScenePos)
Q_PROPERTY(qreal swipeAngle READ swipeAngle WRITE setSwipeAngle)
public:
explicit KTwoFingerSwipe(QObject* parent = nullptr);
~KTwoFingerSwipe() override;
QPointF pos() const;
void setPos(QPointF pos);
QPointF screenPos() const;
void setScreenPos(QPointF screenPos);
QPointF scenePos() const;
void setScenePos(QPointF scenePos);
qreal swipeAngle() const;
void setSwipeAngle(qreal swipeAngle);
private:
QPointF m_pos;
QPointF m_screenPos;
QPointF m_scenePos;
qreal m_swipeAngle;
};
class DOLPHIN_EXPORT KTwoFingerSwipeRecognizer : public QGestureRecognizer
{
public:
explicit KTwoFingerSwipeRecognizer();
~KTwoFingerSwipeRecognizer() override;
QGesture* create(QObject*) override;
Result recognize(QGesture*, QObject*, QEvent*) override;
private:
Q_DISABLE_COPY( KTwoFingerSwipeRecognizer )
qint64 m_touchBeginnTimestamp;
bool m_gestureAlreadyTriggered;
};
#endif /* KTWOFINGERSWIPE_H */
/*
* SPDX-FileCopyrightText: 2020 Steffen Hartleib <steffenhartleib@t-online.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
// Self
#include "ktwofingertap.h"
// Qt
#include <QTouchEvent>
#include <QApplication>
#include <QGraphicsWidget>
KTwoFingerTapRecognizer::KTwoFingerTapRecognizer() :
QGestureRecognizer(),
m_gestureTriggered(false)
{
}
KTwoFingerTapRecognizer::~KTwoFingerTapRecognizer()
{
}
QGesture* KTwoFingerTapRecognizer::create(QObject*)
{
return static_cast<QGesture*>(new KTwoFingerTap());
}
QGestureRecognizer::Result KTwoFingerTapRecognizer::recognize(QGesture* gesture, QObject* watched, QEvent* event)
{
if (qobject_cast<QGraphicsWidget*>(watched)) {
return Ignore;
}
KTwoFingerTap* const kTwoFingerTap = static_cast<KTwoFingerTap*>(gesture);
const QTouchEvent* touchEvent = static_cast<const QTouchEvent*>(event);
switch (event->type()) {
case QEvent::TouchBegin: {
kTwoFingerTap->setHotSpot(touchEvent->touchPoints().first().startScreenPos());
kTwoFingerTap->setPos(touchEvent->touchPoints().first().startPos());
kTwoFingerTap->setScreenPos(touchEvent->touchPoints().first().startScreenPos());
kTwoFingerTap->setScenePos(touchEvent->touchPoints().first().startScenePos());
m_gestureTriggered = false;
return MayBeGesture;
}
case QEvent::TouchUpdate: {
if (touchEvent->touchPoints().size() > 2) {
m_gestureTriggered = false;
return CancelGesture;
}
if (touchEvent->touchPoints().size() == 2) {
if ((touchEvent->touchPoints().first().startPos() - touchEvent->touchPoints().first().pos()).manhattanLength() >= QApplication::startDragDistance()) {
m_gestureTriggered = false;
return CancelGesture;
}
if ((touchEvent->touchPoints().at(1).startPos() - touchEvent->touchPoints().at(1).pos()).manhattanLength() >= QApplication::startDragDistance()) {
m_gestureTriggered = false;
return CancelGesture;
}
if (touchEvent->touchPointStates() & Qt::TouchPointPressed) {
m_gestureTriggered = true;
}
if (touchEvent->touchPointStates() & Qt::TouchPointReleased && m_gestureTriggered) {
m_gestureTriggered = false;
return FinishGesture;
}
}
break;
}
default:
return Ignore;
}
return Ignore;
}
KTwoFingerTap::KTwoFingerTap(QObject* parent) :
QGesture(parent),
m_pos(QPointF(-1, -1)),
m_screenPos(QPointF(-1, -1)),
m_scenePos(QPointF(-1, -1))
{
}
KTwoFingerTap::~KTwoFingerTap()
{
}
QPointF KTwoFingerTap::pos() const
{
return m_pos;
}
void KTwoFingerTap::setPos(QPointF _pos)
{
m_pos = _pos;
}
QPointF KTwoFingerTap::screenPos() const
{
return m_screenPos;
}
void KTwoFingerTap::setScreenPos(QPointF _screenPos)
{
m_screenPos = _screenPos;
}
QPointF KTwoFingerTap::scenePos() const
{
return m_scenePos;
}
void KTwoFingerTap::setScenePos(QPointF _scenePos)
{
m_scenePos = _scenePos;
}
/*
* SPDX-FileCopyrightText: 2020 Steffen Hartleib <steffenhartleib@t-online.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef KTWOFINGERTAP_H
#define KTWOFINGERTAP_H
#include <dolphin_export.h>
// Qt
#include <QGesture>
#include <QGestureRecognizer>
class DOLPHIN_EXPORT KTwoFingerTap : public QGesture
{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
Q_PROPERTY(QPointF screenPos READ screenPos WRITE setScreenPos)
Q_PROPERTY(QPointF scenePos READ scenePos WRITE setScenePos)
public:
explicit KTwoFingerTap(QObject* parent = nullptr);
~KTwoFingerTap() override;
QPointF pos() const;
void setPos(QPointF pos);
QPointF screenPos() const;
void setScreenPos(QPointF screenPos);
QPointF scenePos() const;
void setScenePos(QPointF scenePos);
private:
QPointF m_pos;
QPointF m_screenPos;
QPointF m_scenePos;
};
class DOLPHIN_EXPORT KTwoFingerTapRecognizer : public QGestureRecognizer
{
public:
explicit KTwoFingerTapRecognizer();
~KTwoFingerTapRecognizer() override;
QGesture* create(QObject*) override;
Result recognize(QGesture*, QObject*, QEvent*) override;
private:
Q_DISABLE_COPY(KTwoFingerTapRecognizer)
bool m_gestureTriggered;
};
#endif /* KTWOFINGERTAP_H */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment