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