Polish the manual touch test

Introduce C++11, nullptr, for, port to Qt 5 connection syntax.

Change-Id: I2d233ccd68bad533af8d4674d91236b2c049e997
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
This commit is contained in:
Friedemann Kleint 2018-04-23 10:55:33 +02:00
parent dbc0a5ba70
commit 578c96f0bb

View File

@ -47,8 +47,8 @@
#include <QDebug> #include <QDebug>
#include <QTextStream> #include <QTextStream>
bool optIgnoreTouch = false; static bool optIgnoreTouch = false;
QVector<Qt::GestureType> optGestures; static QVector<Qt::GestureType> optGestures;
static inline void drawEllipse(const QPointF &center, qreal hDiameter, qreal vDiameter, const QColor &color, QPainter &painter) static inline void drawEllipse(const QPointF &center, qreal hDiameter, qreal vDiameter, const QColor &color, QPainter &painter)
{ {
@ -275,10 +275,10 @@ class TouchTestWidget : public QWidget {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints) Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints)
public: public:
explicit TouchTestWidget(QWidget *parent = 0) : QWidget(parent), m_drawPoints(true) explicit TouchTestWidget(QWidget *parent = nullptr) : QWidget(parent), m_drawPoints(true)
{ {
setAttribute(Qt::WA_AcceptTouchEvents); setAttribute(Qt::WA_AcceptTouchEvents);
foreach (Qt::GestureType t, optGestures) for (Qt::GestureType t : optGestures)
grabGesture(t); grabGesture(t);
} }
@ -337,10 +337,11 @@ bool TouchTestWidget::event(QEvent *event)
case QEvent::TouchBegin: case QEvent::TouchBegin:
case QEvent::TouchUpdate: case QEvent::TouchUpdate:
if (m_drawPoints) { if (m_drawPoints) {
foreach (const QTouchEvent::TouchPoint &p, static_cast<const QTouchEvent *>(event)->touchPoints()) for (const QTouchEvent::TouchPoint &p : static_cast<const QTouchEvent *>(event)->touchPoints())
m_points.append(Point(p.pos(), TouchPoint, Qt::MouseEventNotSynthesized, p.ellipseDiameters())); m_points.append(Point(p.pos(), TouchPoint, Qt::MouseEventNotSynthesized, p.ellipseDiameters()));
update(); update();
} }
Q_FALLTHROUGH();
case QEvent::TouchEnd: case QEvent::TouchEnd:
if (optIgnoreTouch) if (optIgnoreTouch)
event->ignore(); event->ignore();
@ -358,7 +359,8 @@ bool TouchTestWidget::event(QEvent *event)
void TouchTestWidget::handleGestureEvent(QGestureEvent *gestureEvent) void TouchTestWidget::handleGestureEvent(QGestureEvent *gestureEvent)
{ {
foreach (QGesture *gesture, gestureEvent->gestures()) { const auto gestures = gestureEvent->gestures();
for (QGesture *gesture : gestures) {
if (optGestures.contains(gesture->gestureType())) { if (optGestures.contains(gesture->gestureType())) {
switch (gesture->state()) { switch (gesture->state()) {
case Qt::NoGesture: case Qt::NoGesture:
@ -389,7 +391,7 @@ void TouchTestWidget::paintEvent(QPaintEvent *)
const QRectF geom = QRectF(QPointF(0, 0), QSizeF(size())); const QRectF geom = QRectF(QPointF(0, 0), QSizeF(size()));
painter.fillRect(geom, Qt::white); painter.fillRect(geom, Qt::white);
painter.drawRect(QRectF(geom.topLeft(), geom.bottomRight() - QPointF(1, 1))); painter.drawRect(QRectF(geom.topLeft(), geom.bottomRight() - QPointF(1, 1)));
foreach (const Point &point, m_points) { for (const Point &point : qAsConst(m_points)) {
if (geom.contains(point.pos)) { if (geom.contains(point.pos)) {
if (point.type == MouseRelease) if (point.type == MouseRelease)
drawEllipse(point.pos, point.horizontalDiameter, point.verticalDiameter, point.color(), painter); drawEllipse(point.pos, point.horizontalDiameter, point.verticalDiameter, point.color(), painter);
@ -397,7 +399,7 @@ void TouchTestWidget::paintEvent(QPaintEvent *)
fillEllipse(point.pos, point.horizontalDiameter, point.verticalDiameter, point.color(), painter); fillEllipse(point.pos, point.horizontalDiameter, point.verticalDiameter, point.color(), painter);
} }
} }
foreach (const GesturePtr &gp, m_gestures) for (const GesturePtr &gp : qAsConst(m_gestures))
gp->draw(geom, painter); gp->draw(geom, painter);
} }
@ -429,24 +431,24 @@ MainWindow::MainWindow()
QMenu *fileMenu = menuBar()->addMenu("File"); QMenu *fileMenu = menuBar()->addMenu("File");
QAction *dumpDeviceAction = fileMenu->addAction(QStringLiteral("Dump devices")); QAction *dumpDeviceAction = fileMenu->addAction(QStringLiteral("Dump devices"));
dumpDeviceAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D)); dumpDeviceAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D));
connect(dumpDeviceAction, SIGNAL(triggered()), this, SLOT(dumpTouchDevices())); connect(dumpDeviceAction, &QAction::triggered, this, &MainWindow::dumpTouchDevices);
toolBar->addAction(dumpDeviceAction); toolBar->addAction(dumpDeviceAction);
QAction *clearLogAction = fileMenu->addAction(QStringLiteral("Clear Log")); QAction *clearLogAction = fileMenu->addAction(QStringLiteral("Clear Log"));
clearLogAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L)); clearLogAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
connect(clearLogAction, SIGNAL(triggered()), m_logTextEdit, SLOT(clear())); connect(clearLogAction, &QAction::triggered, m_logTextEdit, &QPlainTextEdit::clear);
toolBar->addAction(clearLogAction); toolBar->addAction(clearLogAction);
QAction *toggleDrawPointAction = fileMenu->addAction(QStringLiteral("Draw Points")); QAction *toggleDrawPointAction = fileMenu->addAction(QStringLiteral("Draw Points"));
toggleDrawPointAction->setCheckable(true); toggleDrawPointAction->setCheckable(true);
toggleDrawPointAction->setChecked(m_touchWidget->drawPoints()); toggleDrawPointAction->setChecked(m_touchWidget->drawPoints());
connect(toggleDrawPointAction, SIGNAL(toggled(bool)), m_touchWidget, SLOT(setDrawPoints(bool))); connect(toggleDrawPointAction, &QAction::toggled, m_touchWidget, &TouchTestWidget::setDrawPoints);
toolBar->addAction(toggleDrawPointAction); toolBar->addAction(toggleDrawPointAction);
QAction *clearPointAction = fileMenu->addAction(QStringLiteral("Clear Points")); QAction *clearPointAction = fileMenu->addAction(QStringLiteral("Clear Points"));
clearPointAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P)); clearPointAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
connect(clearPointAction, SIGNAL(triggered()), m_touchWidget, SLOT(clearPoints())); connect(clearPointAction, &QAction::triggered, m_touchWidget, &TouchTestWidget::clearPoints);
toolBar->addAction(clearPointAction); toolBar->addAction(clearPointAction);
QAction *quitAction = fileMenu->addAction(QStringLiteral("Quit")); QAction *quitAction = fileMenu->addAction(QStringLiteral("Quit"));
quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
toolBar->addAction(quitAction); toolBar->addAction(quitAction);
QSplitter *mainSplitter = new QSplitter(Qt::Vertical, this); QSplitter *mainSplitter = new QSplitter(Qt::Vertical, this);
@ -541,7 +543,7 @@ int main(int argc, char *argv[])
: static_cast<QObject *>(w.touchWidget()); : static_cast<QObject *>(w.touchWidget());
EventFilter *filter = new EventFilter(eventTypes, filterTarget); EventFilter *filter = new EventFilter(eventTypes, filterTarget);
filterTarget->installEventFilter(filter); filterTarget->installEventFilter(filter);
QObject::connect(filter, SIGNAL(eventReceived(QString)), &w, SLOT(appendToLog(QString))); QObject::connect(filter, &EventFilter::eventReceived, &w, &MainWindow::appendToLog);
return a.exec(); return a.exec();
} }