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