Skip to content

Commit 61a34fe

Browse files
committed
Coding - move common code handling QEvent->Aspect_WindowInputListener to OcctQtTools
1 parent 4b72c51 commit 61a34fe

5 files changed

Lines changed: 158 additions & 173 deletions

File tree

occt-qopenglwidget/OcctQOpenGLWidgetViewer.cpp

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -119,44 +119,19 @@ bool OcctQOpenGLWidgetViewer::event(QEvent* theEvent)
119119
if (myView.IsNull())
120120
return QOpenGLWidget::event(theEvent);
121121

122-
const bool isTouch = theEvent->type() == QEvent::TouchBegin
123-
|| theEvent->type() == QEvent::TouchUpdate
124-
|| theEvent->type() == QEvent::TouchEnd;
125-
if (!isTouch)
126-
return QOpenGLWidget::event(theEvent);
127-
128-
bool hasUpdates = false;
129-
const QTouchEvent* aQTouchEvent = static_cast<QTouchEvent*>(theEvent);
130-
for (const QTouchEvent::TouchPoint& aQTouch : aQTouchEvent->touchPoints())
122+
if (theEvent->type() == QEvent::TouchBegin
123+
|| theEvent->type() == QEvent::TouchUpdate
124+
|| theEvent->type() == QEvent::TouchEnd)
131125
{
132-
const Standard_Size aTouchId = aQTouch.id();
133-
const Graphic3d_Vec2d aNewPos2d =
134-
myView->Window()->ConvertPointToBacking(Graphic3d_Vec2d(aQTouch.pos().x(), aQTouch.pos().y()));
135-
const Graphic3d_Vec2i aNewPos2i = Graphic3d_Vec2i(aNewPos2d + Graphic3d_Vec2d(0.5));
136-
if (aQTouch.state() == Qt::TouchPointPressed
137-
&& aNewPos2i.minComp() >= 0)
138-
{
139-
hasUpdates = true;
140-
AIS_ViewController::AddTouchPoint(aTouchId, aNewPos2d);
141-
}
142-
else if (aQTouch.state() == Qt::TouchPointMoved
143-
&& AIS_ViewController::TouchPoints().Contains(aTouchId))
144-
{
145-
hasUpdates = true;
146-
AIS_ViewController::UpdateTouchPoint(aTouchId, aNewPos2d);
147-
}
148-
else if (aQTouch.state() == Qt::TouchPointReleased
149-
&& AIS_ViewController::RemoveTouchPoint(aTouchId))
150-
{
151-
hasUpdates = true;
152-
}
153-
}
126+
theEvent->accept();
127+
myHasTouchInput = true;
128+
if (OcctQtTools::qtHandleTouchEvent(*this, myView, static_cast<QTouchEvent*>(theEvent)))
129+
updateView();
154130

155-
myHasTouchInput = true;
156-
if (hasUpdates)
157-
updateView();
131+
return true;
132+
}
158133

159-
return true;
134+
return QOpenGLWidget::event(theEvent);
160135
}
161136

162137
// ================================================================
@@ -205,11 +180,7 @@ void OcctQOpenGLWidgetViewer::mousePressEvent(QMouseEvent* theEvent)
205180
return; // skip mouse events emulated by system from screen touches
206181

207182
theEvent->accept();
208-
const Graphic3d_Vec2d aPnt2d(theEvent->pos().x(), theEvent->pos().y());
209-
const Graphic3d_Vec2i aPnt2i(myView->Window()->ConvertPointToBacking(aPnt2d) + Graphic3d_Vec2d(0.5));
210-
const Aspect_VKeyMouse aButtons = OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons());
211-
const Aspect_VKeyFlags aFlags = OcctQtTools::qtMouseModifiers2VKeys(theEvent->modifiers());
212-
if (AIS_ViewController::UpdateMouseButtons(aPnt2i, aButtons, aFlags, false))
183+
if (OcctQtTools::qtHandleMouseEvent(*this, myView, theEvent))
213184
updateView();
214185
}
215186

