summaryrefslogtreecommitdiff
path: root/examples/demos/documentviewer/hoverwatcher.cpp
blob: 3c0ffe1e854891dc8fb167ebd24d1010a92401e1 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "hoverwatcher.h"
#include <QGuiApplication>
#include <QWidget>
#include <QMouseEvent>

HoverWatcher::HoverWatcher(QWidget *watched)
    : QObject(watched), m_watched(watched)
{
    Q_ASSERT(watched);
    m_cursorShapes[Entered].emplace(Qt::OpenHandCursor);
    m_cursorShapes[MousePress].emplace(Qt::ClosedHandCursor);
    m_cursorShapes[MouseRelease].emplace(Qt::OpenHandCursor);
    // no default for Left => restore override cursor
    m_watched->installEventFilter(this);
}

HoverWatcher::~HoverWatcher()
{
    m_watched->removeEventFilter(this);
}

typedef QHash<QWidget *, HoverWatcher*> WatchMap;
Q_GLOBAL_STATIC(WatchMap, qt_allHoverWatchers)

HoverWatcher *HoverWatcher::watcher(QWidget *watched)
{
    if (qt_allHoverWatchers()->contains(watched))
        return qt_allHoverWatchers()->value(watched);

    HoverWatcher *watcher = new HoverWatcher(watched);
    qt_allHoverWatchers()->insert(watched, watcher);
    return watcher;
}

/*!
   \overload Const version of watcher
 */
const HoverWatcher *HoverWatcher::watcher(const QWidget *watched)
{
    return watcher(const_cast<QWidget *>(watched));
}

void HoverWatcher::dismiss(QWidget *watched)
{
    if (!hasWatcher(watched))
        return;

    delete qt_allHoverWatchers()->take(watched);
}

bool HoverWatcher::hasWatcher(QWidget *widget)
{
    return qt_allHoverWatchers()->contains(widget);
}

static constexpr HoverWatcher::HoverAction toHoverAction(QEvent::Type et)
{
    switch (et) {
    case QEvent::Type::Enter:
        return HoverWatcher::HoverAction::Entered;
    case QEvent::Type::Leave:
        return HoverWatcher::HoverAction::Left;
    case QEvent::Type::MouseButtonPress:
        return HoverWatcher::HoverAction::MousePress;
    case QEvent::Type::MouseButtonRelease:
        return HoverWatcher::HoverAction::MouseRelease;
    default:
        return HoverWatcher::HoverAction::Ignore;
    }
}

void HoverWatcher::handleAction (HoverWatcher::HoverAction action)
{
    const Qt::CursorShape newShape = cursorShape(action);
    if (QGuiApplication::overrideCursor()
            && (QGuiApplication::overrideCursor()->shape() == newShape
            || action == HoverAction::Ignore)) {
        return;
    }

    QGuiApplication::setOverrideCursor(cursorShape(action));
    emit hoverAction(action);

    switch (action) {
    case HoverAction::Entered:
        emit entered();
        break;
    case HoverAction::Left:
        emit left();
        break;
    case HoverAction::MousePress:
        emit mousePressed();
        break;
    case HoverAction::MouseRelease: {
        emit mouseReleased();
    }
        break;
    case HoverAction::Ignore:
        break;
    }
}

bool HoverWatcher::hasShape(HoverAction action) const
{
    return action != HoverAction::Ignore && m_cursorShapes[action].has_value();
}

void HoverWatcher::setApplicationCursor(HoverAction action) const
{
    if (!hasShape(action)) {
        QGuiApplication::restoreOverrideCursor();
        return;
    }

    QGuiApplication::setOverrideCursor(cursorShape(action));
}

bool HoverWatcher::eventFilter(QObject *obj, QEvent *event)
{
    Q_ASSERT(obj == m_watched); // don't install event filters elsewhere

    // Ignore irrelevant events
    const auto action = toHoverAction(event->type());
    if (action == HoverAction::Ignore)
        return false;

    // React to a QScroller having been installed or removed
    // A Scroller sends a fake mouse release to QPoint (-1, -1)
    // => needs to be ignored and end of scrolling processed instead
    static bool hasScroller = false;
    if (QScroller::hasScroller(m_watched) != hasScroller) {
        hasScroller = QScroller::hasScroller(m_watched);
        static QMetaObject::Connection con;
        if (hasScroller) {
            con = connect(QScroller::scroller(m_watched), &QScroller::stateChanged,
                          this, &HoverWatcher::handleScrollerStateChange);
        } else {
            disconnect(con);
        }
    }

    // Ignore fake mouse release event sent by scroller
    if (action == HoverAction::MouseRelease && hasScroller) {
        QMouseEvent *me = static_cast<QMouseEvent *>(event);
        if (me->pos().x() < -9000000 )
            return false;
    }

    // Ignore unpermitted mouse buttons
    if (action == HoverAction::MousePress) {
        QMouseEvent *me = static_cast<QMouseEvent *>(event);
        if (!m_mouseButtons.testFlag(me->button()))
            return false;
    }

    handleAction(action);
    return false;
}

Qt::CursorShape HoverWatcher::cursorShape(HoverAction type) const
{
    const Qt::CursorShape fallback = Qt::ArrowCursor;
    if (type == HoverAction::Ignore)
        return fallback;

    return m_cursorShapes[type].value_or(fallback);
}

void HoverWatcher::setCursorShape(HoverAction type, Qt::CursorShape shape)
{
    if (type == HoverAction::Ignore)
        return;
    m_cursorShapes[type].emplace(shape);
}

void HoverWatcher::unSetCursorShape(HoverAction type)
{
    if (type == HoverAction::Ignore)
        return;
    m_cursorShapes[type].reset();
}

void HoverWatcher::setMouseButtons(Qt::MouseButtons buttons)
{
    m_mouseButtons = buttons;
}

void HoverWatcher::setMouseButton(Qt::MouseButton button, bool enable)
{
    m_mouseButtons.setFlag(button, enable);;
}

/*!
   \brief This slot handles a QScroller state change, in case the watched
    widget uses a scroller. It translates \param state into the appropriate
    action.
 */
void HoverWatcher::handleScrollerStateChange(QScroller::State state)
{
    switch (state) {
    case QScroller::State::Pressed:
    case QScroller::State::Dragging:
    case QScroller::State::Scrolling:
        handleAction(HoverAction::MousePress);
        break;
    case QScroller::State::Inactive:
        handleAction(HoverAction::MouseRelease);
        break;
    }
}