Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[modules/shaper.git] / src / XGUI / XGUI_Viewer.cpp
1 #include "XGUI_Viewer.h"
2 #include "XGUI_MainWindow.h"
3 #include "XGUI_ViewWindow.h"
4 #include "XGUI_ViewPort.h"
5
6 #include <QMdiArea>
7 #include <QMdiSubWindow>
8 #include <QApplication>
9
10 #include <V3d_View.hxx>
11
12 #include <Aspect_DisplayConnection.hxx>
13 #include <Graphic3d.hxx>
14 #include <Graphic3d_GraphicDriver.hxx>
15 #include <Geom_Axis2Placement.hxx>
16 #include <AIS_Drawer.hxx>
17 #include <Prs3d_DatumAspect.hxx>
18 #include <Prs3d_LineAspect.hxx>
19 #include <V3d_View.hxx>
20 #include <Visual3d_View.hxx>
21 #include <AIS_ListOfInteractive.hxx>
22 #include <AIS_ListIteratorOfListOfInteractive.hxx>
23 #include <AIS_Shape.hxx>
24
25 #include <QMouseEvent>
26
27 #ifdef WIN32
28 #include <WNT_Window.hxx>
29 #else
30 #include <Xw_Window.hxx>
31 #endif
32
33 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
34 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
35 static bool isInitialized = false;
36
37 /*!
38  Creates viewer 3d [ static ]
39  */
40 Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CString displayName,
41                                 const Standard_CString domain, const Standard_Real viewSize,
42                                 const V3d_TypeOfOrientation viewProjection,
43                                 const Standard_Boolean computedMode,
44                                 const Standard_Boolean defaultComputedMode)
45 {
46   static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
47   if (aGraphicDriver.IsNull()) {
48     Handle(Aspect_DisplayConnection) aDisplayConnection;
49 #ifndef WIN32
50     aDisplayConnection = new Aspect_DisplayConnection( displayName );
51 #else
52     aDisplayConnection = new Aspect_DisplayConnection();
53 #endif
54     aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
55   }
56
57   return new V3d_Viewer(aGraphicDriver, name, domain, viewSize, viewProjection, Quantity_NOC_GRAY30,
58                         V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
59                         V3d_TEX_NONE);
60 }
61
62 // VSR: Uncomment below line to allow texture background support in OCC viewer
63 #define OCC_ENABLE_TEXTURED_BACKGROUND
64
65 /*!
66  Get data for supported background modes: gradient types, identifiers and supported image formats
67  */
68 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
69 {
70   gradList << tr("Horizontal gradient") << tr("Vertical gradient")
71       << tr("First diagonal gradient") << tr("Second diagonal gradient")
72       << tr("First corner gradient") << tr("Second corner gradient")
73       << tr("Third corner gradient") << tr("Fourth corner gradient");
74   idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
75       << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
76       << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
77 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
78   txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
79 #endif
80   return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
81 }
82
83 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
84     : QObject(theParent), 
85     myMainWindow(theParent), 
86     myPreselectionEnabled(true), 
87     mySelectionEnabled(true), 
88     myMultiSelectionEnabled(true), 
89     myIsRelative(true), 
90     myInteractionStyle(XGUI::STANDARD), 
91     myTrihedronSize(100),
92     myActiveView(0)
93 {
94   if (!isInitialized) {
95     isInitialized = true;
96
97     // standard interaction style
98     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
99     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
100
101     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
102     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
103
104     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
105     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
106
107     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
108     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
109
110     // "key free" interaction style
111     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
112     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
113
114     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
115     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
116
117     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
118     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
119
120     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
121     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
122   }
123
124   // init CasCade viewers
125   myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
126                              V3d_XposYnegZpos, Standard_True, Standard_True);
127   myV3dViewer->SetDefaultLights();
128
129   // init selector
130   myAISContext = new AIS_InteractiveContext(myV3dViewer);
131   myAISContext->SelectionColor(Quantity_NOC_WHITE);
132
133   // display isoline on planar faces (box for ex.)
134   myAISContext->IsoOnPlane(true);
135
136   if (DisplayTrihedron) {
137     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
138     myTrihedron = new AIS_Trihedron(anAxis);
139     myTrihedron->SetInfiniteState( Standard_True);
140
141     Quantity_Color Col(193 / 255., 205 / 255., 193 / 255., Quantity_TOC_RGB);
142     myTrihedron->SetArrowColor(Col.Name());
143     myTrihedron->SetSize(myTrihedronSize);
144     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
145     if (drawer->HasDatumAspect()) {
146       Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
147       daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
148       daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
149       daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
150     }
151   }
152   // set zooming style to standard
153   //myZoomingStyle = 0;
154
155   QMdiArea* aMDI = myMainWindow->mdiArea();
156   connect(aMDI, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(onWindowActivated(QMdiSubWindow*)));
157
158 }
159
160 XGUI_Viewer::~XGUI_Viewer(void)
161 {
162   myAISContext.Nullify();
163   myV3dViewer.Nullify();
164 }
165
166 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
167 {
168   // create view frame
169   XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
170   // get main view window (created by view frame)
171   //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
172   // initialize main view window
173   //initView( vw );
174   // set default background for view window
175   //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
176   //// connect signal from viewport
177     //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
178     //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
179     if (myViews.size() == 0) 
180         setTrihedronShown(true);
181
182     view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::white, QColor(Qt::blue).lighter()));
183   //view->setBackground(XGUI_ViewBackground(Qt::black));
184
185   QMdiArea* aMDI = myMainWindow->mdiArea();
186   QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
187     addView(aWnd);
188   aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
189   aWnd->show();
190   return aWnd;
191 }
192
193 void XGUI_Viewer::setLocalSelection(const AIS_ListOfInteractive& theAISObjects, const int theMode,
194                                     const bool isUpdateViewer)
195 {
196   Handle(AIS_InteractiveContext) ic = AISContext();
197
198   // Open local context if there is no one
199   bool allObjects = false; // calculate by AIS shape
200   if (!ic->HasOpenedContext()) {
201     ic->ClearCurrents(false);
202     ic->OpenLocalContext(allObjects, true, true);
203   }
204
205   // Activate selection of objects from prs
206   AIS_ListIteratorOfListOfInteractive aIter(theAISObjects);
207   for (; aIter.More(); aIter.Next()) {
208     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
209     if (!anAIS.IsNull()) {
210       if (anAIS->IsKind(STANDARD_TYPE(AIS_Shape))) {
211         ic->Load(anAIS, -1, false);
212         ic->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theMode));
213       }
214       else if (anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron)) {
215         ic->Load(anAIS, -1, false);
216         ic->Activate(anAIS, theMode);
217       }
218     }
219   }
220   if (isUpdateViewer)
221     ic->UpdateCurrentViewer();
222 }
223
224 void XGUI_Viewer::setGlobalSelection(const bool isUpdateViewer)
225 {
226   Handle(AIS_InteractiveContext) ic = AISContext();
227   if (!ic.IsNull()) {
228     ic->CloseAllContexts(false);
229     if (isUpdateViewer)
230       ic->UpdateCurrentViewer();
231   }
232 }
233
234 void XGUI_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
235 {
236   theList.Clear();
237   for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
238     theList.Append(myAISContext->SelectedInteractive());
239 }
240
241 void XGUI_Viewer::getSelectedShapes(NCollection_List<TopoDS_Shape>& theList)
242 {
243   Handle(AIS_InteractiveContext) ic = AISContext();
244
245   for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
246     TopoDS_Shape aShape = ic->SelectedShape();
247     if (!aShape.IsNull())
248       theList.Append(aShape);
249   }
250 }
251
252 void XGUI_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
253 {
254   AIS_ListIteratorOfListOfInteractive aIt;
255   for (aIt.Initialize(theList); aIt.More(); aIt.Next())
256     myAISContext->AddOrRemoveSelected(aIt.Value(), false);
257   myAISContext->UpdateCurrentViewer();
258 }
259
260 /*! Sets hot button
261  *\param theOper - hot operation
262  *\param theState - adding state to state map operations.
263  *\param theButton - adding state to button map operations.
264  */
265 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
266                                XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
267                                Qt::MouseButtons theButton)
268 {
269   myStateMap[theInteractionStyle][theOper] = theState;
270   myButtonMap[theInteractionStyle][theOper] = theButton;
271 }
272
273 /*! Gets hot button for operation \a theOper.
274  *\param theOper - input hot operation
275  *\param theState - output state from state map operations.
276  *\param theButton - output state from button map operations.
277  */
278 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
279                                XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
280                                Qt::MouseButtons& theButton)
281 {
282   theState = myStateMap[theInteractionStyle][theOper];
283   theButton = myButtonMap[theInteractionStyle][theOper];
284 }
285
286 void XGUI_Viewer::setViewProjection(double theX, double theY, double theZ)
287 {
288   XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(myActiveView->widget());
289   if (aWindow) {
290     Handle(V3d_View) aView3d = aWindow->viewPort()->getView();
291     if ( !aView3d.IsNull() ) 
292       aView3d->SetProj(theX, theY, theZ);
293     aWindow->viewPort()->fitAll();
294   }
295 }
296
297 /*!
298  Changes visibility of trihedron to opposite
299  */
300 void XGUI_Viewer::toggleTrihedron()
301 {
302   setTrihedronShown(!isTrihedronVisible());
303 }
304
305 /*!
306  \return true if trihedron is visible
307  */
308 bool XGUI_Viewer::isTrihedronVisible() const
309 {
310   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
311 }
312
313 /*!
314  Sets visibility state of trihedron
315  \param on - new state
316  */
317
318 void XGUI_Viewer::setTrihedronShown(bool on)
319 {
320   if (myTrihedron.IsNull())
321     return;
322
323   if (on) {
324     myAISContext->Display(myTrihedron);
325     myAISContext->Deactivate(myTrihedron);
326   } else {
327     myAISContext->Erase(myTrihedron);
328   }
329 }
330
331 /*!
332  \return trihedron size
333  */
334 double XGUI_Viewer::trihedronSize() const
335 {
336   double sz = 0;
337   if (!myTrihedron.IsNull())
338     sz = myTrihedron->Size();
339   return sz;
340 }
341
342 /*!
343  Changes trihedron size
344  \param sz - new size
345  */
346 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
347 {
348   if (myTrihedronSize != sz || isRelative != myIsRelative) {
349     myTrihedronSize = sz;
350     myIsRelative = isRelative;
351     updateTrihedron();
352   }
353 }
354
355 /*! 
356  * Update the size of the trihedron
357  */
358 void XGUI_Viewer::updateTrihedron()
359 {
360   if (myTrihedron.IsNull())
361     return;
362
363   if (myIsRelative) {
364     double newSz, oldSz;
365
366     if (computeTrihedronSize(newSz, oldSz))
367       myTrihedron->SetSize(newSz);
368
369   } else if (myTrihedron->Size() != myTrihedronSize) {
370     myTrihedron->SetSize(myTrihedronSize);
371   }
372 }
373
374 /*!
375  Get new and current trihedron size corresponding to the current model size
376  */
377 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
378 {
379   theNewSize = 100;
380   theSize = 100;
381
382   //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
383   Handle(V3d_Viewer) viewer = v3dViewer();
384   viewer->InitActiveViews();
385   if (!viewer->MoreActiveViews())
386     return false;
387
388   Handle(V3d_View) view3d = viewer->ActiveView();
389   //SRN: END of fix
390
391   if (view3d.IsNull())
392     return false;
393
394   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
395   double aMaxSide;
396
397   view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
398
399   if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
400       || Ymax == RealLast() || Zmax == RealLast())
401     return false;
402
403   aMaxSide = Xmax - Xmin;
404   if (aMaxSide < Ymax - Ymin)
405     aMaxSide = Ymax - Ymin;
406   if (aMaxSide < Zmax - Zmin)
407     aMaxSide = Zmax - Zmin;
408
409   // IPAL21687
410   // The boundary box of the view may be initialized but nullified
411   // (case of infinite objects)
412   if (aMaxSide < Precision::Confusion())
413     return false;
414
415   static float EPS = (float)5.0E-3;
416   theSize = trihedron()->Size();
417   //theNewSize = aMaxSide*aSizeInPercents / 100.0;
418
419   return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
420 }
421
422 void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
423 {
424     if ( !theView )
425         return;
426
427     emit deleteView( static_cast<XGUI_ViewWindow*>(theView->widget()) );
428     removeView( theView );
429
430     // if this is last view
431     if (myViews.size() == 0) {
432         Standard_Integer aViewsNb = 0;
433         for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
434             ++aViewsNb;
435         if ( aViewsNb < 2 ) {
436             //clean up presentations before last view is closed
437             myAISContext->RemoveAll(Standard_False);
438         }
439     }
440 }
441
442 /*!Remove view window \a theView from view manager.
443  *And close the last view, if it has \a theView.
444 */
445 void XGUI_Viewer::removeView( QMdiSubWindow* theView )
446 {
447     XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
448
449     aWindow->disconnect( this );
450     myViews.removeAt( myViews.indexOf( theView ) );
451     if ( myActiveView == theView )
452         myActiveView = 0;
453     if ( myViews.size() == 0 )
454         emit lastViewClosed();
455 }
456
457
458 /*void XGUI_Viewer::onViewMapped()
459 {
460   setTrihedronShown(true);
461 }*/
462
463
464 void XGUI_Viewer::addView(QMdiSubWindow* theView)
465 {
466     XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
467
468     connect(aWindow, SIGNAL(closed(QMdiSubWindow*)),
469             this,    SLOT(onViewClosed(QMdiSubWindow*)));
470
471     connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)),
472             this,    SIGNAL(tryCloseView(XGUI_ViewWindow*)));
473
474     connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)),
475             this,    SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
476
477     connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
478             this,    SIGNAL(mouseRelease(XGUI_ViewWindow*, QMouseEvent*)));
479
480     connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)),
481             this,    SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
482
483     connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
484             this,    SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
485
486     connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)),
487             this,    SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
488
489     connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)),
490             this,    SIGNAL(keyRelease(XGUI_ViewWindow*, QKeyEvent*)));
491
492 //    connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
493 //            this,    SLOT  (onContextMenuRequested( QContextMenuEvent* )));
494
495     connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)),
496             this, SLOT(onMouseMove(XGUI_ViewWindow*, QMouseEvent*)));
497
498     connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)),
499             this, SLOT(onMouseReleased(XGUI_ViewWindow*, QMouseEvent*)));
500
501     myViews.append(theView);
502 }
503
504 /*!
505     Emit activated for view \a view.
506 */
507 void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
508 {
509   if (view && (view != myActiveView) && (!view->isMinimized())) {
510     myActiveView = view;
511     ((XGUI_ViewWindow*)myActiveView->widget())->windowActivated();
512     QList<QMdiSubWindow*>::iterator aIt;
513     for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
514       if ((*aIt) != myActiveView) {
515         ((XGUI_ViewWindow*)(*aIt)->widget())->windowDeactivated();
516       }
517     }
518   }
519 }
520
521
522 void XGUI_Viewer::onWindowMinimized(QMdiSubWindow* theWnd)
523 {
524   if (myActiveView == theWnd) {
525     myActiveView = 0;
526     QList<QMdiSubWindow*>::iterator aIt;
527     for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
528       if (!(*aIt)->widget()->isMinimized()) {
529         (*aIt)->raise();
530         onWindowActivated(*aIt);
531         break;
532       }
533     }
534   }
535 }
536
537 /*!
538   SLOT: called on mouse move, processes hilighting
539 */
540 void XGUI_Viewer::onMouseMove(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
541 {
542   XGUI_ViewPort* aViewPort = theWindow->viewPort();
543   Handle(V3d_View) aView3d = aViewPort->getView();
544
545   if ( !aView3d.IsNull() )
546     myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
547 }
548
549 /*!
550   SLOT: called on mouse button release, finishes selection
551 */
552 void XGUI_Viewer::onMouseReleased(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
553 {
554   myAISContext->Select();
555
556   emit selectionChanged();
557 }