Salome HOME
8878b837ab62fd30509d33378dff4fd254ec8038
[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   if (thePopup->count() > 0) thePopup->insertSeparator();
185   thePopup->insertItem("Change background...", this, SLOT(onChangeBgColor()));
186
187   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
188   if ( aView ) {
189     if ( !aView->getToolBar()->isVisible() ) {
190       if (thePopup->count() > 0) thePopup->insertSeparator();
191       thePopup->insertItem("Show toolbar", this, SLOT(onShowToolbar()));
192     }
193   }
194 }
195
196 //*********************************************************************
197 void OCCViewer_Viewer::onChangeBgColor()
198 {
199   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
200   if( !aView )
201     return;
202   OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
203   if( !aViewPort3d )
204     return;
205   QColor aColorActive = aViewPort3d->backgroundColor();
206
207   QColor selColor = QColorDialog::getColor( aColorActive, aView);
208   if ( selColor.isValid() )
209     aViewPort3d->setBackgroundColor(selColor);
210 }
211
212 //*********************************************************************
213 void OCCViewer_Viewer::onShowToolbar() {
214   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
215   if ( aView )
216     aView->getToolBar()->show();    
217 }
218
219 //*********************************************************************
220 void OCCViewer_Viewer::update()
221 {
222   if (!myV3dViewer.IsNull())
223     myV3dViewer->Update();
224 }
225
226 //*********************************************************************
227 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
228 {
229   theList.Clear();
230   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
231     theList.Append(myAISContext->SelectedInteractive());
232 }
233
234 //*********************************************************************
235 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
236 {
237   AIS_ListIteratorOfListOfInteractive aIt;
238   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
239     myAISContext->SetSelected(aIt.Value(), false);
240   myAISContext->UpdateCurrentViewer();
241 }
242
243 //*********************************************************************
244 void OCCViewer_Viewer::performSelectionChanged()
245 {
246     emit selectionChanged();
247 }
248
249 //****************************************************************
250
251 void OCCViewer_Viewer::onClearViewAspects()
252 {
253     clearViewAspects();
254 }
255
256 //****************************************************************
257
258 void OCCViewer_Viewer::clearViewAspects()
259 {
260         myViewAspects.clear();
261 }
262
263 //****************************************************************
264
265 const viewAspectList& OCCViewer_Viewer::getViewAspects()
266 {
267         return myViewAspects;
268 }
269
270 //****************************************************************
271
272 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
273 {
274         myViewAspects.append( aParams );
275 }
276
277 //****************************************************************
278
279 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
280 {
281         myViewAspects = aViewList;
282 }
283
284 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
285                                   bool hilight, bool update )
286 {
287   bool isInLocal = myAISContext->HasOpenedContext();
288   if( !obj.IsNull() )
289     if( !isInLocal )
290     {
291       if ( hilight && !myAISContext->IsSelected( obj ) )
292         myAISContext->AddOrRemoveCurrentObject( obj, false );
293       else if ( !hilight && myAISContext->IsSelected( obj ) )
294         myAISContext->AddOrRemoveCurrentObject( obj, false );
295     }
296
297   if ( update )
298     myV3dViewer->Redraw();
299     
300   return false;
301 }
302
303 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
304 {
305   if ( myAISContext->HasOpenedContext() )
306     myAISContext->ClearSelected( updateviewer );
307   else
308     myAISContext->ClearCurrents( updateviewer );
309   return false;
310 }
311
312 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
313                                    bool onlyInViewer )
314 {
315   AIS_ListOfInteractive List;
316   myAISContext->DisplayedObjects(List);
317
318   if( !onlyInViewer )
319   {
320     AIS_ListOfInteractive List1;
321     myAISContext->ObjectsInCollector(List1);
322     List.Append(List1);
323   }
324
325   AIS_ListIteratorOfListOfInteractive ite(List);
326   for ( ; ite.More(); ite.Next() )
327     if( ite.Value()==obj )
328       return true;
329
330   return false;
331 }
332
333 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
334 {
335   return myAISContext->IsDisplayed( obj );
336 }
337
338 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
339                                  const QColor& color,
340                                  bool update )
341 {
342   if( !obj.IsNull() )
343   {
344     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
345                                                color.green() / 255.,
346                                                color.blue() / 255.,
347                                                Quantity_TOC_RGB );
348     obj->SetColor( CSFColor );
349   }
350
351   if( update )
352     myV3dViewer->Update();
353 }
354
355 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
356                                              int mode, bool update )
357 {
358   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
359   if( update )
360     myV3dViewer->Update();
361 }
362
363 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
364                                         float trans, bool update )
365 {
366   myAISContext->SetTransparency( obj, trans, false );
367   myAISContext->Redisplay( obj, Standard_False, Standard_True );
368   if( update )
369     myV3dViewer->Update();
370 }