Salome HOME
NRI : KERNEL is now defined in KERNELCatalog.
[modules/kernel.git] / src / OCCViewer / OCCViewer_ViewFrame.cxx
1 using namespace std;
2 //  File      : OCCViewer_ViewFrame.cxx
3 //  Created   : Wed Mar 20 10:50:22 2002
4 //  Author    : Nicolas REJNERI
5 //  Project   : SALOME
6 //  Module    : OCCViewer
7 //  Copyright : Open CASCADE 2002
8 //  $Header$
9
10
11 #include "OCCViewer_ViewFrame.h"
12 #include "OCCViewer_Viewer3d.h"
13 #include "OCCViewer_ViewPort.h"
14 #include "OCCViewer_ViewPort3d.h"
15
16 #include "QAD.h"
17 #include "QAD_Tools.h"
18 #include "QAD_Desktop.h"
19 #include "QAD_ViewFrame.h"
20 #include "QAD_MessageBox.h"
21 #include "QAD_Application.h"
22 #include "utilities.h"
23
24 #include "SALOME_Selection.h"
25 #include "SALOME_AISShape.hxx"
26
27 // QT Include
28 #include <qapplication.h>
29
30 // Open CASCADE Include
31 #include <V3d_View.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33
34
35 /*!
36     Constructor
37 */
38 OCCViewer_ViewFrame::OCCViewer_ViewFrame(QWidget* parent, const QString& title) 
39   : QAD_ViewFrame (parent, "")
40 {
41   initialize();
42 }
43
44 /*!
45     Constructor
46 */
47 OCCViewer_ViewFrame::OCCViewer_ViewFrame( QWidget* parent ) 
48   : QAD_ViewFrame (parent, "")
49 {
50   initialize();
51 }
52
53 /*!
54     Destructor
55 */
56 OCCViewer_ViewFrame::~OCCViewer_ViewFrame()
57 {
58   cleanup();
59 }
60
61 /*!
62     Init viewframe
63 */
64 void OCCViewer_ViewFrame::initialize()
65 {
66   myViewPort = NULL;
67   myViewer   = new OCCViewer_Viewer3d( this );
68   
69   /* enable/disable selection */
70   myViewer->enableSelection( true );
71   myViewer->enableMultipleSelection( true );
72 }
73
74 /*!
75     Cleanup viewframe 
76 */
77 void OCCViewer_ViewFrame::cleanup()
78 {
79   
80
81
82 /*!
83     Sets the viewport for this frame
84 */
85 void OCCViewer_ViewFrame::setViewPort( OCCViewer_ViewPort* view )
86 {
87   myViewPort = view;
88   initViewPort();               
89   setCentralWidget ( myViewPort );
90 }
91
92 /*!
93     Inits the viewport for this frame
94 */
95 void OCCViewer_ViewFrame::initViewPort()
96 {
97   /* Active Key Event */
98   setFocus();
99
100   /* Initial attributes */
101   myViewPort->setCursor( cursor() );
102   myViewPort->setBackgroundColor( backgroundColor() );
103   
104   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)), 
105                                 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
106
107   /*  Listen to my viewport */  
108   QAD_ASSERT ( QObject::connect(myViewPort, 
109                                 SIGNAL(vpTransformationStarted (OCCViewer_ViewPort::OperationType)), 
110                                 this, 
111                                 SIGNAL(vfTransformationStarted(OCCViewer_ViewPort::OperationType))) );
112   QAD_ASSERT ( QObject::connect( myViewPort, 
113                                  SIGNAL(vpTransformationFinished (OCCViewer_ViewPort::OperationType)),
114                                  this, 
115                                  SIGNAL(vfTransformationFinished(OCCViewer_ViewPort::OperationType))) );
116
117   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpDrawExternal (QPainter*)), 
118                                 this, SIGNAL(vfDrawExternal(QPainter*))) );     
119   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMousePress (QMouseEvent*)), 
120                                 this, SIGNAL(vfMousePress(QMouseEvent*))) );
121   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseRelease (QMouseEvent*)), 
122                                 this, SIGNAL(vfMouseRelease(QMouseEvent*))) );
123   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseMove (QMouseEvent*)), 
124                                 this, SIGNAL(vfMouseMove(QMouseEvent*))) );
125   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpMouseDoubleClick (QMouseEvent*)), 
126                                 this, SIGNAL(vfMouseDoubleClick(QMouseEvent*))) );
127
128   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyPress (QKeyEvent*)), 
129                                 this, SIGNAL(vfKeyPress(QKeyEvent*))) );
130   QAD_ASSERT ( QObject::connect(myViewPort, SIGNAL(vpKeyRelease (QKeyEvent*)), 
131                                 this, SIGNAL(vfKeyRelease(QKeyEvent*))) );
132 }
133
134 /*!
135   Returns widget containing 3D-Viewer
136 */
137 QWidget* OCCViewer_ViewFrame::getViewWidget() 
138 {
139   return (QWidget*)getViewPort();
140 }
141
142 /*!
143     Returns the viewport of this frame  
144 */
145 OCCViewer_ViewPort* OCCViewer_ViewFrame::getViewPort() const
146 {
147   return myViewPort;
148 }
149
150 /*!
151     Sets the cursor for the viewframe's viewport
152 */
153 void OCCViewer_ViewFrame::setCursor( const QCursor& cursor)
154 {
155   if ( myViewPort ) 
156     myViewPort->QWidget::setCursor(cursor);
157 }
158
159 /*!
160     Returns the current cursor 
161 */
162 QCursor OCCViewer_ViewFrame::cursor() const
163 {
164   if ( myViewPort ) 
165     return myViewPort->cursor();
166   return QMainWindow::cursor();
167 }
168
169 /*!
170     Set background of the viewport
171 */
172 void OCCViewer_ViewFrame::setBackgroundColor( const QColor& color)
173 {
174   if ( myViewPort )
175     myViewPort->setBackgroundColor(color);
176 }
177
178 /*!
179     Returns background of the viewport
180 */
181 QColor OCCViewer_ViewFrame::backgroundColor() const
182 {
183   if ( myViewPort )
184     return myViewPort->backgroundColor();
185   return QMainWindow::backgroundColor();
186 }
187
188 /*!
189     Sets the parent viewer for the window
190 */
191 void OCCViewer_ViewFrame::setViewer(OCCViewer_Viewer3d* viewer)
192 {
193   myViewer = viewer;
194 }
195
196 /*!
197   Returns the parent viewer for the window
198 */
199 OCCViewer_Viewer3d* OCCViewer_ViewFrame::getViewer() const
200 {
201   return myViewer;
202 }
203
204 /*!
205     Returns 'true' if viewframe is visible
206 */
207 void OCCViewer_ViewFrame::setVisible( bool visible )
208 {
209   if ( visible == QWidget::isVisible() )
210     return;
211   
212   if ( visible ) show();
213   else hide();
214 }
215
216 /*!
217    Called when viewframe is about to close
218 */
219 void OCCViewer_ViewFrame::closeEvent(QCloseEvent* e)
220 {
221   emit vfViewClosing(e);  /* notify our viewer */
222 }
223
224 /*!
225    Called when viewframe is resized 
226 */
227 void OCCViewer_ViewFrame::resizeEvent(QResizeEvent* e)
228 {
229   emit vfResize(e);     
230 }
231
232 void OCCViewer_ViewFrame::keyPressEvent( QKeyEvent *k )
233 {
234   emit vfKeyPress(k);
235 }
236
237 /*!
238     Fits all objects in the active view
239 */
240 void OCCViewer_ViewFrame::onViewFitAll()
241 {
242   myViewPort->fitAll();
243 }
244
245 /*!
246     Fits all obejcts within a rectangular area of the active view
247 */
248 void OCCViewer_ViewFrame::onViewFitArea()
249 {
250   myViewPort->activateWindowFit();
251   QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_SKETCHAREA") );
252 }
253
254 /*!
255     Moves the active view
256 */
257 void OCCViewer_ViewFrame::onViewPan()
258 {
259   myViewPort->activatePanning();
260 }
261
262 /*!
263     Zooms the active view
264 */
265 void OCCViewer_ViewFrame::onViewZoom()
266 {
267   myViewPort->activateZoom();
268 }
269
270 /*!
271     Sets a new center of the active view
272 */
273 void OCCViewer_ViewFrame::onViewGlobalPan()
274 {
275   myViewPort->activateGlobalPanning();
276   QAD_Application::getDesktop()->putInfo( tr("PRP_VW3D_POINTCENTER") );
277 }
278
279 /*!
280     Rotates the active view
281 */
282 void OCCViewer_ViewFrame::onViewRotate()
283 {
284   myViewPort->activateRotation();
285 }
286
287 /*!
288     Reset the active view
289 */
290 void OCCViewer_ViewFrame::onViewReset()
291 {
292   myViewPort->reset();
293 }
294
295 /*!
296     Provides front projection of the active view
297 */
298 void OCCViewer_ViewFrame::onViewFront()
299 {
300   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
301   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xpos);
302   onViewFitAll();
303 }
304
305 /*!
306     Provides back projection of the active view
307 */
308 void OCCViewer_ViewFrame::onViewBack()
309 {
310   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
311   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Xneg);
312   onViewFitAll();
313 }
314
315 /*!
316     Provides right projection of the active view
317 */
318 void OCCViewer_ViewFrame::onViewRight()
319 {
320   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
321   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Yneg);
322   onViewFitAll();
323 }
324
325 /*!
326     Provides left projection of the active view
327 */
328 void OCCViewer_ViewFrame::onViewLeft()
329 {
330   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
331   if ( !view3d.IsNull() ) view3d->SetProj (V3d_Ypos);
332   onViewFitAll();
333 }
334
335 /*!
336     Provides bottom projection of the active view
337 */
338 void OCCViewer_ViewFrame::onViewBottom()
339 {
340   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
341   if ( !view3d.IsNull() ) view3d->SetProj(V3d_Zneg);
342   onViewFitAll();
343 }
344
345 /*!
346     Provides top projection of the active view
347 */
348 void OCCViewer_ViewFrame::onViewTop()
349 {
350   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)myViewPort)->getView();
351   if ( !view3d.IsNull() ) view3d->SetProj( V3d_Zpos );
352   onViewFitAll();
353 }
354
355 /*!
356   Display/hide Trihedron
357 */
358 void OCCViewer_ViewFrame::onViewTrihedron()
359 {
360   if (!myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() ))
361     myViewer->getAISContext()->Display( myViewer->getTrihedron() );
362   else
363     myViewer->getAISContext()->Erase( myViewer->getTrihedron() );
364 }
365
366 void OCCViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName )
367 {
368   myViewer->rename(IObject, newName);
369 }
370
371 void OCCViewer_ViewFrame::unHighlightAll() 
372 {
373   myViewer->unHighlightAll();
374 }
375
376 void OCCViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject, bool highlight, bool update ) 
377 {
378   myViewer->highlight(IObject, highlight, update);
379 }
380
381 bool OCCViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
382 {
383   return myViewer->isInViewer( IObject );
384 }
385
386 bool OCCViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
387 {
388   return myViewer->isVisible( IObject );
389 }
390
391 void OCCViewer_ViewFrame::setPopupServer( QAD_Application* App )
392 {
393   myViewer->setPopupServer( App );
394 }
395
396 void OCCViewer_ViewFrame::undo(SALOMEDS::Study_var aStudy,
397                                   const char* StudyFrameEntry)
398 {
399   AIS_ListOfInteractive List1;
400   myViewer->getAISContext()->ObjectsInCollector(List1);
401   AIS_ListIteratorOfListOfInteractive ite1(List1);
402   while (ite1.More()) {
403     if (ite1.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
404       Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite1.Value());
405       if ( aSh->hasIO() ) {
406         Handle(SALOME_InteractiveObject) IO = aSh->getIO();
407         if ( IO->hasEntry() ) { 
408           if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry))
409             myViewer->getAISContext()->Display(aSh);
410         }
411       }
412     }
413     ite1.Next();
414   }
415   
416   AIS_ListOfInteractive List;
417   myViewer->getAISContext()->DisplayedObjects(List);
418   AIS_ListIteratorOfListOfInteractive ite(List);
419   while (ite.More()) {
420     if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
421       Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
422       if ( aSh->hasIO() ) {
423         Handle(SALOME_InteractiveObject) IO = aSh->getIO();
424         if ( IO->hasEntry() ) { 
425           if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry ))
426             myViewer->getAISContext()->Erase(aSh,true,true);
427         }
428       }
429     }
430     ite.Next();
431   }
432 }
433
434 void OCCViewer_ViewFrame::redo(SALOMEDS::Study_var aStudy,
435                                   const char* StudyFrameEntry)
436 {
437   SALOMEDS::SObject_var RefSO;
438   SALOMEDS::SObject_var SO = aStudy->FindObjectID( StudyFrameEntry );
439   SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(SO);
440   for (; it->More();it->Next()){
441     SALOMEDS::SObject_var CSO= it->Value();
442     if (CSO->ReferencedObject(RefSO)) {
443       
444       AIS_ListOfInteractive List;
445       myViewer->getAISContext()->ObjectsInCollector(List);
446       AIS_ListIteratorOfListOfInteractive ite(List);
447       while (ite.More()) {
448         if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
449           Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
450           if ( aSh->hasIO() ) {
451             Handle(SALOME_InteractiveObject) IO = aSh->getIO();
452             if ( IO->hasEntry() ) { 
453               if ( strcmp(IO->getEntry(),RefSO->GetID()) == 0 )
454                 myViewer->getAISContext()->Display(aSh, false);
455             }
456           }
457         }
458         ite.Next();
459       }
460       
461     }
462   }
463   
464   AIS_ListOfInteractive List1;
465   myViewer->getAISContext()->DisplayedObjects(List1);
466   AIS_ListIteratorOfListOfInteractive ite1(List1);
467   while (ite1.More()) {
468     if (ite1.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
469       Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite1.Value());
470       if ( aSh->hasIO() ) {
471         Handle(SALOME_InteractiveObject) IO = aSh->getIO();
472         if ( IO->hasEntry() ) { 
473           if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry ))
474             myViewer->getAISContext()->Erase(aSh,false,true);
475         }
476       }
477     }
478     ite1.Next();
479   }
480   Repaint();
481 }
482
483 void OCCViewer_ViewFrame::SetTrihedronSize( int dim )
484 {
485   myViewer->setTrihedronSize( dim );
486 }
487
488 /* selection */
489 Handle(SALOME_InteractiveObject) OCCViewer_ViewFrame::FindIObject(const char* Entry)
490 {
491   Handle(SALOME_InteractiveObject) IO;
492   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
493       
494   AIS_ListOfInteractive List;
495   ic->DisplayedObjects(List);
496   AIS_ListOfInteractive List1;
497   ic->ObjectsInCollector(List1);
498   List.Append(List1);
499
500   AIS_ListIteratorOfListOfInteractive ite(List);
501   while (ite.More()) {
502     if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
503       Handle(SALOME_AISShape) aSh
504         = Handle(SALOME_AISShape)::DownCast(ite.Value());
505       if ( aSh->hasIO() ) {
506         IO = aSh->getIO();
507         if ( IO->hasEntry() ) {
508           if ( strcmp( IO->getEntry(), Entry ) == 0 ) {
509             MESSAGE ( "IO found")
510             return IO;
511           }
512         }
513       }
514     }
515     ite.Next();
516   }
517   MESSAGE ( "IO not found")
518   return IO;
519 }
520
521 /* display */           
522 void OCCViewer_ViewFrame::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
523 {
524   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
525   SALOME_Selection* Sel
526     = SALOME_Selection::Selection( myStudy->getSelection() );
527
528   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
529
530   AIS_ListOfInteractive List;
531   ic->DisplayedObjects(List);
532   AIS_ListOfInteractive List1;
533   ic->ObjectsInCollector(List1);
534   List.Append(List1);
535   
536   AIS_ListIteratorOfListOfInteractive ite(List);
537   while (ite.More()) {
538     if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
539       Handle(SALOME_AISShape) aSh
540         = Handle(SALOME_AISShape)::DownCast(ite.Value());
541       if ( aSh->hasIO() ) {
542         Handle(SALOME_InteractiveObject) IO = aSh->getIO();
543         if ( IO->isSame( IObject ) ) {
544           ic->Display(aSh, false);
545           //      ic->AddOrRemoveCurrentObject(aSh, false); repeated in Viewer3d after next statement
546           Sel->AddIObject(IO, false);
547           break;
548         }
549       }
550     }
551     ite.Next();
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   while (ite.More()) {
573     if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape))) {
574       Handle(SALOME_AISShape) aSh
575         = Handle(SALOME_AISShape)::DownCast(ite.Value());
576       if ( aSh->hasIO() ) {
577         Handle(SALOME_InteractiveObject) IO = aSh->getIO();
578         if ( !IO->isSame( IObject ) ) {
579           ic->Erase(aSh, false);
580           Sel->RemoveIObject(IO, false);
581         } else {
582           ic->Display(aSh, false);
583           Sel->AddIObject(IO, false);
584         }
585       }
586     }
587     ite.Next();
588   }
589   Repaint();
590 }
591 void OCCViewer_ViewFrame::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
592 {
593   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
594   SALOME_Selection* Sel
595     = SALOME_Selection::Selection( myStudy->getSelection() );
596
597   Handle (AIS_InteractiveContext) ic = myViewer->getAISContext();
598   
599   AIS_ListOfInteractive List;
600   ic->DisplayedObjects(List);
601   AIS_ListIteratorOfListOfInteractive ite(List);
602   while (ite.More()) 
603     {
604       if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
605         {
606           Handle(SALOME_AISShape) aSh
607             = Handle(SALOME_AISShape)::DownCast(ite.Value());
608           if ( aSh->hasIO() )
609             {
610               Handle(SALOME_InteractiveObject) IO = aSh->getIO();
611               if ( IO->isSame( IObject ) )
612                 {
613                   ic->Erase(aSh, false);
614                   Sel->RemoveIObject(IO, false);
615                   break;
616                 }
617             }
618         }
619       ite.Next();
620     }
621   if (update)
622     Repaint();
623 }
624
625 void OCCViewer_ViewFrame::DisplayAll()
626 {
627   myViewer->getAISContext()->DisplayAll(Standard_False,Standard_True);
628 }
629 void OCCViewer_ViewFrame::EraseAll()
630 {
631   Standard_Boolean isTrihedronDisplayed = 
632     myViewer->getAISContext()->IsDisplayed( myViewer->getTrihedron() );
633   myViewer->getAISContext()->EraseAll();
634   if (isTrihedronDisplayed)
635     myViewer->getAISContext()->Display( myViewer->getTrihedron() );
636   else
637     Repaint();
638 }
639
640
641
642 void OCCViewer_ViewFrame::Repaint(){
643   myViewer->getViewer3d()->Update();
644 }