/
View3DInventorViewer.cpp
2711 lines (2200 loc) · 81.6 KB
/
View3DInventorViewer.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2004 J?rgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <float.h>
# ifdef FC_OS_WIN32
# include <windows.h>
# endif
# ifdef FC_OS_MACOSX
# include <OpenGL/gl.h>
# else
# include <GL/gl.h>
# endif
# include <Inventor/SbBox.h>
# include <Inventor/actions/SoGetBoundingBoxAction.h>
# include <Inventor/actions/SoGetMatrixAction.h>
# include <Inventor/actions/SoHandleEventAction.h>
# include <Inventor/actions/SoToVRML2Action.h>
# include <Inventor/actions/SoWriteAction.h>
# include <Inventor/manips/SoClipPlaneManip.h>
# include <Inventor/nodes/SoBaseColor.h>
# include <Inventor/nodes/SoCallback.h>
# include <Inventor/nodes/SoCoordinate3.h>
# include <Inventor/nodes/SoCube.h>
# include <Inventor/nodes/SoDirectionalLight.h>
# include <Inventor/nodes/SoEventCallback.h>
# include <Inventor/nodes/SoFaceSet.h>
# include <Inventor/nodes/SoImage.h>
# include <Inventor/nodes/SoIndexedFaceSet.h>
# include <Inventor/nodes/SoLightModel.h>
# include <Inventor/nodes/SoLocateHighlight.h>
# include <Inventor/nodes/SoMaterial.h>
# include <Inventor/nodes/SoMaterialBinding.h>
# include <Inventor/nodes/SoOrthographicCamera.h>
# include <Inventor/nodes/SoPerspectiveCamera.h>
# include <Inventor/nodes/SoRotationXYZ.h>
# include <Inventor/nodes/SoSeparator.h>
# include <Inventor/nodes/SoShapeHints.h>
# include <Inventor/nodes/SoSwitch.h>
# include <Inventor/nodes/SoTransform.h>
# include <Inventor/nodes/SoTranslation.h>
# include <Inventor/nodes/SoSelection.h>
# include <Inventor/actions/SoBoxHighlightRenderAction.h>
# include <Inventor/events/SoEvent.h>
# include <Inventor/events/SoKeyboardEvent.h>
# include <Inventor/events/SoLocation2Event.h>
# include <Inventor/events/SoMotion3Event.h>
# include <Inventor/events/SoMouseButtonEvent.h>
# include <Inventor/actions/SoRayPickAction.h>
# include <Inventor/projectors/SbSphereSheetProjector.h>
# include <Inventor/SoOffscreenRenderer.h>
# include <Inventor/SoPickedPoint.h>
# include <Inventor/VRMLnodes/SoVRMLGroup.h>
# include <QEventLoop>
# include <QGLFramebufferObject>
# include <QKeyEvent>
# include <QWheelEvent>
# include <QMessageBox>
# include <QTimer>
# include <QStatusBar>
#include <QBitmap>
#endif
#include <sstream>
#include <Base/Console.h>
#include <Base/Stream.h>
#include <Base/FileInfo.h>
#include <Base/Sequencer.h>
#include <Base/Tools.h>
#include <zipios++/gzipoutputstream.h>
#include "View3DInventorViewer.h"
#include "ViewProviderDocumentObject.h"
#include "SoFCBackgroundGradient.h"
#include "SoFCColorBar.h"
#include "SoFCColorLegend.h"
#include "SoFCColorGradient.h"
#include "SoFCOffscreenRenderer.h"
#include "SoFCSelection.h"
#include "SoFCUnifiedSelection.h"
#include "SoFCInteractiveElement.h"
#include "SoFCBoundingBox.h"
#include "SoAxisCrossKit.h"
#include "View3DInventorRiftViewer.h"
#include "Selection.h"
#include "SoFCSelectionAction.h"
#include "SoFCVectorizeU3DAction.h"
#include "SoFCVectorizeSVGAction.h"
#include "SoFCDB.h"
#include "Application.h"
#include "MainWindow.h"
#include "NavigationStyle.h"
#include "ViewProvider.h"
#include "SpaceballEvent.h"
#include "GLPainter.h"
#include <Quarter/eventhandlers/EventFilter.h>
#include <Quarter/devices/InputDevice.h>
#include "View3DViewerPy.h"
#include <Inventor/draggers/SoCenterballDragger.h>
//#define FC_LOGGING_CB
#define new DEBUG_CLIENTBLOCK
using namespace Gui;
/*** zoom-style cursor ******/
#define ZOOM_WIDTH 16
#define ZOOM_HEIGHT 16
#define ZOOM_BYTES ((ZOOM_WIDTH + 7) / 8) * ZOOM_HEIGHT
#define ZOOM_HOT_X 5
#define ZOOM_HOT_Y 7
static unsigned char zoom_bitmap[ZOOM_BYTES] =
{
0x00, 0x0f, 0x80, 0x1c, 0x40, 0x38, 0x20, 0x70,
0x90, 0xe4, 0xc0, 0xcc, 0xf0, 0xfc, 0x00, 0x0c,
0x00, 0x0c, 0xf0, 0xfc, 0xc0, 0xcc, 0x90, 0xe4,
0x20, 0x70, 0x40, 0x38, 0x80, 0x1c, 0x00, 0x0f
};
static unsigned char zoom_mask_bitmap[ZOOM_BYTES] =
{
0x00,0x0f,0x80,0x1f,0xc0,0x3f,0xe0,0x7f,0xf0,0xff,0xf0,0xff,0xf0,0xff,0x00,
0x0f,0x00,0x0f,0xf0,0xff,0xf0,0xff,0xf0,0xff,0xe0,0x7f,0xc0,0x3f,0x80,0x1f,
0x00,0x0f
};
/*** pan-style cursor *******/
#define PAN_WIDTH 16
#define PAN_HEIGHT 16
#define PAN_BYTES ((PAN_WIDTH + 7) / 8) * PAN_HEIGHT
#define PAN_HOT_X 7
#define PAN_HOT_Y 7
static unsigned char pan_bitmap[PAN_BYTES] =
{
0xc0, 0x03, 0x60, 0x02, 0x20, 0x04, 0x10, 0x08,
0x68, 0x16, 0x54, 0x2a, 0x73, 0xce, 0x01, 0x80,
0x01, 0x80, 0x73, 0xce, 0x54, 0x2a, 0x68, 0x16,
0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0xc0, 0x03
};
static unsigned char pan_mask_bitmap[PAN_BYTES] =
{
0xc0,0x03,0xe0,0x03,0xe0,0x07,0xf0,0x0f,0xe8,0x17,0xdc,0x3b,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xdc,0x3b,0xe8,0x17,0xf0,0x0f,0xe0,0x07,0xc0,0x03,
0xc0,0x03
};
/*** rotate-style cursor ****/
#define ROTATE_WIDTH 16
#define ROTATE_HEIGHT 16
#define ROTATE_BYTES ((ROTATE_WIDTH + 7) / 8) * ROTATE_HEIGHT
#define ROTATE_HOT_X 6
#define ROTATE_HOT_Y 8
static unsigned char rotate_bitmap[ROTATE_BYTES] = {
0xf0, 0xef, 0x18, 0xb8, 0x0c, 0x90, 0xe4, 0x83,
0x34, 0x86, 0x1c, 0x83, 0x00, 0x81, 0x00, 0xff,
0xff, 0x00, 0x81, 0x00, 0xc1, 0x38, 0x61, 0x2c,
0xc1, 0x27, 0x09, 0x30, 0x1d, 0x18, 0xf7, 0x0f
};
static unsigned char rotate_mask_bitmap[ROTATE_BYTES] = {
0xf0,0xef,0xf8,0xff,0xfc,0xff,0xfc,0xff,0x3c,0xfe,0x1c,0xff,0x00,0xff,0x00,
0xff,0xff,0x00,0xff,0x00,0xff,0x38,0x7f,0x3c,0xff,0x3f,0xff,0x3f,0xff,0x1f,
0xf7,0x0f
};
/*!
As ProgressBar has no chance to control the incoming Qt events of Quarter so we need to stop
the event handling to prevent the scenegraph from being selected or deselected
while the progress bar is running.
*/
class Gui::ViewerEventFilter : public QObject
{
public:
ViewerEventFilter() {};
~ViewerEventFilter() {};
bool eventFilter(QObject* obj, QEvent* event) {
// Bug #0000607: Some mices also support horizontal scrolling which however might
// lead to some unwanted zooming when pressing the MMB for panning.
// Thus, we filter out horizontal scrolling.
if(event->type() == QEvent::Wheel) {
QWheelEvent* we = static_cast<QWheelEvent*>(event);
if(we->orientation() == Qt::Horizontal)
return true;
}
if(event->type() == QEvent::KeyPress) {
QKeyEvent* ke = static_cast<QKeyEvent*>(event);
if(ke->matches(QKeySequence::SelectAll)) {
static_cast<View3DInventorViewer*>(obj)->selectAll();
return true;
}
}
if(Base::Sequencer().isRunning() &&
Base::Sequencer().isBlocking())
return false;
if(event->type() == Spaceball::ButtonEvent::ButtonEventType) {
Spaceball::ButtonEvent* buttonEvent = static_cast<Spaceball::ButtonEvent*>(event);
if(!buttonEvent) {
Base::Console().Log("invalid spaceball button event\n");
return true;
}
}
if(event->type() == Spaceball::MotionEvent::MotionEventType) {
Spaceball::MotionEvent* motionEvent = static_cast<Spaceball::MotionEvent*>(event);
if(!motionEvent) {
Base::Console().Log("invalid spaceball motion event\n");
return true;
}
}
return false;
}
};
class SpaceNavigatorDevice : public Quarter::InputDevice {
public:
SpaceNavigatorDevice(void) {};
virtual ~SpaceNavigatorDevice() {};
virtual const SoEvent* translateEvent(QEvent* event) {
if(event->type() == Spaceball::MotionEvent::MotionEventType) {
Spaceball::MotionEvent* motionEvent = static_cast<Spaceball::MotionEvent*>(event);
if(!motionEvent) {
Base::Console().Log("invalid spaceball motion event\n");
return NULL;
}
motionEvent->setHandled(true);
float xTrans, yTrans, zTrans;
xTrans = static_cast<float>(motionEvent->translationX());
yTrans = static_cast<float>(motionEvent->translationY());
zTrans = static_cast<float>(motionEvent->translationZ());
SbVec3f translationVector(xTrans, yTrans, zTrans);
static float rotationConstant(.0001f);
SbRotation xRot, yRot, zRot;
xRot.setValue(SbVec3f(1.0, 0.0, 0.0), static_cast<float>(motionEvent->rotationX()) * rotationConstant);
yRot.setValue(SbVec3f(0.0, 1.0, 0.0), static_cast<float>(motionEvent->rotationY()) * rotationConstant);
zRot.setValue(SbVec3f(0.0, 0.0, 1.0), static_cast<float>(motionEvent->rotationZ()) * rotationConstant);
SoMotion3Event* motion3Event = new SoMotion3Event;
motion3Event->setTranslation(translationVector);
motion3Event->setRotation(xRot * yRot * zRot);
return motion3Event;
}
return NULL;
};
};
/** \defgroup View3D 3D Viewer
* \ingroup GUI
*
* The 3D Viewer is one of the major components in a CAD/CAE systems.
* Therefore an overview and some remarks to the FreeCAD 3D viewing system.
*
* \section overview Overview
* \todo Overview and complements for the 3D Viewer
*/
// *************************************************************************
View3DInventorViewer::View3DInventorViewer(QWidget* parent)
: Quarter::SoQTQuarterAdaptor(parent), editViewProvider(0), navigation(0),
framebuffer(0), axisCross(0), axisGroup(0), editing(FALSE), redirected(FALSE),
allowredir(FALSE), overrideMode("As Is")
{
init();
};
View3DInventorViewer::View3DInventorViewer(const QGLFormat& format, QWidget* parent)
: Quarter::SoQTQuarterAdaptor(format, parent), editViewProvider(0), navigation(0),
framebuffer(0), axisCross(0), axisGroup(0), editing(FALSE), redirected(FALSE),
allowredir(FALSE), overrideMode("As Is")
{
init();
};
void View3DInventorViewer::init()
{
Gui::Selection().Attach(this);
// Coin should not clear the pixel-buffer, so the background image
// is not removed.
this->setClearWindow(FALSE);
// setting up the defaults for the spin rotation
initialize();
SoOrthographicCamera* cam = new SoOrthographicCamera;
cam->position = SbVec3f(0, 0, 1);
cam->height = 1;
cam->nearDistance = 0.5;
cam->farDistance = 1.5;
// setup light sources
SoDirectionalLight* hl = this->getHeadlight();
backlight = new SoDirectionalLight();
backlight->ref();
backlight->setName("backlight");
backlight->direction.setValue(-hl->direction.getValue());
backlight->on.setValue(FALSE); // by default off
// Set up background scenegraph with image in it.
backgroundroot = new SoSeparator;
backgroundroot->ref();
this->backgroundroot->addChild(cam);
// Background stuff
pcBackGround = new SoFCBackgroundGradient;
pcBackGround->ref();
// Set up foreground, overlayed scenegraph.
this->foregroundroot = new SoSeparator;
this->foregroundroot->ref();
SoLightModel* lm = new SoLightModel;
lm->model = SoLightModel::BASE_COLOR;
SoBaseColor* bc = new SoBaseColor;
bc->rgb = SbColor(1, 1, 0);
cam = new SoOrthographicCamera;
cam->position = SbVec3f(0, 0, 5);
cam->height = 10;
cam->nearDistance = 0;
cam->farDistance = 10;
// dragger
//SoSeparator * dragSep = new SoSeparator();
//SoScale *scale = new SoScale();
//scale->scaleFactor = SbVec3f (0.2,0.2,0.2);
//dragSep->addChild(scale);
//SoCenterballDragger *dragger = new SoCenterballDragger();
//dragger->center = SbVec3f (0.8,0.8,0);
////dragger->rotation = SbRotation(rrot[0],rrot[1],rrot[2],rrot[3]);
//dragSep->addChild(dragger);
this->foregroundroot->addChild(cam);
this->foregroundroot->addChild(lm);
this->foregroundroot->addChild(bc);
//this->foregroundroot->addChild(dragSep);
#if 0
// NOTE: For every mouse click event the SoSelection searches for the picked
// point which causes a certain slow-down because for all objects the primitives
// must be created. Using an SoSeparator avoids this drawback.
SoSelection* selectionRoot = new SoSelection();
selectionRoot->addSelectionCallback(View3DInventorViewer::selectCB, this);
selectionRoot->addDeselectionCallback(View3DInventorViewer::deselectCB, this);
selectionRoot->setPickFilterCallback(View3DInventorViewer::pickFilterCB, this);
#else
// NOTE: For every mouse click event the SoFCUnifiedSelection searches for the picked
// point which causes a certain slow-down because for all objects the primitives
// must be created. Using an SoSeparator avoids this drawback.
selectionRoot = new Gui::SoFCUnifiedSelection();
selectionRoot->applySettings();
#endif
// set the ViewProvider root node
pcViewProviderRoot = selectionRoot;
// increase refcount before passing it to setScenegraph(), to avoid
// premature destruction
pcViewProviderRoot->ref();
// is not really working with Coin3D.
//redrawOverlayOnSelectionChange(pcSelection);
setSceneGraph(pcViewProviderRoot);
// Event callback node
pEventCallback = new SoEventCallback();
pEventCallback->setUserData(this);
pEventCallback->ref();
pcViewProviderRoot->addChild(pEventCallback);
pEventCallback->addEventCallback(SoEvent::getClassTypeId(), handleEventCB, this);
dimensionRoot = new SoSwitch(SO_SWITCH_NONE);
pcViewProviderRoot->addChild(dimensionRoot);
dimensionRoot->addChild(new SoSwitch()); //first one will be for the 3d dimensions.
dimensionRoot->addChild(new SoSwitch()); //second one for the delta dimensions.
// This is a callback node that logs all action that traverse the Inventor tree.
#if defined (FC_DEBUG) && defined(FC_LOGGING_CB)
SoCallback* cb = new SoCallback;
cb->setCallback(interactionLoggerCB, this);
pcViewProviderRoot->addChild(cb);
#endif
// Set our own render action which show a bounding box if
// the SoFCSelection::BOX style is set
this->getSoRenderManager()->setGLRenderAction(new SoBoxSelectionRenderAction);
// set the transperency and antialiasing settings
// getGLRenderAction()->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
getSoRenderManager()->getGLRenderAction()->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_SORTED_TRIANGLE_BLEND);
// getGLRenderAction()->setSmoothing(true);
// Settings
setSeekTime(0.4f);
if(isSeekValuePercentage() == false)
setSeekValueAsPercentage(true);
setSeekDistance(100);
setViewing(false);
setBackgroundColor(QColor(25, 25, 25));
setGradientBackground(true);
// set some callback functions for user interaction
addStartCallback(interactionStartCB);
addFinishCallback(interactionFinishCB);
//filter a few qt events
viewerEventFilter = new ViewerEventFilter;
installEventFilter(viewerEventFilter);
getEventFilter()->registerInputDevice(new SpaceNavigatorDevice);
//create the cursors
QBitmap cursor = QBitmap::fromData(QSize(ROTATE_WIDTH, ROTATE_HEIGHT), rotate_bitmap);
QBitmap mask = QBitmap::fromData(QSize(ROTATE_WIDTH, ROTATE_HEIGHT), rotate_mask_bitmap);
spinCursor = QCursor(cursor, mask, ROTATE_HOT_X, ROTATE_HOT_Y);
cursor = QBitmap::fromData(QSize(ZOOM_WIDTH, ZOOM_HEIGHT), zoom_bitmap);
mask = QBitmap::fromData(QSize(ZOOM_WIDTH, ZOOM_HEIGHT), zoom_mask_bitmap);
zoomCursor = QCursor(cursor, mask, ZOOM_HOT_X, ZOOM_HOT_Y);
cursor = QBitmap::fromData(QSize(PAN_WIDTH, PAN_HEIGHT), pan_bitmap);
mask = QBitmap::fromData(QSize(PAN_WIDTH, PAN_HEIGHT), pan_mask_bitmap);
panCursor = QCursor(cursor, mask, PAN_HOT_X, PAN_HOT_Y);
}
View3DInventorViewer::~View3DInventorViewer()
{
// cleanup
this->backgroundroot->unref();
this->backgroundroot = 0;
this->foregroundroot->unref();
this->foregroundroot = 0;
this->pcBackGround->unref();
this->pcBackGround = 0;
setSceneGraph(0);
this->pEventCallback->unref();
this->pEventCallback = 0;
this->pcViewProviderRoot->unref();
this->pcViewProviderRoot = 0;
this->backlight->unref();
this->backlight = 0;
delete this->navigation;
// Note: When closing the application the main window doesn't exist any more.
if(getMainWindow())
getMainWindow()->setPaneText(2, QLatin1String(""));
Gui::Selection().Detach(this);
removeEventFilter(viewerEventFilter);
delete viewerEventFilter;
}
void View3DInventorViewer::setDocument(Gui::Document* pcDocument)
{
// write the document the viewer belongs to to the selection node
selectionRoot->pcDocument = pcDocument;
}
void View3DInventorViewer::initialize()
{
navigation = new CADNavigationStyle();
navigation->setViewer(this);
this->axiscrossEnabled = TRUE;
this->axiscrossSize = 10;
}
/// @cond DOXERR
void View3DInventorViewer::OnChange(Gui::SelectionSingleton::SubjectType& rCaller,
Gui::SelectionSingleton::MessageType Reason)
{
if(Reason.Type == SelectionChanges::AddSelection ||
Reason.Type == SelectionChanges::RmvSelection ||
Reason.Type == SelectionChanges::SetSelection ||
Reason.Type == SelectionChanges::ClrSelection) {
SoFCSelectionAction cAct(Reason);
cAct.apply(pcViewProviderRoot);
}
}
/// @endcond
SbBool View3DInventorViewer::hasViewProvider(ViewProvider* pcProvider) const
{
return _ViewProviderSet.find(pcProvider) != _ViewProviderSet.end();
}
/// adds an ViewProvider to the view, e.g. from a feature
void View3DInventorViewer::addViewProvider(ViewProvider* pcProvider)
{
SoSeparator* root = pcProvider->getRoot();
if(root) {
pcViewProviderRoot->addChild(root);
_ViewProviderMap[root] = pcProvider;
}
SoSeparator* fore = pcProvider->getFrontRoot();
if(fore) foregroundroot->addChild(fore);
SoSeparator* back = pcProvider->getBackRoot();
if(back) backgroundroot->addChild(back);
pcProvider->setOverrideMode(this->getOverrideMode());
_ViewProviderSet.insert(pcProvider);
}
void View3DInventorViewer::removeViewProvider(ViewProvider* pcProvider)
{
if(this->editViewProvider == pcProvider)
resetEditingViewProvider();
SoSeparator* root = pcProvider->getRoot();
if(root) {
pcViewProviderRoot->removeChild(root);
_ViewProviderMap.erase(root);
}
SoSeparator* fore = pcProvider->getFrontRoot();
if(fore) foregroundroot->removeChild(fore);
SoSeparator* back = pcProvider->getBackRoot();
if(back) backgroundroot->removeChild(back);
_ViewProviderSet.erase(pcProvider);
}
SbBool View3DInventorViewer::setEditingViewProvider(Gui::ViewProvider* p, int ModNum)
{
if(this->editViewProvider)
return false; // only one view provider is editable at a time
bool ok = p->startEditing(ModNum);
if(ok) {
this->editViewProvider = p;
this->editViewProvider->setEditViewer(this, ModNum);
addEventCallback(SoEvent::getClassTypeId(), Gui::ViewProvider::eventCallback,this->editViewProvider);
}
return ok;
}
/// reset from edit mode
void View3DInventorViewer::resetEditingViewProvider()
{
if(this->editViewProvider) {
this->editViewProvider->unsetEditViewer(this);
this->editViewProvider->finishEditing();
removeEventCallback(SoEvent::getClassTypeId(), Gui::ViewProvider::eventCallback,this->editViewProvider);
this->editViewProvider = 0;
}
}
/// reset from edit mode
SbBool View3DInventorViewer::isEditingViewProvider() const
{
return this->editViewProvider ? true : false;
}
/// display override mode
void View3DInventorViewer::setOverrideMode(const std::string& mode)
{
if(mode == overrideMode)
return;
overrideMode = mode;
for(std::set<ViewProvider*>::iterator it = _ViewProviderSet.begin(); it != _ViewProviderSet.end(); ++it)
(*it)->setOverrideMode(mode);
}
/// update override mode. doesn't affect providers
void View3DInventorViewer::updateOverrideMode(const std::string& mode)
{
if(mode == overrideMode)
return;
overrideMode = mode;
}
void View3DInventorViewer::clearBuffer(void* userdata, SoAction* action)
{
if(action->isOfType(SoGLRenderAction::getClassTypeId())) {
// do stuff specific for GL rendering here.
glClear(GL_DEPTH_BUFFER_BIT);
}
}
void View3DInventorViewer::setGLWidget(void* userdata, SoAction* action)
{
//FIXME: This causes the Coin error message:
// Coin error in SoNode::GLRenderS(): GL error: 'GL_STACK_UNDERFLOW', nodetype:
// Separator (set envvar COIN_GLERROR_DEBUGGING=1 and re-run to get more information)
if(action->isOfType(SoGLRenderAction::getClassTypeId())) {
QWidget* gl = reinterpret_cast<QWidget*>(userdata);
SoGLWidgetElement::set(action->getState(), qobject_cast<QGLWidget*>(gl));
}
}
void View3DInventorViewer::handleEventCB(void* ud, SoEventCallback* n)
{
View3DInventorViewer* that = reinterpret_cast<View3DInventorViewer*>(ud);
SoGLRenderAction* glra = that->getSoRenderManager()->getGLRenderAction();
SoAction* action = n->getAction();
SoGLRenderActionElement::set(action->getState(), glra);
SoGLWidgetElement::set(action->getState(), qobject_cast<QGLWidget*>(that->getGLWidget()));
}
void View3DInventorViewer::setGradientBackground(bool on)
{
if(on && backgroundroot->findChild(pcBackGround) == -1)
backgroundroot->addChild(pcBackGround);
else if(!on && backgroundroot->findChild(pcBackGround) != -1)
backgroundroot->removeChild(pcBackGround);
}
bool View3DInventorViewer::hasGradientBackground() const
{
return (backgroundroot->findChild(pcBackGround) != -1);
}
void View3DInventorViewer::setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor)
{
pcBackGround->setColorGradient(fromColor, toColor);
}
void View3DInventorViewer::setGradientBackgroundColor(const SbColor& fromColor,
const SbColor& toColor,
const SbColor& midColor)
{
pcBackGround->setColorGradient(fromColor, toColor, midColor);
}
void View3DInventorViewer::setEnabledFPSCounter(bool on)
{
#if defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
setenv("COIN_SHOW_FPS_COUNTER", (on?"1":"0"), 1);
#else
on ? _putenv("COIN_SHOW_FPS_COUNTER=1") : _putenv("COIN_SHOW_FPS_COUNTER=0");
#endif
}
void View3DInventorViewer::setAxisCross(bool on)
{
SoNode* scene = getSceneGraph();
SoSeparator* sep = static_cast<SoSeparator*>(scene);
if(on) {
if(!axisGroup) {
axisCross = new Gui::SoShapeScale;
Gui::SoAxisCrossKit* axisKit = new Gui::SoAxisCrossKit();
axisKit->set("xAxis.appearance.drawStyle", "lineWidth 2");
axisKit->set("yAxis.appearance.drawStyle", "lineWidth 2");
axisKit->set("zAxis.appearance.drawStyle", "lineWidth 2");
axisCross->setPart("shape", axisKit);
axisCross->scaleFactor = 1.0f;
axisGroup = new SoSkipBoundingGroup;
axisGroup->addChild(axisCross);
sep->addChild(axisGroup);
}
}
else {
if(axisGroup) {
sep->removeChild(axisGroup);
axisGroup = 0;
}
}
}
bool View3DInventorViewer::hasAxisCross(void)
{
return axisGroup;
}
void View3DInventorViewer::setNavigationType(Base::Type t)
{
if(t.isBad())
return;
if(this->navigation && this->navigation->getTypeId() == t)
return; // nothing to do
Base::BaseClass* base = static_cast<Base::BaseClass*>(t.createInstance());
if(!base)
return;
if(!base->getTypeId().isDerivedFrom(NavigationStyle::getClassTypeId())) {
delete base;
return;
}
NavigationStyle* ns = static_cast<NavigationStyle*>(base);
ns->operator = (*this->navigation);
delete this->navigation;
this->navigation = ns;
this->navigation->setViewer(this);
}
NavigationStyle* View3DInventorViewer::navigationStyle() const
{
return this->navigation;
}
SoDirectionalLight* View3DInventorViewer::getBacklight(void) const
{
return this->backlight;
}
void View3DInventorViewer::setBacklight(SbBool on)
{
this->backlight->on = on;
}
SbBool View3DInventorViewer::isBacklight(void) const
{
return this->backlight->on.getValue();
}
void View3DInventorViewer::setSceneGraph(SoNode* root)
{
inherited::setSceneGraph(root);
SoSearchAction sa;
sa.setNode(this->backlight);
//we want the rendered scene with all lights and cameras, viewer->getSceneGraph would return
//the geometry scene only
SoNode* scene = this->getSoRenderManager()->getSceneGraph();
if(scene && scene->getTypeId().isDerivedFrom(SoSeparator::getClassTypeId())) {
sa.apply(scene);
if(!sa.getPath())
static_cast<SoSeparator*>(scene)->insertChild(this->backlight, 0);
}
}
void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage& img) const
{
// if no valid color use the current background
bool useBackground = false;
SbViewportRegion vp(getSoRenderManager()->getViewportRegion());
if(w>0 && h>0)
vp.setWindowSize((short)w, (short)h);
//NOTE: To support pixels per inch we must use SbViewportRegion::setPixelsPerInch( ppi );
//The default value is 72.0.
//If we need to support grayscale images with must either use SoOffscreenRenderer::LUMINANCE or
//SoOffscreenRenderer::LUMINANCE_TRANSPARENCY.
SoFCOffscreenRenderer& renderer = SoFCOffscreenRenderer::instance();
renderer.setViewportRegion(vp);
SoCallback* cb = 0;
// if we use transparency then we must not set a background color
switch(eBackgroundType) {
case Current:
if(backgroundroot->findChild(pcBackGround) == -1) {
const QColor col = this->backgroundColor();
renderer.setBackgroundColor(SbColor(col.redF(), col.greenF(), col.blueF()));
}
else {
useBackground = true;
cb = new SoCallback;
cb->setCallback(clearBuffer);
}
break;
case White:
renderer.setBackgroundColor(SbColor(1.0, 1.0, 1.0));
break;
case Black:
renderer.setBackgroundColor(SbColor(0.0, 0.0, 0.0));
break;
case Transparent:
renderer.setComponents(SoFCOffscreenRenderer::RGB_TRANSPARENCY);
break;
default:
break;
}
SoSeparator* root = new SoSeparator;
root->ref();
SoCamera* camera = getSoRenderManager()->getCamera();
if(useBackground) {
root->addChild(backgroundroot);
root->addChild(cb);
}
root->addChild(getHeadlight());
root->addChild(camera);
SoCallback* gl = new SoCallback;
gl->setCallback(setGLWidget, this->getGLWidget());
root->addChild(gl);
root->addChild(pcViewProviderRoot);
if(useBackground)
root->addChild(cb);
root->addChild(foregroundroot);
try {
// render the scene
if(!renderer.render(root))
throw Base::Exception("Offscreen rendering failed");
renderer.writeToImage(img);
root->unref();
}
catch(...) {
root->unref();
throw; // re-throw exception
}
}
void View3DInventorViewer::saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const
{
const QColor col = this->backgroundColor();
switch(eBackgroundType) {
case Current:
va->setBackgroundColor(true, SbColor(col.redF(), col.greenF(), col.blueF()));
break;
case White:
va->setBackgroundColor(true, SbColor(1.0, 1.0, 1.0));
break;
case Black:
va->setBackgroundColor(true, SbColor(0.0, 0.0, 0.0));
break;
case Transparent:
break; // not supported
default:
break;
}
float border = 10.0f;
SbVec2s vpsize = this->getSoRenderManager()->getViewportRegion().getViewportSizePixels();
float vpratio = ((float)vpsize[0]) / ((float)vpsize[1]);
if(vpratio > 1.0f) {
va->setOrientation(SoVectorizeAction::LANDSCAPE);
vpratio = 1.0f / vpratio;
}
else {
va->setOrientation(SoVectorizeAction::PORTRAIT);
}
va->beginStandardPage(SoVectorizeAction::PageSize(pagesize), border);
// try to fill as much "paper" as possible
SbVec2f size = va->getPageSize();
float pageratio = size[0] / size[1];
float xsize, ysize;
if(pageratio < vpratio) {
xsize = size[0];
ysize = xsize / vpratio;
}
else {
ysize = size[1];
xsize = ysize * vpratio;
}
float offx = border + (size[0]-xsize) * 0.5f;
float offy = border + (size[1]-ysize) * 0.5f;
va->beginViewport(SbVec2f(offx, offy), SbVec2f(xsize, ysize));
va->calibrate(this->getSoRenderManager()->getViewportRegion());
va->apply(this->getSoRenderManager()->getSceneGraph());
va->endViewport();
va->endPage();
}
void View3DInventorViewer::startSelection(View3DInventorViewer::SelectionMode mode)
{
navigation->startSelection(NavigationStyle::SelectionMode(mode));
}
void View3DInventorViewer::stopSelection()
{
navigation->stopSelection();
}
bool View3DInventorViewer::isSelecting() const
{
return navigation->isSelecting();
}
const std::vector<SbVec2s>& View3DInventorViewer::getPolygon(SbBool* clip_inner) const
{
return navigation->getPolygon(clip_inner);
}
SbVec2f View3DInventorViewer::screenCoordsOfPath(SoPath* path) const
{
// Generate a matrix (well, a SoGetMatrixAction) that
// moves us to the picked object's coordinate space.
SoGetMatrixAction gma(getSoRenderManager()->getViewportRegion());
gma.apply(path);
// Use that matrix to translate the origin in the picked
// object's coordinate space into object space
SbVec3f imageCoords(0, 0, 0);
SbMatrix m = gma.getMatrix().transpose();
m.multMatrixVec(imageCoords, imageCoords);
// Now, project the object space coordinates of the object
// into "normalized" screen coordinates.
SbViewVolume vol = getSoRenderManager()->getCamera()->getViewVolume();
vol.projectToScreen(imageCoords, imageCoords);
// Translate "normalized" screen coordinates to pixel coords.
//
// Note: for some reason, projectToScreen() doesn't seem to
// handle non-square viewports properly. The X and Y are
// scaled such that [0,1] fits within the smaller of the window
// width or height. For instance, in a window that's 400px
// tall and 800px wide, the Y will be within [0,1], but X can
// vary within [-0.5,1.5]...
int width = getGLWidget()->width(),
height = getGLWidget()->height();
if(width >= height) {
// "Landscape" orientation, to square
imageCoords[0] *= height;
imageCoords[0] += (width-height) / 2.0;
imageCoords[1] *= height;
}