Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / OCCViewer / OCCViewer_ViewFrame.cxx
1 //  SALOME OCCViewer : build OCC Viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
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 GNU 
14 //  Lesser General Public License for more details. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : OCCViewer_ViewFrame.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "OCCViewer_ViewFrame.h"
30 #include "OCCViewer_Viewer3d.h"
31 #include "OCCViewer_ViewPort.h"
32 #include "OCCViewer_ViewPort3d.h"
33 #include "OCCViewer_Prs.h"
34
35 #include "QAD.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"
42
43 #include "QAD_Config.h"
44 #include "QAD_Settings.h"
45 #include "SALOME_Selection.h"
46 #include "SALOME_AISShape.hxx"
47 #include "SALOMEGUI.h"
48 #include "SALOMEDS_Tool.hxx"
49 #include "ToolsGUI.h"
50
51 // QT Include
52 #include <qapplication.h>
53
54 // Open CASCADE Include
55 #include <V3d_View.hxx>
56 #include <AIS_ListIteratorOfListOfInteractive.hxx>
57 #include <Visual3d_View.hxx>
58
59 // IDL headers
60 #include <SALOMEconfig.h>
61 #include CORBA_SERVER_HEADER(SALOMEDS)
62 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
63
64 using namespace std;
65
66 //=======================================================================
67 // name    : getMapOfEntry
68 // Purpose : Convert list of interactive objects in map <entry <--> interactive object>
69 //=======================================================================
70 static void getMapOfEntry( const AIS_ListOfInteractive&                    theList,
71                            QMap< QString, Handle(AIS_InteractiveObject) >& theMap )
72 {
73   AIS_ListIteratorOfListOfInteractive anIter( theList );
74   for ( ; anIter.More(); anIter.Next() )
75   {
76     Handle(SALOME_InteractiveObject) anIO =
77       Handle(SALOME_InteractiveObject)::DownCast( anIter.Value() );
78     if ( !anIO.IsNull() )
79       theMap[ anIO->getEntry() ] = anIter.Value();
80   }
81 }
82
83
84 /*
85   Class       : OCCViewer_ViewFrame
86   Description : View frame for Open CASCADE view
87 */
88
89
90 //=======================================================================
91 // name    : OCCViewer_ViewFrame
92 // Purpose : Constructor
93 //=======================================================================
94 OCCViewer_ViewFrame::OCCViewer_ViewFrame( QWidget* parent, const QString& title ) 
95   : QAD_ViewFrame ( parent, "" )
96 {
97   initialize();
98 }
99
100 //=======================================================================
101 // name    : OCCViewer_ViewFrame
102 // Purpose : Constructor
103 //=======================================================================
104 OCCViewer_ViewFrame::OCCViewer_ViewFrame( QWidget* parent ) 
105   : QAD_ViewFrame (parent, "")
106 {
107   initialize();
108 }
109
110 //=======================================================================
111 // name    : ~OCCViewer_ViewFrame
112 // Purpose : Destructor
113 //=======================================================================
114 OCCViewer_ViewFrame::~OCCViewer_ViewFrame()
115 {
116   cleanup();
117 }
118
119 //=======================================================================
120 // name    : initialize
121 // Purpose : Initialize view frame (called from constructors )
122 //=======================================================================
123 void OCCViewer_ViewFrame::initialize()
124 {
125   myViewPort = NULL;
126   myViewer   = new OCCViewer_Viewer3d( this );
127   
128   /* enable/disable selection */
129   myViewer->enableSelection( true );
130   myViewer->enableMultipleSelection( true );
131 }
132
133 //=======================================================================
134 // name    : cleanup
135 // Purpose : Cleanup viewframe 
136 //=======================================================================
137 void OCCViewer_ViewFrame::cleanup()
138 {
139
140
141 //=======================================================================
142 // name    : setViewPort
143 // Purpose : Sets the viewport for this frame
144 //=======================================================================
145 void OCCViewer_ViewFrame::setViewPort( OCCViewer_ViewPort* view )
146 {
147   myViewPort = view;
148   initViewPort();               
149   setCentralWidget ( myViewPort );
150 }
151
152 //=======================================================================
153 // name    : initViewPort
154 // Purpose : Inits the viewport for this frame
155 //=======================================================================
156 void OCCViewer_ViewFrame::initViewPort()
157 {
158   /* Active Key Event */
159   
160   setFocus();
161
162   /* Initial attributes */
163   
164   myViewPort->setCursor( cursor() );
165   myViewPort->setBackgroundColor( backgroundColor() );
166   
167   QAD_ASSERT( QObject::connect(myViewPort, SIGNAL( vpKeyPress ( QKeyEvent* ) ), 
168               this, SIGNAL(vfKeyPress(QKeyEvent*))) );
169
170   /*  Listen to my viewport */
171   
172   QAD_ASSERT( connect( myViewPort, SIGNAL( vpTransformationStarted( OCCViewer_ViewPort::OperationType ) ), 
173                        this, SIGNAL( vfTransformationStarted( OCCViewer_ViewPort::OperationType ) ) ) );
174   QAD_ASSERT( connect( myViewPort, SIGNAL( vpTransformationFinished ( OCCViewer_ViewPort::OperationType ) ),
175                        this, SIGNAL( vfTransformationFinished( OCCViewer_ViewPort::OperationType ) ) ) );
176
177   QAD_ASSERT( connect( myViewPort, SIGNAL( vpDrawExternal( QPainter* ) ), 
178                        this, SIGNAL( vfDrawExternal( QPainter* ) ) ) ); 
179   QAD_ASSERT( connect( myViewPort, SIGNAL( vpMousePress( QMouseEvent* ) ), 
180                        this, SIGNAL( vfMousePress( QMouseEvent* ) ) ) );
181   QAD_ASSERT( connect( myViewPort, SIGNAL( vpMouseRelease( QMouseEvent* ) ),
182                        this, SIGNAL( vfMouseRelease( QMouseEvent* ) ) ) );
183   QAD_ASSERT( connect( myViewPort, SIGNAL( vpMouseMove( QMouseEvent* ) ), 
184                        this, SIGNAL( vfMouseMove( QMouseEvent* ) ) ) );
185   QAD_ASSERT( connect( myViewPort, SIGNAL( vpMouseDoubleClick( QMouseEvent* ) ), 
186                        this, SIGNAL( vfMouseDoubleClick( QMouseEvent* ) ) ) );
187
188   QAD_ASSERT( connect( myViewPort, SIGNAL( vpKeyPress( QKeyEvent* ) ), 
189                        this, SIGNAL( vfKeyPress( QKeyEvent* ) ) ) );
190   QAD_ASSERT( connect( myViewPort, SIGNAL( vpKeyRelease( QKeyEvent* ) ),
191                        this, SIGNAL( vfKeyRelease( QKeyEvent* ) ) ) );
192 }
193
194 //=======================================================================
195 // name    : getViewWidget
196 // Purpose : Returns widget containing 3D-Viewer
197 //=======================================================================
198 QWidget* OCCViewer_ViewFrame::getViewWidget() 
199 {
200   return (QWidget*)getViewPort();
201 }
202
203 //=======================================================================
204 // name    : getViewPort
205 // Purpose : Returns the viewport of this frame 
206 //=======================================================================
207 OCCViewer_ViewPort* OCCViewer_ViewFrame::getViewPort() const
208 {
209   return myViewPort;
210 }
211
212 //=======================================================================
213 // name    : Sets the cursor for the viewframe's viewport
214 // Purpose : setCursor
215 //=======================================================================
216 void OCCViewer_ViewFrame::setCursor( const QCursor& cursor)
217 {
218   if ( myViewPort ) 
219     myViewPort->QWidget::setCursor( cursor );
220 }
221
222
223 //=======================================================================
224 // name    : cursor
225 // Purpose : Returns the current cursor 
226 //=======================================================================
227 QCursor OCCViewer_ViewFrame::cursor() const
228 {
229   if ( myViewPort ) 
230     return myViewPort->cursor();
231   return QMainWindow::cursor();
232 }
233
234 //=======================================================================
235 // name    : setBackgroundColor
236 // Purpose : Set background of the viewport
237 //=======================================================================
238 void OCCViewer_ViewFrame::setBackgroundColor( const QColor& color)
239 {
240   if ( myViewPort )
241     myViewPort->setBackgroundColor( color );
242 }
243
244 //=======================================================================
245 // name    : backgroundColor
246 // Purpose : Returns background of the viewport
247 //=======================================================================
248 QColor OCCViewer_ViewFrame::backgroundColor() const
249 {
250   if ( myViewPort )
251     return myViewPort->backgroundColor();
252   return QMainWindow::backgroundColor();
253 }
254
255 //=======================================================================
256 // name    : setViewer
257 // Purpose : Sets the parent viewer for the window
258 //=======================================================================
259 void OCCViewer_ViewFrame::setViewer( OCCViewer_Viewer3d* viewer )
260 {
261   myViewer = viewer;
262 }
263
264 //=======================================================================
265 // name    : getViewer
266 // Purpose : Returns the parent viewer for the window
267 //=======================================================================
268 OCCViewer_Viewer3d* OCCViewer_ViewFrame::getViewer() const
269 {
270   return myViewer;
271 }
272
273 //=======================================================================
274 // name    : setVisible
275 // Purpose : Show/hide view
276 //=======================================================================
277 void OCCViewer_ViewFrame::setVisible( const bool visible )
278 {
279   if ( visible == QWidget::isVisible() )
280     return;
281   
282   if ( visible )
283     show();
284   else
285     hide();
286 }
287
288 //=======================================================================
289 // name    : closeEvent
290 // Purpose : Called when viewframe is about to close
291 //=======================================================================
292 void OCCViewer_ViewFrame::closeEvent( QCloseEvent* e )
293 {
294   emit vfViewClosing( e );  /* notify our viewer */
295 }
296
297 //=======================================================================
298 // name    : resizeEvent
299 // Purpose : Called when viewframe is resized 
300 //=======================================================================
301 void OCCViewer_ViewFrame::resizeEvent( QResizeEvent* e )
302 {
303   emit vfResize( e );
304 }
305
306 //=======================================================================
307 // name    : keyPressEvent
308 // Purpose : Called when key is pressed
309 //=======================================================================
310 void OCCViewer_ViewFrame::keyPressEvent( QKeyEvent *k )
311 {
312   emit vfKeyPress(k);
313 }
314
315 //=======================================================================
316 // name    : onViewFitAll
317 // Purpose : Fits all objects in the active view
318 //=======================================================================
319 void OCCViewer_ViewFrame::onViewFitAll()
320 {
321   myViewPort->fitAll();
322 }
323
324 //=======================================================================
325 // name    : onViewFitArea
326 // Purpose : Fits all obejcts within a rectangular area of the active view
327 //=======================================================================
328 void OCCViewer_ViewFrame::onViewFitArea()
329 {
330   myViewPort->activateWindowFit();
331   QAD_Application::getDesktop()->putInfo( tr( "PRP_VW3D_SKETCHAREA" ) );
332 }
333
334 //=======================================================================
335 // name    : onViewPan
336 // Purpose : Moves the active view
337 //=======================================================================
338 void OCCViewer_ViewFrame::onViewPan()
339 {
340   myViewPort->activatePanning();
341 }
342
343 //=======================================================================
344 // name    : onViewZoom
345 // Purpose : Zooms the active view
346 //=======================================================================
347 void OCCViewer_ViewFrame::onViewZoom()
348 {
349   myViewPort->activateZoom();
350 }
351
352 //=======================================================================
353 // name    : onViewGlobalPan
354 // Purpose : Sets a new center of the active view
355 //=======================================================================
356 void OCCViewer_ViewFrame::onViewGlobalPan()
357 {
358   myViewPort->activateGlobalPanning();
359   QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_POINTCENTER") );
360 }
361
362 //=======================================================================
363 // name    : onViewRotate
364 // Purpose : Rotates the active view
365 //=======================================================================
366 void OCCViewer_ViewFrame::onViewRotate()
367 {
368   myViewPort->activateRotation();
369 }
370
371 //=======================================================================
372 // name    : onViewReset
373 // Purpose : Reset the active view
374 //=======================================================================
375 void OCCViewer_ViewFrame::onViewReset()
376 {
377   myViewPort->reset();
378 }
379
380 //=======================================================================
381 // name    : onViewFront
382 // Purpose : Provides front projection of the active view
383 //=======================================================================
384 void OCCViewer_ViewFrame::onViewFront()
385 {
386   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
387   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xpos);
388   onViewFitAll();
389 }
390
391 //=======================================================================
392 // name    : onViewBack
393 // Purpose : Provides back projection of the active view
394 //=======================================================================
395 void OCCViewer_ViewFrame::onViewBack()
396 {
397   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
398   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xneg);
399   onViewFitAll();
400 }
401
402 //=======================================================================
403 // name    : onViewRight
404 // Purpose : Provides right projection of the active view
405 //=======================================================================
406 void OCCViewer_ViewFrame::onViewRight()
407 {
408   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
409   if ( !view3d.IsNull() )
410     view3d->SetProj( V3d_Ypos );
411   onViewFitAll();
412 }
413
414 //=======================================================================
415 // name    : onViewLeft
416 // Purpose : Provides left projection of the active view
417 //=======================================================================
418 void OCCViewer_ViewFrame::onViewLeft()
419 {
420   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
421   if ( !view3d.IsNull() )
422     view3d->SetProj( V3d_Yneg );
423   onViewFitAll();
424 }
425
426 //=======================================================================
427 // name    : onViewBottom
428 // Purpose : Provides bottom projection of the active view
429 //=======================================================================
430 void OCCViewer_ViewFrame::onViewBottom()
431 {
432   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
433   if ( !view3d.IsNull() ) view3d->SetProj(V3d_Zneg);
434   onViewFitAll();
435 }
436
437 //=======================================================================
438 // name    : onViewTop
439 // Purpose : Provides top projection of the active view
440 //=======================================================================
441 void OCCViewer_ViewFrame::onViewTop()
442 {
443   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
444   if ( !view3d.IsNull() ) view3d->SetProj( V3d_Zpos );
445   onViewFitAll();
446 }
447
448 //=======================================================================
449 // name    : onViewTrihedron
450 // Purpose : Display/hide Trihedron
451 //=======================================================================
452 void OCCViewer_ViewFrame::onViewTrihedron()
453 {
454   if (!myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() ))
455     myViewer->getAISContext()->Display( myViewer->getTrihedron() );
456   else
457     myViewer->getAISContext()->Erase( myViewer->getTrihedron() );
458   onAdjustTrihedron();
459 }
460
461 //=======================================================================
462 // name    : rename
463 // Purpose : Rename entry object
464 //=======================================================================
465 void OCCViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject,
466                                   const QString newName )
467 {
468   myViewer->rename(IObject, newName);
469 }
470
471 //=======================================================================
472 // name    : unHighlightAll
473 // Purpose : Unhighlight all objects
474 //=======================================================================
475 void OCCViewer_ViewFrame::unHighlightAll() 
476 {
477   myViewer->unHighlightAll();
478 }
479
480 //=======================================================================
481 // name    : highlight
482 // Purpose : Highlight object
483 //=======================================================================
484 void OCCViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject,
485                                      const bool highlight,
486                                      const bool update ) 
487 {
488   myViewer->highlight( IObject, highlight, update );
489 }
490
491 //=======================================================================
492 // name    : isInViewer
493 // Purpose : Magic function
494 //=======================================================================
495 bool OCCViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
496 {
497   return myViewer->isInViewer( IObject );
498 }
499
500 //=======================================================================
501 // name    : isVisible
502 // Purpose : Verify whether object is visible
503 //=======================================================================
504 bool OCCViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
505 {
506   return myViewer->isVisible( IObject );
507 }
508
509 //=======================================================================
510 // name    : setPopupServer
511 // Purpose : Set popup server
512 //=======================================================================
513 void OCCViewer_ViewFrame::setPopupServer( QAD_Application* App )
514 {
515   myViewer->setPopupServer( App );
516 }
517
518 //=======================================================================
519 // name    : redisplayAll
520 // Purpose : Redisplay all objects of active component in accordance with
521 //           their display flags
522 //=======================================================================
523 void OCCViewer_ViewFrame::redisplayAll( QAD_Study* theQADStudy, const bool theToUpdate ) 
524 {
525   SALOMEDS::Study_var      aStudy     = theQADStudy->getStudyDocument();
526   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
527   SALOMEGUI*               aGUI       = aDesktop->getActiveGUI();
528   const QString&           aCompName  = aDesktop->getComponentDataType();
529   SALOMEDS::SObject_var    aComponent =
530     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
531
532   if ( aComponent->_is_nil() )
533     return;
534
535   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
536   bool isTrhDisplayed = anIC->IsDisplayed( myViewer->getTrihedron() );
537   
538   anIC->DisplayAll( true, false );
539   anIC->EraseAll( false, false );
540
541   if ( isTrhDisplayed )
542     anIC->Display( myViewer->getTrihedron(), false );
543
544   std::list<SALOMEDS::SObject_var> aList;
545   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
546
547   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
548   for ( ; anIter != aList.end(); ++anIter )
549   {
550     SALOMEDS::SObject_var anObj = (*anIter);
551     if ( ToolsGUI::GetVisibility( aStudy, anObj, this ) )
552     {
553       Handle(SALOME_InteractiveObject) anIObj = new SALOME_InteractiveObject();
554       anIObj->setEntry( anObj->GetID() );
555       aGUI->BuildPresentation( anIObj, this );
556     }
557   }
558
559   if ( theToUpdate )
560     Repaint();
561 }
562
563 //=======================================================================
564 // name    : undo
565 // Purpose : Redisplay all objects of active component in accordance with
566 //           their display flags. Called when undo operation is complited
567 //=======================================================================
568 void OCCViewer_ViewFrame::undo( QAD_Study* theQADStudy, const char* )
569 {
570   redisplayAll( theQADStudy );
571 }
572
573 //=======================================================================
574 // name    : redo
575 // Purpose : Redisplay all objects of active component in accordance with
576 //           their display flags. Called when undo operation is complited
577 //=======================================================================
578 void OCCViewer_ViewFrame::redo( QAD_Study* theQADStudy, const char* )
579 {
580   redisplayAll( theQADStudy );
581 }
582
583 //=======================================================================
584 // name    : FindIObject
585 // Purpose : Find in context SALOME_InteractiveObject by entry
586 //=======================================================================
587 Handle(SALOME_InteractiveObject) OCCViewer_ViewFrame::FindIObject( const char* Entry )
588 {
589   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
590       
591   AIS_ListOfInteractive List;
592   ic->DisplayedObjects( List );
593   AIS_ListOfInteractive List1;
594   ic->ObjectsInCollector( List1 );
595   List.Append( List1 );
596
597   AIS_ListIteratorOfListOfInteractive ite( List );
598   for ( ; ite.More(); ite.Next() )
599   {
600     Handle(SALOME_InteractiveObject) anObj =
601       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
602
603     if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), Entry ) == 0 )
604     {
605       MESSAGE ( "IO found")
606       return anObj;
607     }
608   }
609   MESSAGE ( "IO not found")
610   return Handle(SALOME_InteractiveObject)();
611 }
612
613 //=======================================================================
614 // name    : Display
615 // Purpose : Display object
616 //=======================================================================
617 void OCCViewer_ViewFrame::Display( const Handle(SALOME_InteractiveObject)& theIObject,
618                                    bool toUpdate )
619 {
620   if ( theIObject.IsNull() )
621     return;
622
623   QAD_Study*          aQADStudy = QAD_Application::getDesktop()->getActiveStudy();
624   SALOME_Selection*   aSel      = SALOME_Selection::Selection( aQADStudy->getSelection() );
625   SALOMEDS::Study_var aStudy    = aQADStudy->getStudyDocument();
626
627   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
628
629   AIS_ListOfInteractive aList;
630   anIC->ObjectsInCollector( aList );
631   AIS_ListIteratorOfListOfInteractive anIter( aList );
632   for ( ; anIter.More(); anIter.Next() )
633   {
634     Handle(SALOME_InteractiveObject) anObj =
635       Handle(SALOME_InteractiveObject)::DownCast( anIter.Value()->GetOwner() );
636
637     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( theIObject ) )
638     {
639       anIC->Display( anIter.Value(), false );
640       aSel->AddIObject( anObj, false );
641       ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
642       break;
643     }
644   }
645   
646   if ( toUpdate )
647     Repaint();
648 }
649
650 //=======================================================================
651 // name    : DisplayOnly
652 // Purpose : Display object and erase all other ones
653 //=======================================================================
654 void OCCViewer_ViewFrame::DisplayOnly( const Handle(SALOME_InteractiveObject)& theIO )
655 {
656   EraseAll();
657   Display( theIO );
658 }
659
660 //=======================================================================
661 // name    : Erase
662 // Purpose : Erase object
663 //=======================================================================
664 void OCCViewer_ViewFrame::Erase( const Handle(SALOME_InteractiveObject)& theIObject,
665                                  bool                                    toUpdate )
666 {
667   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
668   SALOME_Selection* aSel = SALOME_Selection::Selection( aStudy->getSelection() );
669
670   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
671   
672   AIS_ListOfInteractive aList;
673   anIC->DisplayedObjects( aList );
674   
675   AIS_ListIteratorOfListOfInteractive ite( aList );
676   for ( ; ite.More(); ite.Next() )
677   {
678     Handle(SALOME_InteractiveObject) anObj =
679       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
680
681     if ( !anObj.IsNull() && anObj->hasEntry() )
682     {
683       if ( anObj->isSame( theIObject ) )
684       {
685         anIC->Erase( ite.Value(), false );
686         aSel->RemoveIObject( anObj, false );
687         ToolsGUI::SetVisibility( aStudy->getStudyDocument(), anObj->getEntry(), false, this );
688         break;
689       }
690     }
691   }
692
693   if ( toUpdate )
694     Repaint();
695 }
696
697 //=======================================================================
698 // name    : DisplayAll
699 // Purpose : Display all objects of active component
700 //=======================================================================
701 void OCCViewer_ViewFrame::DisplayAll()
702 {
703   SALOMEDS::Study_var      aStudy     = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
704   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
705   SALOMEGUI*               aGUI       = aDesktop->getActiveGUI();
706   const QString&           aCompName  = aDesktop->getComponentDataType();
707   SALOMEDS::SObject_var    aComponent =
708     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
709
710   if ( aComponent->_is_nil() )
711     return;
712
713   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
714
715   AIS_ListOfInteractive aDispList;
716   AIS_ListOfInteractive aCollList;
717   anIC->DisplayedObjects( aDispList );
718   anIC->ObjectsInCollector( aCollList );
719
720   QMap< QString, Handle(AIS_InteractiveObject) > aDispMap;
721   QMap< QString, Handle(AIS_InteractiveObject) > aCollMap;
722   getMapOfEntry( aDispList, aDispMap );
723   getMapOfEntry( aCollList, aCollMap );
724
725   std::list<SALOMEDS::SObject_var> aList;
726   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
727
728   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
729   for ( ; anIter != aList.end(); ++anIter )
730   {
731     SALOMEDS::SObject_var anObj = (*anIter);
732     if ( anObj->_is_nil() )
733       continue;
734
735     const char* aEntry = anObj->GetID();
736     if ( aCollMap.contains( aEntry ) )
737     {
738       anIC->DisplayFromCollector( aCollMap[ aEntry ], false );
739       ToolsGUI::SetVisibility( aStudy, aEntry, true, this );
740     }
741     else if ( !aDispMap.contains( aEntry ) )
742     {
743       Handle(SALOME_InteractiveObject) anIObj = new SALOME_InteractiveObject();
744       anIObj->setEntry( anObj->GetID() );
745       aGUI->BuildPresentation( anIObj, this );
746     }
747   }
748
749   Repaint();
750 }
751
752 //=======================================================================
753 // name    : EraseAll
754 // Purpose : Erase all objects
755 //=======================================================================
756 void OCCViewer_ViewFrame::EraseAll()
757 {
758   SALOMEDS::Study_var aStudy =
759     QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
760
761   Standard_Boolean isTrihedronDisplayed =
762     myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() );
763
764   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
765
766   AIS_ListOfInteractive aList;
767   anIC->DisplayedObjects( aList );
768   AIS_ListIteratorOfListOfInteractive anIter( aList );
769   for ( ; anIter.More(); anIter.Next() )
770   {
771     if ( anIC->IsDisplayed( myViewer->getTrihedron() ) &&
772          anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) )
773       continue;
774
775     Handle(AIS_InteractiveObject) anIO = anIter.Value();
776     anIC->Erase( anIO, false, true );
777
778     Handle(SALOME_InteractiveObject) anObj =
779       Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
780
781     if ( !anObj.IsNull() && anObj->hasEntry() )
782       ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), false, this );
783   }
784
785   if ( isTrihedronDisplayed )
786     myViewer->getAISContext()->Display( myViewer->getTrihedron() );
787   else
788     Repaint();
789 }
790
791 //=======================================================================
792 // name    : Repaint
793 // Purpose : Uodate view
794 //=======================================================================
795 void OCCViewer_ViewFrame::Repaint()
796 {
797   onAdjustTrihedron();
798   myViewer->getViewer3d()->Update();
799 }
800
801 //=======================================================================
802 // name    : getTrihedronSize
803 // Purpose : Get new and current trihedron size corresponding to the
804 //           current model size
805 //=======================================================================
806 bool OCCViewer_ViewFrame::getTrihedronSize( double& theNewSize, double& theSize )
807 {
808   theNewSize = 100;
809   theSize = 100;
810
811   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
812
813   if ( view3d.IsNull() )
814     return false;
815
816   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
817   double aMaxSide;
818
819   view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
820
821   if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
822        Xmax == RealLast()  || Ymax == RealLast()  || Zmax == RealLast() )
823     return false;
824
825   aMaxSide = Xmax - Xmin;
826   if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
827   if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
828
829   static float aSizeInPercents = 105;
830   QString aSetting = QAD_CONFIG->getSetting("Viewer:TrihedronSize");
831   if (!aSetting.isEmpty())
832     aSizeInPercents = aSetting.toFloat();
833
834   static float EPS = 5.0E-3;
835   theSize = myViewer->getTrihedron()->Size();
836   theNewSize = aMaxSide*aSizeInPercents / 100.0;
837
838   return fabs( theNewSize - theSize ) > theSize * EPS ||
839          fabs( theNewSize - theSize) > theNewSize * EPS;
840 }
841
842 //=======================================================================
843 // name    : AdjustTrihedrons
844 // Purpose : Adjust trihedron size in accordance with size of model
845 //=======================================================================
846 void OCCViewer_ViewFrame::AdjustTrihedrons( const bool forced )
847 {
848   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
849
850   if ( !myViewer->isTrihedronDisplayed() )
851     return;
852   else
853   {
854     AIS_ListOfInteractive List;
855     ic->DisplayedObjects(List);
856     if ( List.First() == List.Last() && List.First() == myViewer->getTrihedron() )
857     {
858       myViewer->setTrihedronSize( 100 );
859       return;
860     }
861   }
862
863   double aNewSize = 100, aSize = 100;
864   if ( getTrihedronSize( aNewSize, aSize ) || forced )
865     myViewer->setTrihedronSize( aNewSize );
866 }
867
868 //=======================================================================
869 // name    : onAdjustTrihedron
870 // Purpose : Slot. Called when trihedrons must be resized
871 //=======================================================================
872 void OCCViewer_ViewFrame::onAdjustTrihedron()
873 {
874   AdjustTrihedrons( false );
875 }
876
877 //=======================================================================
878 // name    : Display
879 // Purpose : Display presentation
880 //=======================================================================
881 void OCCViewer_ViewFrame::Display( const SALOME_OCCPrs* prs )
882 {
883   // try do downcast object
884   const OCCViewer_Prs* anOCCPrs = dynamic_cast<const OCCViewer_Prs*>( prs );
885   if ( !anOCCPrs || anOCCPrs->IsNull() )
886     return;
887
888   // get context
889   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
890   // get all displayed objects
891   AIS_ListOfInteractive List;
892   ic->DisplayedObjects( List );
893   // get objects in the collector
894   AIS_ListOfInteractive ListCollector;
895   ic->ObjectsInCollector( ListCollector );
896
897   // get objects to be displayed
898   AIS_ListOfInteractive anAISObjects;
899   anOCCPrs->GetObjects( anAISObjects );
900
901   AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
902   for ( ; aIter.More(); aIter.Next() )
903   {
904     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
905     if ( !anAIS.IsNull() )
906     {
907       // try to find presentation in the viewer
908       bool bDisplayed = false;
909       AIS_ListIteratorOfListOfInteractive ite( List );
910       for ( ; ite.More(); ite.Next() )
911       {
912         // compare presentations by handles
913         // if the object is already displayed - nothing to do more
914         if ( ite.Value() == anAIS )
915         {
916           // Deactivate object if necessary
917           if ( !anOCCPrs->ToActivate() )
918             ic->Deactivate( anAIS );
919           bDisplayed = true;
920           break;
921         }
922       }
923
924       if ( bDisplayed )
925         continue;
926
927       // then try to find presentation in the collector
928       bDisplayed = false;
929       ite.Initialize( ListCollector );
930       for ( ; ite.More(); ite.Next() )
931       {
932         // compare presentations by handles
933         // if the object is in collector - display it
934         if ( ite.Value() == anAIS )
935         {
936           ic->DisplayFromCollector( anAIS, false );
937
938           // Deactivate object if necessary
939           if ( !anOCCPrs->ToActivate() )
940             ic->Deactivate( anAIS );
941           bDisplayed = true;
942
943           // Set visibility flag
944           Handle(SALOME_InteractiveObject) anObj =
945             Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
946           if ( !anObj.IsNull() && anObj->hasEntry() )
947           {
948             SALOMEDS::Study_var aStudy =
949               QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
950             ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
951           }
952
953           break;
954         }
955       }
956       if ( bDisplayed )
957         continue;
958
959       // if object is not displayed and not found in the collector - display it
960       if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
961       {
962         Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
963         double aNewSize = 100, aSize = 100;
964         getTrihedronSize( aNewSize, aSize );
965         aTrh->SetSize( aTrh == myViewer->getTrihedron() ? aNewSize : 0.5 * aNewSize );
966       }
967
968       ic->Display( anAIS, false );
969
970       // Set visibility flag
971       Handle(SALOME_InteractiveObject) anObj =
972         Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
973       if ( !anObj.IsNull() && anObj->hasEntry() )
974       {
975         SALOMEDS::Study_var aStudy =
976           QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
977         ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
978       }
979
980       // Deactivate object if necessary
981       if ( !anOCCPrs->ToActivate() )
982         ic->Deactivate( anAIS );
983     }
984   }
985 }
986
987 //=======================================================================
988 // name    : Erase
989 // Purpose : Erase presentation
990 //=======================================================================
991 void OCCViewer_ViewFrame::Erase( const SALOME_OCCPrs* prs, const bool forced )
992 {
993   // try do downcast object
994   const OCCViewer_Prs* anOCCPrs = dynamic_cast<const OCCViewer_Prs*>( prs );
995   if ( !anOCCPrs || anOCCPrs->IsNull() )
996     return;
997
998   // get context
999   Handle(AIS_InteractiveContext) ic = myViewer->getAISContext();
1000
1001   // get objects to be erased
1002   AIS_ListOfInteractive anAISObjects;
1003   anOCCPrs->GetObjects( anAISObjects );
1004
1005   AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
1006   for ( ; aIter.More(); aIter.Next() ) {
1007     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
1008     if ( !anAIS.IsNull() ) {
1009       // erase the object from context : move it to collector
1010       ic->Erase( anAIS, false, forced ? false : true );
1011
1012       // Set visibility flag if necessary
1013       if ( !forced )
1014       {
1015         Handle(SALOME_InteractiveObject) anObj =
1016           Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
1017         if ( !anObj.IsNull() && anObj->hasEntry() )
1018         {
1019           SALOMEDS::Study_var aStudy =
1020             QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
1021           ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
1022         }
1023       }
1024     }
1025   }
1026 }
1027
1028 //=======================================================================
1029 // name    : CreatePrs
1030 // Purpose : Create presentation corresponding to the entry
1031 //=======================================================================
1032 SALOME_Prs* OCCViewer_ViewFrame::CreatePrs( const char* entry )
1033 {
1034   OCCViewer_Prs* prs = new OCCViewer_Prs();
1035   if ( entry )
1036   {
1037     // get context
1038     Handle(AIS_InteractiveContext) ic = myViewer->getAISContext();
1039
1040     // get displayed objects
1041     AIS_ListOfInteractive List;
1042     ic->DisplayedObjects( List );
1043     // get objects in the collector
1044     AIS_ListOfInteractive ListCollector;
1045     ic->ObjectsInCollector( ListCollector );
1046     List.Append( ListCollector );
1047
1048     AIS_ListIteratorOfListOfInteractive ite( List );
1049     for ( ; ite.More(); ite.Next() )
1050     {
1051       Handle(SALOME_InteractiveObject) anObj =
1052         Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
1053
1054       if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
1055         prs->AddObject( ite.Value() );
1056     }
1057   }
1058   return prs;
1059 }
1060
1061 //=======================================================================
1062 // name    : LocalSelection
1063 // Purpose : Activates selection of sub shapes
1064 //=======================================================================
1065 void OCCViewer_ViewFrame::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
1066 {
1067   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
1068   
1069   const OCCViewer_Prs* anOCCPrs = dynamic_cast<const OCCViewer_Prs*>( thePrs );
1070   if ( anIC.IsNull() )
1071     return;
1072   
1073   // Open local context if there is no one
1074   bool allObjects = thePrs == 0 || thePrs->IsNull();
1075   if ( !anIC->HasOpenedContext() ) {
1076     anIC->ClearCurrents( false );
1077     anIC->OpenLocalContext( allObjects, true, true );
1078   }
1079
1080   AIS_ListOfInteractive anObjs;
1081   // Get objects to be activated
1082   if ( allObjects ) 
1083     anIC->DisplayedObjects( anObjs );
1084   else
1085     anOCCPrs->GetObjects( anObjs );
1086
1087   // Activate selection of objects from prs
1088   AIS_ListIteratorOfListOfInteractive aIter( anObjs );
1089   for ( ; aIter.More(); aIter.Next() ) {
1090     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
1091     if ( !anAIS.IsNull() )
1092     {
1093       if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
1094       {
1095         anIC->Load( anAIS, -1, false );
1096         anIC->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
1097       }
1098       else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
1099       {
1100         anIC->Load( anAIS, -1, false );
1101         anIC->Activate( anAIS, theMode );
1102       }
1103     }
1104   }
1105 }
1106
1107 //=======================================================================
1108 // name    : GlobalSelection
1109 // Purpose : Deactivates selection of sub shapes
1110 //=======================================================================
1111 void OCCViewer_ViewFrame::GlobalSelection( const bool update ) const
1112 {
1113   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
1114   if ( !anIC.IsNull() )
1115     anIC->CloseAllContexts( false );
1116   if ( update )
1117     anIC->CurrentViewer()->Redraw();
1118 }
1119
1120 //=======================================================================
1121 // name    : BeforeDisplay
1122 // Purpose : Axiluary method called before displaying of objects
1123 //=======================================================================
1124 void  OCCViewer_ViewFrame::BeforeDisplay( SALOME_Displayer* d )
1125 {
1126   d->BeforeDisplay( this, SALOME_OCCViewType() );
1127 }
1128
1129 //=======================================================================
1130 // name    : AfterDisplay
1131 // Purpose : Axiluary method called after displaying of objects
1132 //=======================================================================
1133 void OCCViewer_ViewFrame::AfterDisplay( SALOME_Displayer* d )
1134 {
1135   d->AfterDisplay( this, SALOME_OCCViewType() );
1136 }
1137
1138 #define INCREMENT_FOR_OP 10
1139
1140 //=======================================================================
1141 // name    : onPanLeft
1142 // Purpose : Performs incremental panning to the left
1143 //=======================================================================
1144 void OCCViewer_ViewFrame::onPanLeft()
1145 {
1146   myViewPort->incrementalPan( -INCREMENT_FOR_OP, 0 );
1147 }
1148
1149 //=======================================================================
1150 // name    : onPanRight
1151 // Purpose : Performs incremental panning to the right
1152 //=======================================================================
1153 void OCCViewer_ViewFrame::onPanRight()
1154 {
1155   myViewPort->incrementalPan( INCREMENT_FOR_OP, 0 );
1156 }
1157
1158 //=======================================================================
1159 // name    : onPanUp
1160 // Purpose : Performs incremental panning to the top
1161 //=======================================================================
1162 void OCCViewer_ViewFrame::onPanUp()
1163 {
1164   myViewPort->incrementalPan( 0, INCREMENT_FOR_OP );
1165 }
1166
1167 //=======================================================================
1168 // name    : onPanDown
1169 // Purpose : Performs incremental panning to the bottom
1170 //=======================================================================
1171 void OCCViewer_ViewFrame::onPanDown()
1172 {
1173   myViewPort->incrementalPan( 0, -INCREMENT_FOR_OP );
1174 }
1175
1176 //=======================================================================
1177 // name    : onZoomIn
1178 // Purpose : Performs incremental zooming in
1179 //=======================================================================
1180 void OCCViewer_ViewFrame::onZoomIn()
1181 {
1182   myViewPort->incrementalZoom( INCREMENT_FOR_OP );
1183 }
1184
1185 //=======================================================================
1186 // name    : onZoomOut
1187 // Purpose : Performs incremental zooming out
1188 //=======================================================================
1189 void OCCViewer_ViewFrame::onZoomOut()
1190 {
1191   myViewPort->incrementalZoom( -INCREMENT_FOR_OP );
1192 }
1193
1194 //=======================================================================
1195 // name    : onRotateLeft
1196 // Purpose : Performs incremental rotating to the left
1197 //=======================================================================
1198 void OCCViewer_ViewFrame::onRotateLeft()
1199 {
1200   myViewPort->incrementalRotate( -INCREMENT_FOR_OP, 0 );
1201 }
1202
1203 //=======================================================================
1204 // name    : onRotateRight
1205 // Purpose : Performs incremental rotating to the right
1206 //=======================================================================
1207 void OCCViewer_ViewFrame::onRotateRight()
1208 {
1209   myViewPort->incrementalRotate( INCREMENT_FOR_OP, 0 );
1210 }
1211
1212 //=======================================================================
1213 // name    : onRotateUp
1214 // Purpose : Performs incremental rotating to the top
1215 //=======================================================================
1216 void OCCViewer_ViewFrame::onRotateUp()
1217 {
1218   myViewPort->incrementalRotate( 0, -INCREMENT_FOR_OP );
1219 }
1220
1221 //=======================================================================
1222 // name    : onRotateDown
1223 // Purpose : Performs incremental rotating to the bottom
1224 //=======================================================================
1225 void OCCViewer_ViewFrame::onRotateDown()
1226 {
1227   myViewPort->incrementalRotate( 0, INCREMENT_FOR_OP );
1228 }