Skip to content

Commit f4aa91b

Browse files
committed
Enable and redirect QTouchEvent events to AIS_ViewController
1 parent 9d637a6 commit f4aa91b

4 files changed

Lines changed: 113 additions & 1 deletion

File tree

occt-qopenglwidget/OcctQOpenGLWidgetViewer.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ OcctQOpenGLWidgetViewer::OcctQOpenGLWidgetViewer(QWidget* theParent)
101101
Graphic3d_RenderingParams::PerfCounters_FrameRate | Graphic3d_RenderingParams::PerfCounters_Triangles);
102102

103103
// Qt widget setup
104+
setAttribute(Qt::WA_AcceptTouchEvents); // necessary to recieve QTouchEvent events
104105
setMouseTracking(true);
105106
setBackgroundRole(QPalette::NoRole); // or NoBackground
106107
setFocusPolicy(Qt::StrongFocus); // set focus policy to threat QContextMenuEvent from keyboard
@@ -237,6 +238,53 @@ void OcctQOpenGLWidgetViewer::initializeGL()
237238
}
238239
}
239240

241+
// ================================================================
242+
// Function : event
243+
// ================================================================
244+
bool OcctQOpenGLWidgetViewer::event(QEvent* theEvent)
245+
{
246+
if (myView.IsNull())
247+
return QOpenGLWidget::event(theEvent);
248+
249+
const bool isTouch = theEvent->type() == QEvent::TouchBegin
250+
|| theEvent->type() == QEvent::TouchUpdate
251+
|| theEvent->type() == QEvent::TouchEnd;
252+
if (!isTouch)
253+
return QOpenGLWidget::event(theEvent);
254+
255+
bool hasUpdates = false;
256+
const QTouchEvent* aQTouchEvent = static_cast<QTouchEvent*>(theEvent);
257+
for (const QTouchEvent::TouchPoint& aQTouch : aQTouchEvent->touchPoints())
258+
{
259+
const Standard_Size aTouchId = aQTouch.id();
260+
const Graphic3d_Vec2d aNewPos2d(aQTouch.pos().x(), aQTouch.pos().y());
261+
const Graphic3d_Vec2i aNewPos2i = Graphic3d_Vec2i(aNewPos2d + Graphic3d_Vec2d(0.5));
262+
if (aQTouch.state() == Qt::TouchPointPressed
263+
&& aNewPos2i.minComp() >= 0)
264+
{
265+
hasUpdates = true;
266+
AddTouchPoint(aTouchId, aNewPos2d);
267+
}
268+
else if (aQTouch.state() == Qt::TouchPointMoved
269+
&& TouchPoints().Contains(aTouchId))
270+
{
271+
hasUpdates = true;
272+
UpdateTouchPoint(aTouchId, aNewPos2d);
273+
}
274+
else if (aQTouch.state() == Qt::TouchPointReleased
275+
&& RemoveTouchPoint(aTouchId))
276+
{
277+
hasUpdates = true;
278+
}
279+
}
280+
281+
myHasTouchInput = true;
282+
if (hasUpdates)
283+
updateView();
284+
285+
return true;
286+
}
287+
240288
// ================================================================
241289
// Function : closeEvent
242290
// ================================================================
@@ -278,6 +326,9 @@ void OcctQOpenGLWidgetViewer::mousePressEvent(QMouseEvent* theEvent)
278326
if (myView.IsNull())
279327
return;
280328

329+
if (myHasTouchInput && theEvent->source() == Qt::MouseEventSynthesizedBySystem)
330+
return; // skip mouse events emulated by system from screen touches
331+
281332
const Graphic3d_Vec2i aPnt(theEvent->pos().x(), theEvent->pos().y());
282333
const Aspect_VKeyFlags aFlags = OcctQtTools::qtMouseModifiers2VKeys(theEvent->modifiers());
283334
if (UpdateMouseButtons(aPnt, OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons()), aFlags, false))
@@ -308,6 +359,9 @@ void OcctQOpenGLWidgetViewer::mouseMoveEvent(QMouseEvent* theEvent)
308359
if (myView.IsNull())
309360
return;
310361

362+
if (myHasTouchInput && theEvent->source() == Qt::MouseEventSynthesizedBySystem)
363+
return; // skip mouse events emulated by system from screen touches
364+
311365
const Graphic3d_Vec2i aNewPos(theEvent->pos().x(), theEvent->pos().y());
312366
if (UpdateMousePosition(aNewPos,
313367
OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons()),

occt-qopenglwidget/OcctQOpenGLWidgetViewer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class OcctQOpenGLWidgetViewer : public QOpenGLWidget, public AIS_ViewController
6363
// virtual void resizeGL(int , int ) override;
6464

6565
protected: // user input events
66+
virtual bool event(QEvent* theEvent) override;
6667
virtual void closeEvent(QCloseEvent* theEvent) override;
6768
virtual void keyPressEvent(QKeyEvent* theEvent) override;
6869
virtual void mousePressEvent(QMouseEvent* theEvent) override;
@@ -90,6 +91,7 @@ class OcctQOpenGLWidgetViewer : public QOpenGLWidget, public AIS_ViewController
9091

9192
QString myGlInfo;
9293
bool myIsCoreProfile = true;
94+
bool myHasTouchInput = false;
9395
};
9496

