Debian Codegraph @

src/Gui/SplitView3DInventor.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> *
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 <QSplitter>
25 #include <QSurfaceFormat>
26 #include <Inventor/nodes/SoDirectionalLight.h>
27 #include <Inventor/nodes/SoOrthographicCamera.h>
28 #include <Inventor/nodes/SoPerspectiveCamera.h>
29
30
31 #include <Base/Builder3D.h>
32 #include <Base/Interpreter.h>
33
34 #include "SplitView3DInventor.h"
35 #include "Application.h"
36 #include "Camera.h"
37 #include "Document.h"
38 #include "Navigation/NavigationStyle.h"
39 #include "SoFCSelectionAction.h"
40 #include "View3DInventorViewer.h"
41 #include "View3DPy.h"
42 #include "View3DSettings.h"
43
44
45 using namespace Gui;
46
47 TYPESYSTEM_SOURCE_ABSTRACT(Gui::AbstractSplitView, Gui::MDIView)
48
49 AbstractSplitView::AbstractSplitView(Gui::Document* pcDocument, QWidget* parent, Qt::WindowFlags wflags)
50 : MDIView(pcDocument, parent, wflags)
51 {
52 _viewerPy = nullptr;
53 // important for highlighting
54 setMouseTracking(true);
55 }
56
57 AbstractSplitView::~AbstractSplitView()
58 {
59 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
60 ++it) {
61 delete *it;
62 }
63 if (_viewerPy) {
64 Base::PyGILStateLocker lock;
65 Py_DECREF(_viewerPy);
66 }
67 }
68
69 void AbstractSplitView::deleteSelf()
70 {
71 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
72 ++it) {
73 (*it)->setSceneGraph(nullptr);
74 }
75 MDIView::deleteSelf();
76 }
77
78 void AbstractSplitView::setDocumentOfViewers(Gui::Document* document)
79 {
80 for (auto view : _viewer) {
81 view->setDocument(document);
82 }
83 }
84
85 void AbstractSplitView::viewAll()
86 {
87 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
88 ++it) {
89 (*it)->viewAll();
90 }
91 }
92
93 bool AbstractSplitView::containsViewProvider(const ViewProvider* vp) const
94 {
95 for (auto it = _viewer.begin(); it != _viewer.end(); ++it) {
96 if ((*it)->containsViewProvider(vp)) {
97 return true;
98 }
99 }
100
101 return false;
102 }
103
104 void AbstractSplitView::setupSettings()
105 {
106 viewSettings = std::make_unique<View3DSettings>(
107 App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View"),
108 _viewer
109 );
110 // tmp. disabled will be activated after redesign of 3d viewer
111 // check whether the simple or the Full Mouse model is used
112 viewSettings->ignoreNavigationStyle = true;
113 // Disable VBO for split screen as this leads to random crashes
114 viewSettings->ignoreVBO = true;
115 viewSettings->ignoreTransparent = true;
116 viewSettings->ignoreRenderCache = true;
117 viewSettings->ignoreDimensions = true;
118 viewSettings->applySettings();
119
120 for (auto view : _viewer) {
121 NaviCubeSettings naviSettings(
122 App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/NaviCube"),
123 view
124 );
125 naviSettings.applySettings();
126 }
127 }
128
129 View3DInventorViewer* AbstractSplitView::getViewer(unsigned int n) const
130 {
131 return (_viewer.size() > n ? _viewer[n] : nullptr);
132 }
133
134 void AbstractSplitView::onUpdate()
135 {
136 update();
137 }
138
139 const char* AbstractSplitView::getName() const
140 {
141 return "SplitView3DInventor";
142 }
143
144 bool AbstractSplitView::onMsg(const char* pMsg, const char**)
145 {
146 if (strcmp("ViewFit", pMsg) == 0) {
147 viewAll();
148 return true;
149 }
150 else if (strcmp("ViewBottom", pMsg) == 0) {
151 SbRotation rot(Camera::rotation(Camera::Bottom));
152 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
153 ++it) {
154 SoCamera* cam = (*it)->getSoRenderManager()->getCamera();
155 cam->orientation.setValue(rot);
156 (*it)->viewAll();
157 }
158 return true;
159 }
160 else if (strcmp("ViewFront", pMsg) == 0) {
161 SbRotation rot(Camera::rotation(Camera::Front));
162 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
163 ++it) {
164 SoCamera* cam = (*it)->getSoRenderManager()->getCamera();
165 cam->orientation.setValue(rot);
166 (*it)->viewAll();
167 }
168 return true;
169 }
170 else if (strcmp("ViewLeft", pMsg) == 0) {
171 SbRotation rot(Camera::rotation(Camera::Left));
172 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
173 ++it) {
174 SoCamera* cam = (*it)->getSoRenderManager()->getCamera();
175 cam->orientation.setValue(rot);
176 (*it)->viewAll();
177 }
178 return true;
179 }
180 else if (strcmp("ViewRear", pMsg) == 0) {
181 SbRotation rot(Camera::rotation(Camera::Rear));
182 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
183 ++it) {
184 SoCamera* cam = (*it)->getSoRenderManager()->getCamera();
185 cam->orientation.setValue(rot);
186 (*it)->viewAll();
187 }
188 return true;
189 }
190 else if (strcmp("ViewRight", pMsg) == 0) {
191 SbRotation rot(Camera::rotation(Camera::Right));
192 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
193 ++it) {
194 SoCamera* cam = (*it)->getSoRenderManager()->getCamera();
195 cam->orientation.setValue(rot);
196 (*it)->viewAll();
197 }
198 return true;
199 }
200 else if (strcmp("ViewTop", pMsg) == 0) {
201 SbRotation rot(Camera::rotation(Camera::Top));
202 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
203 ++it) {
204 SoCamera* cam = (*it)->getSoRenderManager()->getCamera();
205 cam->orientation.setValue(rot);
206 (*it)->viewAll();
207 }
208 return true;
209 }
210 else if (strcmp("ViewAxo", pMsg) == 0) {
211 SbRotation rot(Camera::rotation(Camera::Isometric));
212 for (std::vector<View3DInventorViewer*>::iterator it = _viewer.begin(); it != _viewer.end();
213 ++it) {
214 SoCamera* cam = (*it)->getSoRenderManager()->getCamera();
215 cam->orientation.setValue(rot);
216 (*it)->viewAll();
217 }
218 return true;
219 }
220
221 return false;
222 }
223
224 bool AbstractSplitView::onHasMsg(const char* pMsg) const
225 {
226 if (strcmp("CanPan", pMsg) == 0) {
227 return true;
228 }
229 else if (strcmp("ViewFit", pMsg) == 0) {
230 return true;
231 }
232 else if (strcmp("ViewBottom", pMsg) == 0) {
233 return true;
234 }
235 else if (strcmp("ViewFront", pMsg) == 0) {
236 return true;
237 }
238 else if (strcmp("ViewLeft", pMsg) == 0) {
239 return true;
240 }
241 else if (strcmp("ViewRear", pMsg) == 0) {
242 return true;
243 }
244 else if (strcmp("ViewRight", pMsg) == 0) {
245 return true;
246 }
247 else if (strcmp("ViewTop", pMsg) == 0) {
248 return true;
249 }
250 else if (strcmp("ViewAxo", pMsg) == 0) {
251 return true;
252 }
253 else if (strcmp("AllowsOverlayOnHover", pMsg) == 0) {
254 return true;
255 }
256 return false;
257 }
258
259 void AbstractSplitView::setOverrideCursor(const QCursor& aCursor)
260 {
261 Q_UNUSED(aCursor);
262 //_viewer->getWidget()->setCursor(aCursor);
263 }
264
265 PyObject* AbstractSplitView::getPyObject()
266 {
267 if (!_viewerPy) {
268 _viewerPy = new AbstractSplitViewPy(this);
269 }
270 Py_INCREF(_viewerPy);
271 return _viewerPy;
272 }
273
274 void AbstractSplitView::setPyObject(PyObject*)
275 {
276 throw Base::AttributeError("Attribute is read-only");
277 }
278
279 int AbstractSplitView::getSize()
280 {
281 return static_cast<int>(_viewer.size());
282 }
283
284 // ------------------------------------------------------
285
286 void AbstractSplitViewPy::init_type()
287 {
288 behaviors().name("AbstractSplitViewPy");
289 behaviors().doc("Python binding class for the Inventor viewer class");
290 // you must have overwritten the virtual functions
291 behaviors().supportRepr();
292 behaviors().supportGetattr();
293 behaviors().supportSetattr();
294 behaviors().supportSequenceType();
295
296 add_varargs_method("fitAll", &AbstractSplitViewPy::fitAll, "fitAll()");
297 add_varargs_method("viewBottom", &AbstractSplitViewPy::viewBottom, "viewBottom()");
298 add_varargs_method("viewFront", &AbstractSplitViewPy::viewFront, "viewFront()");
299 add_varargs_method("viewLeft", &AbstractSplitViewPy::viewLeft, "viewLeft()");
300 add_varargs_method("viewRear", &AbstractSplitViewPy::viewRear, "viewRear()");
301 add_varargs_method("viewRight", &AbstractSplitViewPy::viewRight, "viewRight()");
302 add_varargs_method("viewTop", &AbstractSplitViewPy::viewTop, "viewTop()");
303 add_varargs_method("viewAxometric", &AbstractSplitViewPy::viewIsometric, "viewAxometric()");
304 add_varargs_method("viewIsometric", &AbstractSplitViewPy::viewIsometric, "viewIsometric()");
305 add_varargs_method("getViewer", &AbstractSplitViewPy::getViewer, "getViewer(index)");
306 add_varargs_method("close", &AbstractSplitViewPy::close, "close()");
307 add_varargs_method(
308 "cast_to_base",
309 &AbstractSplitViewPy::cast_to_base,
310 "cast_to_base() cast to MDIView class"
311 );
312 behaviors().readyType();
313 }
314
315 AbstractSplitViewPy::AbstractSplitViewPy(AbstractSplitView* vi)
316 : base(vi)
317 {}
318
319 AbstractSplitViewPy::~AbstractSplitViewPy() = default;
320
321 Py::Object AbstractSplitViewPy::cast_to_base(const Py::Tuple&)
322 {
323 return Gui::MDIViewPy::create(base.getMDIViewPtr());
324 }
325
326 Py::Object AbstractSplitViewPy::repr()
327 {
328 if (!getSplitViewPtr()) {
329 throw Py::RuntimeError("Cannot print representation of deleted object");
330 }
331
332 return Py::String("AbstractSplitView");
333 }
334
335 // Since with PyCXX it's not possible to make a sub-class of MDIViewPy
336 // a trick is to use MDIViewPy as class member and override getattr() to
337 // join the attributes of both classes. This way all methods of MDIViewPy
338 // appear for SheetViewPy, too.
339 Py::Object AbstractSplitViewPy::getattr(const char* attr)
340 {
341 getSplitViewPtr();
342 std::string name(attr);
343 if (name == "__dict__" || name == "__class__") {
344 Py::Dict dict_self(BaseType::getattr("__dict__"));
345 Py::Dict dict_base(base.getattr("__dict__"));
346 for (const auto& it : dict_base) {
347 dict_self.setItem(it.first, it.second);
348 }
349 return dict_self;
350 }
351
352 try {
353 return BaseType::getattr(attr);
354 }
355 catch (Py::AttributeError& e) {
356 e.clear();
357 return base.getattr(attr);
358 }
359 }
360
361 AbstractSplitView* AbstractSplitViewPy::getSplitViewPtr()
362 {
363 auto view = qobject_cast<AbstractSplitView*>(base.getMDIViewPtr());
364 if (!(view && view->getViewer(0))) {
365 throw Py::RuntimeError("Object already deleted");
366 }
367 return view;
368 }
369
370 Py::Object AbstractSplitViewPy::fitAll(const Py::Tuple& args)
371 {
372 if (!PyArg_ParseTuple(args.ptr(), "")) {
373 throw Py::Exception();
374 }
375
376 try {
377 getSplitViewPtr()->onMsg("ViewFit", nullptr);
378 }
379 catch (const Base::Exception& e) {
380 throw Py::RuntimeError(e.what());
381 }
382 catch (const std::exception& e) {
383 throw Py::RuntimeError(e.what());
384 }
385 catch (...) {
386 throw Py::RuntimeError("Unknown C++ exception");
387 }
388 return Py::None();
389 }
390
391 Py::Object AbstractSplitViewPy::viewBottom(const Py::Tuple& args)
392 {
393 if (!PyArg_ParseTuple(args.ptr(), "")) {
394 throw Py::Exception();
395 }
396
397 try {
398 getSplitViewPtr()->onMsg("ViewBottom", nullptr);
399 }
400 catch (const Base::Exception& e) {
401 throw Py::RuntimeError(e.what());
402 }
403 catch (const std::exception& e) {
404 throw Py::RuntimeError(e.what());
405 }
406 catch (...) {
407 throw Py::RuntimeError("Unknown C++ exception");
408 }
409
410 return Py::None();
411 }
412
413 Py::Object AbstractSplitViewPy::viewFront(const Py::Tuple& args)
414 {
415 if (!PyArg_ParseTuple(args.ptr(), "")) {
416 throw Py::Exception();
417 }
418
419 try {
420 getSplitViewPtr()->onMsg("ViewFront", nullptr);
421 }
422 catch (const Base::Exception& e) {
423 throw Py::RuntimeError(e.what());
424 }
425 catch (const std::exception& e) {
426 throw Py::RuntimeError(e.what());
427 }
428 catch (...) {
429 throw Py::RuntimeError("Unknown C++ exception");
430 }
431
432 return Py::None();
433 }
434
435 Py::Object AbstractSplitViewPy::viewLeft(const Py::Tuple& args)
436 {
437 if (!PyArg_ParseTuple(args.ptr(), "")) {
438 throw Py::Exception();
439 }
440
441 try {
442 getSplitViewPtr()->onMsg("ViewLeft", nullptr);
443 }
444 catch (const Base::Exception& e) {
445 throw Py::RuntimeError(e.what());
446 }
447 catch (const std::exception& e) {
448 throw Py::RuntimeError(e.what());
449 }
450 catch (...) {
451 throw Py::RuntimeError("Unknown C++ exception");
452 }
453
454 return Py::None();
455 }
456
457 Py::Object AbstractSplitViewPy::viewRear(const Py::Tuple& args)
458 {
459 if (!PyArg_ParseTuple(args.ptr(), "")) {
460 throw Py::Exception();
461 }
462
463 try {
464 getSplitViewPtr()->onMsg("ViewRear", nullptr);
465 }
466 catch (const Base::Exception& e) {
467 throw Py::RuntimeError(e.what());
468 }
469 catch (const std::exception& e) {
470 throw Py::RuntimeError(e.what());
471 }
472 catch (...) {
473 throw Py::RuntimeError("Unknown C++ exception");
474 }
475
476 return Py::None();
477 }
478
479 Py::Object AbstractSplitViewPy::viewRight(const Py::Tuple& args)
480 {
481 if (!PyArg_ParseTuple(args.ptr(), "")) {
482 throw Py::Exception();
483 }
484
485 try {
486 getSplitViewPtr()->onMsg("ViewRight", nullptr);
487 }
488 catch (const Base::Exception& e) {
489 throw Py::RuntimeError(e.what());
490 }
491 catch (const std::exception& e) {
492 throw Py::RuntimeError(e.what());
493 }
494 catch (...) {
495 throw Py::RuntimeError("Unknown C++ exception");
496 }
497
498 return Py::None();
499 }
500
501 Py::Object AbstractSplitViewPy::viewTop(const Py::Tuple& args)
502 {
503 if (!PyArg_ParseTuple(args.ptr(), "")) {
504 throw Py::Exception();
505 }
506
507 try {
508 getSplitViewPtr()->onMsg("ViewTop", nullptr);
509 }
510 catch (const Base::Exception& e) {
511 throw Py::RuntimeError(e.what());
512 }
513 catch (const std::exception& e) {
514 throw Py::RuntimeError(e.what());
515 }
516 catch (...) {
517 throw Py::RuntimeError("Unknown C++ exception");
518 }
519
520 return Py::None();
521 }
522
523 Py::Object AbstractSplitViewPy::viewIsometric(const Py::Tuple& args)
524 {
525 if (!PyArg_ParseTuple(args.ptr(), "")) {
526 throw Py::Exception();
527 }
528
529 try {
530 getSplitViewPtr()->onMsg("ViewAxo", nullptr);
531 }
532 catch (const Base::Exception& e) {
533 throw Py::RuntimeError(e.what());
534 }
535 catch (const std::exception& e) {
536 throw Py::RuntimeError(e.what());
537 }
538 catch (...) {
539 throw Py::RuntimeError("Unknown C++ exception");
540 }
541
542 return Py::None();
543 }
544
545 Py::Object AbstractSplitViewPy::getViewer(const Py::Tuple& args)
546 {
547 int viewIndex;
548 if (!PyArg_ParseTuple(args.ptr(), "i", &viewIndex)) {
549 throw Py::Exception();
550 }
551
552 try {
553 Gui::View3DInventorViewer* view = getSplitViewPtr()->getViewer(viewIndex);
554 if (!view) {
555 throw Py::IndexError("Index out of range");
556 }
557 return Py::asObject(view->getPyObject());
558 }
559 catch (const Base::Exception& e) {
560 throw Py::RuntimeError(e.what());
561 }
562 catch (const std::exception& e) {
563 throw Py::RuntimeError(e.what());
564 }
565 catch (const Py::Exception&) {
566 // re-throw
567 throw;
568 }
569 catch (...) {
570 throw Py::RuntimeError("Unknown C++ exception");
571 }
572 }
573
574 Py::Object AbstractSplitViewPy::sequence_item(Py_ssize_t viewIndex)
575 {
576 AbstractSplitView* view = getSplitViewPtr();
577 if (viewIndex >= view->getSize() || viewIndex < 0) {
578 throw Py::IndexError("Index out of range");
579 }
580 PyObject* viewer = view->getViewer(viewIndex)->getPyObject();
581 return Py::asObject(viewer);
582 }
583
584 PyCxx_ssize_t AbstractSplitViewPy::sequence_length()
585 {
586 AbstractSplitView* view = getSplitViewPtr();
587 return view->getSize();
588 }
589
590 Py::Object AbstractSplitViewPy::close(const Py::Tuple& args)
591 {
592 if (!PyArg_ParseTuple(args.ptr(), "")) {
593 throw Py::Exception();
594 }
595
596 AbstractSplitView* view = getSplitViewPtr();
597 view->close();
598 if (view->parentWidget()) {
599 view->parentWidget()->deleteLater();
600 }
601
602 return Py::None();
603 }
604
605 // ------------------------------------------------------
606
607 TYPESYSTEM_SOURCE_ABSTRACT(Gui::SplitView3DInventor, Gui::AbstractSplitView)
608
609 SplitView3DInventor::SplitView3DInventor(
610 int views,
611 Gui::Document* pcDocument,
612 QWidget* parent,
613 Qt::WindowFlags wflags
614 )
615 : AbstractSplitView(pcDocument, parent, wflags)
616 {
617 // anti-aliasing settings
618 bool smoothing = false;
619 bool glformat = false;
620 int samples = View3DInventorViewer::getNumSamples();
621 QSurfaceFormat f;
622
623 if (samples > 1) {
624 glformat = true;
625 f.setSamples(samples);
626 }
627 else if (samples > 0) {
628 smoothing = true;
629 }
630
631 // minimal 2 views
632 while (views < 2) {
633 views++;
634 }
635
636 QSplitter* mainSplitter = nullptr;
637
638 // if views < 3 show them as a row
639 if (views <= 3) {
640 mainSplitter = new QSplitter(Qt::Horizontal, this);
641 for (int i = 0; i < views; i++) {
642 if (glformat) {
643 _viewer.push_back(new View3DInventorViewer(f, mainSplitter));
644 }
645 else {
646 _viewer.push_back(new View3DInventorViewer(mainSplitter));
647 }
648 }
649 }
650 else {
651 mainSplitter = new QSplitter(Qt::Vertical, this);
652 auto topSplitter = new QSplitter(Qt::Horizontal, mainSplitter);
653 auto botSplitter = new QSplitter(Qt::Horizontal, mainSplitter);
654
655 if (glformat) {
656 _viewer.push_back(new View3DInventorViewer(f, topSplitter));
657 _viewer.push_back(new View3DInventorViewer(f, topSplitter));
658 }
659 else {
660 _viewer.push_back(new View3DInventorViewer(topSplitter));
661 _viewer.push_back(new View3DInventorViewer(topSplitter));
662 }
663
664 for (int i = 2; i < views; i++) {
665 if (glformat) {
666 _viewer.push_back(new View3DInventorViewer(f, botSplitter));
667 }
668 else {
669 _viewer.push_back(new View3DInventorViewer(botSplitter));
670 }
671 }
672
673 topSplitter->setOpaqueResize(true);
674 botSplitter->setOpaqueResize(true);
675 }
676
677 if (smoothing) {
678 for (std::vector<int>::size_type i = 0; i != _viewer.size(); i++) {
679 _viewer[i]->getSoRenderManager()->getGLRenderAction()->setSmoothing(true);
680 }
681 }
682
683 mainSplitter->show();
684 setCentralWidget(mainSplitter);
685
686 setDocumentOfViewers(pcDocument);
687
688 // apply the user settings
689 setupSettings();
690 }
691
692 SplitView3DInventor::~SplitView3DInventor() = default;
693
694 #include "moc_SplitView3DInventor.cpp"