]> SALOME platform Git repositories - modules/gui.git/blob - src/OCCViewer/OCCViewer_ViewModel.cxx
Salome HOME
c23da3779c3f6b963d00b5ff51462a843460fe5b
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19
20 #include "OCCViewer_ViewModel.h"
21 #include "OCCViewer_ViewWindow.h"
22 #include "OCCViewer_VService.h"
23 #include "OCCViewer_ViewPort3d.h"
24
25 #include "SUIT_ViewWindow.h"
26 #include "SUIT_Desktop.h"
27 #include "SUIT_Session.h"
28
29 #include <qpainter.h>
30 #include <qapplication.h>
31 #include <qcolordialog.h>
32 #include <qpalette.h>
33 #include <qpopupmenu.h>
34
35 #include <AIS_Axis.hxx>
36 #include <AIS_Drawer.hxx>
37 #include <AIS_ListIteratorOfListOfInteractive.hxx>
38
39 #include <Geom_Axis2Placement.hxx>
40 #include <Prs3d_DatumAspect.hxx>
41 #include <Prs3d_LineAspect.hxx>
42
43 OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron )
44 : SUIT_ViewModel(),
45 myBgColor( Qt::black )
46 {
47   // init CasCade viewers
48   myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
49                                               V3d_XposYnegZpos, true, true );
50
51   myV3dViewer->Init();
52
53   myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
54                                                  V3d_XposYnegZpos, true, true );
55   myV3dCollector->Init();
56
57   // init selector
58   myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
59
60   myAISContext->SelectionColor( Quantity_NOC_WHITE );
61   
62   // display isoline on planar faces (box for ex.)
63   myAISContext->IsoOnPlane( true );
64
65   clearViewAspects();
66
67   /* create trihedron */
68   if( DisplayTrihedron )
69   {
70     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
71     myTrihedron = new AIS_Trihedron(anAxis);
72     myTrihedron->SetInfiniteState( Standard_True );
73
74     Quantity_Color Col(193/255., 205/255., 193/255., Quantity_TOC_RGB);
75     //myTrihedron->SetColor( Col );
76     myTrihedron->SetArrowColor( Col.Name() );
77     myTrihedron->SetSize(100);
78     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
79     if (drawer->HasDatumAspect()) {
80         Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
81         daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
82         daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
83         daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
84     }
85
86     myAISContext->Display(myTrihedron);
87     myAISContext->Deactivate(myTrihedron);
88   }
89
90   // selection
91   mySelectionEnabled = true;
92   myMultiSelectionEnabled = true;
93 }
94
95
96 OCCViewer_Viewer::~OCCViewer_Viewer() 
97 {
98 }
99
100 QColor OCCViewer_Viewer::backgroundColor() const
101 {
102   return myBgColor;
103 }
104
105 void OCCViewer_Viewer::setBackgroundColor( const QColor& c )
106 {
107   if ( c.isValid() )
108     myBgColor = c;
109 }
110
111 void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
112 {
113   if ( view ) {
114     view->initLayout();
115     
116     OCCViewer_ViewPort3d* vp3d = view->getViewPort();
117     if ( vp3d )
118       vp3d->setBackgroundColor( myBgColor );
119   }
120 }
121
122
123 SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
124 {
125   OCCViewer_ViewWindow* view = new OCCViewer_ViewWindow(theDesktop, this);
126   initView( view );
127   return view;
128 }
129
130 //*********************************************************************
131 void OCCViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
132 {
133   SUIT_ViewModel::setViewManager(theViewManager);
134   if (theViewManager) {
135     connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
136             this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
137
138     connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
139             this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
140
141     connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
142             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
143   }
144 }
145
146
147 //*********************************************************************
148 void OCCViewer_Viewer::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
149 {
150   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
151 }
152
153
154 //*********************************************************************
155 void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
156 {
157   if (!mySelectionEnabled) return;
158   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
159
160   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
161   myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
162 }
163
164
165 //*********************************************************************
166 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
167 {
168   if (!mySelectionEnabled) return;
169   if (theEvent->button() != Qt::LeftButton) return;
170   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
171
172
173   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
174   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
175   bool aHasShift = (theEvent->state() & Qt::ShiftButton);
176
177   if (myStartPnt == myEndPnt)
178   {
179     if (aHasShift && myMultiSelectionEnabled)
180       myAISContext->ShiftSelect();
181     else
182       myAISContext->Select();
183   }
184   else
185   {
186     if (aHasShift && myMultiSelectionEnabled)
187       myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
188                                 myEndPnt.x(), myEndPnt.y(),
189                                 aView->getViewPort()->getView(), Standard_False );
190     else
191       myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
192                            myEndPnt.x(), myEndPnt.y(),
193                            aView->getViewPort()->getView(), Standard_False );
194
195     int Nb = myAISContext->NbSelected();
196     if( Nb>1 && !myMultiSelectionEnabled )
197     {
198         myAISContext->InitSelected();
199         Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
200         if( !anOwner.IsNull() )
201         {
202             myAISContext->ClearSelected( Standard_False );
203             myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
204         }
205     }
206
207     myAISContext->UpdateCurrentViewer();
208   }
209   emit selectionChanged();
210 }
211
212
213 //*********************************************************************
214 void OCCViewer_Viewer::enableSelection(bool isEnabled)
215 {
216   mySelectionEnabled = isEnabled;
217   //!! To be done for view windows
218 }
219
220 //*********************************************************************
221 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
222 {
223   myMultiSelectionEnabled = isEnable;
224   //!! To be done for view windows
225 }
226
227 //*********************************************************************
228 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
229 {
230   thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
231   thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
232
233   thePopup->insertSeparator();
234
235   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
236   if ( aView && !aView->getToolBar()->isVisible() )
237     thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
238 }
239
240 void OCCViewer_Viewer::onDumpView()
241 {
242   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
243   if ( aView )
244     aView->onDumpView();
245 }
246
247 //*********************************************************************
248 void OCCViewer_Viewer::onChangeBgColor()
249 {
250   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
251   if( !aView )
252     return;
253   OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
254   if( !aViewPort3d )
255     return;
256   QColor aColorActive = aViewPort3d->backgroundColor();
257
258   QColor selColor = QColorDialog::getColor( aColorActive, aView);
259   if ( selColor.isValid() )
260     aViewPort3d->setBackgroundColor(selColor);
261 }
262
263 //*********************************************************************
264 void OCCViewer_Viewer::onShowToolbar() {
265   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
266   if ( aView )
267     aView->getToolBar()->show();    
268 }
269
270 //*********************************************************************
271 void OCCViewer_Viewer::update()
272 {
273   if (!myV3dViewer.IsNull())
274     myV3dViewer->Update();
275 }
276
277 //*********************************************************************
278 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
279 {
280   theList.Clear();
281   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
282     theList.Append(myAISContext->SelectedInteractive());
283 }
284
285 //*********************************************************************
286 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
287 {
288   AIS_ListIteratorOfListOfInteractive aIt;
289   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
290     myAISContext->SetSelected(aIt.Value(), false);
291   myAISContext->UpdateCurrentViewer();
292 }
293
294 //*********************************************************************
295 void OCCViewer_Viewer::performSelectionChanged()
296 {
297     emit selectionChanged();
298 }
299
300 //****************************************************************
301
302 void OCCViewer_Viewer::onClearViewAspects()
303 {
304     clearViewAspects();
305 }
306
307 //****************************************************************
308
309 void OCCViewer_Viewer::clearViewAspects()
310 {
311         myViewAspects.clear();
312 }
313
314 //****************************************************************
315
316 const viewAspectList& OCCViewer_Viewer::getViewAspects()
317 {
318         return myViewAspects;
319 }
320
321 //****************************************************************
322
323 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
324 {
325         myViewAspects.append( aParams );
326 }
327
328 //****************************************************************
329
330 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
331 {
332         myViewAspects = aViewList;
333 }
334
335 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
336                                   bool hilight, bool update )
337 {
338   bool isInLocal = myAISContext->HasOpenedContext();
339   if( !obj.IsNull() )
340     if( !isInLocal )
341     {
342       if ( hilight && !myAISContext->IsSelected( obj ) )
343         myAISContext->AddOrRemoveCurrentObject( obj, false );
344       else if ( !hilight && myAISContext->IsSelected( obj ) )
345         myAISContext->AddOrRemoveCurrentObject( obj, false );
346     }
347
348   if ( update )
349     myV3dViewer->Redraw();
350     
351   return false;
352 }
353
354 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
355 {
356   if ( myAISContext->HasOpenedContext() )
357     myAISContext->ClearSelected( updateviewer );
358   else
359     myAISContext->ClearCurrents( updateviewer );
360   return false;
361 }
362
363 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
364                                    bool onlyInViewer )
365 {
366   AIS_ListOfInteractive List;
367   myAISContext->DisplayedObjects(List);
368
369   if( !onlyInViewer )
370   {
371     AIS_ListOfInteractive List1;
372     myAISContext->ObjectsInCollector(List1);
373     List.Append(List1);
374   }
375
376   AIS_ListIteratorOfListOfInteractive ite(List);
377   for ( ; ite.More(); ite.Next() )
378     if( ite.Value()==obj )
379       return true;
380
381   return false;
382 }
383
384 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
385 {
386   return myAISContext->IsDisplayed( obj );
387 }
388
389 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
390                                  const QColor& color,
391                                  bool update )
392 {
393   if( !obj.IsNull() )
394   {
395     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
396                                                color.green() / 255.,
397                                                color.blue() / 255.,
398                                                Quantity_TOC_RGB );
399     obj->SetColor( CSFColor );
400   }
401
402   if( update )
403     myV3dViewer->Update();
404 }
405
406 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
407                                              int mode, bool update )
408 {
409   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
410   if( update )
411     myV3dViewer->Update();
412 }
413
414 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
415                                         float trans, bool update )
416 {
417   myAISContext->SetTransparency( obj, trans, false );
418   myAISContext->Redisplay( obj, Standard_False, Standard_True );
419   if( update )
420     myV3dViewer->Update();
421 }
422
423 //****************************************************************
424 void OCCViewer_Viewer::toggleTrihedron()
425 {
426   setTrihedronShown( !isTrihedronVisible() );
427 }
428
429 bool OCCViewer_Viewer::isTrihedronVisible() const
430 {
431   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
432 }
433
434 void OCCViewer_Viewer::setTrihedronShown( const bool on )
435 {
436   if ( myTrihedron.IsNull() )
437     return;
438
439   if ( on )
440     myAISContext->Display( myTrihedron );
441   else
442     myAISContext->Erase( myTrihedron );
443 }
444
445 int OCCViewer_Viewer::trihedronSize() const
446 {
447   int sz = 0;
448   if ( !myTrihedron.IsNull() )
449     sz = (int)myTrihedron->Size();
450   return sz;
451 }
452
453 void OCCViewer_Viewer::setTrihedronSize( const int sz )
454 {
455   if ( !myTrihedron.IsNull() )
456     myTrihedron->SetSize( sz );
457 }
458
459 void OCCViewer_Viewer::setIsos( const int u, const int v )
460 {
461   Handle(AIS_InteractiveContext) ic = getAISContext();
462   if ( ic.IsNull() )
463   return;
464
465   ic->SetIsoNumber( u, AIS_TOI_IsoU );
466   ic->SetIsoNumber( v, AIS_TOI_IsoV );
467 }
468
469 void OCCViewer_Viewer::isos( int& u, int& v ) const
470 {
471   Handle(AIS_InteractiveContext) ic = getAISContext();
472   if ( !ic.IsNull() )
473   {
474     u = ic->IsoNumber( AIS_TOI_IsoU );
475     v = ic->IsoNumber( AIS_TOI_IsoV );
476   }
477 }