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