]> SALOME platform Git repositories - modules/kernel.git/blob - src/OCCViewer/OCCViewer_ViewFrame.cxx
Salome HOME
PR: mergefrom_BR_CCRT_11Nov04
[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
48 // QT Include
49 #include <qapplication.h>
50
51 // Open CASCADE Include
52 #include <V3d_View.hxx>
53 #include <AIS_ListIteratorOfListOfInteractive.hxx>
54 #include <Visual3d_View.hxx>
55 using namespace std;
56
57 /*!
58     Constructor
59 */
60 OCCViewer_ViewFrame::OCCViewer_ViewFrame(QWidget* parent, const QString& title) 
61   : QAD_ViewFrame (parent, "")
62 {
63   initialize();
64 }
65
66 /*!
67     Constructor
68 */
69 OCCViewer_ViewFrame::OCCViewer_ViewFrame( QWidget* parent ) 
70   : QAD_ViewFrame (parent, "")
71 {
72   initialize();
73 }
74
75 /*!
76     Destructor
77 */
78 OCCViewer_ViewFrame::~OCCViewer_ViewFrame()
79 {
80   cleanup();
81 }
82
83 /*!
84     Init viewframe
85 */
86 void OCCViewer_ViewFrame::initialize()
87 {
88   myViewPort = NULL;
89   myViewer   = new OCCViewer_Viewer3d( this );
90   
91   /* enable/disable selection */
92   myViewer->enableSelection( true );
93   myViewer->enableMultipleSelection( true );
94 }
95
96 /*!
97     Cleanup viewframe 
98 */
99 void OCCViewer_ViewFrame::cleanup()
100 {
101   
102
103
104 /*!
105     Sets the viewport for this frame
106 */
107 void OCCViewer_ViewFrame::setViewPort( OCCViewer_ViewPort* view )
108 {
109   myViewPort = view;
110   initViewPort();               
111   setCentralWidget ( myViewPort );
112 }
113
114 /*!
115     Inits the viewport for this frame
116 */
117 void OCCViewer_ViewFrame::initViewPort()
118 {
119   /* Active Key Event */
120   setFocus();
121
122   /* Initial attributes */
123   myViewPort->setCursor( cursor() );
124   myViewPort->setBackgroundColor( backgroundColor() );
125   
126   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)), 
127                                 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
128
129   /*  Listen to my viewport */  
130   QAD_ASSERT ( QObject::connect(myViewPort, 
131                                 SIGNAL(vpTransformationStarted (OCCViewer_ViewPort::OperationType)), 
132                                 this, 
133                                 SIGNAL(vfTransformationStarted(OCCViewer_ViewPort::OperationType))) );
134   QAD_ASSERT ( QObject::connect( myViewPort, 
135                                  SIGNAL(vpTransformationFinished (OCCViewer_ViewPort::OperationType)),
136                                  this, 
137                                  SIGNAL(vfTransformationFinished(OCCViewer_ViewPort::OperationType))) );
138
139   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpDrawExternal (QPainter*)), 
140                                 this, SIGNAL(vfDrawExternal(QPainter*))) );     
141   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMousePress (QMouseEvent*)), 
142                                 this, SIGNAL(vfMousePress(QMouseEvent*))) );
143   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseRelease (QMouseEvent*)), 
144                                 this, SIGNAL(vfMouseRelease(QMouseEvent*))) );
145   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseMove (QMouseEvent*)), 
146                                 this, SIGNAL(vfMouseMove(QMouseEvent*))) );
147   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseDoubleClick (QMouseEvent*)), 
148                                 this, SIGNAL(vfMouseDoubleClick(QMouseEvent*))) );
149
150   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)), 
151                                 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
152   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyRelease (QKeyEvent*)), 
153                                 this, SIGNAL(vfKeyRelease(QKeyEvent*))) );
154 }
155
156 /*!
157   Returns widget containing 3D-Viewer
158 */
159 QWidget* OCCViewer_ViewFrame::getViewWidget() 
160 {
161   return (QWidget*)getViewPort();
162 }
163
164 /*!
165     Returns the viewport of this frame  
166 */
167 OCCViewer_ViewPort* OCCViewer_ViewFrame::getViewPort() const
168 {
169   return myViewPort;
170 }
171
172 /*!
173     Sets the cursor for the viewframe's viewport
174 */
175 void OCCViewer_ViewFrame::setCursor( const QCursor& cursor)
176 {
177   if ( myViewPort ) 
178     myViewPort->QWidget::setCursor(cursor);
179 }
180
181 /*!
182     Returns the current cursor 
183 */
184 QCursor OCCViewer_ViewFrame::cursor() const
185 {
186   if ( myViewPort ) 
187     return myViewPort->cursor();
188   return QMainWindow::cursor();
189 }
190
191 /*!
192     Set background of the viewport
193 */
194 void OCCViewer_ViewFrame::setBackgroundColor( const QColor& color)
195 {
196   if ( myViewPort )
197     myViewPort->setBackgroundColor(color);
198 }
199
200 /*!
201     Returns background of the viewport
202 */
203 QColor OCCViewer_ViewFrame::backgroundColor() const
204 {
205   if ( myViewPort )
206     return myViewPort->backgroundColor();
207   return QMainWindow::backgroundColor();
208 }
209
210 /*!
211     Sets the parent viewer for the window
212 */
213 void OCCViewer_ViewFrame::setViewer(OCCViewer_Viewer3d* viewer)
214 {
215   myViewer = viewer;
216 }
217
218 /*!
219   Returns the parent viewer for the window
220 */
221 OCCViewer_Viewer3d* OCCViewer_ViewFrame::getViewer() const
222 {
223   return myViewer;
224 }
225
226 /*!
227     Returns 'true' if viewframe is visible
228 */
229 void OCCViewer_ViewFrame::setVisible( bool visible )
230 {
231   if ( visible == QWidget::isVisible() )
232     return;
233   
234   if ( visible ) show();
235   else hide();
236 }
237
238 /*!
239    Called when viewframe is about to close
240 */
241 void OCCViewer_ViewFrame::closeEvent(QCloseEvent* e)
242 {
243   emit vfViewClosing(e);  /* notify our viewer */
244 }
245
246 /*!
247    Called when viewframe is resized 
248 */
249 void OCCViewer_ViewFrame::resizeEvent(QResizeEvent* e)
250 {
251   emit vfResize(e);     
252 }
253
254 void OCCViewer_ViewFrame::keyPressEvent( QKeyEvent *k )
255 {
256   emit vfKeyPress(k);
257 }
258
259 /*!
260     Fits all objects in the active view
261 */
262 void OCCViewer_ViewFrame::onViewFitAll()
263 {
264   myViewPort->fitAll();
265 }
266
267 /*!
268     Fits all obejcts within a rectangular area of the active view
269 */
270 void OCCViewer_ViewFrame::onViewFitArea()
271 {
272   myViewPort->activateWindowFit();
273   QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_SKETCHAREA") );
274 }
275
276 /*!
277     Moves the active view
278 */
279 void OCCViewer_ViewFrame::onViewPan()
280 {
281   myViewPort->activatePanning();
282 }
283
284 /*!
285     Zooms the active view
286 */
287 void OCCViewer_ViewFrame::onViewZoom()
288 {
289   myViewPort->activateZoom();
290 }
291
292 /*!
293     Sets a new center of the active view
294 */
295 void OCCViewer_ViewFrame::onViewGlobalPan()
296 {
297   myViewPort->activateGlobalPanning();
298   QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_POINTCENTER") );
299 }
300
301 /*!
302     Rotates the active view
303 */
304 void OCCViewer_ViewFrame::onViewRotate()
305 {
306   myViewPort->activateRotation();
307 }
308
309 /*!
310     Reset the active view
311 */
312 void OCCViewer_ViewFrame::onViewReset()
313 {
314   myViewPort->reset();
315 }
316
317 /*!
318     Provides front projection of the active view
319 */
320 void OCCViewer_ViewFrame::onViewFront()
321 {
322   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
323   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xpos);
324   onViewFitAll();
325 }
326
327 /*!
328     Provides back projection of the active view
329 */
330 void OCCViewer_ViewFrame::onViewBack()
331 {
332   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
333   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xneg);
334   onViewFitAll();
335 }
336
337 /*!
338     Provides right projection of the active view
339 */
340 void OCCViewer_ViewFrame::onViewRight()
341 {
342   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
343   if ( !view3d.IsNull() )
344     view3d->SetProj( V3d_Ypos );
345   onViewFitAll();
346 }
347
348 /*!
349     Provides left projection of the active view
350 */
351 void OCCViewer_ViewFrame::onViewLeft()
352 {
353   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
354   if ( !view3d.IsNull() )
355     view3d->SetProj( V3d_Yneg );
356   onViewFitAll();
357 }
358
359 /*!
360     Provides bottom projection of the active view
361 */
362 void OCCViewer_ViewFrame::onViewBottom()
363 {
364   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
365   if ( !view3d.IsNull() ) view3d->SetProj(V3d_Zneg);
366   onViewFitAll();
367 }
368
369 /*!
370     Provides top projection of the active view
371 */
372 void OCCViewer_ViewFrame::onViewTop()
373 {
374   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
375   if ( !view3d.IsNull() ) view3d->SetProj( V3d_Zpos );
376   onViewFitAll();
377 }
378
379 /*!
380   Display/hide Trihedron
381 */
382 void OCCViewer_ViewFrame::onViewTrihedron()
383 {
384   if (!myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() ))
385     myViewer->getAISContext()->Display( myViewer->getTrihedron() );
386   else
387     myViewer->getAISContext()->Erase( myViewer->getTrihedron() );
388   onAdjustTrihedron();
389 }
390
391 void OCCViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName )
392 {
393   myViewer->rename(IObject, newName);
394 }
395
396 void OCCViewer_ViewFrame::unHighlightAll() 
397 {
398   myViewer->unHighlightAll();
399 }
400
401 void OCCViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject, bool highlight, bool update ) 
402 {
403   myViewer->highlight(IObject, highlight, update);
404 }
405
406 bool OCCViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
407 {
408   return myViewer->isInViewer( IObject );
409 }
410
411 bool OCCViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
412 {
413   return myViewer->isVisible( IObject );
414 }
415
416 void OCCViewer_ViewFrame::setPopupServer( QAD_Application* App )
417 {
418   myViewer->setPopupServer( App );
419 }
420
421 void OCCViewer_ViewFrame::undo(QAD_Study* theStudy, const char* StudyFrameEntry)
422 {
423   SALOMEDS::Study_var aStudy = theStudy->getStudyDocument();
424   AIS_ListOfInteractive List1;
425   myViewer->getAISContext()->ObjectsInCollector(List1);
426   AIS_ListIteratorOfListOfInteractive ite1(List1);
427   for( ; ite1.More(); ite1.Next() )
428   {
429     Handle(SALOME_InteractiveObject) anObj =
430       Handle(SALOME_InteractiveObject)::DownCast( ite1.Value()->GetOwner() );
431       
432     if ( !anObj.IsNull() &&
433           anObj->hasEntry() &&
434           theStudy->isInViewer( anObj->getEntry(), StudyFrameEntry) )
435     {
436       myViewer->getAISContext()->Display( ite1.Value() );
437     }
438   }
439   
440   AIS_ListOfInteractive List;
441   myViewer->getAISContext()->DisplayedObjects(List);
442   AIS_ListIteratorOfListOfInteractive ite(List);
443   for ( ; ite.More(); ite.Next() )
444   {
445     Handle(SALOME_InteractiveObject) anObj =
446       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
447
448     if ( !anObj.IsNull() &&
449           anObj->hasEntry() &&
450           theStudy->isInViewer( anObj->getEntry(), StudyFrameEntry) )
451     {
452       myViewer->getAISContext()->Erase( ite.Value(), true, true ); 
453     }
454   }
455 }
456
457 void OCCViewer_ViewFrame::redo(QAD_Study* theStudy, const char* StudyFrameEntry)
458 {
459   SALOMEDS::Study_var aStudy = theStudy->getStudyDocument();
460   SALOMEDS::SObject_var RefSO;
461   SALOMEDS::SObject_var SO = aStudy->FindObjectID( StudyFrameEntry );
462   SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(SO);
463   for (; it->More();it->Next()){
464     SALOMEDS::SObject_var CSO= it->Value();
465     if (CSO->ReferencedObject(RefSO))
466     {
467       AIS_ListOfInteractive List;
468       myViewer->getAISContext()->ObjectsInCollector(List);
469       
470       AIS_ListIteratorOfListOfInteractive ite(List);
471       for ( ; ite.More(); ite.Next() )
472       {
473         Handle(SALOME_InteractiveObject) anObj =
474           Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
475           
476         if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), RefSO->GetID() ) == 0 )
477           myViewer->getAISContext()->Display( ite.Value(), false );
478       }
479     }
480   }
481   
482   AIS_ListOfInteractive List1;
483   myViewer->getAISContext()->DisplayedObjects(List1);
484   AIS_ListIteratorOfListOfInteractive ite1(List1);
485   for ( ; ite1.More(); ite1.Next() )
486   {
487     Handle(SALOME_InteractiveObject) anObj =
488       Handle(SALOME_InteractiveObject)::DownCast( ite1.Value()->GetOwner() );
489
490     if ( !anObj.IsNull() && anObj->hasEntry() && !theStudy->isInViewer( anObj->getEntry(), StudyFrameEntry ) )
491       myViewer->getAISContext()->Erase( ite1.Value(), false, true );
492   }
493   
494   Repaint();
495 }
496
497 /* selection */
498 Handle(SALOME_InteractiveObject) OCCViewer_ViewFrame::FindIObject(const char* Entry)
499 {
500   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
501       
502   AIS_ListOfInteractive List;
503   ic->DisplayedObjects(List);
504   AIS_ListOfInteractive List1;
505   ic->ObjectsInCollector(List1);
506   List.Append(List1);
507
508   AIS_ListIteratorOfListOfInteractive ite(List);
509   for ( ; ite.More(); ite.Next() )
510   {
511     Handle(SALOME_InteractiveObject) anObj =
512       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
513
514     if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), Entry ) == 0 )
515     {
516       MESSAGE ( "IO found")
517       return anObj;
518     }
519   }
520   MESSAGE ( "IO not found")
521   return Handle(SALOME_InteractiveObject)();
522 }
523
524 /* display */           
525 void OCCViewer_ViewFrame::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
526 {
527   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
528   SALOME_Selection* Sel
529     = SALOME_Selection::Selection( myStudy->getSelection() );
530
531   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
532
533   AIS_ListOfInteractive List;
534   ic->DisplayedObjects(List);
535   AIS_ListOfInteractive List1;
536   ic->ObjectsInCollector(List1);
537   List.Append(List1);
538   
539   AIS_ListIteratorOfListOfInteractive ite( List );
540   for ( ; ite.More(); ite.Next() )
541   {
542     Handle(SALOME_InteractiveObject) anObj =
543       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
544
545     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( IObject ) )
546     {
547       ic->Display( ite.Value(), false );
548       Sel->AddIObject( anObj, false );
549       break;
550     }
551   }
552   
553   if ( update )
554     Repaint();
555 }
556
557 void OCCViewer_ViewFrame::DisplayOnly(const Handle(SALOME_InteractiveObject)& IObject)
558 {
559   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
560   SALOME_Selection* Sel
561     = SALOME_Selection::Selection( myStudy->getSelection() );
562
563   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
564   
565   AIS_ListOfInteractive List;
566   ic->DisplayedObjects(List);
567   AIS_ListOfInteractive List1;
568   ic->ObjectsInCollector(List1);
569   List.Append(List1);
570   
571   AIS_ListIteratorOfListOfInteractive ite( List );
572   for ( ; ite.More(); ite.Next() )
573   {
574     Handle(SALOME_InteractiveObject) anObj =
575       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
576
577     if ( !anObj.IsNull() && anObj->hasEntry() )
578     {
579       if ( !anObj->isSame( IObject ) )
580       {
581         ic->Erase( ite.Value(), false );
582         Sel->RemoveIObject( anObj, false );
583       }
584       else
585       {
586         ic->Display( ite.Value(), false );
587         Sel->AddIObject( anObj, false );
588       }
589     }
590   }
591   
592   Repaint();
593 }
594 void OCCViewer_ViewFrame::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
595 {
596   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
597   SALOME_Selection* Sel
598     = SALOME_Selection::Selection( myStudy->getSelection() );
599
600   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
601   
602   AIS_ListOfInteractive List;
603   ic->DisplayedObjects(List);
604   
605   AIS_ListIteratorOfListOfInteractive ite( List );
606   for ( ; ite.More(); ite.Next() )
607   {
608     Handle(SALOME_InteractiveObject) anObj =
609       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
610
611     if ( !anObj.IsNull() && anObj->hasEntry() )
612     {
613       if ( anObj->isSame( IObject ) )
614       {
615         ic->Erase( ite.Value(), false );
616         Sel->RemoveIObject( anObj, false );
617         break;
618       }
619     }
620   }
621   
622   if ( update )
623     Repaint();
624 }
625
626 void OCCViewer_ViewFrame::DisplayAll()
627 {
628   myViewer->getAISContext()->DisplayAll(Standard_False,Standard_True);
629 }
630 void OCCViewer_ViewFrame::EraseAll()
631 {
632   Standard_Boolean isTrihedronDisplayed = 
633     myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() );
634   myViewer->getAISContext()->EraseAll();
635   if (isTrihedronDisplayed)
636     myViewer->getAISContext()->Display( myViewer->getTrihedron() );
637   else
638     Repaint();
639 }
640
641
642
643 void OCCViewer_ViewFrame::Repaint()
644 {
645   onAdjustTrihedron();
646   myViewer->getViewer3d()->Update();
647 }
648
649 //==========================================================
650 /*!
651  *  OCCViewer_ViewFrame::getTrihedronSize
652  *  Get new an current trihedron size
653  */
654 //==========================================================
655 bool OCCViewer_ViewFrame::getTrihedronSize( double& theNewSize, double& theSize )
656 {
657   theNewSize = 100;
658   theSize = 100;
659
660   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
661
662   if ( view3d.IsNull() )
663     return false;
664     
665   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
666   double aMaxSide;
667   double aPercents;
668
669   view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
670
671   if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
672        Xmax == RealLast()  || Ymax == RealLast()  || Zmax == RealLast() )
673     return false;
674
675   aMaxSide = Xmax - Xmin;
676   if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
677   if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
678
679   static float aSizeInPercents = 105;
680   QString aSetting = QAD_CONFIG->getSetting("Viewer:TrihedronSize");
681   if (!aSetting.isEmpty())
682     aSizeInPercents = aSetting.toFloat();
683
684   static float EPS = 5.0E-3;
685   theSize = myViewer->getTrihedron()->Size();
686   theNewSize = aMaxSide*aSizeInPercents / 100.0;
687
688   return fabs( theNewSize - theSize ) > theSize * EPS ||
689          fabs( theNewSize - theSize) > theNewSize * EPS;
690 }
691
692 void OCCViewer_ViewFrame::AdjustTrihedrons( const bool forced )
693 {
694   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
695
696   if ( !myViewer->isTrihedronDisplayed() )
697     return;
698   else
699   {
700     AIS_ListOfInteractive List;
701     ic->DisplayedObjects(List);
702     if ( List.First() == List.Last() && List.First() == myViewer->getTrihedron() )
703     {
704       myViewer->setTrihedronSize( 100 );
705       return;
706     }
707   }
708
709   double aNewSize = 100, aSize = 100;
710   if ( getTrihedronSize( aNewSize, aSize ) || forced )
711     myViewer->setTrihedronSize( aNewSize );
712 }
713
714 void OCCViewer_ViewFrame::onAdjustTrihedron()
715 {
716   AdjustTrihedrons( false );
717 }
718
719 //==========================================================
720 /*!
721  *  OCCViewer_ViewFrame::Display
722  *  Display presentation
723  */
724 //==========================================================
725 void OCCViewer_ViewFrame::Display( const SALOME_OCCPrs* prs )
726 {
727   // try do downcast object
728   const OCCViewer_Prs* anOCCPrs = dynamic_cast<const OCCViewer_Prs*>( prs );
729   if ( !anOCCPrs || anOCCPrs->IsNull() )
730     return;
731
732   // get context
733   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
734   // get all displayed objects
735   AIS_ListOfInteractive List;
736   ic->DisplayedObjects( List );
737   // get objects in he collector
738   AIS_ListOfInteractive ListCollector;
739   ic->ObjectsInCollector( ListCollector );
740
741   // get objects to be displayed
742   AIS_ListOfInteractive anAISObjects;
743   anOCCPrs->GetObjects( anAISObjects );
744
745   AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
746   for ( ; aIter.More(); aIter.Next() ) {
747     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
748     if ( !anAIS.IsNull() ) {
749       // try to find presentation in the viewer
750       bool bDisplayed = false;
751       AIS_ListIteratorOfListOfInteractive ite( List );
752       while ( ite.More() ) {
753         // compare presentations by handles
754         // if the object is already displayed - nothing to do more
755         if ( ite.Value() == anAIS ) {
756     
757     // Deactivate object if necessary
758     if ( !anOCCPrs->ToActivate() )
759       ic->Deactivate( anAIS );
760           bDisplayed = true;
761           break;
762         }
763         ite.Next();
764       }
765       if ( bDisplayed )
766         continue;
767       // then try to find presentation in the collector
768       bDisplayed = false;
769       ite.Initialize( ListCollector );
770       while ( ite.More() ) {
771         // compare presentations by handles
772         // if the object is in collector - display it
773         if ( ite.Value() == anAIS ) {
774           ic->DisplayFromCollector( anAIS, false );
775
776   // Deactivate object if necessary
777   if ( !anOCCPrs->ToActivate() )
778       ic->Deactivate( anAIS );
779           bDisplayed = true;
780           break;
781         }
782         ite.Next();
783       }
784       if ( bDisplayed )
785         continue;
786       // if object is not displayed and not found in the collector - display it
787       if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
788       {
789         Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
790         double aNewSize = 100, aSize = 100;
791         getTrihedronSize( aNewSize, aSize );
792         aTrh->SetSize( aTrh == myViewer->getTrihedron() ? aNewSize : 0.5 * aNewSize );
793       }
794       
795       ic->Display( anAIS, false );
796
797       // Deactivate object if necessary
798       if ( !anOCCPrs->ToActivate() )
799         ic->Deactivate( anAIS );
800     }
801   }
802 }
803
804 //==========================================================
805 /*!
806  *  OCCViewer_ViewFrame::Erase
807  *  Erase presentation
808  */
809 //==========================================================
810 void OCCViewer_ViewFrame::Erase( const SALOME_OCCPrs* prs, const bool forced )
811 {
812   // try do downcast object
813   const OCCViewer_Prs* anOCCPrs = dynamic_cast<const OCCViewer_Prs*>( prs );
814   if ( !anOCCPrs || anOCCPrs->IsNull() )
815     return;
816
817   // get context
818   Handle(AIS_InteractiveContext) ic = myViewer->getAISContext();
819
820   // get objects to be erased
821   AIS_ListOfInteractive anAISObjects;
822   anOCCPrs->GetObjects( anAISObjects );
823
824   AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
825   for ( ; aIter.More(); aIter.Next() ) {
826     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
827     if ( !anAIS.IsNull() ) {
828       // erase the object from context : move it to collector
829       ic->Erase( anAIS, false, forced ? false : true );
830     }
831   }
832 }
833
834 //==========================================================
835 /*!
836  *  OCCViewer_ViewFrame::CreatePrs
837  *  Create presentation by entry
838  */
839 //==========================================================
840 SALOME_Prs* OCCViewer_ViewFrame::CreatePrs( const char* entry )
841 {
842   OCCViewer_Prs* prs = new OCCViewer_Prs();
843   if ( entry )
844   {
845     // get context
846     Handle(AIS_InteractiveContext) ic = myViewer->getAISContext();
847
848     // get displayed objects
849     AIS_ListOfInteractive List;
850     ic->DisplayedObjects( List );
851     // get objects in the collector
852     AIS_ListOfInteractive ListCollector;
853     ic->ObjectsInCollector( ListCollector );
854     List.Append( ListCollector );
855
856     AIS_ListIteratorOfListOfInteractive ite( List );
857     for ( ; ite.More(); ite.Next() )
858     {
859       Handle(SALOME_InteractiveObject) anObj =
860         Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
861
862       if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
863         prs->AddObject( ite.Value() );
864     }
865   }
866   return prs;
867 }
868
869 //==========================================================
870 /*!
871  *  OCCViewer_ViewFrame::LocalSelection
872  *  Activates selection of sub shapes
873  */
874 //==========================================================
875 void OCCViewer_ViewFrame::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
876 {
877   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
878   
879   const OCCViewer_Prs* anOCCPrs = dynamic_cast<const OCCViewer_Prs*>( thePrs );
880   if ( anIC.IsNull() )
881     return;
882   
883   // Open local context if there is no one
884   bool allObjects = thePrs == 0 || thePrs->IsNull();
885   if ( !anIC->HasOpenedContext() ) {
886     anIC->ClearCurrents( false );
887     anIC->OpenLocalContext( allObjects, true, true );
888   }
889
890   AIS_ListOfInteractive anObjs;
891   // Get objects to be activated
892   if ( allObjects ) 
893     anIC->DisplayedObjects( anObjs );
894   else
895     anOCCPrs->GetObjects( anObjs );
896
897   // Activate selection of objects from prs
898   AIS_ListIteratorOfListOfInteractive aIter( anObjs );
899   for ( ; aIter.More(); aIter.Next() ) {
900     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
901     if ( !anAIS.IsNull() )
902     {
903       if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
904       {
905         anIC->Load( anAIS, -1, false );
906         anIC->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
907       }
908       else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
909       {
910         anIC->Load( anAIS, -1, false );
911         anIC->Activate( anAIS, theMode );
912       }
913     }
914   }
915 }
916
917 //==========================================================
918 /*!
919  *  OCCViewer_ViewFrame::GlobalSelection
920  *  Deactivates selection of sub shapes
921  */
922 //==========================================================
923 void OCCViewer_ViewFrame::GlobalSelection( const bool update ) const
924 {
925   Handle(AIS_InteractiveContext) anIC = myViewer->getAISContext();
926   if ( !anIC.IsNull() )
927     anIC->CloseAllContexts( false );
928   if ( update )
929     anIC->CurrentViewer()->Redraw();
930 }
931
932 //==========================================================
933 /*!
934  *  OCCViewer_ViewFrame::BeforeDisplay
935  *  Axiluary method called before displaying of objects
936  */
937 //==========================================================
938 void  OCCViewer_ViewFrame::BeforeDisplay( SALOME_Displayer* d )
939 {
940   d->BeforeDisplay( this, SALOME_OCCViewType() );
941 }
942
943 //==========================================================
944 /*!
945  *  OCCViewer_ViewFrame::AfterDisplay
946  *  Axiluary method called after displaying of objects
947  */
948 //==========================================================
949 void  OCCViewer_ViewFrame::AfterDisplay( SALOME_Displayer* d )
950 {
951   d->AfterDisplay( this, SALOME_OCCViewType() );
952 }
953