Salome HOME
8f8e8fabf1e8d3f5ea70004a16f920f3f31dc7b5
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "OCCViewer_ViewModel.h"
23 #include "OCCViewer_ViewWindow.h"
24 #include "OCCViewer_VService.h"
25 #include "OCCViewer_ViewPort3d.h"
26
27 #include "SUIT_ViewWindow.h"
28 #include "SUIT_ViewManager.h"
29 #include "SUIT_Desktop.h"
30 #include "SUIT_Session.h"
31
32 #include "QtxActionToolMgr.h"
33
34 #include <QPainter>
35 #include <QApplication>
36 #include <QColorDialog>
37 #include <QPalette>
38 #include <QMenu>
39 #include <QMouseEvent>
40 #include <QToolBar>
41 #include <QDesktopWidget>
42
43 #include <AIS_Axis.hxx>
44 #include <AIS_Drawer.hxx>
45 #include <AIS_ListOfInteractive.hxx>
46 #include <AIS_ListIteratorOfListOfInteractive.hxx>
47
48 #include <Geom_Axis2Placement.hxx>
49 #include <Prs3d_Drawer.hxx>
50 #include <Prs3d_DatumAspect.hxx>
51 #include <Prs3d_LineAspect.hxx>
52 #include <Prs3d_LengthAspect.hxx>
53 #include <Prs3d_AngleAspect.hxx>
54 #include <Prs3d_TextAspect.hxx>
55
56 /*!
57   Constructor
58   \param DisplayTrihedron - is trihedron displayed
59 */
60 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron, bool DisplayStaticTrihedron )
61 : SUIT_ViewModel(),
62   myBgColor( Qt::black ),
63   myShowStaticTrihedron( DisplayStaticTrihedron )
64 {
65   // init CasCade viewers
66   myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
67                                               V3d_XposYnegZpos, true, true );
68
69   myV3dViewer->Init();
70
71   myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
72                                                  V3d_XposYnegZpos, true, true );
73   myV3dCollector->Init();
74
75   // init selector
76   myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
77
78   myAISContext->SelectionColor( Quantity_NOC_WHITE );
79   
80   // display isoline on planar faces (box for ex.)
81   myAISContext->IsoOnPlane( true );
82
83   double h = QApplication::desktop()->screenGeometry( QApplication::desktop()->primaryScreen() ).height() / 300. ;
84   Handle(Prs3d_Drawer) drawer = myAISContext->DefaultDrawer();
85   Handle(Prs3d_TextAspect) ta = drawer->TextAspect();
86   ta->SetHeight(100); // VSR: workaround for CAS.CADE bug (is it really needed ???)
87   ta->SetHeight(h);
88   drawer->SetTextAspect(ta);
89   drawer->AngleAspect()->SetTextAspect(ta);
90   drawer->LengthAspect()->SetTextAspect(ta);
91   
92   clearViewAspects();
93
94   /* create trihedron */
95   if( DisplayTrihedron )
96   {
97     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
98     myTrihedron = new AIS_Trihedron(anAxis);
99     myTrihedron->SetInfiniteState( Standard_True );
100
101     Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
102     //myTrihedron->SetColor( Col );
103     myTrihedron->SetArrowColor( Col.Name() );
104     myTrihedron->SetSize(100);
105     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
106     if (drawer->HasDatumAspect()) {
107         Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
108         daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
109         daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
110         daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
111     }
112
113     myAISContext->Display(myTrihedron);
114     myAISContext->Deactivate(myTrihedron);
115   }
116
117   // selection
118   mySelectionEnabled = true;
119   myMultiSelectionEnabled = true;
120 }
121
122 /*!
123   Destructor
124 */
125 OCCViewer_Viewer::~OCCViewer_Viewer() 
126 {
127 }
128
129 /*!
130   \return background color of viewer
131 */
132 QColor OCCViewer_Viewer::backgroundColor() const
133 {
134   return myBgColor;
135 }
136
137 /*!
138   Sets background color
139   \param c - new background color
140 */
141 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
142 {
143   if ( c.isValid() )
144     myBgColor = c;
145 }
146
147 /*!
148   Start initialization of view window
149   \param view - view window to be initialized
150 */
151 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
152 {
153   if ( view ) {
154     view->initLayout();
155     view->initSketchers();
156     
157     OCCViewer_ViewPort3d* vp3d = view->getViewPort();
158     if ( vp3d )
159       vp3d->setBackgroundColor( myBgColor );
160   }
161 }
162
163 /*!
164   Creates new view window
165   \param theDesktop - main window of application
166 */
167 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
168 {
169   OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
170   initView( view );
171   return view;
172 }
173
174 /*!
175   Sets new view manager
176   \param theViewManager - new view manager
177 */
178 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
179 {
180   SUIT_ViewModel::setViewManager(theViewManager);
181   if (theViewManager) {
182     connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
183             this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
184
185     connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
186             this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
187
188     connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
189             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
190   }
191 }
192
193 /*!
194   SLOT: called on mouse button press, stores current mouse position as start point for transformations
195 */
196 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
197 {
198   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
199 }
200
201 /*!
202   SLOT: called on mouse move, processes transformation or hilighting
203 */
204 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
205 {
206   if (!mySelectionEnabled) return;
207   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
208
209   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
210
211   if ( isSelectionEnabled() ) {
212     if (aView->getViewPort()->isBusy()) return; // Check that the ViewPort initialization completed
213                                                 // To Prevent call move event if the View port is not initialized
214                                                 // IPAL 20883
215     Handle(V3d_View) aView3d = aView->getViewPort()->getView();
216     if ( !aView3d.IsNull() )
217       myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
218   }
219 }
220
221
222 /*!
223   SLOT: called on mouse button release, finishes transformation or selection
224 */
225 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
226 {
227   if (!mySelectionEnabled) return;
228   if (theEvent->button() != Qt::LeftButton) return;
229   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
230
231
232   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
233   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
234   bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
235   
236   if (!aHasShift) emit deselection();
237
238   if (myStartPnt == myEndPnt)
239   {
240     if (aHasShift && myMultiSelectionEnabled)
241       myAISContext->ShiftSelect();
242     else
243       myAISContext->Select();
244   }
245   else
246   {
247     if (aHasShift && myMultiSelectionEnabled)
248       myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
249                                 myEndPnt.x(), myEndPnt.y(),
250                                 aView->getViewPort()->getView(), Standard_False );
251     else
252       myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
253                            myEndPnt.x(), myEndPnt.y(),
254                            aView->getViewPort()->getView(), Standard_False );
255
256     int Nb = myAISContext->NbSelected();
257     if( Nb>1 && !myMultiSelectionEnabled )
258     {
259         myAISContext->InitSelected();
260         Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
261         if( !anOwner.IsNull() )
262         {
263             myAISContext->ClearSelected( Standard_False );
264             myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
265         }
266     }
267
268     myAISContext->UpdateCurrentViewer();
269   }
270   emit selectionChanged();
271 }
272
273
274 /*!
275   Sets selection enabled status
276   \param isEnabled - new status
277 */
278 void OCCViewer_Viewer::enableSelection(bool isEnabled)
279 {
280   mySelectionEnabled = isEnabled;
281   //!! To be done for view windows
282   if ( !myViewManager )
283     return;
284
285   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
286   for ( int i = 0; i < (int)wins.count(); i++ )
287   {
288     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
289     if ( win )
290       win->updateEnabledDrawMode();
291   }
292 }
293
294 /*!
295   Sets multiselection enabled status
296   \param isEnabled - new status
297 */
298 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
299 {
300   myMultiSelectionEnabled = isEnable;
301   //!! To be done for view windows
302   if ( !myViewManager )
303     return;
304
305   QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
306   for ( int i = 0; i < (int)wins.count(); i++ )
307   {
308     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
309     if ( win )
310       win->updateEnabledDrawMode();
311   }
312 }
313
314 /*!
315   Builds popup for occ viewer
316 */
317 void OCCViewer_Viewer::contextMenuPopup(QMenu* thePopup)
318 {
319   thePopup->addAction( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
320   thePopup->addAction( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
321
322   thePopup->addSeparator();
323
324   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
325
326   //Support of several toolbars in the popup menu
327   QList<QToolBar*> lst = qFindChildren<QToolBar*>( aView );
328   QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
329   for( ; it!=last; it++ )
330     thePopup->addAction( (*it)->toggleViewAction() );
331 }
332
333 /*!
334   SLOT: called on dump view operation is activated, stores scene to raster file
335 */
336 void OCCViewer_Viewer::onDumpView()
337 {
338   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
339   if ( aView )
340     aView->onDumpView();
341 }
342
343 /*!
344   SLOT: called if background color is to be changed changed, passes new color to view port
345 */
346 void OCCViewer_Viewer::onChangeBgColor()
347 {
348   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
349   if( !aView )
350     return;
351   OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
352   if( !aViewPort3d )
353     return;
354   QColor aColorActive = aViewPort3d->backgroundColor();
355
356   QColor selColor = QColorDialog::getColor( aColorActive, aView);
357   if ( selColor.isValid() )
358     aViewPort3d->setBackgroundColor(selColor);
359 }
360
361 /*!
362   Updates OCC 3D viewer
363 */
364 void OCCViewer_Viewer::update()
365 {
366   if (!myV3dViewer.IsNull())
367     myV3dViewer->Update();
368
369   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
370   if ( aView )
371     aView->updateGravityCoords();
372 }
373
374 /*!
375   \return objects selected in 3D viewer
376   \param theList - list to be filled with selected objects
377 */
378 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
379 {
380   theList.Clear();
381   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
382     theList.Append(myAISContext->SelectedInteractive());
383 }
384
385 /*!
386   Selects objects in 3D viewer. Other selected objects are left as selected
387   \param theList - list objects to be selected
388 */
389 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
390 {
391   AIS_ListIteratorOfListOfInteractive aIt;
392   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
393     myAISContext->AddOrRemoveSelected(aIt.Value(), false);
394   myAISContext->UpdateCurrentViewer();
395 }
396
397 /*!
398   Auxiliary method to emit signal selectionChanged()
399 */
400 void OCCViewer_Viewer::performSelectionChanged()
401 {
402     emit selectionChanged();
403 }
404
405 /*!
406   SLOT, clears view aspects
407 */
408 void OCCViewer_Viewer::onClearViewAspects()
409 {
410     clearViewAspects();
411 }
412
413 /*!
414   Clears view aspects
415 */
416 void OCCViewer_Viewer::clearViewAspects()
417 {
418         myViewAspects.clear();
419 }
420
421 /*!
422   \return const reference to list of view aspects
423 */
424 const viewAspectList& OCCViewer_Viewer::getViewAspects()
425 {
426         return myViewAspects;
427 }
428
429 /*!
430   Appends new view aspect
431   \param aParams - new view aspects
432 */
433 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
434 {
435         myViewAspects.append( aParams );
436 }
437
438 /*!
439   Replaces old view aspects by new ones
440   \param aViewList - list of new view aspects
441 */
442 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
443 {
444         myViewAspects = aViewList;
445 }
446
447 /*!
448   Hilights/unhilights object in viewer
449   \param obj - object to be updated
450   \param hilight - if it is true, object will be hilighted, otherwise it will be unhilighted
451   \param update - update current viewer
452 */
453 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
454                                   bool hilight, bool update )
455 {
456   bool isInLocal = myAISContext->HasOpenedContext();
457   if( !obj.IsNull() )
458     if( !isInLocal )
459     {
460       if ( hilight && !myAISContext->IsSelected( obj ) )
461         myAISContext->AddOrRemoveCurrentObject( obj, false );
462       else if ( !hilight && myAISContext->IsSelected( obj ) )
463         myAISContext->AddOrRemoveCurrentObject( obj, false );
464     }
465
466   if ( update )
467     myV3dViewer->Redraw();
468     
469   return false;
470 }
471
472 /*!
473   Unhilights all objects in viewer
474   \param updateviewer - update current viewer
475 */
476 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
477 {
478   if ( myAISContext->HasOpenedContext() )
479     myAISContext->ClearSelected( updateviewer );
480   else
481     myAISContext->ClearCurrents( updateviewer );
482   return false;
483 }
484
485 /*!
486   \return true if object is in viewer or in collector
487   \param obj - object to be checked
488   \param onlyInViewer - search object only in viewer (so object must be displayed)
489 */
490 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
491                                    bool onlyInViewer )
492 {
493   AIS_ListOfInteractive List;
494   myAISContext->DisplayedObjects(List);
495
496   if( !onlyInViewer )
497   {
498     AIS_ListOfInteractive List1;
499     myAISContext->ObjectsInCollector(List1);
500     List.Append(List1);
501   }
502
503   AIS_ListIteratorOfListOfInteractive ite(List);
504   for ( ; ite.More(); ite.Next() )
505     if( ite.Value()==obj )
506       return true;
507
508   return false;
509 }
510
511 /*!
512   \return true if object is displayed in viewer
513   \param obj - object to be checked
514 */
515 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
516 {
517   return myAISContext->IsDisplayed( obj );
518 }
519
520 /*!
521   Sets color of object
522   \param obj - object to be updated
523   \param color - new color
524   \param update - update current viewer
525 */
526 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
527                                  const QColor& color,
528                                  bool update )
529 {
530   if( !obj.IsNull() )
531   {
532     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
533                                                color.green() / 255.,
534                                                color.blue() / 255.,
535                                                Quantity_TOC_RGB );
536     obj->SetColor( CSFColor );
537   }
538
539   if( update )
540     myV3dViewer->Update();
541 }
542
543 /*!
544   Changes display mode of object
545   \param obj - object to be processed
546   \param mode - new display mode
547   \param update - update current viewer
548 */
549 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
550                                              int mode, bool update )
551 {
552   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, update );
553   if( update )
554     myV3dViewer->Update();
555 }
556
557 /*!
558   Changes transparency of object
559   \param obj - object to be processed
560   \param trans - new transparency
561   \param update - update current viewer
562 */
563 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
564                                         float trans, bool update )
565 {
566   myAISContext->SetTransparency( obj, trans, false );
567   myAISContext->Redisplay( obj, Standard_False, Standard_True );
568   if( update )
569     myV3dViewer->Update();
570 }
571
572 /*!
573   Changes visibility of trihedron to opposite
574 */
575 void OCCViewer_Viewer::toggleTrihedron()
576 {
577   setTrihedronShown( !isTrihedronVisible() );
578 }
579
580 /*!
581   \return true if trihedron is visible
582 */
583 bool OCCViewer_Viewer::isTrihedronVisible() const
584 {
585   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
586 }
587
588 /*!
589   Sets visibility state of trihedron
590   \param on - new state
591 */
592
593 void OCCViewer_Viewer::setTrihedronShown( const bool on )
594 {
595   if ( myTrihedron.IsNull() )
596     return;
597
598   if ( on )
599     myAISContext->Display( myTrihedron );
600   else
601     myAISContext->Erase( myTrihedron );
602 }
603
604 /*!
605   \return trihedron size
606 */
607 double OCCViewer_Viewer::trihedronSize() const
608 {
609   double sz = 0;
610   if ( !myTrihedron.IsNull() )
611     sz = myTrihedron->Size();
612   return sz;
613 }
614
615 /*!
616   Changes trihedron size
617   \param sz - new size
618 */
619 void OCCViewer_Viewer::setTrihedronSize( const double sz )
620 {
621   if ( !myTrihedron.IsNull() )
622     myTrihedron->SetSize( sz );
623 }
624
625 /*!
626   Set number of isolines
627   \param u - u-isolines (first parametric co-ordinate)
628   \param v - v-isolines (second parametric co-ordinate)
629 */
630 void OCCViewer_Viewer::setIsos( const int u, const int v )
631 {
632   Handle(AIS_InteractiveContext) ic = getAISContext();
633   if ( ic.IsNull() )
634   return;
635
636   ic->SetIsoNumber( u, AIS_TOI_IsoU );
637   ic->SetIsoNumber( v, AIS_TOI_IsoV );
638 }
639
640 /*!
641   \return number of isolines
642   \param u - to return u-isolines (first parametric co-ordinate)
643   \param v - to return v-isolines (second parametric co-ordinate)
644 */
645 void OCCViewer_Viewer::isos( int& u, int& v ) const
646 {
647   Handle(AIS_InteractiveContext) ic = getAISContext();
648   if ( !ic.IsNull() )
649   {
650     u = ic->IsoNumber( AIS_TOI_IsoU );
651     v = ic->IsoNumber( AIS_TOI_IsoV );
652   }
653 }