summaryrefslogtreecommitdiff
path: root/examples/demos/documentviewer/hoverwatcher.h
blob: 8ddfcd1f67ce5ba7b81abd11a17d0e18eb244e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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