Skip to content

Commit 84e71bd

Browse files
committed
Touch support for Widgets
1 parent 8903b94 commit 84e71bd

File tree

8 files changed

+420
-10
lines changed

8 files changed

+420
-10
lines changed

inc/extension_qt/q_view_executor.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
#include <QtCore/QDebug>
1414
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
1515
#include <QtWidgets/QWidget>
16-
#include <QtGui/QTouchDevice>
16+
#include <QtGui/QTouchDevice>
1717
#else
1818
#include <QtGui/QWidget>
1919
#endif
20+
#include <QtGui/QTouchEvent>
2021

2122
namespace webdriver {
2223

@@ -48,7 +49,9 @@ class QViewCmdExecutor : public ViewCmdExecutor {
4849

4950
protected:
5051
QWidget* getView(const ViewId& viewId, Error** error);
51-
52+
QTouchEvent::TouchPoint createTouchPoint(Qt::TouchPointState state, QPointF &point);
53+
QTouchEvent* createSimpleTouchEvent(QEvent::Type eventType, Qt::TouchPointStates touchPointStates, QPointF point);
54+
QTouchEvent* createTouchEvent(QEvent::Type eventType, Qt::TouchPointStates touchPointStates, const QList<QTouchEvent::TouchPoint> &touchPoints);
5255
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
5356
QTouchDevice touchDevice;
5457
#endif

inc/extension_qt/widget_view_executor.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,16 @@ class QWidgetViewCmdExecutor : public QViewCmdExecutor {
128128
virtual void GetStorageSize(StorageType type, int* size, Error** error) NOT_SUPPORTED_IMPL;
129129
virtual void GetGeoLocation(base::DictionaryValue** geolocation, Error** error) NOT_SUPPORTED_IMPL;
130130
virtual void SetGeoLocation(const base::DictionaryValue* geolocation, Error** error) NOT_SUPPORTED_IMPL;
131-
virtual void TouchClick(const ElementId& element, Error **error) NOT_SUPPORTED_IMPL;
132-
virtual void TouchDoubleClick(const ElementId& element, Error **error) NOT_SUPPORTED_IMPL;
133-
virtual void TouchDown(const int &x, const int &y, Error **error) NOT_SUPPORTED_IMPL;
134-
virtual void TouchUp(const int &x, const int &y, Error **error) NOT_SUPPORTED_IMPL;
135-
virtual void TouchMove(const int &x, const int &y, Error **error) NOT_SUPPORTED_IMPL;
136-
virtual void TouchLongClick(const ElementId& element, Error **error) NOT_SUPPORTED_IMPL;
131+
virtual void TouchClick(const ElementId& element, Error **error);
132+
virtual void TouchDoubleClick(const ElementId& element, Error **error);
133+
virtual void TouchDown(const int &x, const int &y, Error **error);
134+
virtual void TouchUp(const int &x, const int &y, Error **error);
135+
virtual void TouchMove(const int &x, const int &y, Error **error);
136+
virtual void TouchLongClick(const ElementId& element, Error **error);
137137
virtual void TouchScroll(const int &xoffset, const int &yoffset, Error **error) NOT_SUPPORTED_IMPL;
138-
virtual void TouchScroll(const ElementId &element, const int &xoffset, const int &yoffset, Error **error) NOT_SUPPORTED_IMPL;
138+
virtual void TouchScroll(const ElementId &element, const int &xoffset, const int &yoffset, Error **error);
139139
virtual void TouchFlick(const int &xSpeed, const int &ySpeed, Error **error) NOT_SUPPORTED_IMPL;
140-
virtual void TouchFlick(const ElementId &element, const int &xoffset, const int &yoffset, const int &speed, Error **error) NOT_SUPPORTED_IMPL;
140+
virtual void TouchFlick(const ElementId &element, const int &xoffset, const int &yoffset, const int &speed, Error **error);
141141
virtual void GetPlayerState(const ElementId& element, PlayerState*, Error**error);
142142
virtual void SetPlayerState(const ElementId& element, PlayerState, Error**error);
143143
virtual void GetPlayerVolume(const ElementId& element, double*, Error**error);

src/Test/TouchTest.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "TouchTest.h"
2+
3+
TouchTestWidget::TouchTestWidget(QWidget *parent) :
4+
QWidget(parent)
5+
{
6+
resize(400, 620);
7+
8+
touchPanel = new QFrame(this);
9+
touchPanel->installEventFilter(this);
10+
touchPanel->setObjectName("touchArea");
11+
touchPanel->setFrameShape(QFrame::Box);
12+
touchPanel->setAttribute(Qt::WA_AcceptTouchEvents);
13+
14+
resultLabel = new QLabel(this);
15+
resultLabel->setObjectName("resultLabel");
16+
QVBoxLayout* layout = new QVBoxLayout(this);
17+
layout->addWidget(resultLabel);
18+
layout->addWidget(touchPanel);
19+
setLayout(layout);
20+
21+
pressLabel = new QLabel(touchPanel);
22+
pressLabel->setText("Pressed");
23+
pressLabel->setObjectName("pressLabel");
24+
pressLabel->setVisible(false);
25+
26+
releaseLabel = new QLabel(touchPanel);
27+
releaseLabel->setText("Released");
28+
releaseLabel->setObjectName("releaseLabel");
29+
releaseLabel->setVisible(false);
30+
31+
moveLabel = new QLabel(touchPanel);
32+
moveLabel->setText("Move");
33+
moveLabel->setObjectName("moveLabel");
34+
moveLabel->setVisible(false);
35+
}
36+
37+
38+
bool TouchTestWidget::eventFilter(QObject *o, QEvent *e)
39+
{
40+
if (o == touchPanel)
41+
{
42+
if ((e->type() == QEvent::TouchBegin))
43+
{
44+
resultLabel->setText(resultLabel->text()+"Pressed");
45+
46+
QTouchEvent* te = static_cast<QTouchEvent*>(e);
47+
if (te)
48+
{
49+
pressLabel->setVisible(true);
50+
QPoint tpos = QPoint(te->touchPoints().at(0).pos().x(), te->touchPoints().at(0).pos().y());
51+
pressLabel->move(tpos);
52+
}
53+
54+
}
55+
if (e->type() == QEvent::TouchUpdate)
56+
{
57+
resultLabel->setText(resultLabel->text()+"Moved");
58+
59+
QTouchEvent* te = static_cast<QTouchEvent*>(e);
60+
if (te)
61+
{
62+
moveLabel->setVisible(true);
63+
QPoint tpos = QPoint(te->touchPoints().at(0).pos().x(), te->touchPoints().at(0).pos().y());
64+
moveLabel->move(tpos);
65+
moveLabel->resize(100, 20);
66+
moveLabel->raise();
67+
68+
}
69+
}
70+
if (e->type() == QEvent::TouchEnd)
71+
{
72+
resultLabel->setText(resultLabel->text()+"Released");
73+
74+
QTouchEvent* te = static_cast<QTouchEvent*>(e);
75+
if (te)
76+
{
77+
releaseLabel->setVisible(true);
78+
QPoint tpos = QPoint(te->touchPoints().at(0).pos().x(), te->touchPoints().at(0).pos().y());
79+
releaseLabel->move(tpos);
80+
qDebug()<<releaseLabel->geometry();
81+
}
82+
}
83+
}
84+
return false;
85+
}

src/Test/TouchTest.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef TOUCHTEST_H
2+
#define TOUCHTEST_H
3+
4+
#include "CommonQtTestHeaders.h"
5+
6+
class TouchTestWidget : public QWidget
7+
{
8+
public:
9+
explicit TouchTestWidget(QWidget *parent = 0);
10+
11+
protected:
12+
bool eventFilter(QObject *o, QEvent *e);
13+
14+
private:
15+
QFrame* touchPanel;
16+
QLabel* resultLabel;
17+
QLabel* pressLabel;
18+
QLabel* releaseLabel;
19+
QLabel* moveLabel;
20+
};
21+
22+
#endif // TOUCHTEST_H

src/Test/main.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ std::string tests::testDataFolder;
3535
#include "StaleElementReferenceTest.h"
3636
#include "VisibilityTest.h"
3737
#include "BasicMouseInterfaceTest.h"
38+
#include "TouchTest.h"
3839

3940
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
4041
#include "WindowWithDeclarativeViewTest.h"
@@ -137,6 +138,7 @@ int main(int argc, char *argv[])
137138
widgetCreator->RegisterViewClass<StaleElementReferenceTestWidget>("StaleElementReferenceTestWidget");
138139
widgetCreator->RegisterViewClass<VisibilityTestWidget>("VisibilityTestWidget");
139140
widgetCreator->RegisterViewClass<BasicMouseInterfaceTestWidget>("BasicMouseInterfaceTestWidget");
141+
widgetCreator->RegisterViewClass<TouchTestWidget>("TouchTestWidget");
140142
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
141143
widgetCreator->RegisterViewClass<WindowWithDeclarativeViewTestWidget>("WindowWithDeclarativeViewTestWidget");
142144
#endif

src/webdriver/extension_qt/q_view_executor.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <QtWidgets/QDesktopWidget>
1616
#include <QtWidgets/QMessageBox>
1717
#include <QtWidgets/QInputDialog>
18+
#include <QtCore/QDateTime>
1819
#ifdef OS_ANDROID
1920
#include <qpa/qplatformnativeinterface.h>
2021
#include <jni.h>
@@ -372,4 +373,42 @@ void QViewCmdExecutor::GetOrientation(std::string *orientation, Error **error)
372373
#endif
373374
}
374375

376+
QTouchEvent::TouchPoint QViewCmdExecutor::createTouchPoint(Qt::TouchPointState state, QPointF &point)
377+
{
378+
QTouchEvent::TouchPoint touchPoint(1);
379+
touchPoint.setPos(point);
380+
touchPoint.setState(state);
381+
touchPoint.setPressure(1);
382+
return touchPoint;
383+
}
384+
385+
QTouchEvent* QViewCmdExecutor::createSimpleTouchEvent(QEvent::Type eventType, Qt::TouchPointStates touchPointStates, QPointF point)
386+
{
387+
QList<QTouchEvent::TouchPoint> points;
388+
Qt::TouchPointState touchPointState;
389+
if (touchPointStates & Qt::TouchPointPressed)
390+
touchPointState = Qt::TouchPointPressed;
391+
else if (touchPointStates & Qt::TouchPointReleased)
392+
touchPointState = Qt::TouchPointReleased;
393+
else {
394+
touchPointState = Qt::TouchPointMoved;
395+
}
396+
QTouchEvent::TouchPoint touchPoint = createTouchPoint(touchPointState, point);
397+
points.append(touchPoint);
398+
return createTouchEvent(eventType, touchPointStates, points);
399+
}
400+
401+
QTouchEvent* QViewCmdExecutor::createTouchEvent(QEvent::Type eventType, Qt::TouchPointStates touchPointStates, const QList<QTouchEvent::TouchPoint> &touchPoints)
402+
{
403+
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
404+
QTouchEvent *touchEvent = new QTouchEvent(eventType, &touchDevice, Qt::NoModifier, touchPointStates, touchPoints);
405+
QDateTime current = QDateTime::currentDateTime();
406+
ulong timestame = current.toMSecsSinceEpoch() & (((qint64)1<<(sizeof(ulong)*8))-1);
407+
touchEvent->setTimestamp(timestame);
408+
#else
409+
QTouchEvent *touchEvent = new QTouchEvent(eventType, QTouchEvent::TouchScreen, Qt::NoModifier, touchPointStates, touchPoints);
410+
#endif
411+
return touchEvent;
412+
}
413+
375414
} // namespace webdriver

0 commit comments

Comments
 (0)