Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the QtTest module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #if !defined(QTESTMOUSE_H) |
43 | #define QTESTMOUSE_H |
44 | |
45 | #if 0 |
46 | // inform syncqt |
47 | #pragma qt_no_master_include |
48 | #endif |
49 | |
50 | #include <QtTest/qtest_global.h> |
51 | #include <QtTest/qtestassert.h> |
52 | #include <QtTest/qtestsystem.h> |
53 | #include <QtTest/qtestspontaneevent.h> |
54 | |
55 | #include <QtCore/qpoint.h> |
56 | #include <QtCore/qstring.h> |
57 | #include <QtGui/qapplication.h> |
58 | #include <QtGui/qevent.h> |
59 | #include <QtGui/qwidget.h> |
60 | |
61 | QT_BEGIN_HEADER |
62 | |
63 | QT_BEGIN_NAMESPACE |
64 | |
65 | QT_MODULE(Test) |
66 | |
67 | namespace QTest |
68 | { |
69 | enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDClick, MouseMove }; |
70 | |
71 | static void mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button, |
72 | Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1) |
73 | { |
74 | QTEST_ASSERT(widget); |
75 | extern int Q_TESTLIB_EXPORT defaultMouseDelay(); |
76 | |
77 | if (delay == -1 || delay < defaultMouseDelay()) |
78 | delay = defaultMouseDelay(); |
79 | if(delay > 0) |
80 | QTest::qWait(delay); |
81 | |
82 | if (pos.isNull()) |
83 | pos = widget->rect().center(); |
84 | |
85 | if (action == MouseClick) { |
86 | mouseEvent(MousePress, widget, button, stateKey, pos); |
87 | mouseEvent(MouseRelease, widget, button, stateKey, pos); |
88 | return; |
89 | } |
90 | |
91 | QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask); |
92 | QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask); |
93 | |
94 | stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask); |
95 | |
96 | QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey); |
97 | switch (action) |
98 | { |
99 | case MousePress: |
100 | me = QMouseEvent(QEvent::MouseButtonPress, pos, widget->mapToGlobal(pos), button, button, stateKey); |
101 | break; |
102 | case MouseRelease: |
103 | me = QMouseEvent(QEvent::MouseButtonRelease, pos, widget->mapToGlobal(pos), button, 0, stateKey); |
104 | break; |
105 | case MouseDClick: |
106 | me = QMouseEvent(QEvent::MouseButtonDblClick, pos, widget->mapToGlobal(pos), button, button, stateKey); |
107 | break; |
108 | case MouseMove: |
109 | QCursor::setPos(widget->mapToGlobal(pos)); |
110 | #ifdef QT_MAC_USE_COCOA |
111 | QTest::qWait(20); |
112 | #else |
113 | qApp->processEvents(); |
114 | #endif |
115 | return; |
116 | default: |
117 | QTEST_ASSERT(false); |
118 | } |
119 | QSpontaneKeyEvent::setSpontaneous(&me); |
120 | if (!qApp->notify(widget, &me)) { |
121 | static const char *mouseActionNames[] = |
122 | { "MousePress", "MouseRelease", "MouseClick", "MouseDClick", "MouseMove"}; |
123 | QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget"); |
124 | QTest::qWarn(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toAscii().data()); |
125 | } |
126 | |
127 | } |
128 | |
129 | inline void mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, |
130 | QPoint pos = QPoint(), int delay=-1) |
131 | { mouseEvent(MousePress, widget, button, stateKey, pos, delay); } |
132 | inline void mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, |
133 | QPoint pos = QPoint(), int delay=-1) |
134 | { mouseEvent(MouseRelease, widget, button, stateKey, pos, delay); } |
135 | inline void mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, |
136 | QPoint pos = QPoint(), int delay=-1) |
137 | { mouseEvent(MouseClick, widget, button, stateKey, pos, delay); } |
138 | inline void mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0, |
139 | QPoint pos = QPoint(), int delay=-1) |
140 | { mouseEvent(MouseDClick, widget, button, stateKey, pos, delay); } |
141 | inline void mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1) |
142 | { mouseEvent(MouseMove, widget, Qt::NoButton, 0, pos, delay); } |
143 | |
144 | } |
145 | |
146 | QT_END_NAMESPACE |
147 | |
148 | QT_END_HEADER |
149 | |
150 | #endif // QTESTMOUSE_H |
151 |
Warning: That file was not part of the compilation database. It may have many parsing errors.