2023-10-22 03:59:27 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <QQuickItem>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <Qt>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @short A component that is similar to MouseArea but allows for a
|
|
|
|
|
* simpler tracking of dragging movements after pressing and holding.
|
|
|
|
|
*
|
|
|
|
|
* @author Devin Lin <devin@kde.org>
|
|
|
|
|
*/
|
|
|
|
|
class DelegateTouchArea : public QQuickItem
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
2025-07-16 17:02:18 +00:00
|
|
|
QML_ELEMENT
|
2023-10-22 03:59:27 +00:00
|
|
|
|
|
|
|
|
Q_PROPERTY(bool pressed READ pressed NOTIFY pressedChanged FINAL)
|
|
|
|
|
Q_PROPERTY(bool hovered READ hovered NOTIFY hoveredChanged FINAL)
|
2026-04-12 12:15:13 +00:00
|
|
|
Q_PROPERTY(bool dragging READ dragging NOTIFY draggingChanged FINAL)
|
2023-10-22 03:59:27 +00:00
|
|
|
Q_PROPERTY(Qt::CursorShape cursorShape READ cursorShape WRITE setCursorShape RESET unsetCursor NOTIFY cursorShapeChanged FINAL)
|
2023-11-05 05:14:39 +00:00
|
|
|
Q_PROPERTY(QPointF pressPosition READ pressPosition NOTIFY pressPositionChanged FINAL)
|
2023-10-22 03:59:27 +00:00
|
|
|
|
|
|
|
|
QML_NAMED_ELEMENT(DelegateTouchArea)
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
DelegateTouchArea(QQuickItem *parent = nullptr);
|
|
|
|
|
|
|
|
|
|
bool pressed();
|
|
|
|
|
bool hovered();
|
2026-04-12 12:15:13 +00:00
|
|
|
bool dragging();
|
2023-10-22 03:59:27 +00:00
|
|
|
Qt::CursorShape cursorShape();
|
|
|
|
|
void setCursorShape(Qt::CursorShape cursorShape);
|
|
|
|
|
void unsetCursor();
|
2023-11-05 05:14:39 +00:00
|
|
|
QPointF pressPosition();
|
2023-10-22 03:59:27 +00:00
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
|
void clicked();
|
|
|
|
|
void rightMousePress();
|
|
|
|
|
void pressAndHold();
|
|
|
|
|
void pressAndHoldReleased();
|
2026-04-12 12:15:13 +00:00
|
|
|
void draggingChanged();
|
|
|
|
|
void dragMoved(qreal deltaX);
|
2023-10-22 03:59:27 +00:00
|
|
|
void pressedChanged(bool pressed);
|
|
|
|
|
void hoveredChanged(bool hovered);
|
|
|
|
|
void cursorShapeChanged();
|
2023-11-05 05:14:39 +00:00
|
|
|
void pressPositionChanged();
|
2023-10-22 03:59:27 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void mousePressEvent(QMouseEvent *event) override;
|
|
|
|
|
void mouseReleaseEvent(QMouseEvent *event) override;
|
|
|
|
|
void mouseMoveEvent(QMouseEvent *event) override;
|
|
|
|
|
void mouseUngrabEvent() override;
|
|
|
|
|
void touchEvent(QTouchEvent *event) override;
|
|
|
|
|
void touchUngrabEvent() override;
|
|
|
|
|
void hoverEnterEvent(QHoverEvent *event) override;
|
|
|
|
|
void hoverLeaveEvent(QHoverEvent *event) override;
|
|
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
|
void startPressAndHold();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void setPressed(bool pressed);
|
|
|
|
|
void setHovered(bool hovered);
|
|
|
|
|
void setDragging(bool dragging);
|
|
|
|
|
|
|
|
|
|
void handlePressEvent(QPointerEvent *event, QPointF point);
|
|
|
|
|
void handleReleaseEvent(QPointerEvent *event, bool click);
|
|
|
|
|
void handleMoveEvent(QPointerEvent *event, QPointF point);
|
|
|
|
|
|
|
|
|
|
bool m_pressed{false};
|
|
|
|
|
bool m_hovered{false};
|
2026-04-12 12:15:13 +00:00
|
|
|
bool m_dragging{false};
|
2023-10-22 03:59:27 +00:00
|
|
|
bool m_pressAndHeld{false};
|
|
|
|
|
Qt::CursorShape m_cursorShape{Qt::ArrowCursor};
|
2023-11-05 05:14:39 +00:00
|
|
|
QPointF m_mouseDownPosition{};
|
2023-10-22 03:59:27 +00:00
|
|
|
|
|
|
|
|
QTimer *m_pressAndHoldTimer{nullptr};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QML_DECLARE_TYPE(DelegateTouchArea)
|