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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QApplication>
#include <QDebug>
#include <QMainWindow>
#include <QStatusBar>
#include <QPlainTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QComboBox>
#include <QTimer>
#include <QLineEdit>
// Compiles with Qt 4.8 and Qt 5.
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
bool eventFilter(QObject *, QEvent *);
private slots:
void showModalDialog();
void mouseGrabToggled(bool);
void delayedMouseGrab();
void grabMouseWindowToggled(bool);
void delayedMouseWindowGrab();
void keyboardGrabToggled(bool);
void grabKeyboardWindowToggled(bool);
void forceNativeWidgets();
private:
void toggleMouseWidgetGrab(QWidget *w, bool on);
void toggleKeyboardWidgetGrab(QWidget *w, bool on);
int m_mouseEventCount;
int m_enterLeaveEventCount;
QPlainTextEdit *m_logEdit;
QCheckBox *m_grabMouseCheckBox;
QCheckBox *m_grabMouseWindowCheckBox;
QCheckBox *m_grabKeyboardCheckBox;
QCheckBox *m_grabKeyboardWindowCheckBox;
QPushButton *m_forceNativeButton;
QString m_lastMouseMoveEvent;
};
class ClickableLabel : public QLabel
{
Q_OBJECT
public:
explicit ClickableLabel(const QString &text, QWidget *parent = 0) : QLabel(text, parent) {}
signals:
void pressed();
protected:
void mousePressEvent(QMouseEvent *ev)
{
emit pressed();
QLabel::mousePressEvent(ev);
}
};
static const char testCasesC[] =
"- Drag a scrollbar, move mouse out of the window. The scrollbar should still react.\n\n"
"- Press mouse inside window, move outside while pressing the button. Mouse events"
" should be reported until the button is released.\n\n"
"- 'Show modal dialog on press' opens a modal dialog on mouse press. This should not lock up.\n\n"
"- Check the 'Grab Mouse' box. Only the checkbox should then receive mouse move events.\n\n"
"- Open popup menu. Mouse events should all go to popup menu while it is visible.\n\n"
"- Click delayed grab and then open popup immediately. Wait for Grab checkbox to be marked. "
"Click on popup menu to close it. Mouse events should be going to Grab checkbox. "
"Click on Grab checkbox to clear it. UI should respond normally after that. \n\n"
;
MainWindow::MainWindow()
: m_mouseEventCount(0)
, m_enterLeaveEventCount(0)
, m_logEdit(new QPlainTextEdit(this))
, m_grabMouseCheckBox(new QCheckBox(QLatin1String("Grab Mouse")))
, m_grabMouseWindowCheckBox(new QCheckBox(QLatin1String("Grab Mouse Window Ctrl+W")))
, m_grabKeyboardCheckBox(new QCheckBox(QLatin1String("Grab Keyboard")))
, m_grabKeyboardWindowCheckBox(new QCheckBox(QLatin1String("Grab Keyboard Window")))
, m_forceNativeButton(new QPushButton(QLatin1String("Force native widgets")))
{
setObjectName(QLatin1String("MainWindow"));
setMinimumWidth(800);
setWindowTitle(QString::fromLatin1("Manual Grab Test %1").arg(QLatin1String(QT_VERSION_STR)));
QMenu *fileMenu = menuBar()->addMenu(QLatin1String("File"));
fileMenu->setObjectName("FileMenu");
QAction *quit = fileMenu->addAction(QLatin1String("Quit"));
quit->setShortcut(QKeySequence::Quit);
connect(quit, SIGNAL(triggered()), this, SLOT(close()));
QMenu *editMenu = menuBar()->addMenu(QLatin1String("Edit"));
editMenu->setObjectName("EditMenu");
QAction *clearLog = editMenu->addAction(QLatin1String("Clear Log"));
connect(clearLog, SIGNAL(triggered()), m_logEdit, SLOT(clear()));
QWidget *w = new QWidget(this);
w->setObjectName(QLatin1String("CentralWidget"));
QVBoxLayout *layout = new QVBoxLayout(w);
QPlainTextEdit *instructions = new QPlainTextEdit(this);
instructions->setObjectName(QLatin1String("InstructionsEdit"));
instructions->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
instructions->setPlainText(QLatin1String(testCasesC));
instructions->setReadOnly(true);
layout->addWidget(instructions);
int row = 0;
QGridLayout *controlLayout = new QGridLayout;
layout->addLayout(controlLayout);
QPushButton *modalDialogButton = new QPushButton("Show modal dialog on release");
modalDialogButton->setObjectName(QLatin1String("ModalDialogButton"));
connect(modalDialogButton, SIGNAL(clicked()), this, SLOT(showModalDialog()));
controlLayout->addWidget(modalDialogButton, row, 0);
ClickableLabel *modalDialogLabel = new ClickableLabel("Show modal dialog on press");
modalDialogLabel->setObjectName(QLatin1String("ModalDialogLabel"));
controlLayout->addWidget(modalDialogLabel, row, 1);
connect(modalDialogLabel, SIGNAL(pressed()), this, SLOT(showModalDialog()));
row++;
m_grabMouseCheckBox->setObjectName(QLatin1String("GrabCheckBox"));
connect(m_grabMouseCheckBox, SIGNAL(toggled(bool)), this, SLOT(mouseGrabToggled(bool)));
controlLayout->addWidget(m_grabMouseCheckBox, row, 0);
QPushButton *delayedGrabButton = new QPushButton("Delayed grab");
delayedGrabButton->setObjectName(QLatin1String("DelayedGrabButton"));
connect(delayedGrabButton, SIGNAL(clicked()), this, SLOT(delayedMouseGrab()));
controlLayout->addWidget(delayedGrabButton, row, 1);
row++;
m_grabMouseWindowCheckBox->setObjectName(QLatin1String("GrabWindowCheckBox"));
m_grabMouseWindowCheckBox->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W));
connect(m_grabMouseWindowCheckBox, SIGNAL(toggled(bool)), this, SLOT(grabMouseWindowToggled(bool)));
controlLayout->addWidget(m_grabMouseWindowCheckBox, row, 0);
QPushButton *delayedWindowGrabButton = new QPushButton("Delayed window grab");
delayedWindowGrabButton->setObjectName(QLatin1String("DelayedWindowGrabButton"));
connect(delayedWindowGrabButton, SIGNAL(clicked()), this, SLOT(delayedMouseWindowGrab()));
controlLayout->addWidget(delayedWindowGrabButton, row, 1);
row++;
m_grabKeyboardCheckBox->setObjectName(QLatin1String("GrabKeyboardBox"));
connect(m_grabKeyboardCheckBox, SIGNAL(toggled(bool)), this, SLOT(keyboardGrabToggled(bool)));
controlLayout->addWidget(m_grabKeyboardCheckBox, row, 0);
m_grabKeyboardWindowCheckBox->setObjectName(QLatin1String("GrabKeyboardWindowBox"));
connect(m_grabKeyboardWindowCheckBox, SIGNAL(toggled(bool)), this, SLOT(grabKeyboardWindowToggled(bool)));
controlLayout->addWidget(m_grabKeyboardWindowCheckBox, row, 1);
row++;
QComboBox *combo = new QComboBox;
combo->addItems(QStringList() << QLatin1String("Popup test 1") << QLatin1String("Popup test 2"));
controlLayout->addWidget(combo, row, 0);
QPushButton *popupMenuButton = new QPushButton("Popup menu");
popupMenuButton->setObjectName(QLatin1String("PopupMenuButton"));
QMenu *popupMenu = new QMenu(this);
popupMenu->setObjectName(QLatin1String("PopupMenu"));
popupMenu->addAction(tr("&First Item"));
popupMenu->addAction(tr("&Second Item"));
popupMenu->addAction(tr("&Third Item"));
popupMenu->addAction(tr("F&ourth Item"));
popupMenuButton->setMenu(popupMenu);
controlLayout->addWidget(popupMenuButton, row, 1);
row++;
m_forceNativeButton->setObjectName("ForceNativeWidgetsButton");
controlLayout->addWidget(m_forceNativeButton, row, 0);
connect(m_forceNativeButton, SIGNAL(clicked()), this, SLOT(forceNativeWidgets()));
row++;
QLineEdit *lineEdit = new QLineEdit(this);
lineEdit->setObjectName(QLatin1String("LineEdit"));
controlLayout->addWidget(lineEdit, row, 0, 1, 2);
m_logEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_logEdit->setObjectName(QLatin1String("LogEdit"));
layout->addWidget(m_logEdit);
setCentralWidget(w);
qApp->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
if (o->isWidgetType()) {
switch (e->type()) {
case QEvent::Enter: {
QString message;
QDebug debug(&message);
#if QT_VERSION >= 0x050000
const QEnterEvent *ee = static_cast<QEnterEvent *>(e);
debug.nospace() << '#' << m_enterLeaveEventCount++ << " Enter for " << o->objectName()
<< " at " << ee->localPos() << " global: " << ee->globalPos();
#else
debug.nospace() << '#' << m_enterLeaveEventCount++ << " Enter for " << o->objectName();
#endif
m_logEdit->appendPlainText(message);
}
break;
case QEvent::Leave: {
QString message;
QDebug debug(&message);
debug.nospace() << '#' << m_enterLeaveEventCount++ << " Leave for " << o->objectName();
m_logEdit->appendPlainText(message);
}
break;
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease: {
const QMouseEvent *me = static_cast<QMouseEvent *>(e);
QString message;
QDebug debug = QDebug(&message).nospace();
debug << '#' << m_mouseEventCount++ << ' ';
if (e->type() == QEvent::MouseButtonPress) {
if (me->buttons() & Qt::LeftButton)
debug << "Left button press";
if (me->buttons() & Qt::MiddleButton)
debug << "Middle button press";
if (me->buttons() & Qt::RightButton)
debug << "Right button press";
} else {
debug << "Button release";
}
debug << " on " << o->objectName() << " Mousegrabber " << QWidget::mouseGrabber();
m_logEdit->appendPlainText(message);
}
break;
case QEvent::MouseMove: {
const QMouseEvent *me = static_cast<const QMouseEvent *>(e);
const QWidget *widgetUnderMouse = QApplication::widgetAt(me->globalPos());
QString message;
QDebug d = QDebug(&message).nospace();
d << " Mouse move reported for " << o->objectName();
if (widgetUnderMouse) {
d << " over " << widgetUnderMouse;
} else {
d << " outside ";
}
d << " mouse grabber " << QWidget::mouseGrabber();
// Compress mouse move event logging.
if (message != m_lastMouseMoveEvent) {
m_lastMouseMoveEvent = message;
m_logEdit->appendPlainText(QString::fromLatin1("#%1 %2").arg(m_mouseEventCount++).arg(message));
}
}
break;
case QEvent::KeyRelease:
case QEvent::KeyPress: {
const QKeyEvent *ke = static_cast<const QKeyEvent *>(e);
QString message;
QDebug d = QDebug(&message).nospace();
d << (e->type() == QEvent::KeyPress ? "Key press" : "Key release")
<< ' ' << ke->text() << " on " << o << " key grabber " << QWidget::keyboardGrabber();
m_logEdit->appendPlainText(message);
}
break;
default:
break;
}
}
return QMainWindow::eventFilter(o ,e);
}
void MainWindow::showModalDialog()
{
QMessageBox::information(this, QLatin1String("Information"), QLatin1String("Modal Dialog"));
}
void MainWindow::toggleMouseWidgetGrab(QWidget *w, bool on)
{
if (on) {
m_logEdit->appendPlainText(w->objectName() + QLatin1String(" grabbed mouse."));
w->grabMouse();
} else {
w->releaseMouse();
m_logEdit->appendPlainText(w->objectName() + QLatin1String(" released mouse."));
}
}
void MainWindow::toggleKeyboardWidgetGrab(QWidget *w, bool on)
{
if (on) {
m_logEdit->appendPlainText(w->objectName() + QLatin1String(" grabbed keyboard."));
w->grabKeyboard();
} else {
w->releaseKeyboard();
m_logEdit->appendPlainText(w->objectName() + QLatin1String(" released keyboard."));
}
}
void MainWindow::mouseGrabToggled(bool g)
{
toggleMouseWidgetGrab(m_grabMouseCheckBox, g);
}
void MainWindow::delayedMouseGrab()
{
QTimer::singleShot(2000, m_grabMouseCheckBox, SLOT(animateClick()));
}
void MainWindow::grabMouseWindowToggled(bool g)
{
toggleMouseWidgetGrab(this, g);
}
void MainWindow::delayedMouseWindowGrab()
{
QTimer::singleShot(2000, m_grabMouseWindowCheckBox, SLOT(animateClick()));
}
void MainWindow::keyboardGrabToggled(bool g)
{
toggleKeyboardWidgetGrab(m_grabKeyboardCheckBox, g);
}
void MainWindow::grabKeyboardWindowToggled(bool g)
{
toggleKeyboardWidgetGrab(this, g);
}
void MainWindow::forceNativeWidgets()
{
const WId platformWid = m_forceNativeButton->winId();
#if QT_VERSION < 0x050000 && defined(Q_OS_WIN)
const quintptr wid = quintptr(platformWid); // HWND on Qt 4.8/Windows.
#else
const WId wid = platformWid;
#endif
m_logEdit->appendPlainText(QString::fromLatin1("Created native widget %1").arg(wid));
m_forceNativeButton->setEnabled(false);
m_forceNativeButton->setText(QLatin1String("Native widgets created"));
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
|