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