summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestwheel.h
blob: 564a586b4bbca184cceecc524316ca400935870d (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
// Copyright (C) 2023 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

#ifndef QTESTWHEEL_H
#define QTESTWHEEL_H

#if 0
// inform syncqt
#pragma qt_no_master_include
#endif

#include <QtTest/qttestglobal.h>
#include <QtTest/qtestassert.h>
#include <QtTest/qtestsystem.h>
#include <QtTest/qtestspontaneevent.h>
#include <QtCore/qpoint.h>
#include <QtCore/qstring.h>
#include <QtCore/qpointer.h>
#include <QtGui/qevent.h>
#include <QtGui/qwindow.h>

#include <QtCore/QDebug>

QT_BEGIN_NAMESPACE

Q_GUI_EXPORT void qt_handleWheelEvent(QWindow *window, const QPointF &local,
                                      const QPointF &global, QPoint pixelDelta,
                                      QPoint angleDelta, Qt::KeyboardModifiers mods, Qt::ScrollPhase phase);

namespace QTest
{
    /*! \internal
        This function creates a mouse wheel event and calls
        QWindowSystemInterface::handleWheelEvent().
        \a window is the window that should be receiving the event and \a pos
        provides the location of the event in the window's local coordinates.
        \a angleDelta contains the wheel rotation angle, while \a pixelDelta
        contains the scrolling distance in pixels on screen.
        The keyboard states at the time of the event are specified by \a stateKey.
        The scrolling phase of the event is specified by \a phase.
    */
    [[maybe_unused]] static void wheelEvent(QWindow *window, QPointF pos,
                                            QPoint angleDelta, QPoint pixelDelta = QPoint(0, 0),
                                            Qt::KeyboardModifiers stateKey = Qt::NoModifier,
                                            Qt::ScrollPhase phase = Qt::NoScrollPhase)
    {
        QTEST_ASSERT(window);

        // pos is in window local coordinates
        const QSize windowSize = window->geometry().size();
        if (windowSize.width() <= pos.x() || windowSize.height() <= pos.y()) {
            qWarning("Mouse event at %d, %d occurs outside target window (%dx%d).",
                        static_cast<int>(pos.x()), static_cast<int>(pos.y()), windowSize.width(), windowSize.height());
        }

        if (pos.isNull())
            pos = QPoint(window->width() / 2, window->height() / 2);

        QPointF global = window->mapToGlobal(pos);
        QPointer<QWindow> w(window);

        if (angleDelta.isNull() && pixelDelta.isNull())
            qWarning("No angle or pixel delta specified.");

        qt_handleWheelEvent(w, pos, global, pixelDelta, angleDelta, stateKey, phase);
        qApp->processEvents();
    }
}

QT_END_NAMESPACE

#endif // QTESTWHEEL_H