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