@@ -223,11 +194,7 @@ void OcctQOpenGLWidgetViewer::mouseReleaseEvent(QMouseEvent* theEvent)
223194
return;
224195

225196
theEvent->accept();
226-
const Graphic3d_Vec2d aPnt2d(theEvent->pos().x(), theEvent->pos().y());
227-
const Graphic3d_Vec2i aPnt2i(myView->Window()->ConvertPointToBacking(aPnt2d) + Graphic3d_Vec2d(0.5));
228-
const Aspect_VKeyMouse aButtons = OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons());
229-
const Aspect_VKeyFlags aFlags = OcctQtTools::qtMouseModifiers2VKeys(theEvent->modifiers());
230-
if (AIS_ViewController::UpdateMouseButtons(aPnt2i, aButtons, aFlags, false))
197+
if (OcctQtTools::qtHandleMouseEvent(*this, myView, theEvent))
231198
updateView();
232199
}
233200

@@ -244,11 +211,7 @@ void OcctQOpenGLWidgetViewer::mouseMoveEvent(QMouseEvent* theEvent)
244211
return; // skip mouse events emulated by system from screen touches
245212

246213
theEvent->accept();
247-
const Graphic3d_Vec2d aPnt2d(theEvent->pos().x(), theEvent->pos().y());
248-
const Graphic3d_Vec2i aPnt2i(myView->Window()->ConvertPointToBacking(aPnt2d) + Graphic3d_Vec2d(0.5));
249-
const Aspect_VKeyMouse aButtons = OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons());
250-
const Aspect_VKeyFlags aFlags = OcctQtTools::qtMouseModifiers2VKeys(theEvent->modifiers());
251-
if (AIS_ViewController::UpdateMousePosition(aPnt2i, aButtons, aFlags, false))
214+
if (OcctQtTools::qtHandleMouseEvent(*this, myView, theEvent))
252215
updateView();
253216
}
254217

@@ -262,14 +225,14 @@ void OcctQOpenGLWidgetViewer::wheelEvent(QWheelEvent* theEvent)
262225
return;
263226

264227
theEvent->accept();
228+
229+
#if (OCC_VERSION_HEX >= 0x070700)
265230
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
266231
const Graphic3d_Vec2d aPnt2d(theEvent->position().x(), theEvent->position().y());
267232
#else
268233
const Graphic3d_Vec2d aPnt2d(theEvent->pos().x(), theEvent->pos().y());
269234
#endif
270235
const Graphic3d_Vec2i aPnt2i(myView->Window()->ConvertPointToBacking(aPnt2d) + Graphic3d_Vec2d(0.5));
271-
272-
#if (OCC_VERSION_HEX >= 0x070700)
273236
if (!myView->Subviews().IsEmpty())
274237
{
275238
Handle(V3d_View) aPickedView = myView->PickSubview(aPnt2i);
@@ -283,7 +246,7 @@ void OcctQOpenGLWidgetViewer::wheelEvent(QWheelEvent* theEvent)
283246
}
284247
#endif
285248

286-
if (AIS_ViewController::UpdateZoom(Aspect_ScrollDelta(aPnt2i, double(theEvent->angleDelta().y()) / 8.0)))
249+
if (OcctQtTools::qtHandleWheelEvent(*this, myView, theEvent))
287250
updateView();
288251
}
289252

occt-qt-tools/OcctQtTools.cpp

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include "OcctQtTools.h"
44

5+
#include <Aspect_ScrollDelta.hxx>
56
#include <OpenGl_Caps.hxx>
67
#include <OSD_Environment.hxx>
78
#include <Standard_Version.hxx>
9+
#include <V3d_View.hxx>
810