9597
#endif // _OcctQOpenGLWidgetViewer_HeaderFile

occt-qwidget/OcctQWidgetViewer.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ OcctQWidgetViewer::OcctQWidgetViewer(QWidget* theParent)
6161
// Qt widget setup
6262
setAttribute(Qt::WA_PaintOnScreen);
6363
setAttribute(Qt::WA_NoSystemBackground);
64-
setAttribute(Qt::WA_NativeWindow); // request native window for this widget to create OpenGL context
64+
setAttribute(Qt::WA_NativeWindow); // request native window for this widget to create OpenGL context
65+
setAttribute(Qt::WA_AcceptTouchEvents); // necessary to recieve QTouchEvent events
6566
setMouseTracking(true);
6667
setBackgroundRole(QPalette::NoRole); // or NoBackground
6768
setFocusPolicy(Qt::StrongFocus); // set focus policy to threat QContextMenuEvent from keyboard
@@ -150,6 +151,53 @@ void OcctQWidgetViewer::initializeGL()
150151
}
151152
}
152153

154+
// ================================================================
155+
// Function : event
156+
// ================================================================
157+
bool OcctQWidgetViewer::event(QEvent* theEvent)
158+
{
159+
if (myView.IsNull())
160+
return QWidget::event(theEvent);
161+
162+
const bool isTouch = theEvent->type() == QEvent::TouchBegin
163+
|| theEvent->type() == QEvent::TouchUpdate
164+
|| theEvent->type() == QEvent::TouchEnd;
165+
if (!isTouch)
166+
return QWidget::event(theEvent);
167+
168+
bool hasUpdates = false;
169+
const QTouchEvent* aQTouchEvent = static_cast<QTouchEvent*>(theEvent);
170+
for (const QTouchEvent::TouchPoint& aQTouch : aQTouchEvent->touchPoints())
171+
{
172+
const Standard_Size aTouchId = aQTouch.id();
173+
const Graphic3d_Vec2d aNewPos2d(aQTouch.pos().x(), aQTouch.pos().y());
174+
const Graphic3d_Vec2i aNewPos2i = Graphic3d_Vec2i(aNewPos2d + Graphic3d_Vec2d(0.5));
175+
if (aQTouch.state() == Qt::TouchPointPressed
176+
&& aNewPos2i.minComp() >= 0)
177+
{
178+
hasUpdates = true;
179+
AddTouchPoint(aTouchId, aNewPos2d);
180+
}
181+
else if (aQTouch.state() == Qt::TouchPointMoved
182+
&& TouchPoints().Contains(aTouchId))
183+
{
184+
hasUpdates = true;
185+
UpdateTouchPoint(aTouchId, aNewPos2d);
186+
}
187+
else if (aQTouch.state() == Qt::TouchPointReleased
188+
&& RemoveTouchPoint(aTouchId))
189+
{
190+
hasUpdates = true;
191+
}
192+
}
193+
194+
myHasTouchInput = true;
195+
if (hasUpdates)
196+
updateView();
197+
198+
return true;
199+
}
200+
153201
// ================================================================
154202
// Function : closeEvent
155203
// ================================================================
@@ -191,6 +239,9 @@ void OcctQWidgetViewer::mousePressEvent(QMouseEvent* theEvent)
191239
if (myView.IsNull())
192240
return;
193241

242+
if (myHasTouchInput && theEvent->source() == Qt::MouseEventSynthesizedBySystem)
243+
return; // skip mouse events emulated by system from screen touches
244+
194245
const Graphic3d_Vec2i aPnt(theEvent->pos().x(), theEvent->pos().y());
195246
const Aspect_VKeyFlags aFlags = OcctQtTools::qtMouseModifiers2VKeys(theEvent->modifiers());
196247
if (UpdateMouseButtons(aPnt, OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons()), aFlags, false))
@@ -221,6 +272,9 @@ void OcctQWidgetViewer::mouseMoveEvent(QMouseEvent* theEvent)
221272
if (myView.IsNull())
222273
return;
223274

275+
if (myHasTouchInput && theEvent->source() == Qt::MouseEventSynthesizedBySystem)
276+
return; // skip mouse events emulated by system from screen touches
277+
224278
const Graphic3d_Vec2i aNewPos(theEvent->pos().x(), theEvent->pos().y());
225279
if (UpdateMousePosition(aNewPos,
226280
OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons()),

occt-qwidget/OcctQWidgetViewer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class OcctQWidgetViewer : public QWidget, public AIS_ViewController
7171
virtual QPaintEngine* paintEngine() const override { return nullptr; }
7272

7373
protected: // user input events
74+
virtual bool event(QEvent* theEvent) override;
7475
virtual void closeEvent(QCloseEvent* theEvent) override;
7576
virtual void keyPressEvent(QKeyEvent* theEvent) override;
7677
virtual void mousePressEvent(QMouseEvent* theEvent) override;
@@ -98,6 +99,7 @@ class OcctQWidgetViewer : public QWidget, public AIS_ViewController
9899

99100
QString myGlInfo;
100101
bool myIsCoreProfile = true;
102+
bool myHasTouchInput = false;
101103
};
102104

103105
#endif // _OcctQWidgetViewer_HeaderFile

0 commit comments

Comments
 (0)