Debian Codegraph @

src/Gui/View3DInventor.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <string>
25 #include <QApplication>
26 #include <QKeyEvent>
27 #include <QEvent>
28 #include <QDropEvent>
29 #include <QDragEnterEvent>
30 #include <QLayout>
31 #include <QMdiSubWindow>
32 #include <QMessageBox>
33 #include <QMimeData>
34 #include <QPainter>
35 #include <QPrinter>
36 #include <QPrintDialog>
37 #include <QPrintPreviewDialog>
38 #include <QStackedWidget>
39 #include <QSurfaceFormat>
40 #include <QTimer>
41 #include <QUrl>
42 #include <QWindow>
43 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
44 #include <Inventor/fields/SoSFString.h>
45 #include <Inventor/nodes/SoOrthographicCamera.h>
46 #include <Inventor/nodes/SoPerspectiveCamera.h>
47 #include <Inventor/nodes/SoSeparator.h>
48 #include <Inventor/SoPickedPoint.h>
49
50
51 #include <App/Application.h>
52 #include <App/Document.h>
53 #include <App/GeoFeature.h>
54 #include <Base/Builder3D.h>
55 #include <Base/Console.h>
56 #include <Base/Interpreter.h>
57
58 #include <Gui/PreferencePages/DlgSettingsPDF.h>
59
60 #include "View3DInventor.h"
61 #include "View3DSettings.h"
62 #include "Application.h"
63 #include "BitmapFactory.h"
64 #include "Camera.h"
65 #include "Document.h"
66 #include "FileDialog.h"
67 #include "MainWindow.h"
68 #include "NaviCube.h"
69 #include "Navigation/NavigationStyle.h"
70 #include "SoFCDB.h"
71 #include "SoFCSelectionAction.h"
72 #include "SoFCVectorizeSVGAction.h"
73 #include "View3DInventorViewer.h"
74 #include "View3DPy.h"
75 #include "ViewProvider.h"
76 #include "ViewProviderDocumentObject.h"
77 #include "WaitCursor.h"
78
79 #include "Utilities.h"
80
81 using namespace Gui;
82
83 void GLOverlayWidget::paintEvent(QPaintEvent*)
84 {
85 QPainter paint(this);
86 paint.drawImage(0, 0, image);
87 paint.end();
88 }
89
90 /* TRANSLATOR Gui::View3DInventor */
91
92 TYPESYSTEM_SOURCE_ABSTRACT(Gui::View3DInventor, Gui::MDIView)
93
94 View3DInventor::View3DInventor(
95 Gui::Document* pcDocument,
96 QWidget* parent,
97 const QOpenGLWidget* sharewidget,
98 Qt::WindowFlags wflags
99 )
100 : MDIView(pcDocument, parent, wflags)
101 , _viewerPy(nullptr)
102 {
103 stack = new QStackedWidget(this);
104 // important for highlighting
105 setMouseTracking(true);
106 // accept drops on the window, get handled in dropEvent, dragEnterEvent
107 setAcceptDrops(true);
108
109 // anti-aliasing settings
110 bool smoothing = false;
111 bool glformat = false;
112 int samples = View3DInventorViewer::getNumSamples();
113 QSurfaceFormat f;
114
115 if (samples > 1) {
116 glformat = true;
117 f.setSamples(samples);
118 }
119 else if (samples > 0) {
120 smoothing = true;
121 }
122
123 if (glformat) {
124 _viewer = new View3DInventorViewer(f, this, sharewidget);
125 }
126 else {
127 _viewer = new View3DInventorViewer(this, sharewidget);
128 }
129
130 if (smoothing) {
131 _viewer->getSoRenderManager()->getGLRenderAction()->setSmoothing(true);
132 }
133
134 // create the inventor widget and set the defaults
135 _viewer->setDocument(this->_pcDocument);
136 stack->addWidget(_viewer->getWidget());
137 // https://forum.freecad.org/viewtopic.php?f=3&t=6055&sid=150ed90cbefba50f1e2ad4b4e6684eba
138 // describes a minor error but trying to fix it leads to a major issue
139 // https://forum.freecad.org/viewtopic.php?f=3&t=6085&sid=3f4bcab8007b96aaf31928b564190fd7
140 // so the change is commented out
141 // By default, the wheel events are processed by the 3d view AND the mdi area.
142 //_viewer->getGLWidget()->setAttribute(Qt::WA_NoMousePropagation);
143 setCentralWidget(stack);
144
145 // apply the user settings
146 applySettings();
147
148 stopSpinTimer = new QTimer(this);
149 connect(stopSpinTimer, &QTimer::timeout, this, &View3DInventor::stopAnimating);
150
151 setWindowIcon(Gui::BitmapFactory().pixmap("Document"));
152 }
153
154 View3DInventor::~View3DInventor()
155 {
156 if (_pcDocument) {
157 SoCamera* Cam = _viewer->getSoRenderManager()->getCamera();
158 if (Cam) {
159 _pcDocument->saveCameraSettings(SoFCDB::writeNodesToString(Cam).c_str());
160 }
161 }
162
163 viewSettings.reset();
164
165 // If we destroy this viewer by calling 'delete' directly the focus proxy widget which is
166 // defined by a widget in SoQtViewer isn't reset. This widget becomes a dangling pointer and
167 // makes the application crash. (Probably it's better to destroy this viewer by calling
168 // close().) See also Gui::Document::~Document().
169 QWidget* foc = qApp->focusWidget();
170 if (foc) {
171 QWidget* par = foc->parentWidget();
172 while (par) {
173 if (par == this) {
174 foc->setFocusProxy(nullptr);
175 foc->clearFocus();
176 break;
177 }
178 par = par->parentWidget();
179 }
180 }
181
182 if (_viewerPy) {
183 Base::PyGILStateLocker lock;
184 Py_DECREF(_viewerPy);
185 }
186
187 // here is from time to time trouble!!!
188 delete _viewer;
189 }
190
191 void View3DInventor::deleteSelf()
192 {
193 _viewer->setSceneGraph(nullptr);
194 _viewer->setDocument(nullptr);
195 MDIView::deleteSelf();
196 }
197
198 View3DInventor* View3DInventor::clone()
199 {
200 auto mdiView = _pcDocument->createView(getClassTypeId(), CreateViewMode::Clone);
201 auto view3D = static_cast<View3DInventor*>(mdiView);
202
203 view3D->cloneFrom(*this);
204 view3D->getViewer()->setAxisCross(getViewer()->hasAxisCross());
205
206 // FIXME: Add parameter to define behaviour by the calling instance
207 // View provider editing
208
209 int editMode;
210 ViewProvider* editViewProvider = _pcDocument->getInEdit(nullptr, nullptr, &editMode);
211 if (editViewProvider) {
212 getViewer()->resetEditingViewProvider();
213 view3D->getViewer()->setEditingViewProvider(editViewProvider, editMode);
214 }
215
216 return view3D;
217 }
218
219 PyObject* View3DInventor::getPyObject()
220 {
221 if (!_viewerPy) {
222 _viewerPy = new View3DInventorPy(this);
223 }
224
225 Py_INCREF(_viewerPy);
226 return _viewerPy;
227 }
228
229 void View3DInventor::applySettings()
230 {
231 viewSettings = std::make_unique<View3DSettings>(
232 App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View"),
233 _viewer
234 );
235 naviSettings = std::make_unique<NaviCubeSettings>(
236 App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/NaviCube"),
237 _viewer
238 );
239 viewSettings->applySettings();
240 naviSettings->applySettings();
241 }
242
243 void View3DInventor::onRename(Gui::Document* pDoc)
244 {
245 SoSFString name;
246 name.setValue(pDoc->getDocument()->getName());
247 SoFCDocumentAction cAct(name);
248 cAct.apply(_viewer->getSceneGraph());
249 }
250
251 void View3DInventor::onUpdate()
252 {
253 #ifdef FC_LOGUPDATECHAIN
254 Base::Console().log("Acti: Gui::View3DInventor::onUpdate()");
255 #endif
256 update();
257 _viewer->redraw();
258 }
259
260 void View3DInventor::viewAll()
261 {
262 _viewer->viewAll();
263 }
264
265 const char* View3DInventor::getName() const
266 {
267 return "View3DInventor";
268 }
269
270 void View3DInventor::print()
271 {
272 QPrinter printer(QPrinter::ScreenResolution);
273 printer.setFullPage(true);
274 restorePrinterSettings(&printer);
275
276 QPrintDialog dlg(&printer, this);
277 if (dlg.exec() == QDialog::Accepted) {
278 Gui::WaitCursor wc;
279 print(&printer);
280 savePrinterSettings(&printer);
281 }
282 }
283
284 void View3DInventor::printPdf()
285 {
286 QString filename = FileDialog::getSaveFileName(
287 this,
288 tr("Export PDF"),
289 QString(),
290 QStringLiteral("%1 (*.pdf)").arg(tr("PDF file"))
291 );
292 if (!filename.isEmpty()) {
293 Gui::WaitCursor wc;
294 QPrinter printer(QPrinter::ScreenResolution);
295 // setPdfVersion sets the printed PDF Version to what is chosen in
296 // Preferences/Import-Export/PDF more details under:
297 // https://www.kdab.com/creating-pdfa-documents-qt/
298 printer.setPdfVersion(Gui::Dialog::DlgSettingsPDF::evaluatePDFVersion());
299 printer.setOutputFormat(QPrinter::PdfFormat);
300 printer.setPageOrientation(QPageLayout::Landscape);
301 printer.setOutputFileName(filename);
302 printer.setCreator(QString::fromStdString(App::Application::getNameWithVersion()));
303 print(&printer);
304 }
305 }
306
307 void View3DInventor::printPreview()
308 {
309 QPrinter printer(QPrinter::ScreenResolution);
310 printer.setFullPage(true);
311 restorePrinterSettings(&printer);
312
313 QPrintPreviewDialog dlg(&printer, this);
314 connect(
315 &dlg,
316 &QPrintPreviewDialog::paintRequested,
317 this,
318 qOverload<QPrinter*>(&View3DInventor::print)
319 );
320 dlg.exec();
321 savePrinterSettings(&printer);
322 }
323
324 void View3DInventor::print(QPrinter* printer)
325 {
326 QPainter p(printer);
327 p.setRenderHints(QPainter::Antialiasing);
328 if (!p.isActive() && !printer->outputFileName().isEmpty()) {
329 qApp->setOverrideCursor(Qt::ArrowCursor);
330 QMessageBox::critical(
331 this,
332 tr("Opening file failed"),
333 tr("Can't open file '%1' for writing.").arg(printer->outputFileName())
334 );
335 qApp->restoreOverrideCursor();
336 return;
337 }
338
339 QRect rect = printer->pageLayout().paintRectPixels(printer->resolution());
340 QImage img;
341 _viewer->imageFromFramebuffer(rect.width(), rect.height(), 8, QColor(255, 255, 255), img);
342 p.drawImage(0, 0, img);
343 p.end();
344 }
345
346 bool View3DInventor::containsViewProvider(const ViewProvider* vp) const
347 {
348 return _viewer->containsViewProvider(vp);
349 }
350
351 // **********************************************************************************
352
353 bool View3DInventor::onMsg(const char* pMsg, const char** ppReturn)
354 {
355 if (strcmp("ViewFit", pMsg) == 0) {
356 _viewer->viewAll();
357 return true;
358 }
359 else if (strcmp("ViewVR", pMsg) == 0) {
360 // call the VR portion of the viewer
361 _viewer->viewVR();
362 return true;
363 }
364 else if (strcmp("ViewSelection", pMsg) == 0) {
365 _viewer->viewSelection();
366 return true;
367 }
368 else if (strcmp("SetStereoRedGreen", pMsg) == 0) {
369 _viewer->setStereoMode(Quarter::SoQTQuarterAdaptor::ANAGLYPH);
370 return true;
371 }
372 else if (strcmp("SetStereoQuadBuff", pMsg) == 0) {
373 _viewer->setStereoMode(Quarter::SoQTQuarterAdaptor::QUAD_BUFFER);
374 return true;
375 }
376 else if (strcmp("SetStereoInterleavedRows", pMsg) == 0) {
377 _viewer->setStereoMode(Quarter::SoQTQuarterAdaptor::INTERLEAVED_ROWS);
378 return true;
379 }
380 else if (strcmp("SetStereoInterleavedColumns", pMsg) == 0) {
381 _viewer->setStereoMode(Quarter::SoQTQuarterAdaptor::INTERLEAVED_COLUMNS);
382 return true;
383 }
384 else if (strcmp("SetStereoOff", pMsg) == 0) {
385 _viewer->setStereoMode(Quarter::SoQTQuarterAdaptor::MONO);
386 return true;
387 }
388 else if (strcmp("GetCamera", pMsg) == 0) {
389 SoCamera* Cam = _viewer->getSoRenderManager()->getCamera();
390 if (!Cam) {
391 return false;
392 }
393 *ppReturn = SoFCDB::writeNodesToString(Cam).c_str();
394 return true;
395 }
396 else if (strncmp("SetCamera", pMsg, 9) == 0) {
397 return setCamera(pMsg + 10);
398 }
399 else if (strncmp("Dump", pMsg, 4) == 0) {
400 dump(pMsg + 5);
401 return true;
402 }
403 else if (strcmp("ViewBottom", pMsg) == 0) {
404 _viewer->setCameraOrientation(Camera::rotation(Camera::Bottom));
405 _viewer->viewAll();
406 return true;
407 }
408 else if (strcmp("ViewFront", pMsg) == 0) {
409 _viewer->setCameraOrientation(Camera::rotation(Camera::Front));
410 _viewer->viewAll();
411 return true;
412 }
413 else if (strcmp("ViewLeft", pMsg) == 0) {
414 _viewer->setCameraOrientation(Camera::rotation(Camera::Left));
415 _viewer->viewAll();
416 return true;
417 }
418 else if (strcmp("ViewRear", pMsg) == 0) {
419 _viewer->setCameraOrientation(Camera::rotation(Camera::Rear));
420 _viewer->viewAll();
421 return true;
422 }
423 else if (strcmp("ViewRight", pMsg) == 0) {
424 _viewer->setCameraOrientation(Camera::rotation(Camera::Right));
425 _viewer->viewAll();
426 return true;
427 }
428 else if (strcmp("ViewTop", pMsg) == 0) {
429 _viewer->setCameraOrientation(Camera::rotation(Camera::Top));
430 _viewer->viewAll();
431 return true;
432 }
433 else if (strcmp("ViewAxo", pMsg) == 0) {
434 _viewer->setCameraOrientation(Camera::rotation(Camera::Isometric));
435 _viewer->viewAll();
436 return true;
437 }
438 else if (strcmp("OrthographicCamera", pMsg) == 0) {
439 _viewer->setCameraType(SoOrthographicCamera::getClassTypeId());
440 return true;
441 }
442 else if (strcmp("PerspectiveCamera", pMsg) == 0) {
443 _viewer->setCameraType(SoPerspectiveCamera::getClassTypeId());
444 return true;
445 }
446 else if (strcmp("Undo", pMsg) == 0) {
447 getGuiDocument()->undo(1);
448 return true;
449 }
450 else if (strcmp("Redo", pMsg) == 0) {
451 getGuiDocument()->redo(1);
452 return true;
453 }
454 else if (strcmp("Save", pMsg) == 0) {
455 getGuiDocument()->save();
456 return true;
457 }
458 else if (strcmp("SaveAs", pMsg) == 0) {
459 getGuiDocument()->saveAs();
460 return true;
461 }
462 else if (strcmp("SaveCopy", pMsg) == 0) {
463 getGuiDocument()->saveCopy();
464 return true;
465 }
466 else if (strcmp("AlignToSelection", pMsg) == 0) {
467 _viewer->alignToSelection();
468 return true;
469 }
470 else if (strcmp("ZoomIn", pMsg) == 0) {
471 View3DInventorViewer* viewer = getViewer();
472 viewer->navigationStyle()->zoomIn();
473 return true;
474 }
475 else if (strcmp("ZoomOut", pMsg) == 0) {
476 View3DInventorViewer* viewer = getViewer();
477 viewer->navigationStyle()->zoomOut();
478 return true;
479 }
480
481 return false;
482 }
483
484 bool View3DInventor::onHasMsg(const char* pMsg) const
485 {
486 if (strcmp("CanPan", pMsg) == 0) {
487 return true;
488 }
489 else if (strcmp("Save", pMsg) == 0) {
490 return true;
491 }
492 else if (strcmp("SaveAs", pMsg) == 0) {
493 return true;
494 }
495 else if (strcmp("SaveCopy", pMsg) == 0) {
496 return true;
497 }
498 else if (strcmp("Undo", pMsg) == 0) {
499 App::Document* doc = getAppDocument();
500 return doc && doc->getAvailableUndos() > 0;
501 }
502 else if (strcmp("Redo", pMsg) == 0) {
503 App::Document* doc = getAppDocument();
504 return doc && doc->getAvailableRedos() > 0;
505 }
506 else if (strcmp("Print", pMsg) == 0) {
507 return true;
508 }
509 else if (strcmp("PrintPreview", pMsg) == 0) {
510 return true;
511 }
512 else if (strcmp("PrintPdf", pMsg) == 0) {
513 return true;
514 }
515 else if (strcmp("SetStereoRedGreen", pMsg) == 0) {
516 return true;
517 }
518 else if (strcmp("SetStereoQuadBuff", pMsg) == 0) {
519 return true;
520 }
521 else if (strcmp("SetStereoInterleavedRows", pMsg) == 0) {
522 return true;
523 }
524 else if (strcmp("SetStereoInterleavedColumns", pMsg) == 0) {
525 return true;
526 }
527 else if (strcmp("SetStereoOff", pMsg) == 0) {
528 return true;
529 }
530 else if (strcmp("ViewFit", pMsg) == 0) {
531 return true;
532 }
533 else if (strcmp("ViewVR", pMsg) == 0) {
534 #ifdef BUILD_VR
535 return true;
536 #else
537 return false;
538 #endif
539 }
540 else if (strcmp("ViewSelection", pMsg) == 0) {
541 return true;
542 }
543 else if (strcmp("ViewBottom", pMsg) == 0) {
544 return true;
545 }
546 else if (strcmp("ViewFront", pMsg) == 0) {
547 return true;
548 }
549 else if (strcmp("ViewLeft", pMsg) == 0) {
550 return true;
551 }
552 else if (strcmp("ViewRear", pMsg) == 0) {
553 return true;
554 }
555 else if (strcmp("ViewRight", pMsg) == 0) {
556 return true;
557 }
558 else if (strcmp("ViewTop", pMsg) == 0) {
559 return true;
560 }
561 else if (strcmp("ViewAxo", pMsg) == 0) {
562 return true;
563 }
564 else if (strcmp("GetCamera", pMsg) == 0) {
565 return true;
566 }
567 else if (strncmp("SetCamera", pMsg, 9) == 0) {
568 return true;
569 }
570 else if (strncmp("Dump", pMsg, 4) == 0) {
571 return true;
572 }
573 else if (strcmp("AlignToSelection", pMsg) == 0) {
574 return true;
575 }
576 if (strcmp("ZoomIn", pMsg) == 0) {
577 return true;
578 }
579 if (strcmp("ZoomOut", pMsg) == 0) {
580 return true;
581 }
582 if (strcmp("AllowsOverlayOnHover", pMsg) == 0) {
583 return true;
584 }
585
586 return false;
587 }
588
589 bool View3DInventor::setCamera(const char* pCamera)
590 {
591 SoCamera* CamViewer = _viewer->getSoRenderManager()->getCamera();
592 if (!CamViewer) {
593 throw Base::RuntimeError("No camera set so far…");
594 }
595
596 SoInput in;
597 in.setBuffer((void*)pCamera, std::strlen(pCamera));
598
599 SoNode* Cam;
600 SoDB::read(&in, Cam);
601
602 if (!Cam || !Cam->isOfType(SoCamera::getClassTypeId())) {
603 throw Base::RuntimeError("Camera settings failed to read");
604 }
605
606 // this is to make sure to reliably delete the node
607 CoinPtr<SoNode> camPtr {Cam};
608
609 // toggle between perspective and orthographic camera
610 if (Cam->getTypeId() != CamViewer->getTypeId()) {
611 _viewer->setCameraType(Cam->getTypeId());
612 CamViewer = _viewer->getSoRenderManager()->getCamera();
613 }
614
615 SoPerspectiveCamera* CamViewerP = nullptr;
616 SoOrthographicCamera* CamViewerO = nullptr;
617
618 if (CamViewer->getTypeId() == SoPerspectiveCamera::getClassTypeId()) {
619 CamViewerP = static_cast<SoPerspectiveCamera*>(CamViewer); // safe downward cast, knows the type
620 }
621 else if (CamViewer->getTypeId() == SoOrthographicCamera::getClassTypeId()) {
622 CamViewerO = static_cast<SoOrthographicCamera*>(CamViewer); // safe downward cast, knows
623 // the type
624 }
625
626 if (Cam->getTypeId() == SoPerspectiveCamera::getClassTypeId()) {
627 if (CamViewerP) {
628 CamViewerP->position = static_cast<SoPerspectiveCamera*>(Cam)->position;
629 CamViewerP->orientation = static_cast<SoPerspectiveCamera*>(Cam)->orientation;
630 CamViewerP->nearDistance = static_cast<SoPerspectiveCamera*>(Cam)->nearDistance;
631 CamViewerP->farDistance = static_cast<SoPerspectiveCamera*>(Cam)->farDistance;
632 CamViewerP->focalDistance = static_cast<SoPerspectiveCamera*>(Cam)->focalDistance;
633 }
634 else {
635 throw Base::TypeError("Camera type mismatch");
636 }
637 }
638 else if (Cam->getTypeId() == SoOrthographicCamera::getClassTypeId()) {
639 if (CamViewerO) {
640 CamViewerO->viewportMapping = static_cast<SoOrthographicCamera*>(Cam)->viewportMapping;
641 CamViewerO->position = static_cast<SoOrthographicCamera*>(Cam)->position;
642 CamViewerO->orientation = static_cast<SoOrthographicCamera*>(Cam)->orientation;
643 CamViewerO->nearDistance = static_cast<SoOrthographicCamera*>(Cam)->nearDistance;
644 CamViewerO->farDistance = static_cast<SoOrthographicCamera*>(Cam)->farDistance;
645 CamViewerO->focalDistance = static_cast<SoOrthographicCamera*>(Cam)->focalDistance;
646 CamViewerO->aspectRatio = static_cast<SoOrthographicCamera*>(Cam)->aspectRatio;
647 CamViewerO->height = static_cast<SoOrthographicCamera*>(Cam)->height;
648 }
649 else {
650 throw Base::TypeError("Camera type mismatch");
651 }
652 }
653
654 return true;
655 }
656
657 void View3DInventor::toggleClippingPlane()
658 {
659 _viewer->toggleClippingPlane();
660 }
661
662 bool View3DInventor::hasClippingPlane() const
663 {
664 return _viewer->hasClippingPlane();
665 }
666
667 void View3DInventor::setOverlayWidget(QWidget* widget)
668 {
669 removeOverlayWidget();
670 stack->addWidget(widget);
671 stack->setCurrentIndex(1);
672 }
673
674 void View3DInventor::removeOverlayWidget()
675 {
676 stack->setCurrentIndex(0);
677 QWidget* overlay = stack->widget(1);
678 if (overlay) {
679 stack->removeWidget(overlay);
680 }
681 }
682
683 void View3DInventor::setOverrideCursor(const QCursor& aCursor)
684 {
685 _viewer->getWidget()->setCursor(aCursor);
686 }
687
688 void View3DInventor::restoreOverrideCursor()
689 {
690 _viewer->getWidget()->setCursor(QCursor(Qt::ArrowCursor));
691 }
692
693 // defined in SoFCDB.cpp
694 extern SoNode* replaceSwitchesInSceneGraph(SoNode*);
695
696 void View3DInventor::dump(const char* filename, bool onlyVisible)
697 {
698 SoGetPrimitiveCountAction action;
699 action.setCanApproximate(true);
700 action.apply(_viewer->getSceneGraph());
701
702 SoNode* node = _viewer->getSceneGraph();
703 if (onlyVisible) {
704 node = replaceSwitchesInSceneGraph(node);
705 node->ref();
706 }
707
708 if (action.getTriangleCount() > 100000 || action.getPointCount() > 30000
709 || action.getLineCount() > 10000) {
710 _viewer->dumpToFile(node, filename, true);
711 }
712 else {
713 _viewer->dumpToFile(node, filename, false);
714 }
715
716 if (onlyVisible) {
717 node->unref();
718 }
719 }
720
721 void View3DInventor::windowStateChanged(QWidget* view)
722 {
723 bool canStartTimer = false;
724 if (this != view) {
725 // If both views are child widgets of the workspace and view is maximized this view
726 // must be hidden, hence we can start the timer.
727 // Note: If view is top-level or fullscreen it doesn't necessarily hide the other view
728 // e.g. if it is on a second monitor.
729 canStartTimer = (!this->isWindow() && !view->isWindow() && view->isMaximized());
730 }
731 else if (isMinimized()) {
732 // I am the active view but minimized
733 canStartTimer = true;
734 }
735
736 if (canStartTimer) {
737 int msecs = viewSettings->stopAnimatingIfDeactivated();
738 if (!stopSpinTimer->isActive() && msecs >= 0) { // if < 0 do not stop rotation
739 stopSpinTimer->setSingleShot(true);
740 stopSpinTimer->start(msecs);
741 }
742 }
743 else if (stopSpinTimer->isActive()) {
744 // If this view may be visible again we can stop the timer
745 stopSpinTimer->stop();
746 }
747 }
748
749 void View3DInventor::stopAnimating()
750 {
751 _viewer->stopAnimating();
752 }
753
754 /**
755 * Drops the event \a e and writes the right Python command.
756 */
757 void View3DInventor::dropEvent(QDropEvent* e)
758 {
759 const QMimeData* data = e->mimeData();
760 if (data->hasUrls()) {
761 getMainWindow()->loadUrls(getAppDocument(), data->urls());
762 }
763 else {
764 MDIView::dropEvent(e);
765 }
766 }
767
768 void View3DInventor::dragEnterEvent(QDragEnterEvent* e)
769 {
770 // Here we must allow uri drags and check them in dropEvent
771 const QMimeData* data = e->mimeData();
772 if (data->hasUrls()) {
773 e->accept();
774 }
775 else {
776 e->ignore();
777 }
778 }
779
780 void View3DInventor::setCurrentViewMode(ViewMode mode)
781 {
782 ViewMode oldmode = currentViewMode();
783 if (mode == oldmode) {
784 return;
785 }
786
787 if (mode == Child) {
788 // Fix in two steps:
789 // The mdi view got a QWindow when it became a top-level widget and when resetting it to a
790 // child widget the QWindow must be deleted because it has an impact on resize events and
791 // may break the layout of mdi view inside the QMdiSubWindow. In the second step below the
792 // layout must be invalidated after it's again a child widget to make sure the mdi view fits
793 // into the QMdiSubWindow.
794 QWindow* winHandle = this->windowHandle();
795 if (winHandle) {
796 winHandle->destroy();
797 }
798 }
799
800 MDIView::setCurrentViewMode(mode);
801
802 // This widget becomes the focus proxy of the embedded GL widget if we leave
803 // the 'Child' mode. If we reenter 'Child' mode the focus proxy is reset to 0.
804 // If we change from 'TopLevel' mode to 'Fullscreen' mode or vice versa nothing
805 // happens.
806 // Grabbing keyboard when leaving 'Child' mode (as done in a recent version) should
807 // be avoided because when two or more windows are either in 'TopLevel' or 'Fullscreen'
808 // mode only the last window gets all key event even if it is not the active one.
809 //
810 // It is important to set the focus proxy to get all key events otherwise we would lose
811 // control after redirecting the first key event to the GL widget.
812
813 if (oldmode == Child) {
814 _viewer->getGLWidget()->setFocusProxy(this);
815 }
816 else if (mode == Child) {
817 _viewer->getGLWidget()->setFocusProxy(nullptr);
818
819 // Step two
820 auto mdi = qobject_cast<QMdiSubWindow*>(parentWidget());
821 if (mdi && mdi->layout()) {
822 mdi->layout()->invalidate();
823 }
824 }
825 }
826
827 RayPickInfo View3DInventor::getObjInfoRay(Base::Vector3d* startvec, Base::Vector3d* dirvec)
828 {
829 double vsx, vsy, vsz;
830 double vdx, vdy, vdz;
831 vsx = startvec->x;
832 vsy = startvec->y;
833 vsz = startvec->z;
834 vdx = dirvec->x;
835 vdy = dirvec->y;
836 vdz = dirvec->z;
837 // near plane clipping is required to avoid false intersections
838 float nearClippingPlane = 0.1F;
839
840 RayPickInfo ret = {false, Base::Vector3d(), "", "", std::nullopt, std::nullopt, std::nullopt};
841 SoRayPickAction action(getViewer()->getSoRenderManager()->getViewportRegion());
842 action.setRay(SbVec3f(vsx, vsy, vsz), SbVec3f(vdx, vdy, vdz), nearClippingPlane);
843 action.apply(getViewer()->getSoRenderManager()->getSceneGraph());
844 SoPickedPoint* Point = action.getPickedPoint();
845
846 if (!Point) {
847 return ret;
848 }
849
850 ret.point = Base::convertTo<Base::Vector3d>(Point->getPoint());
851 ViewProvider* vp = getViewer()->getViewProviderByPath(Point->getPath());
852 if (vp && vp->isDerivedFrom<ViewProviderDocumentObject>()) {
853 if (!vp->isSelectable()) {
854 return ret;
855 }
856 auto vpd = static_cast<ViewProviderDocumentObject*>(vp);
857 if (vp->useNewSelectionModel()) {
858 std::string subname;
859 if (!vp->getElementPicked(Point, subname)) {
860 return ret;
861 }
862 auto obj = vpd->getObject();
863 if (!obj) {
864 return ret;
865 }
866 if (!subname.empty()) {
867 App::ElementNamePair elementName;
868 auto sobj = App::GeoFeature::resolveElement(obj, subname.c_str(), elementName);
869 if (!sobj) {
870 return ret;
871 }
872 if (sobj != obj) {
873 ret.parentObject = obj->getExportName();
874 ret.subName = subname;
875 obj = sobj;
876 }
877 subname = !elementName.oldName.empty() ? elementName.oldName : elementName.newName;
878 }
879 ret.document = obj->getDocument()->getName();
880 ret.object = obj->getNameInDocument();
881 ret.component = subname;
882 ret.isValid = true;
883 }
884 else {
885 ret.document = vpd->getObject()->getDocument()->getName();
886 ret.object = vpd->getObject()->getNameInDocument();
887 // search for a SoFCSelection node
888 SoFCDocumentObjectAction objaction;
889 objaction.apply(Point->getPath());
890 if (objaction.isHandled()) {
891 ret.component = objaction.componentName.getString();
892 }
893 }
894 // ok, found the node of interest
895 ret.isValid = true;
896 }
897 else {
898 // custom nodes not in a VP: search for a SoFCSelection node
899 SoFCDocumentObjectAction objaction;
900 objaction.apply(Point->getPath());
901 if (objaction.isHandled()) {
902 ret.document = objaction.documentName.getString();
903 ret.object = objaction.objectName.getString();
904 ret.component = objaction.componentName.getString();
905 // ok, found the node of interest
906 ret.isValid = true;
907 }
908 }
909 return ret;
910 }
911
912 void View3DInventor::keyPressEvent(QKeyEvent* e)
913 {
914 // See StdViewDockUndockFullscreen::activated()
915 // With Qt5 one cannot directly use 'setCurrentViewMode'
916 // of an MDI view because it causes rendering problems.
917 // The only reliable solution is to clone the MDI view,
918 // set its view mode and close the original MDI view.
919
920 QMainWindow::keyPressEvent(e);
921 }
922
923 void View3DInventor::keyReleaseEvent(QKeyEvent* e)
924 {
925 QMainWindow::keyReleaseEvent(e);
926 }
927
928 void View3DInventor::focusInEvent(QFocusEvent*)
929 {
930 _viewer->getGLWidget()->setFocus();
931 }
932
933 void View3DInventor::contextMenuEvent(QContextMenuEvent* e)
934 {
935 MDIView::contextMenuEvent(e);
936 }
937
938 void View3DInventor::customEvent(QEvent* e)
939 {
940 if (e->type() == QEvent::User) {
941 auto se = static_cast<NavigationStyleEvent*>(e);
942 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
943 "User parameter:BaseApp/Preferences/View"
944 );
945 if (hGrp->GetBool("SameStyleForAllViews", true)) {
946 hGrp->SetASCII("NavigationStyle", se->style().getName());
947 }
948 else {
949 _viewer->setNavigationType(se->style());
950 }
951 }
952 }
953
954
955 #include "moc_View3DInventor.cpp"