Qt 4.8
qtestmouse.h
Go to the documentation of this file.
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 
62 
64 
65 QT_MODULE(Test)
66 
67 namespace QTest
68 {
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);
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  }
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 
147 
149 
150 #endif // QTESTMOUSE_H
#define Q_TESTLIB_EXPORT
Definition: qtest_global.h:56
MouseAction
This enum describes possible actions for mouse handling.
Definition: qtestmouse.h:69
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
#define QT_MODULE(x)
Definition: qglobal.h:2783
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=0, QPoint pos=QPoint(), int delay=-1)
Simulates double clicking a mouse button with an optional modifier on a widget.
Definition: qtestmouse.h:138
The QString class provides a Unicode character string.
Definition: qstring.h:83
static void setPos(int x, int y)
Moves the cursor (hot spot) to the global screen position (x, y).
Definition: qcursor_mac.mm:315
void mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=0, QPoint pos=QPoint(), int delay=-1)
Simulates clicking a mouse button with an optional modifier on a widget.
Definition: qtestmouse.h:135
void mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=0, QPoint pos=QPoint(), int delay=-1)
Simulates pressing a mouse button with an optional modifier on a widget.
Definition: qtestmouse.h:129
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define qApp
bool isNull() const
Returns true if both the x and y coordinates are set to 0, otherwise returns false.
Definition: qpoint.h:125
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
QPoint center() const
Returns the center point of the rectangle.
Definition: qrect.h:300
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static void qWait(int ms)
Waits for ms milliseconds.
Definition: qtestsystem.h:62
void mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=0, QPoint pos=QPoint(), int delay=-1)
Simulates releasing a mouse button with an optional modifier on a widget.
Definition: qtestmouse.h:132
Q_TESTLIB_EXPORT void qWarn(const char *message)
Definition: qtestcase.cpp:2102
QRect rect
the internal geometry of the widget excluding any window frame
Definition: qwidget.h:168
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
#define QTEST_ASSERT(cond)
Definition: qtestassert.h:53
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void mouseMove(QWidget *widget, QPoint pos=QPoint(), int delay=-1)
Moves the mouse pointer to a widget.
Definition: qtestmouse.h:141
QByteArray toAscii() const Q_REQUIRED_RESULT
Returns an 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4014
static void mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
Definition: qtestmouse.h:71
int Q_TESTLIB_EXPORT defaultMouseDelay()
Definition: qtestcase.cpp:1008
#define QT_END_HEADER
Definition: qglobal.h:137
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...
QPoint mapToGlobal(const QPoint &) const
Translates the widget coordinate pos to global screen coordinates.
MouseButton
Definition: qnamespace.h:150