File indexing completed on 2024-12-29 05:06:02

0001 // SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QQuickItem>
0007 #include <QTimer>
0008 #include <Qt>
0009 
0010 /**
0011  * @short A component that is similar to MouseArea but allows for a
0012  * simpler tracking of dragging movements after pressing and holding.
0013  *
0014  * @author Devin Lin <devin@kde.org>
0015  */
0016 class DelegateTouchArea : public QQuickItem
0017 {
0018     Q_OBJECT
0019 
0020     Q_PROPERTY(bool pressed READ pressed NOTIFY pressedChanged FINAL)
0021     Q_PROPERTY(bool hovered READ hovered NOTIFY hoveredChanged FINAL)
0022     Q_PROPERTY(Qt::CursorShape cursorShape READ cursorShape WRITE setCursorShape RESET unsetCursor NOTIFY cursorShapeChanged FINAL)
0023     Q_PROPERTY(QPointF pressPosition READ pressPosition NOTIFY pressPositionChanged FINAL)
0024 
0025     QML_NAMED_ELEMENT(DelegateTouchArea)
0026 
0027 public:
0028     DelegateTouchArea(QQuickItem *parent = nullptr);
0029 
0030     bool pressed();
0031     bool hovered();
0032     Qt::CursorShape cursorShape();
0033     void setCursorShape(Qt::CursorShape cursorShape);
0034     void unsetCursor();
0035     QPointF pressPosition();
0036 
0037 Q_SIGNALS:
0038     void clicked();
0039     void rightMousePress();
0040     void pressAndHold();
0041     void pressAndHoldReleased();
0042     void pressedChanged(bool pressed);
0043     void hoveredChanged(bool hovered);
0044     void cursorShapeChanged();
0045     void pressPositionChanged();
0046 
0047 protected:
0048     void mousePressEvent(QMouseEvent *event) override;
0049     void mouseReleaseEvent(QMouseEvent *event) override;
0050     void mouseMoveEvent(QMouseEvent *event) override;
0051     void mouseUngrabEvent() override;
0052     void touchEvent(QTouchEvent *event) override;
0053     void touchUngrabEvent() override;
0054     void hoverEnterEvent(QHoverEvent *event) override;
0055     void hoverLeaveEvent(QHoverEvent *event) override;
0056 
0057 private Q_SLOTS:
0058     void startPressAndHold();
0059 
0060 private:
0061     void setPressed(bool pressed);
0062     void setHovered(bool hovered);
0063     void setDragging(bool dragging);
0064 
0065     void handlePressEvent(QPointerEvent *event, QPointF point);
0066     void handleReleaseEvent(QPointerEvent *event, bool click);
0067     void handleMoveEvent(QPointerEvent *event, QPointF point);
0068 
0069     bool m_pressed{false};
0070     bool m_hovered{false};
0071     bool m_pressAndHeld{false};
0072     Qt::CursorShape m_cursorShape{Qt::ArrowCursor};
0073     QPointF m_mouseDownPosition{};
0074 
0075     QTimer *m_pressAndHoldTimer{nullptr};
0076 };
0077 
0078 QML_DECLARE_TYPE(DelegateTouchArea)