From ae618e069418621544a4c78984fbac61f2084195 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 23 Apr 2018 11:38:28 +0200 Subject: Manual touch test: Add handling for multiple touch screens - Add a display label showing the screen parameters. - Add a menu option to launch secondary windows and restructure the code accordingly Change-Id: I2bdb76da0b0a00e62db41e674aa93cef9598fe67 Reviewed-by: Andy Shaw --- tests/manual/touch/main.cpp | 98 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 18 deletions(-) (limited to 'tests/manual') diff --git a/tests/manual/touch/main.cpp b/tests/manual/touch/main.cpp index 9f2dcb3842..a244230a22 100644 --- a/tests/manual/touch/main.cpp +++ b/tests/manual/touch/main.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +52,8 @@ static bool optIgnoreTouch = false; static QVector optGestures; +static QWidgetList mainWindows; + static inline void drawEllipse(const QPointF ¢er, qreal hDiameter, qreal vDiameter, const QColor &color, QPainter &painter) { const QPen oldPen = painter.pen(); @@ -191,6 +195,7 @@ typedef QSharedPointer GesturePtr; typedef QVector GesturePtrs; typedef QVector EventTypeVector; +static EventTypeVector eventTypes; class EventFilter : public QObject { Q_OBJECT @@ -206,6 +211,8 @@ private: const EventTypeVector m_types; }; +static EventFilter *globalEventFilter = nullptr; + bool EventFilter::eventFilter(QObject *o, QEvent *e) { static int n = 0; @@ -406,29 +413,67 @@ void TouchTestWidget::paintEvent(QPaintEvent *) class MainWindow : public QMainWindow { Q_OBJECT -public: MainWindow(); +public: + static MainWindow *createMainWindow(); + QWidget *touchWidget() const { return m_touchWidget; } + void setVisible(bool visible) override; + public slots: void appendToLog(const QString &text) { m_logTextEdit->appendPlainText(text); } void dumpTouchDevices(); private: + void updateScreenLabel(); + void newWindow() { MainWindow::createMainWindow(); } + TouchTestWidget *m_touchWidget; QPlainTextEdit *m_logTextEdit; + QLabel *m_screenLabel; }; +MainWindow *MainWindow::createMainWindow() +{ + MainWindow *result = new MainWindow; + const QSize screenSize = QGuiApplication::primaryScreen()->availableGeometry().size(); + result->resize(screenSize / 2); + const QSize sizeDiff = screenSize - result->size(); + const QPoint pos = QPoint(sizeDiff.width() / 2, sizeDiff.height() / 2) + + mainWindows.size() * QPoint(30, 10); + result->move(pos); + result->show(); + + EventFilter *eventFilter = globalEventFilter; + if (!eventFilter) { + eventFilter = new EventFilter(eventTypes, result->touchWidget()); + result->touchWidget()->installEventFilter(eventFilter); + } + QObject::connect(eventFilter, &EventFilter::eventReceived, result, &MainWindow::appendToLog); + + mainWindows.append(result); + return result; +} + MainWindow::MainWindow() : m_touchWidget(new TouchTestWidget) , m_logTextEdit(new QPlainTextEdit) + , m_screenLabel(new QLabel) { - setWindowTitle(QStringLiteral("Touch Event Tester ") + QT_VERSION_STR); + QString title; + QTextStream(&title) << "Touch Event Tester " << QT_VERSION_STR << ' ' + << qApp->platformName() << " #" << (mainWindows.size() + 1); + setWindowTitle(title); setObjectName("MainWin"); QToolBar *toolBar = new QToolBar(this); addToolBar(Qt::TopToolBarArea, toolBar); QMenu *fileMenu = menuBar()->addMenu("File"); + QAction *newWindowAction = fileMenu->addAction(QStringLiteral("New Window"), this, &MainWindow::newWindow); + newWindowAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N)); + toolBar->addAction(newWindowAction); + fileMenu->addSeparator(); QAction *dumpDeviceAction = fileMenu->addAction(QStringLiteral("Dump devices")); dumpDeviceAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D)); connect(dumpDeviceAction, &QAction::triggered, this, &MainWindow::dumpTouchDevices); @@ -461,9 +506,32 @@ MainWindow::MainWindow() mainSplitter->addWidget(m_logTextEdit); setCentralWidget(mainSplitter); + statusBar()->addPermanentWidget(m_screenLabel); + dumpTouchDevices(); } +void MainWindow::setVisible(bool visible) +{ + QMainWindow::setVisible(visible); + connect(windowHandle(), &QWindow::screenChanged, this, &MainWindow::updateScreenLabel); + updateScreenLabel(); +} + +void MainWindow::updateScreenLabel() +{ + QString text; + QTextStream str(&text); + const QScreen *screen = windowHandle()->screen(); + const QRect geometry = screen->geometry(); + const qreal dpr = screen->devicePixelRatio(); + str << '"' << screen->name() << "\" " << geometry.width() << 'x' << geometry.height() + << forcesign << geometry.x() << geometry.y() << noforcesign; + if (!qFuzzyCompare(dpr, qreal(1))) + str << ", dpr=" << dpr; + m_screenLabel->setText(text); +} + void MainWindow::dumpTouchDevices() { QString message; @@ -522,14 +590,6 @@ int main(int argc, char *argv[]) if (parser.isSet(swipeGestureOption)) optGestures.append(Qt::SwipeGesture); - MainWindow w; - const QSize screenSize = QGuiApplication::primaryScreen()->availableGeometry().size(); - w.resize(screenSize / 2); - const QSize sizeDiff = screenSize - w.size(); - w.move(sizeDiff.width() / 2, sizeDiff.height() / 2); - w.show(); - - EventTypeVector eventTypes; if (!parser.isSet(noMouseLogOption)) eventTypes << QEvent::MouseButtonPress << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick; if (parser.isSet(mouseMoveOption)) @@ -538,14 +598,16 @@ int main(int argc, char *argv[]) eventTypes << QEvent::TouchBegin << QEvent::TouchUpdate << QEvent::TouchEnd; if (!optGestures.isEmpty()) eventTypes << QEvent::Gesture << QEvent::GestureOverride; - QObject *filterTarget = parser.isSet(globalFilterOption) - ? static_cast(&a) - : static_cast(w.touchWidget()); - EventFilter *filter = new EventFilter(eventTypes, filterTarget); - filterTarget->installEventFilter(filter); - QObject::connect(filter, &EventFilter::eventReceived, &w, &MainWindow::appendToLog); - - return a.exec(); + if (parser.isSet(globalFilterOption)) { + globalEventFilter = new EventFilter(eventTypes, &a); + a.installEventFilter(globalEventFilter); + } + + MainWindow::createMainWindow(); + + const int exitCode = a.exec(); + qDeleteAll(mainWindows); + return exitCode; } #include "main.moc" -- cgit v1.2.3