1 // SALOME OCCViewer : build OCC Viewer into Salome desktop
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
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 GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : OCCViewer_ViewFrame.cxx
25 // Author : Nicolas REJNERI
29 #include "OCCViewer_ViewFrame.h"
30 #include "OCCViewer_Viewer3d.h"
31 #include "OCCViewer_ViewPort.h"
32 #include "OCCViewer_ViewPort3d.h"
35 #include "QAD_Tools.h"
36 #include "QAD_Desktop.h"
37 #include "QAD_ViewFrame.h"
38 #include "QAD_MessageBox.h"
39 #include "QAD_Application.h"
40 #include "utilities.h"
42 #include "QAD_Config.h"
43 #include "QAD_Settings.h"
44 #include "SALOME_Selection.h"
45 #include "SALOME_AISShape.hxx"
48 #include <qapplication.h>
50 // Open CASCADE Include
51 #include <V3d_View.hxx>
52 #include <AIS_ListIteratorOfListOfInteractive.hxx>
53 #include <Visual3d_View.hxx>
59 OCCViewer_ViewFrame::OCCViewer_ViewFrame(QWidget* parent, const QString& title)
60 : QAD_ViewFrame (parent, "")
68 OCCViewer_ViewFrame::OCCViewer_ViewFrame( QWidget* parent )
69 : QAD_ViewFrame (parent, "")
77 OCCViewer_ViewFrame::~OCCViewer_ViewFrame()
85 void OCCViewer_ViewFrame::initialize()
88 myViewer = new OCCViewer_Viewer3d( this );
90 /* enable/disable selection */
91 myViewer->enableSelection( true );
92 myViewer->enableMultipleSelection( true );
98 void OCCViewer_ViewFrame::cleanup()
104 Sets the viewport for this frame
106 void OCCViewer_ViewFrame::setViewPort( OCCViewer_ViewPort* view )
110 setCentralWidget ( myViewPort );
114 Inits the viewport for this frame
116 void OCCViewer_ViewFrame::initViewPort()
118 /* Active Key Event */
121 /* Initial attributes */
122 myViewPort->setCursor( cursor() );
123 myViewPort->setBackgroundColor( backgroundColor() );
125 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)),
126 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
128 /* Listen to my viewport */
129 QAD_ASSERT ( QObject::connect(myViewPort,
130 SIGNAL(vpTransformationStarted (OCCViewer_ViewPort::OperationType)),
132 SIGNAL(vfTransformationStarted(OCCViewer_ViewPort::OperationType))) );
133 QAD_ASSERT ( QObject::connect( myViewPort,
134 SIGNAL(vpTransformationFinished (OCCViewer_ViewPort::OperationType)),
136 SIGNAL(vfTransformationFinished(OCCViewer_ViewPort::OperationType))) );
138 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpDrawExternal (QPainter*)),
139 this, SIGNAL(vfDrawExternal(QPainter*))) );
140 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMousePress (QMouseEvent*)),
141 this, SIGNAL(vfMousePress(QMouseEvent*))) );
142 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseRelease (QMouseEvent*)),
143 this, SIGNAL(vfMouseRelease(QMouseEvent*))) );
144 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseMove (QMouseEvent*)),
145 this, SIGNAL(vfMouseMove(QMouseEvent*))) );
146 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseDoubleClick (QMouseEvent*)),
147 this, SIGNAL(vfMouseDoubleClick(QMouseEvent*))) );
149 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)),
150 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
151 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyRelease (QKeyEvent*)),
152 this, SIGNAL(vfKeyRelease(QKeyEvent*))) );
156 Returns widget containing 3D-Viewer
158 QWidget* OCCViewer_ViewFrame::getViewWidget()
160 return (QWidget*)getViewPort();
164 Returns the viewport of this frame
166 OCCViewer_ViewPort* OCCViewer_ViewFrame::getViewPort() const
172 Sets the cursor for the viewframe's viewport
174 void OCCViewer_ViewFrame::setCursor( const QCursor& cursor)
177 myViewPort->QWidget::setCursor(cursor);
181 Returns the current cursor
183 QCursor OCCViewer_ViewFrame::cursor() const
186 return myViewPort->cursor();
187 return QMainWindow::cursor();
191 Set background of the viewport
193 void OCCViewer_ViewFrame::setBackgroundColor( const QColor& color)
196 myViewPort->setBackgroundColor(color);
200 Returns background of the viewport
202 QColor OCCViewer_ViewFrame::backgroundColor() const
205 return myViewPort->backgroundColor();
206 return QMainWindow::backgroundColor();
210 Sets the parent viewer for the window
212 void OCCViewer_ViewFrame::setViewer(OCCViewer_Viewer3d* viewer)
218 Returns the parent viewer for the window
220 OCCViewer_Viewer3d* OCCViewer_ViewFrame::getViewer() const
226 Returns 'true' if viewframe is visible
228 void OCCViewer_ViewFrame::setVisible( bool visible )
230 if ( visible == QWidget::isVisible() )
233 if ( visible ) show();
238 Called when viewframe is about to close
240 void OCCViewer_ViewFrame::closeEvent(QCloseEvent* e)
242 emit vfViewClosing(e); /* notify our viewer */
246 Called when viewframe is resized
248 void OCCViewer_ViewFrame::resizeEvent(QResizeEvent* e)
253 void OCCViewer_ViewFrame::keyPressEvent( QKeyEvent *k )
259 Fits all objects in the active view
261 void OCCViewer_ViewFrame::onViewFitAll()
263 myViewPort->fitAll();
267 Fits all obejcts within a rectangular area of the active view
269 void OCCViewer_ViewFrame::onViewFitArea()
271 myViewPort->activateWindowFit();
272 QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_SKETCHAREA") );
276 Moves the active view
278 void OCCViewer_ViewFrame::onViewPan()
280 myViewPort->activatePanning();
284 Zooms the active view
286 void OCCViewer_ViewFrame::onViewZoom()
288 myViewPort->activateZoom();
292 Sets a new center of the active view
294 void OCCViewer_ViewFrame::onViewGlobalPan()
296 myViewPort->activateGlobalPanning();
297 QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_POINTCENTER") );
301 Rotates the active view
303 void OCCViewer_ViewFrame::onViewRotate()
305 myViewPort->activateRotation();
309 Reset the active view
311 void OCCViewer_ViewFrame::onViewReset()
317 Provides front projection of the active view
319 void OCCViewer_ViewFrame::onViewFront()
321 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
322 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xpos);
327 Provides back projection of the active view
329 void OCCViewer_ViewFrame::onViewBack()
331 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
332 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xneg);
337 Provides right projection of the active view
339 void OCCViewer_ViewFrame::onViewRight()
341 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
342 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Yneg);
347 Provides left projection of the active view
349 void OCCViewer_ViewFrame::onViewLeft()
351 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
352 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Ypos);
357 Provides bottom projection of the active view
359 void OCCViewer_ViewFrame::onViewBottom()
361 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
362 if ( !view3d.IsNull() ) view3d->SetProj(V3d_Zneg);
367 Provides top projection of the active view
369 void OCCViewer_ViewFrame::onViewTop()
371 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
372 if ( !view3d.IsNull() ) view3d->SetProj( V3d_Zpos );
377 Display/hide Trihedron
379 void OCCViewer_ViewFrame::onViewTrihedron()
381 if (!myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() ))
382 myViewer->getAISContext()->Display( myViewer->getTrihedron() );
384 myViewer->getAISContext()->Erase( myViewer->getTrihedron() );
388 void OCCViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName )
390 myViewer->rename(IObject, newName);
393 void OCCViewer_ViewFrame::unHighlightAll()
395 myViewer->unHighlightAll();
398 void OCCViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject, bool highlight, bool update )
400 myViewer->highlight(IObject, highlight, update);
403 bool OCCViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject )
405 return myViewer->isInViewer( IObject );
408 bool OCCViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject )
410 return myViewer->isVisible( IObject );
413 void OCCViewer_ViewFrame::setPopupServer( QAD_Application* App )
415 myViewer->setPopupServer( App );
418 void OCCViewer_ViewFrame::undo(SALOMEDS::Study_var aStudy,
419 const char* StudyFrameEntry)
421 AIS_ListOfInteractive List1;
422 myViewer->getAISContext()->ObjectsInCollector(List1);
423 AIS_ListIteratorOfListOfInteractive ite1(List1);
424 while (ite1.More()) {
425 if (ite1.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
426 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite1.Value());
427 if ( aSh->hasIO() ) {
428 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
429 if ( IO->hasEntry() ) {
430 if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry))
431 myViewer->getAISContext()->Display(aSh);
438 AIS_ListOfInteractive List;
439 myViewer->getAISContext()->DisplayedObjects(List);
440 AIS_ListIteratorOfListOfInteractive ite(List);
442 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
443 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
444 if ( aSh->hasIO() ) {
445 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
446 if ( IO->hasEntry() ) {
447 if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry ))
448 myViewer->getAISContext()->Erase(aSh,true,true);
456 void OCCViewer_ViewFrame::redo(SALOMEDS::Study_var aStudy,
457 const char* StudyFrameEntry)
459 SALOMEDS::SObject_var RefSO;
460 SALOMEDS::SObject_var SO = aStudy->FindObjectID( StudyFrameEntry );
461 SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(SO);
462 for (; it->More();it->Next()){
463 SALOMEDS::SObject_var CSO= it->Value();
464 if (CSO->ReferencedObject(RefSO)) {
466 AIS_ListOfInteractive List;
467 myViewer->getAISContext()->ObjectsInCollector(List);
468 AIS_ListIteratorOfListOfInteractive ite(List);
470 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
471 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
472 if ( aSh->hasIO() ) {
473 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
474 if ( IO->hasEntry() ) {
475 if ( strcmp(IO->getEntry(),RefSO->GetID()) == 0 )
476 myViewer->getAISContext()->Display(aSh, false);
486 AIS_ListOfInteractive List1;
487 myViewer->getAISContext()->DisplayedObjects(List1);
488 AIS_ListIteratorOfListOfInteractive ite1(List1);
489 while (ite1.More()) {
490 if (ite1.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
491 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite1.Value());
492 if ( aSh->hasIO() ) {
493 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
494 if ( IO->hasEntry() ) {
495 if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry ))
496 myViewer->getAISContext()->Erase(aSh,false,true);
506 Handle(SALOME_InteractiveObject) OCCViewer_ViewFrame::FindIObject(const char* Entry)
508 Handle(SALOME_InteractiveObject) IO;
509 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
511 AIS_ListOfInteractive List;
512 ic->DisplayedObjects(List);
513 AIS_ListOfInteractive List1;
514 ic->ObjectsInCollector(List1);
517 AIS_ListIteratorOfListOfInteractive ite(List);
519 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
520 Handle(SALOME_AISShape) aSh
521 = Handle(SALOME_AISShape)::DownCast(ite.Value());
522 if ( aSh->hasIO() ) {
524 if ( IO->hasEntry() ) {
525 if ( strcmp( IO->getEntry(), Entry ) == 0 ) {
526 MESSAGE ( "IO found")
534 MESSAGE ( "IO not found")
539 void OCCViewer_ViewFrame::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
541 QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
542 SALOME_Selection* Sel
543 = SALOME_Selection::Selection( myStudy->getSelection() );
545 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
547 AIS_ListOfInteractive List;
548 ic->DisplayedObjects(List);
549 AIS_ListOfInteractive List1;
550 ic->ObjectsInCollector(List1);
553 AIS_ListIteratorOfListOfInteractive ite(List);
555 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
556 Handle(SALOME_AISShape) aSh
557 = Handle(SALOME_AISShape)::DownCast(ite.Value());
558 if ( aSh->hasIO() ) {
559 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
560 if ( IO->isSame( IObject ) ) {
561 ic->Display(aSh, false);
562 // ic->AddOrRemoveCurrentObject(aSh, false); repeated in Viewer3d after next statement
563 Sel->AddIObject(IO, false);
574 void OCCViewer_ViewFrame::DisplayOnly(const Handle(SALOME_InteractiveObject)& IObject)
576 QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
577 SALOME_Selection* Sel
578 = SALOME_Selection::Selection( myStudy->getSelection() );
580 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
582 AIS_ListOfInteractive List;
583 ic->DisplayedObjects(List);
584 AIS_ListOfInteractive List1;
585 ic->ObjectsInCollector(List1);
588 AIS_ListIteratorOfListOfInteractive ite(List);
590 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
591 Handle(SALOME_AISShape) aSh
592 = Handle(SALOME_AISShape)::DownCast(ite.Value());
593 if ( aSh->hasIO() ) {
594 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
595 if ( !IO->isSame( IObject ) ) {
596 ic->Erase(aSh, false);
597 Sel->RemoveIObject(IO, false);
599 ic->Display(aSh, false);
600 Sel->AddIObject(IO, false);
608 void OCCViewer_ViewFrame::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
610 QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
611 SALOME_Selection* Sel
612 = SALOME_Selection::Selection( myStudy->getSelection() );
614 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
616 AIS_ListOfInteractive List;
617 ic->DisplayedObjects(List);
618 AIS_ListIteratorOfListOfInteractive ite(List);
621 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
623 Handle(SALOME_AISShape) aSh
624 = Handle(SALOME_AISShape)::DownCast(ite.Value());
627 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
628 if ( IO->isSame( IObject ) )
630 ic->Erase(aSh, false);
631 Sel->RemoveIObject(IO, false);
642 void OCCViewer_ViewFrame::DisplayAll()
644 myViewer->getAISContext()->DisplayAll(Standard_False,Standard_True);
646 void OCCViewer_ViewFrame::EraseAll()
648 Standard_Boolean isTrihedronDisplayed =
649 myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() );
650 myViewer->getAISContext()->EraseAll();
651 if (isTrihedronDisplayed)
652 myViewer->getAISContext()->Display( myViewer->getTrihedron() );
659 void OCCViewer_ViewFrame::Repaint()
662 myViewer->getViewer3d()->Update();
665 void OCCViewer_ViewFrame::onAdjustTrihedron()
667 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
669 if (!ic->IsDisplayed(myViewer->getTrihedron()))
673 AIS_ListOfInteractive List;
674 ic->Erase( myViewer->getTrihedron() );
675 ic->DisplayedObjects(List);
676 ic->Display( myViewer->getTrihedron() );
679 myViewer->setTrihedronSize(100);
684 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
686 if (!view3d.IsNull())
688 double Xmin=0, Ymin=0, Zmin=0, Xmax=0, Ymax=0, Zmax=0;
692 view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
694 if (Xmin==RealFirst() || Ymin==RealFirst() || Zmin==RealFirst() ||
695 Xmax==RealLast() || Ymax==RealLast() || Zmax==RealLast())
698 aMaxSide = Xmax - Xmin;
699 if (aMaxSide < Ymax -Ymin) aMaxSide = Ymax -Ymin;
700 if (aMaxSide < Zmax -Zmin) aMaxSide = Zmax -Zmin;
702 static float aSizeInPercents = 105;
703 QString aSetting = QAD_CONFIG->getSetting("Viewer:TrihedronSize");
704 if (!aSetting.isEmpty()) aSizeInPercents = aSetting.toFloat();
706 static float EPS = 5.0E-3;
707 float aSize = myViewer->getTrihedron()->Size();
708 float aNewSize = aMaxSide*aSizeInPercents/100.0;
709 // if the new trihedron size have sufficient difference, then apply the value
710 if(fabs(aNewSize-aSize) > aSize*EPS || fabs(aNewSize-aSize) > aNewSize*EPS)
711 myViewer->setTrihedronSize(aNewSize);