-
Notifications
You must be signed in to change notification settings - Fork 138
/
occView.cpp
435 lines (351 loc) · 9.96 KB
/
occView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright (c) 2018 Shing Liu All Rights Reserved.
*
* File : OccView.cpp
* Author : Shing Liu([email protected])
* Date : 2018-01-08 21:00
* Version : OpenCASCADE7.2.0 & Qt5.7.1
*
* Description : Qt widget for OpenCASCADE viewer.
*/
#include <OpenGl_GraphicDriver.hxx>
#include "occView.h"
#include <QMenu>
#include <QMouseEvent>
#include <QRubberBand>
#include <QStyleFactory>
#include <V3d_View.hxx>
#include <Aspect_Handle.hxx>
#include <Aspect_DisplayConnection.hxx>
#ifdef WNT
#include <WNT_Window.hxx>
#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
#include <Cocoa_Window.hxx>
#else
#undef Bool
#undef CursorShape
#undef None
#undef KeyPress
#undef KeyRelease
#undef FocusIn
#undef FocusOut
#undef FontChange
#undef Expose
#include <Xw_Window.hxx>
#endif
static Handle(Graphic3d_GraphicDriver)& GetGraphicDriver()
{
static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
return aGraphicDriver;
}
OccView::OccView(QWidget* parent )
: QWidget(parent),
myXmin(0),
myYmin(0),
myXmax(0),
myYmax(0),
myCurrentMode(CurAction3d_DynamicRotation),
myDegenerateModeIsOn(Standard_True),
myRectBand(NULL)
{
// No Background
setBackgroundRole( QPalette::NoRole );
// set focus policy to threat QContextMenuEvent from keyboard
setFocusPolicy(Qt::StrongFocus);
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
// Enable the mouse tracking, by default the mouse tracking is disabled.
setMouseTracking( true );
init();
}
void OccView::init()
{
// Create Aspect_DisplayConnection
Handle(Aspect_DisplayConnection) aDisplayConnection =
new Aspect_DisplayConnection();
// Get graphic driver if it exists, otherwise initialise it
if (GetGraphicDriver().IsNull())
{
GetGraphicDriver() = new OpenGl_GraphicDriver(aDisplayConnection);
}
// Get window handle. This returns something suitable for all platforms.
WId window_handle = (WId) winId();
// Create appropriate window for platform
#ifdef WNT
Handle(WNT_Window) wind = new WNT_Window((Aspect_Handle) window_handle);
#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
Handle(Cocoa_Window) wind = new Cocoa_Window((NSView *) window_handle);
#else
Handle(Xw_Window) wind = new Xw_Window(aDisplayConnection, (Window) window_handle);
#endif
// Create V3dViewer and V3d_View
myViewer = new V3d_Viewer(GetGraphicDriver(), Standard_ExtString("viewer3d"));
myView = myViewer->CreateView();
myView->SetWindow(wind);
if (!wind->IsMapped()) wind->Map();
// Create AISInteractiveContext
myContext = new AIS_InteractiveContext(myViewer);
// Set up lights etc
myViewer->SetDefaultLights();
myViewer->SetLightOn();
myView->SetBackgroundColor(Quantity_NOC_BLACK);
myView->MustBeResized();
myView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_GOLD, 0.08, V3d_ZBUFFER);
myContext->SetDisplayMode(AIS_Shaded, Standard_True);
}
const Handle(AIS_InteractiveContext)& OccView::getContext() const
{
return myContext;
}
/*!
Get paint engine for the OpenGL viewer. [ virtual public ]
*/
QPaintEngine* OccView::paintEngine() const
{
return 0;
}
void OccView::paintEvent( QPaintEvent* /*theEvent*/ )
{
myView->Redraw();
}
void OccView::resizeEvent( QResizeEvent* /*theEvent*/ )
{
if( !myView.IsNull() )
{
myView->MustBeResized();
}
}
void OccView::fitAll( void )
{
myView->FitAll();
myView->ZFitAll();
myView->Redraw();
}
void OccView::reset( void )
{
myView->Reset();
}
void OccView::pan( void )
{
myCurrentMode = CurAction3d_DynamicPanning;
}
void OccView::zoom( void )
{
myCurrentMode = CurAction3d_DynamicZooming;
}
void OccView::rotate( void )
{
myCurrentMode = CurAction3d_DynamicRotation;
}
void OccView::mousePressEvent( QMouseEvent* theEvent )
{
if (theEvent->button() == Qt::LeftButton)
{
onLButtonDown((theEvent->buttons() | theEvent->modifiers()), theEvent->pos());
}
else if (theEvent->button() == Qt::MidButton)
{
onMButtonDown((theEvent->buttons() | theEvent->modifiers()), theEvent->pos());
}
else if (theEvent->button() == Qt::RightButton)
{
onRButtonDown((theEvent->buttons() | theEvent->modifiers()), theEvent->pos());
}
}
void OccView::mouseReleaseEvent( QMouseEvent* theEvent )
{
if (theEvent->button() == Qt::LeftButton)
{
onLButtonUp(theEvent->buttons() | theEvent->modifiers(), theEvent->pos());
}
else if (theEvent->button() == Qt::MidButton)
{
onMButtonUp(theEvent->buttons() | theEvent->modifiers(), theEvent->pos());
}
else if (theEvent->button() == Qt::RightButton)
{
onRButtonUp(theEvent->buttons() | theEvent->modifiers(), theEvent->pos());
}
}
void OccView::mouseMoveEvent( QMouseEvent * theEvent )
{
onMouseMove(theEvent->buttons(), theEvent->pos());
}
void OccView::wheelEvent( QWheelEvent * theEvent )
{
onMouseWheel(theEvent->buttons(), theEvent->delta(), theEvent->pos());
}
void OccView::onLButtonDown( const int /*theFlags*/, const QPoint thePoint )
{
// Save the current mouse coordinate in min.
myXmin = thePoint.x();
myYmin = thePoint.y();
myXmax = thePoint.x();
myYmax = thePoint.y();
}
void OccView::onMButtonDown( const int /*theFlags*/, const QPoint thePoint )
{
// Save the current mouse coordinate in min.
myXmin = thePoint.x();
myYmin = thePoint.y();
myXmax = thePoint.x();
myYmax = thePoint.y();
if (myCurrentMode == CurAction3d_DynamicRotation)
{
myView->StartRotation(thePoint.x(), thePoint.y());
}
}
void OccView::onRButtonDown( const int /*theFlags*/, const QPoint /*thePoint*/ )
{
}
void OccView::onMouseWheel( const int /*theFlags*/, const int theDelta, const QPoint thePoint )
{
Standard_Integer aFactor = 16;
Standard_Integer aX = thePoint.x();
Standard_Integer aY = thePoint.y();
if (theDelta > 0)
{
aX += aFactor;
aY += aFactor;
}
else
{
aX -= aFactor;
aY -= aFactor;
}
myView->Zoom(thePoint.x(), thePoint.y(), aX, aY);
}
void OccView::addItemInPopup( QMenu* /*theMenu*/ )
{
}
void OccView::popup( const int /*x*/, const int /*y*/ )
{
}
void OccView::onLButtonUp( const int theFlags, const QPoint thePoint )
{
// Hide the QRubberBand
if (myRectBand)
{
myRectBand->hide();
}
// Ctrl for multi selection.
if (thePoint.x() == myXmin && thePoint.y() == myYmin)
{
if (theFlags & Qt::ControlModifier)
{
multiInputEvent(thePoint.x(), thePoint.y());
}
else
{
inputEvent(thePoint.x(), thePoint.y());
}
}
}
void OccView::onMButtonUp( const int /*theFlags*/, const QPoint thePoint )
{
if (thePoint.x() == myXmin && thePoint.y() == myYmin)
{
panByMiddleButton(thePoint);
}
}
void OccView::onRButtonUp( const int /*theFlags*/, const QPoint thePoint )
{
popup(thePoint.x(), thePoint.y());
}
void OccView::onMouseMove( const int theFlags, const QPoint thePoint )
{
// Draw the rubber band.
if (theFlags & Qt::LeftButton)
{
drawRubberBand(myXmin, myYmin, thePoint.x(), thePoint.y());
dragEvent(thePoint.x(), thePoint.y());
}
// Ctrl for multi selection.
if (theFlags & Qt::ControlModifier)
{
multiMoveEvent(thePoint.x(), thePoint.y());
}
else
{
moveEvent(thePoint.x(), thePoint.y());
}
// Middle button.
if (theFlags & Qt::MidButton)
{
switch (myCurrentMode)
{
case CurAction3d_DynamicRotation:
myView->Rotation(thePoint.x(), thePoint.y());
break;
case CurAction3d_DynamicZooming:
myView->Zoom(myXmin, myYmin, thePoint.x(), thePoint.y());
break;
case CurAction3d_DynamicPanning:
myView->Pan(thePoint.x() - myXmax, myYmax - thePoint.y());
myXmax = thePoint.x();
myYmax = thePoint.y();
break;
default:
break;
}
}
}
void OccView::dragEvent( const int x, const int y )
{
myContext->Select(myXmin, myYmin, x, y, myView, Standard_True);
emit selectionChanged();
}
void OccView::multiDragEvent( const int x, const int y )
{
myContext->ShiftSelect(myXmin, myYmin, x, y, myView, Standard_True);
emit selectionChanged();
}
void OccView::inputEvent( const int x, const int y )
{
Q_UNUSED(x);
Q_UNUSED(y);
myContext->Select(Standard_True);
emit selectionChanged();
}
void OccView::multiInputEvent( const int x, const int y )
{
Q_UNUSED(x);
Q_UNUSED(y);
myContext->ShiftSelect(Standard_True);
emit selectionChanged();
}
void OccView::moveEvent( const int x, const int y )
{
myContext->MoveTo(x, y, myView, Standard_True);
}
void OccView::multiMoveEvent( const int x, const int y )
{
myContext->MoveTo(x, y, myView, Standard_True);
}
void OccView::drawRubberBand( const int minX, const int minY, const int maxX, const int maxY )
{
QRect aRect;
// Set the rectangle correctly.
(minX < maxX) ? (aRect.setX(minX)) : (aRect.setX(maxX));
(minY < maxY) ? (aRect.setY(minY)) : (aRect.setY(maxY));
aRect.setWidth(abs(maxX - minX));
aRect.setHeight(abs(maxY - minY));
if (!myRectBand)
{
myRectBand = new QRubberBand(QRubberBand::Rectangle, this);
// setStyle is important, set to windows style will just draw
// rectangle frame, otherwise will draw a solid rectangle.
myRectBand->setStyle(QStyleFactory::create("windows"));
}
myRectBand->setGeometry(aRect);
myRectBand->show();
}
void OccView::panByMiddleButton( const QPoint& thePoint )
{
Standard_Integer aCenterX = 0;
Standard_Integer aCenterY = 0;
QSize aSize = size();
aCenterX = aSize.width() / 2;
aCenterY = aSize.height() / 2;
myView->Pan(aCenterX - thePoint.x(), thePoint.y() - aCenterY);
}