]> SALOME platform Git repositories - modules/gui.git/blob - src/OCCViewer/OCCViewer_ViewModel.cxx
Salome HOME
b5e3000af19d6baca2ecf4ea2994f2622e71d04a
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
1
2 #include "OCCViewer_ViewModel.h"
3 #include "OCCViewer_ViewWindow.h"
4 #include "OCCViewer_VService.h"
5 #include "OCCViewer_ViewPort3d.h"
6
7 #include "SUIT_ViewWindow.h"
8 #include "SUIT_Desktop.h"
9 #include "SUIT_Session.h"
10
11 #include <qpainter.h>
12 #include <qapplication.h>
13 #include <qcolordialog.h>
14 #include <qpalette.h>
15 #include <qpopupmenu.h>
16
17 #include <AIS_Axis.hxx>
18 #include <AIS_Drawer.hxx>
19 #include <AIS_ListIteratorOfListOfInteractive.hxx>
20
21 #include <Geom_Axis2Placement.hxx>
22 #include <Prs3d_DatumAspect.hxx>
23 #include <Prs3d_LineAspect.hxx>
24
25 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron )
26 : SUIT_ViewModel(),
27 myBgColor( Qt::black )
28 {
29   // init CasCade viewers
30   myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
31                                               V3d_XposYnegZpos, true, true );
32
33   myV3dViewer->Init();
34
35   myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
36                                                  V3d_XposYnegZpos, true, true );
37   myV3dCollector->Init();
38
39   // init selector
40   myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
41
42   myAISContext->SelectionColor( Quantity_NOC_WHITE );
43   
44   // display isoline on planar faces (box for ex.)
45   myAISContext->IsoOnPlane( true );
46
47   clearViewAspects();
48
49   /* create trihedron */
50   if( DisplayTrihedron )
51   {
52     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
53     myTrihedron = new AIS_Trihedron(anAxis);
54     myTrihedron->SetInfiniteState( Standard_True );
55
56     Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
57     //myTrihedron->SetColor( Col );
58     myTrihedron->SetArrowColor( Col.Name() );
59     myTrihedron->SetSize(100);
60     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
61     if (drawer->HasDatumAspect()) {
62         Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
63         daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
64         daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
65         daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
66     }
67
68     myAISContext->Display(myTrihedron);
69     myAISContext->Deactivate(myTrihedron);
70   }
71
72   // selection
73   mySelectionEnabled = true;
74   myMultiSelectionEnabled = true;
75 }
76
77
78 OCCViewer_Viewer::~OCCViewer_Viewer() 
79 {
80 }
81
82 QColor OCCViewer_Viewer::backgroundColor() const
83 {
84   return myBgColor;
85 }
86
87 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
88 {
89   if ( c.isValid() )
90     myBgColor = c;
91 }
92
93 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
94 {
95   if ( view ) {
96     view->initLayout();
97     
98     OCCViewer_ViewPort3d* vp3d = view->getViewPort();
99     if ( vp3d )
100       vp3d->setBackgroundColor( myBgColor );
101   }
102 }
103
104
105 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
106 {
107   OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
108   initView( view );
109   return view;
110 }
111
112 //*********************************************************************
113 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
114 {
115   SUIT_ViewModel::setViewManager(theViewManager);
116   if (theViewManager) {
117     connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
118             this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
119
120     connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
121             this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
122
123     connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
124             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
125   }
126 }
127
128
129 //*********************************************************************
130 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
131 {
132   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
133 }
134
135
136 //*********************************************************************
137 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
138 {
139   if (!mySelectionEnabled) return;
140   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
141
142   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
143   myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
144 }
145
146
147 //*********************************************************************
148 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
149 {
150   if (!mySelectionEnabled) return;
151   if (theEvent->button() != Qt::LeftButton) return;
152   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
153
154
155   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
156   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
157   bool aHasShift = (theEvent->state() & Qt::ShiftButton);
158
159   if (myStartPnt == myEndPnt)
160   {
161     if (aHasShift && myMultiSelectionEnabled)
162       myAISContext->ShiftSelect();
163     else
164       myAISContext->Select();
165   }
166   else
167   {
168     if (aHasShift && myMultiSelectionEnabled)
169       myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
170                                 myEndPnt.x(), myEndPnt.y(),
171                                 aView->getViewPort()->getView(), Standard_False );
172     else
173       myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
174                            myEndPnt.x(), myEndPnt.y(),
175                            aView->getViewPort()->getView(), Standard_False );
176
177     int Nb = myAISContext->NbSelected();
178     if( Nb>1 && !myMultiSelectionEnabled )
179     {
180         myAISContext->InitSelected();
181         Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
182         if( !anOwner.IsNull() )
183         {
184             myAISContext->ClearSelected( Standard_False );
185             myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
186         }
187     }
188
189     myAISContext->UpdateCurrentViewer();
190   }
191   emit selectionChanged();
192 }
193
194
195 //*********************************************************************
196 void OCCViewer_Viewer::enableSelection(bool isEnabled)
197 {
198   mySelectionEnabled = isEnabled;
199   //!! To be done for view windows
200 }
201
202 //*********************************************************************
203 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
204 {
205   myMultiSelectionEnabled = isEnable;
206   //!! To be done for view windows
207 }
208
209 //*********************************************************************
210 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
211 {
212   thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
213   thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
214
215   thePopup->insertSeparator();
216
217   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
218   if ( aView && !aView->getToolBar()->isVisible() )
219     thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
220 }
221
222 void OCCViewer_Viewer::onDumpView()
223 {
224   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
225   if ( aView )
226     aView->onDumpView();
227 }
228
229 //*********************************************************************
230 void OCCViewer_Viewer::onChangeBgColor()
231 {
232   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
233   if( !aView )
234     return;
235   OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
236   if( !aViewPort3d )
237     return;
238   QColor aColorActive = aViewPort3d->backgroundColor();
239
240   QColor selColor = QColorDialog::getColor( aColorActive, aView);
241   if ( selColor.isValid() )
242     aViewPort3d->setBackgroundColor(selColor);
243 }
244
245 //*********************************************************************
246 void OCCViewer_Viewer::onShowToolbar() {
247   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
248   if ( aView )
249     aView->getToolBar()->show();    
250 }
251
252 //*********************************************************************
253 void OCCViewer_Viewer::update()
254 {
255   if (!myV3dViewer.IsNull())
256     myV3dViewer->Update();
257 }
258
259 //*********************************************************************
260 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
261 {
262   theList.Clear();
263   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
264     theList.Append(myAISContext->SelectedInteractive());
265 }
266
267 //*********************************************************************
268 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
269 {
270   AIS_ListIteratorOfListOfInteractive aIt;
271   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
272     myAISContext->SetSelected(aIt.Value(), false);
273   myAISContext->UpdateCurrentViewer();
274 }
275
276 //*********************************************************************
277 void OCCViewer_Viewer::performSelectionChanged()
278 {
279     emit selectionChanged();
280 }
281
282 //****************************************************************
283
284 void OCCViewer_Viewer::onClearViewAspects()
285 {
286     clearViewAspects();
287 }
288
289 //****************************************************************
290
291 void OCCViewer_Viewer::clearViewAspects()
292 {
293         myViewAspects.clear();
294 }
295
296 //****************************************************************
297
298 const viewAspectList& OCCViewer_Viewer::getViewAspects()
299 {
300         return myViewAspects;
301 }
302
303 //****************************************************************
304
305 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
306 {
307         myViewAspects.append( aParams );
308 }
309
310 //****************************************************************
311
312 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
313 {
314         myViewAspects = aViewList;
315 }
316
317 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
318                                   bool hilight, bool update )
319 {
320   bool isInLocal = myAISContext->HasOpenedContext();
321   if( !obj.IsNull() )
322     if( !isInLocal )
323     {
324       if ( hilight && !myAISContext->IsSelected( obj ) )
325         myAISContext->AddOrRemoveCurrentObject( obj, false );
326       else if ( !hilight && myAISContext->IsSelected( obj ) )
327         myAISContext->AddOrRemoveCurrentObject( obj, false );
328     }
329
330   if ( update )
331     myV3dViewer->Redraw();
332     
333   return false;
334 }
335
336 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
337 {
338   if ( myAISContext->HasOpenedContext() )
339     myAISContext->ClearSelected( updateviewer );
340   else
341     myAISContext->ClearCurrents( updateviewer );
342   return false;
343 }
344
345 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
346                                    bool onlyInViewer )
347 {
348   AIS_ListOfInteractive List;
349   myAISContext->DisplayedObjects(List);
350
351   if( !onlyInViewer )
352   {
353     AIS_ListOfInteractive List1;
354     myAISContext->ObjectsInCollector(List1);
355     List.Append(List1);
356   }
357
358   AIS_ListIteratorOfListOfInteractive ite(List);
359   for ( ; ite.More(); ite.Next() )
360     if( ite.Value()==obj )
361       return true;
362
363   return false;
364 }
365
366 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
367 {
368   return myAISContext->IsDisplayed( obj );
369 }
370
371 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
372                                  const QColor& color,
373                                  bool update )
374 {
375   if( !obj.IsNull() )
376   {
377     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
378                                                color.green() / 255.,
379                                                color.blue() / 255.,
380                                                Quantity_TOC_RGB );
381     obj->SetColor( CSFColor );
382   }
383
384   if( update )
385     myV3dViewer->Update();
386 }
387
388 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
389                                              int mode, bool update )
390 {
391   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
392   if( update )
393     myV3dViewer->Update();
394 }
395
396 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
397                                         float trans, bool update )
398 {
399   myAISContext->SetTransparency( obj, trans, false );
400   myAISContext->Redisplay( obj, Standard_False, Standard_True );
401   if( update )
402     myV3dViewer->Update();
403 }
404
405 //****************************************************************
406 void OCCViewer_Viewer::toggleTrihedron()
407 {
408   setTrihedronShown( !isTrihedronVisible() );
409 }
410
411 bool OCCViewer_Viewer::isTrihedronVisible() const
412 {
413   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
414 }
415
416 void OCCViewer_Viewer::setTrihedronShown( const bool on )
417 {
418   if ( myTrihedron.IsNull() )
419     return;
420
421   if ( on )
422     myAISContext->Display( myTrihedron );
423   else
424     myAISContext->Erase( myTrihedron );
425 }
426
427 int OCCViewer_Viewer::trihedronSize() const
428 {
429   int sz = 0;
430   if ( !myTrihedron.IsNull() )
431     sz = (int)myTrihedron->Size();
432   return sz;
433 }
434
435 void OCCViewer_Viewer::setTrihedronSize( const int sz )
436 {
437   if ( !myTrihedron.IsNull() )
438     myTrihedron->SetSize( sz );
439 }
440
441 void OCCViewer_Viewer::setIsos( const int u, const int v )
442 {
443   Handle(AIS_InteractiveContext) ic = getAISContext();
444   if ( ic.IsNull() )
445   return;
446
447   ic->SetIsoNumber( u, AIS_TOI_IsoU );
448   ic->SetIsoNumber( v, AIS_TOI_IsoV );
449 }
450
451 void OCCViewer_Viewer::isos( int& u, int& v ) const
452 {
453   Handle(AIS_InteractiveContext) ic = getAISContext();
454   if ( !ic.IsNull() )
455   {
456     u = ic->IsoNumber( AIS_TOI_IsoU );
457     v = ic->IsoNumber( AIS_TOI_IsoV );
458   }
459 }