911
#include <QCoreApplication>
1012
#include <QGuiApplication>
@@ -71,12 +73,14 @@ void OcctQtTools::qtGlPlatformSetup()
7173
aQsgLoop.Build();
7274
}*/
7375

74-
// enable auto-scaling for high-density screens
76+
// enable auto-scaling for high-density screens and fractional scale factors
77+
// (this is default since Qt6)
78+
#if (QT_VERSION_MAJOR == 5)
7579
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
76-
#if (QT_VERSION_MAJOR == 5 && QT_VERSION_MINOR >= 14)
77-
// this is default since Qt6 (for fractional scale factors)
80+
#if (QT_VERSION_MINOR >= 14)
7881
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
7982
#endif
83+
#endif
8084
}
8185

8286
// ================================================================
@@ -136,6 +140,88 @@ void OcctQtTools::qtGlCapsFromSurfaceFormat(OpenGl_Caps& theCaps, const QSurface
136140
#endif
137141
}
138142

143+
// ================================================================
144+
// Function : qtHandleHoverEvent
145+
// ================================================================
146+
bool OcctQtTools::qtHandleHoverEvent(Aspect_WindowInputListener& theListener,
147+
const Handle(V3d_View)& theView,
148+
const QHoverEvent* theEvent)
149+
{
150+
const Graphic3d_Vec2d aPnt2d(theEvent->pos().x(), theEvent->pos().y());
151+
const Graphic3d_Vec2i aPnt2i(theView->Window()->ConvertPointToBacking(aPnt2d) + Graphic3d_Vec2d(0.5));
152+
const Aspect_VKeyMouse aButtons = Aspect_VKeyMouse_NONE;
153+
const Aspect_VKeyFlags aFlags = OcctQtTools::qtMouseModifiers2VKeys(theEvent->modifiers());
154+
return theListener.UpdateMousePosition(aPnt2i, aButtons, aFlags, false);
155+
}
156+
157+
// ================================================================
158+
// Function : qtHandleMouseEvent
159+
// ================================================================
160+
bool OcctQtTools::qtHandleMouseEvent(Aspect_WindowInputListener& theListener,
161+
const Handle(V3d_View)& theView,
162+
const QMouseEvent* theEvent)
163+
{
164+
const Graphic3d_Vec2d aPnt2d(theEvent->pos().x(), theEvent->pos().y());
165+
const Graphic3d_Vec2i aPnt2i(theView->Window()->ConvertPointToBacking(aPnt2d) + Graphic3d_Vec2d(0.5));
166+
const Aspect_VKeyMouse aButtons = OcctQtTools::qtMouseButtons2VKeys(theEvent->buttons());
167+
const Aspect_VKeyFlags aFlags = OcctQtTools::qtMouseModifiers2VKeys(theEvent->modifiers());
168+
if (theEvent->type() == QEvent::MouseMove)
169+
return theListener.UpdateMousePosition(aPnt2i, aButtons, aFlags, false);
170+
171+
return theListener.UpdateMouseButtons(aPnt2i, aButtons, aFlags, false);
172+
}
173+
174+
// ================================================================
175+
// Function : qtHandleWheelEvent
176+
// ================================================================
177+
bool OcctQtTools::qtHandleWheelEvent(Aspect_WindowInputListener& theListener,
178+
const Handle(V3d_View)& theView,
179+
const QWheelEvent* theEvent)
180+
{
181+
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
182+
const Graphic3d_Vec2d aPnt2d(theEvent->position().x(), theEvent->position().y());
183+
#else
184+
const Graphic3d_Vec2d aPnt2d(theEvent->pos().x(), theEvent->pos().y());
185+
#endif
186+
const Graphic3d_Vec2i aPnt2i(theView->Window()->ConvertPointToBacking(aPnt2d) + Graphic3d_Vec2d(0.5));
187+
return theListener.UpdateMouseScroll(Aspect_ScrollDelta(aPnt2i, double(theEvent->angleDelta().y()) / 120.0));
188+
}
189+
190+
// ================================================================
191+
// Function : qtHandleTouchEvent
192+
// ================================================================
193+
bool OcctQtTools::qtHandleTouchEvent(Aspect_WindowInputListener& theListener,
194+
const Handle(V3d_View)& theView,
195+
const QTouchEvent* theEvent)
196+
{
197+
bool hasUpdates = false;
198+
for (const QTouchEvent::TouchPoint& aQTouch : theEvent->touchPoints())
199+
{
200+
const Standard_Size aTouchId = aQTouch.id();
201+
const Graphic3d_Vec2d aNewPos2d =
202+
theView->Window()->ConvertPointToBacking(Graphic3d_Vec2d(aQTouch.pos().x(), aQTouch.pos().y()));
203+
const Graphic3d_Vec2i aNewPos2i = Graphic3d_Vec2i(aNewPos2d + Graphic3d_Vec2d(0.5));
204+
if (aQTouch.state() == Qt::TouchPointPressed
205+
&& aNewPos2i.minComp() >= 0)
206+
{
207+
hasUpdates = true;
208+
theListener.AddTouchPoint(aTouchId, aNewPos2d);
209+
}
210+
else if (aQTouch.state() == Qt::TouchPointMoved
211+
&& theListener.TouchPoints().Contains(aTouchId))
212+
{
213+
hasUpdates = true;
214+
theListener.UpdateTouchPoint(aTouchId, aNewPos2d);
215+
}
216+
else if (aQTouch.state() == Qt::TouchPointReleased
217+
&& theListener.RemoveTouchPoint(aTouchId))
218+
{
219+
hasUpdates = true;
220+
}
221+
}
222+
return hasUpdates;
223+
}
224+
139225
// ================================================================
140226
// Function : qtMouseButtons2VKeys
141227
// ================================================================

