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