diff options
Diffstat (limited to 'examples/demos/documentviewer/hoverwatcher.h')
-rw-r--r-- | examples/demos/documentviewer/hoverwatcher.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/demos/documentviewer/hoverwatcher.h b/examples/demos/documentviewer/hoverwatcher.h new file mode 100644 index 00000000..8ddfcd1f --- /dev/null +++ b/examples/demos/documentviewer/hoverwatcher.h @@ -0,0 +1,66 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef HOVERWATCHER_H +#define HOVERWATCHER_H +#include <QObject> +#include <QEvent> +#include <QScroller> + +class QWidget; +class HoverWatcher : public QObject +{ + Q_OBJECT + +private: + explicit HoverWatcher(QWidget *watched); + static QMap<QWidget *, HoverWatcher *> m_hoverWatchers; + +public: + ~HoverWatcher(); + + enum HoverAction { + Entered, + MousePress, + MouseRelease, + Left, + Ignore + }; + Q_ENUM(HoverAction); + + bool eventFilter(QObject *obj, QEvent *event) override; + + Qt::CursorShape cursorShape(HoverAction type) const; + Qt::MouseButtons mouseButtons() const { return m_mouseButtons; } + + static HoverWatcher *watcher(QWidget *watched); + static const HoverWatcher *watcher(const QWidget *watched); + static bool hasWatcher(QWidget *widget); + static void dismiss(QWidget *watched); + +public slots: + void setCursorShape(HoverAction type, Qt::CursorShape shape); + void unSetCursorShape(HoverAction type); + void setMouseButtons(Qt::MouseButtons buttons); + void setMouseButton(Qt::MouseButton button, bool enable); + +signals: + void entered(); + void mousePressed(); + void mouseReleased(); + void left(); + void hoverAction(HoverAction action); + +private slots: + void handleScrollerStateChange(QScroller::State state); + +private: + QWidget *m_watched; + std::array<std::optional<Qt::CursorShape>, HoverAction::Ignore> m_cursorShapes; + Qt::MouseButtons m_mouseButtons = Qt::MouseButton::LeftButton; + void handleAction(HoverAction action); + void setApplicationCursor(HoverAction action) const; + bool hasShape(HoverAction action) const; +}; + +#endif // HOVERWATCHER_H |