Skip to content

Commit

Permalink
Polish the manual touch test
Browse files Browse the repository at this point in the history
Introduce C++11, nullptr, for, port to Qt 5 connection syntax.

Change-Id: I2d233ccd68bad533af8d4674d91236b2c049e997
Reviewed-by: Andy Shaw <[email protected]>
  • Loading branch information
FriedemannKleint committed Apr 30, 2018
1 parent dbc0a5b commit 578c96f
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions tests/manual/touch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
#include <QDebug>
#include <QTextStream>

bool optIgnoreTouch = false;
QVector<Qt::GestureType> optGestures;
static bool optIgnoreTouch = false;
static QVector<Qt::GestureType> optGestures;

static inline void drawEllipse(const QPointF &center, qreal hDiameter, qreal vDiameter, const QColor &color, QPainter &painter)
{
Expand Down Expand Up @@ -275,10 +275,10 @@ class TouchTestWidget : public QWidget {
Q_OBJECT
Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints)
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);
foreach (Qt::GestureType t, optGestures)
for (Qt::GestureType t : optGestures)
grabGesture(t);
}

Expand Down Expand Up @@ -337,10 +337,11 @@ bool TouchTestWidget::event(QEvent *event)
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
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()));
update();
}
Q_FALLTHROUGH();
case QEvent::TouchEnd:
if (optIgnoreTouch)
event->ignore();
Expand All @@ -358,7 +359,8 @@ bool TouchTestWidget::event(QEvent *event)

void TouchTestWidget::handleGestureEvent(QGestureEvent *gestureEvent)
{
foreach (QGesture *gesture, gestureEvent->gestures()) {
const auto gestures = gestureEvent->gestures();
for (QGesture *gesture : gestures) {
if (optGestures.contains(gesture->gestureType())) {
switch (gesture->state()) {
case Qt::NoGesture:
Expand Down Expand Up @@ -389,15 +391,15 @@ void TouchTestWidget::paintEvent(QPaintEvent *)
const QRectF geom = QRectF(QPointF(0, 0), QSizeF(size()));
painter.fillRect(geom, Qt::white);
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 (point.type == MouseRelease)
drawEllipse(point.pos, point.horizontalDiameter, point.verticalDiameter, point.color(), painter);
else
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);
}

Expand Down Expand Up @@ -429,24 +431,24 @@ MainWindow::MainWindow()
QMenu *fileMenu = menuBar()->addMenu("File");
QAction *dumpDeviceAction = fileMenu->addAction(QStringLiteral("Dump devices"));
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);
QAction *clearLogAction = fileMenu->addAction(QStringLiteral("Clear Log"));
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);
QAction *toggleDrawPointAction = fileMenu->addAction(QStringLiteral("Draw Points"));
toggleDrawPointAction->setCheckable(true);
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);
QAction *clearPointAction = fileMenu->addAction(QStringLiteral("Clear Points"));
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);
QAction *quitAction = fileMenu->addAction(QStringLiteral("Quit"));
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);

QSplitter *mainSplitter = new QSplitter(Qt::Vertical, this);
Expand Down Expand Up @@ -541,7 +543,7 @@ int main(int argc, char *argv[])
: static_cast<QObject *>(w.touchWidget());
EventFilter *filter = new EventFilter(eventTypes, filterTarget);
filterTarget->installEventFilter(filter);
QObject::connect(filter, SIGNAL(eventReceived(QString)), &w, SLOT(appendToLog(QString)));
QObject::connect(filter, &EventFilter::eventReceived, &w, &MainWindow::appendToLog);

return a.exec();
}
Expand Down

0 comments on commit 578c96f

Please sign in to comment.