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
30 #include "OCCViewer_ViewFrame.h"
31 #include "OCCViewer_Viewer3d.h"
32 #include "OCCViewer_ViewPort.h"
33 #include "OCCViewer_ViewPort3d.h"
36 #include "QAD_Tools.h"
37 #include "QAD_Desktop.h"
38 #include "QAD_ViewFrame.h"
39 #include "QAD_MessageBox.h"
40 #include "QAD_Application.h"
41 #include "utilities.h"
43 #include "SALOME_Selection.h"
44 #include "SALOME_AISShape.hxx"
47 #include <qapplication.h>
49 // Open CASCADE Include
50 #include <V3d_View.hxx>
51 #include <AIS_ListIteratorOfListOfInteractive.hxx>
57 OCCViewer_ViewFrame::OCCViewer_ViewFrame(QWidget* parent, const QString& title)
58 : QAD_ViewFrame (parent, "")
66 OCCViewer_ViewFrame::OCCViewer_ViewFrame( QWidget* parent )
67 : QAD_ViewFrame (parent, "")
75 OCCViewer_ViewFrame::~OCCViewer_ViewFrame()
83 void OCCViewer_ViewFrame::initialize()
86 myViewer = new OCCViewer_Viewer3d( this );
88 /* enable/disable selection */
89 myViewer->enableSelection( true );
90 myViewer->enableMultipleSelection( true );
96 void OCCViewer_ViewFrame::cleanup()
102 Sets the viewport for this frame
104 void OCCViewer_ViewFrame::setViewPort( OCCViewer_ViewPort* view )
108 setCentralWidget ( myViewPort );
112 Inits the viewport for this frame
114 void OCCViewer_ViewFrame::initViewPort()
116 /* Active Key Event */
119 /* Initial attributes */
120 myViewPort->setCursor( cursor() );
121 myViewPort->setBackgroundColor( backgroundColor() );
123 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)),
124 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
126 /* Listen to my viewport */
127 QAD_ASSERT ( QObject::connect(myViewPort,
128 SIGNAL(vpTransformationStarted (OCCViewer_ViewPort::OperationType)),
130 SIGNAL(vfTransformationStarted(OCCViewer_ViewPort::OperationType))) );
131 QAD_ASSERT ( QObject::connect( myViewPort,
132 SIGNAL(vpTransformationFinished (OCCViewer_ViewPort::OperationType)),
134 SIGNAL(vfTransformationFinished(OCCViewer_ViewPort::OperationType))) );
136 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpDrawExternal (QPainter*)),
137 this, SIGNAL(vfDrawExternal(QPainter*))) );
138 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMousePress (QMouseEvent*)),
139 this, SIGNAL(vfMousePress(QMouseEvent*))) );
140 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseRelease (QMouseEvent*)),
141 this, SIGNAL(vfMouseRelease(QMouseEvent*))) );
142 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseMove (QMouseEvent*)),
143 this, SIGNAL(vfMouseMove(QMouseEvent*))) );
144 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseDoubleClick (QMouseEvent*)),
145 this, SIGNAL(vfMouseDoubleClick(QMouseEvent*))) );
147 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)),
148 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
149 QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyRelease (QKeyEvent*)),
150 this, SIGNAL(vfKeyRelease(QKeyEvent*))) );
154 Returns widget containing 3D-Viewer
156 QWidget* OCCViewer_ViewFrame::getViewWidget()
158 return (QWidget*)getViewPort();
162 Returns the viewport of this frame
164 OCCViewer_ViewPort* OCCViewer_ViewFrame::getViewPort() const
170 Sets the cursor for the viewframe's viewport
172 void OCCViewer_ViewFrame::setCursor( const QCursor& cursor)
175 myViewPort->QWidget::setCursor(cursor);
179 Returns the current cursor
181 QCursor OCCViewer_ViewFrame::cursor() const
184 return myViewPort->cursor();
185 return QMainWindow::cursor();
189 Set background of the viewport
191 void OCCViewer_ViewFrame::setBackgroundColor( const QColor& color)
194 myViewPort->setBackgroundColor(color);
198 Returns background of the viewport
200 QColor OCCViewer_ViewFrame::backgroundColor() const
203 return myViewPort->backgroundColor();
204 return QMainWindow::backgroundColor();
208 Sets the parent viewer for the window
210 void OCCViewer_ViewFrame::setViewer(OCCViewer_Viewer3d* viewer)
216 Returns the parent viewer for the window
218 OCCViewer_Viewer3d* OCCViewer_ViewFrame::getViewer() const
224 Returns 'true' if viewframe is visible
226 void OCCViewer_ViewFrame::setVisible( bool visible )
228 if ( visible == QWidget::isVisible() )
231 if ( visible ) show();
236 Called when viewframe is about to close
238 void OCCViewer_ViewFrame::closeEvent(QCloseEvent* e)
240 emit vfViewClosing(e); /* notify our viewer */
244 Called when viewframe is resized
246 void OCCViewer_ViewFrame::resizeEvent(QResizeEvent* e)
251 void OCCViewer_ViewFrame::keyPressEvent( QKeyEvent *k )
257 Fits all objects in the active view
259 void OCCViewer_ViewFrame::onViewFitAll()
261 myViewPort->fitAll();
265 Fits all obejcts within a rectangular area of the active view
267 void OCCViewer_ViewFrame::onViewFitArea()
269 myViewPort->activateWindowFit();
270 QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_SKETCHAREA") );
274 Moves the active view
276 void OCCViewer_ViewFrame::onViewPan()
278 myViewPort->activatePanning();
282 Zooms the active view
284 void OCCViewer_ViewFrame::onViewZoom()
286 myViewPort->activateZoom();
290 Sets a new center of the active view
292 void OCCViewer_ViewFrame::onViewGlobalPan()
294 myViewPort->activateGlobalPanning();
295 QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_POINTCENTER") );
299 Rotates the active view
301 void OCCViewer_ViewFrame::onViewRotate()
303 myViewPort->activateRotation();
307 Reset the active view
309 void OCCViewer_ViewFrame::onViewReset()
315 Provides front projection of the active view
317 void OCCViewer_ViewFrame::onViewFront()
319 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
320 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xpos);
325 Provides back projection of the active view
327 void OCCViewer_ViewFrame::onViewBack()
329 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
330 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xneg);
335 Provides right projection of the active view
337 void OCCViewer_ViewFrame::onViewRight()
339 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
340 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Yneg);
345 Provides left projection of the active view
347 void OCCViewer_ViewFrame::onViewLeft()
349 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
350 if ( !view3d.IsNull() ) view3d->SetProj (V3d_Ypos);
355 Provides bottom projection of the active view
357 void OCCViewer_ViewFrame::onViewBottom()
359 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
360 if ( !view3d.IsNull() ) view3d->SetProj(V3d_Zneg);
365 Provides top projection of the active view
367 void OCCViewer_ViewFrame::onViewTop()
369 Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
370 if ( !view3d.IsNull() ) view3d->SetProj( V3d_Zpos );
375 Display/hide Trihedron
377 void OCCViewer_ViewFrame::onViewTrihedron()
379 if (!myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() ))
380 myViewer->getAISContext()->Display( myViewer->getTrihedron() );
382 myViewer->getAISContext()->Erase( myViewer->getTrihedron() );
385 void OCCViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName )
387 myViewer->rename(IObject, newName);
390 void OCCViewer_ViewFrame::unHighlightAll()
392 myViewer->unHighlightAll();
395 void OCCViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject, bool highlight, bool update )
397 myViewer->highlight(IObject, highlight, update);
400 bool OCCViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject )
402 return myViewer->isInViewer( IObject );
405 bool OCCViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject )
407 return myViewer->isVisible( IObject );
410 void OCCViewer_ViewFrame::setPopupServer( QAD_Application* App )
412 myViewer->setPopupServer( App );
415 void OCCViewer_ViewFrame::undo(SALOMEDS::Study_var aStudy,
416 const char* StudyFrameEntry)
418 AIS_ListOfInteractive List1;
419 myViewer->getAISContext()->ObjectsInCollector(List1);
420 AIS_ListIteratorOfListOfInteractive ite1(List1);
421 while (ite1.More()) {
422 if (ite1.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
423 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite1.Value());
424 if ( aSh->hasIO() ) {
425 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
426 if ( IO->hasEntry() ) {
427 if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry))
428 myViewer->getAISContext()->Display(aSh);
435 AIS_ListOfInteractive List;
436 myViewer->getAISContext()->DisplayedObjects(List);
437 AIS_ListIteratorOfListOfInteractive ite(List);
439 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
440 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
441 if ( aSh->hasIO() ) {
442 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
443 if ( IO->hasEntry() ) {
444 if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry ))
445 myViewer->getAISContext()->Erase(aSh,true,true);
453 void OCCViewer_ViewFrame::redo(SALOMEDS::Study_var aStudy,
454 const char* StudyFrameEntry)
456 SALOMEDS::SObject_var RefSO;
457 SALOMEDS::SObject_var SO = aStudy->FindObjectID( StudyFrameEntry );
458 SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(SO);
459 for (; it->More();it->Next()){
460 SALOMEDS::SObject_var CSO= it->Value();
461 if (CSO->ReferencedObject(RefSO)) {
463 AIS_ListOfInteractive List;
464 myViewer->getAISContext()->ObjectsInCollector(List);
465 AIS_ListIteratorOfListOfInteractive ite(List);
467 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
468 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
469 if ( aSh->hasIO() ) {
470 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
471 if ( IO->hasEntry() ) {
472 if ( strcmp(IO->getEntry(),RefSO->GetID()) == 0 )
473 myViewer->getAISContext()->Display(aSh, false);
483 AIS_ListOfInteractive List1;
484 myViewer->getAISContext()->DisplayedObjects(List1);
485 AIS_ListIteratorOfListOfInteractive ite1(List1);
486 while (ite1.More()) {
487 if (ite1.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
488 Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite1.Value());
489 if ( aSh->hasIO() ) {
490 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
491 if ( IO->hasEntry() ) {
492 if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry ))
493 myViewer->getAISContext()->Erase(aSh,false,true);
502 void OCCViewer_ViewFrame::SetTrihedronSize( int dim )
504 myViewer->setTrihedronSize( dim );
508 Handle(SALOME_InteractiveObject) OCCViewer_ViewFrame::FindIObject(const char* Entry)
510 Handle(SALOME_InteractiveObject) IO;
511 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
513 AIS_ListOfInteractive List;
514 ic->DisplayedObjects(List);
515 AIS_ListOfInteractive List1;
516 ic->ObjectsInCollector(List1);
519 AIS_ListIteratorOfListOfInteractive ite(List);
521 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
522 Handle(SALOME_AISShape) aSh
523 = Handle(SALOME_AISShape)::DownCast(ite.Value());
524 if ( aSh->hasIO() ) {
526 if ( IO->hasEntry() ) {
527 if ( strcmp( IO->getEntry(), Entry ) == 0 ) {
528 MESSAGE ( "IO found")
536 MESSAGE ( "IO not found")
541 void OCCViewer_ViewFrame::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
543 QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
544 SALOME_Selection* Sel
545 = SALOME_Selection::Selection( myStudy->getSelection() );
547 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
549 AIS_ListOfInteractive List;
550 ic->DisplayedObjects(List);
551 AIS_ListOfInteractive List1;
552 ic->ObjectsInCollector(List1);
555 AIS_ListIteratorOfListOfInteractive ite(List);
557 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
558 Handle(SALOME_AISShape) aSh
559 = Handle(SALOME_AISShape)::DownCast(ite.Value());
560 if ( aSh->hasIO() ) {
561 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
562 if ( IO->isSame( IObject ) ) {
563 ic->Display(aSh, false);
564 // ic->AddOrRemoveCurrentObject(aSh, false); repeated in Viewer3d after next statement
565 Sel->AddIObject(IO, false);
576 void OCCViewer_ViewFrame::DisplayOnly(const Handle(SALOME_InteractiveObject)& IObject)
578 QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
579 SALOME_Selection* Sel
580 = SALOME_Selection::Selection( myStudy->getSelection() );
582 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
584 AIS_ListOfInteractive List;
585 ic->DisplayedObjects(List);
586 AIS_ListOfInteractive List1;
587 ic->ObjectsInCollector(List1);
590 AIS_ListIteratorOfListOfInteractive ite(List);
592 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
593 Handle(SALOME_AISShape) aSh
594 = Handle(SALOME_AISShape)::DownCast(ite.Value());
595 if ( aSh->hasIO() ) {
596 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
597 if ( !IO->isSame( IObject ) ) {
598 ic->Erase(aSh, false);
599 Sel->RemoveIObject(IO, false);
601 ic->Display(aSh, false);
602 Sel->AddIObject(IO, false);
610 void OCCViewer_ViewFrame::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
612 QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
613 SALOME_Selection* Sel
614 = SALOME_Selection::Selection( myStudy->getSelection() );
616 Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
618 AIS_ListOfInteractive List;
619 ic->DisplayedObjects(List);
620 AIS_ListIteratorOfListOfInteractive ite(List);
623 if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
625 Handle(SALOME_AISShape) aSh
626 = Handle(SALOME_AISShape)::DownCast(ite.Value());
629 Handle(SALOME_InteractiveObject) IO = aSh->getIO();
630 if ( IO->isSame( IObject ) )
632 ic->Erase(aSh, false);
633 Sel->RemoveIObject(IO, false);
644 void OCCViewer_ViewFrame::DisplayAll()
646 myViewer->getAISContext()->DisplayAll(Standard_False,Standard_True);
648 void OCCViewer_ViewFrame::EraseAll()
650 Standard_Boolean isTrihedronDisplayed =
651 myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() );
652 myViewer->getAISContext()->EraseAll();
653 if (isTrihedronDisplayed)
654 myViewer->getAISContext()->Display( myViewer->getTrihedron() );
661 void OCCViewer_ViewFrame::Repaint(){
662 myViewer->getViewer3d()->Update();