]> SALOME platform Git repositories - modules/gui.git/blob - src/OCCViewer/OCCViewer_ViewModel.cxx
Salome HOME
Join modifications from branch BR_3_1_0deb
[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   if ( isSelectionEnabled() )
162     myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView->getViewPort()->getView());
163 }
164
165
166 //*********************************************************************
167 void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
168 {
169   if (!mySelectionEnabled) return;
170   if (theEvent->button() != Qt::LeftButton) return;
171   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
172
173
174   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
175   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
176   bool aHasShift = (theEvent->state() & Qt::ShiftButton);
177
178   if (myStartPnt == myEndPnt)
179   {
180     if (aHasShift && myMultiSelectionEnabled)
181       myAISContext->ShiftSelect();
182     else
183       myAISContext->Select();
184   }
185   else
186   {
187     if (aHasShift && myMultiSelectionEnabled)
188       myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
189                                 myEndPnt.x(), myEndPnt.y(),
190                                 aView->getViewPort()->getView(), Standard_False );
191     else
192       myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
193                            myEndPnt.x(), myEndPnt.y(),
194                            aView->getViewPort()->getView(), Standard_False );
195
196     int Nb = myAISContext->NbSelected();
197     if( Nb>1 && !myMultiSelectionEnabled )
198     {
199         myAISContext->InitSelected();
200         Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
201         if( !anOwner.IsNull() )
202         {
203             myAISContext->ClearSelected( Standard_False );
204             myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
205         }
206     }
207
208     myAISContext->UpdateCurrentViewer();
209   }
210   emit selectionChanged();
211 }
212
213
214 //*********************************************************************
215 void OCCViewer_Viewer::enableSelection(bool isEnabled)
216 {
217   mySelectionEnabled = isEnabled;
218   //!! To be done for view windows
219   if ( !myViewManager )
220     return;
221
222   QPtrVector<SUIT_ViewWindow> wins = myViewManager->getViews();
223   for ( int i = 0; i < wins.count(); i++ )
224   {
225     OCCViewer_ViewWindow* win = ::qt_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
226     if ( win )
227       win->updateEnabledDrawMode();
228   }
229 }
230
231 //*********************************************************************
232 void OCCViewer_Viewer::enableMultiselection(bool isEnable)
233 {
234   myMultiSelectionEnabled = isEnable;
235   //!! To be done for view windows
236   if ( !myViewManager )
237     return;
238
239   QPtrVector<SUIT_ViewWindow> wins = myViewManager->getViews();
240   for ( int i = 0; i < wins.count(); i++ )
241   {
242     OCCViewer_ViewWindow* win = ::qt_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
243     if ( win )
244       win->updateEnabledDrawMode();
245   }
246 }
247
248 //*********************************************************************
249 void OCCViewer_Viewer::contextMenuPopup(QPopupMenu* thePopup)
250 {
251   thePopup->insertItem( tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
252   thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
253
254   thePopup->insertSeparator();
255
256   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
257   if ( aView && !aView->getToolBar()->isVisible() )
258     thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
259 }
260
261 void OCCViewer_Viewer::onDumpView()
262 {
263   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
264   if ( aView )
265     aView->onDumpView();
266 }
267
268 //*********************************************************************
269 void OCCViewer_Viewer::onChangeBgColor()
270 {
271   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
272   if( !aView )
273     return;
274   OCCViewer_ViewPort3d* aViewPort3d = aView->getViewPort();
275   if( !aViewPort3d )
276     return;
277   QColor aColorActive = aViewPort3d->backgroundColor();
278
279   QColor selColor = QColorDialog::getColor( aColorActive, aView);
280   if ( selColor.isValid() )
281     aViewPort3d->setBackgroundColor(selColor);
282 }
283
284 //*********************************************************************
285 void OCCViewer_Viewer::onShowToolbar() {
286   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
287   if ( aView )
288     aView->getToolBar()->show();    
289 }
290
291 //*********************************************************************
292 void OCCViewer_Viewer::update()
293 {
294   if (!myV3dViewer.IsNull())
295     myV3dViewer->Update();
296 }
297
298 //*********************************************************************
299 void OCCViewer_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
300 {
301   theList.Clear();
302   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
303     theList.Append(myAISContext->SelectedInteractive());
304 }
305
306 //*********************************************************************
307 void OCCViewer_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
308 {
309   AIS_ListIteratorOfListOfInteractive aIt;
310   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
311     myAISContext->SetSelected(aIt.Value(), false);
312   myAISContext->UpdateCurrentViewer();
313 }
314
315 //*********************************************************************
316 void OCCViewer_Viewer::performSelectionChanged()
317 {
318     emit selectionChanged();
319 }
320
321 //****************************************************************
322
323 void OCCViewer_Viewer::onClearViewAspects()
324 {
325     clearViewAspects();
326 }
327
328 //****************************************************************
329
330 void OCCViewer_Viewer::clearViewAspects()
331 {
332         myViewAspects.clear();
333 }
334
335 //****************************************************************
336
337 const viewAspectList& OCCViewer_Viewer::getViewAspects()
338 {
339         return myViewAspects;
340 }
341
342 //****************************************************************
343
344 void OCCViewer_Viewer::appendViewAspect( const viewAspect& aParams )
345 {
346         myViewAspects.append( aParams );
347 }
348
349 //****************************************************************
350
351 void OCCViewer_Viewer::updateViewAspects( const viewAspectList& aViewList )
352 {
353         myViewAspects = aViewList;
354 }
355
356 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
357                                   bool hilight, bool update )
358 {
359   bool isInLocal = myAISContext->HasOpenedContext();
360   if( !obj.IsNull() )
361     if( !isInLocal )
362     {
363       if ( hilight && !myAISContext->IsSelected( obj ) )
364         myAISContext->AddOrRemoveCurrentObject( obj, false );
365       else if ( !hilight && myAISContext->IsSelected( obj ) )
366         myAISContext->AddOrRemoveCurrentObject( obj, false );
367     }
368
369   if ( update )
370     myV3dViewer->Redraw();
371     
372   return false;
373 }
374
375 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
376 {
377   if ( myAISContext->HasOpenedContext() )
378     myAISContext->ClearSelected( updateviewer );
379   else
380     myAISContext->ClearCurrents( updateviewer );
381   return false;
382 }
383
384 bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
385                                    bool onlyInViewer )
386 {
387   AIS_ListOfInteractive List;
388   myAISContext->DisplayedObjects(List);
389
390   if( !onlyInViewer )
391   {
392     AIS_ListOfInteractive List1;
393     myAISContext->ObjectsInCollector(List1);
394     List.Append(List1);
395   }
396
397   AIS_ListIteratorOfListOfInteractive ite(List);
398   for ( ; ite.More(); ite.Next() )
399     if( ite.Value()==obj )
400       return true;
401
402   return false;
403 }
404
405 bool OCCViewer_Viewer::isVisible( const Handle(AIS_InteractiveObject)& obj )
406 {
407   return myAISContext->IsDisplayed( obj );
408 }
409
410 void OCCViewer_Viewer::setColor( const Handle(AIS_InteractiveObject)& obj,
411                                  const QColor& color,
412                                  bool update )
413 {
414   if( !obj.IsNull() )
415   {
416     Quantity_Color CSFColor = Quantity_Color ( color.red() / 255.,
417                                                color.green() / 255.,
418                                                color.blue() / 255.,
419                                                Quantity_TOC_RGB );
420     obj->SetColor( CSFColor );
421   }
422
423   if( update )
424     myV3dViewer->Update();
425 }
426
427 void OCCViewer_Viewer::switchRepresentation( const Handle(AIS_InteractiveObject)& obj,
428                                              int mode, bool update )
429 {
430   myAISContext->SetDisplayMode( obj, (Standard_Integer)mode, true );
431   if( update )
432     myV3dViewer->Update();
433 }
434
435 void OCCViewer_Viewer::setTransparency( const Handle(AIS_InteractiveObject)& obj,
436                                         float trans, bool update )
437 {
438   myAISContext->SetTransparency( obj, trans, false );
439   myAISContext->Redisplay( obj, Standard_False, Standard_True );
440   if( update )
441     myV3dViewer->Update();
442 }
443
444 //****************************************************************
445 void OCCViewer_Viewer::toggleTrihedron()
446 {
447   setTrihedronShown( !isTrihedronVisible() );
448 }
449
450 bool OCCViewer_Viewer::isTrihedronVisible() const
451 {
452   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed( myTrihedron );
453 }
454
455 void OCCViewer_Viewer::setTrihedronShown( const bool on )
456 {
457   if ( myTrihedron.IsNull() )
458     return;
459
460   if ( on )
461     myAISContext->Display( myTrihedron );
462   else
463     myAISContext->Erase( myTrihedron );
464 }
465
466 int OCCViewer_Viewer::trihedronSize() const
467 {
468   int sz = 0;
469   if ( !myTrihedron.IsNull() )
470     sz = (int)myTrihedron->Size();
471   return sz;
472 }
473
474 void OCCViewer_Viewer::setTrihedronSize( const int sz )
475 {
476   if ( !myTrihedron.IsNull() )
477     myTrihedron->SetSize( sz );
478 }
479
480 void OCCViewer_Viewer::setIsos( const int u, const int v )
481 {
482   Handle(AIS_InteractiveContext) ic = getAISContext();
483   if ( ic.IsNull() )
484   return;
485
486   ic->SetIsoNumber( u, AIS_TOI_IsoU );
487   ic->SetIsoNumber( v, AIS_TOI_IsoV );
488 }
489
490 void OCCViewer_Viewer::isos( int& u, int& v ) const
491 {
492   Handle(AIS_InteractiveContext) ic = getAISContext();
493   if ( !ic.IsNull() )
494   {
495     u = ic->IsoNumber( AIS_TOI_IsoU );
496     v = ic->IsoNumber( AIS_TOI_IsoV );
497   }
498 }