summaryrefslogtreecommitdiffstats
path: root/tests/manual/touchmocking/touchmockingapplication.cpp
blob: feedae5cd69308a6cefce1174edf91bc713dbc12 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "touchmockingapplication.h"

#include <QCursor>
#include <QEvent>
#include <QPixmap>

#if defined(QUICK_TOUCHBROWSER)
#    include <QQuickView>
#endif

#if defined(WIDGET_TOUCHBROWSER)
#    include <QMainWindow>
#endif

static inline bool isMouseEvent(QEvent *event)
{
    switch (event->type()) {
    case QEvent::MouseMove:
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
        return true;
    default:
        return false;
    }
}

TouchMockingApplication::TouchMockingApplication(int &argc, char **argv)
    : Application(argc, argv), m_touchPoint(new QCursor(QPixmap(":touchpoint.png")))
{
}

TouchMockingApplication::~TouchMockingApplication()
{
    delete m_touchPoint;
}

bool TouchMockingApplication::notify(QObject *target, QEvent *event)
{
    switch (event->type()) {
    case QEvent::TouchBegin:
        setOverrideCursor(*m_touchPoint);
        break;
    case QEvent::TouchEnd:
        restoreCursor();
        break;
    default:
        break;
    }

// All mouse events that are not accepted by the application will be translated to touch events
// instead (see Qt::AA_SynthesizeTouchForUnhandledMouseEvents).
#if defined(QUICK_TOUCHBROWSER)
    if (isMouseEvent(event) && qobject_cast<QQuickView *>(target)) {
        event->ignore();
        return false;
    }
#elif defined(WIDGET_TOUCHBROWSER)
    // Popups ignore touch evenets so we send MouseEvents directly.
    if (isMouseEvent(event)) {
        if (activePopupWidget()) {
            restoreCursor();
        } else {
            event->ignore();
            return false;
        }
    }
#endif
    return Application::notify(target, event);
}

void TouchMockingApplication::restoreCursor()
{
    while (overrideCursor())
        restoreOverrideCursor();
}