occt-qt-tools/OcctQtTools.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#ifndef _OcctQtTools_HeaderFile
44
#define _OcctQtTools_HeaderFile
55

6-
#include <Aspect_VKey.hxx>
6+
#include <Aspect_WindowInputListener.hxx>
77
#include <Quantity_Color.hxx>
88

99
#include <Standard_WarningsDisable.hxx>
@@ -13,6 +13,7 @@
1313
#include <Standard_WarningsRestore.hxx>
1414

1515
class OpenGl_Caps;
16+
class V3d_View;
1617

1718
//! Auxiliary tools between Qt and OCCT definitions.
1819
namespace OcctQtTools
@@ -43,6 +44,27 @@ Aspect_VKeyFlags qtMouseModifiers2VKeys(Qt::KeyboardModifiers theModifiers);
4344

4445
//! Map Qt key to virtual key.
4546
Aspect_VKey qtKey2VKey(int theKey);
47+
48+
//! Queue Qt mouse hover event to OCCT listener.
49+
bool qtHandleHoverEvent(Aspect_WindowInputListener& theListener,
50+
const Handle(V3d_View)& theView,
51+
const QHoverEvent* theEvent);
52+
53+
//! Queue Qt mouse event to OCCT listener.
54+
bool qtHandleMouseEvent(Aspect_WindowInputListener& theListener,
55+
const Handle(V3d_View)& theView,
56+
const QMouseEvent* theEvent);
57+
58+
//! Queue Qt mouse wheel event to OCCT listener.
59+
bool qtHandleWheelEvent(Aspect_WindowInputListener& theListener,
60+
const Handle(V3d_View)& theView,
61+
const QWheelEvent* theEvent);
62+
63+
//! Queue Qt touch event to OCCT listener.
64+
bool qtHandleTouchEvent(Aspect_WindowInputListener& theListener,
65+
const Handle(V3d_View)& theView,
66+
const QTouchEvent* theEvent);
67+
4668
} // namespace OcctQtTools
4769

4870
#endif // _OcctQtTools_HeaderFile

0 commit comments

Comments
 (0)