Salome HOME
8053c9712bd8f74a8c74f967ce548611a51a6721
[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
9 #include <V3d_View.hxx>
10
11 #include <Aspect_DisplayConnection.hxx>
12 #include <Graphic3d.hxx>
13 #include <Graphic3d_GraphicDriver.hxx>
14 #include <Geom_Axis2Placement.hxx>
15 #include <AIS_Drawer.hxx>
16 #include <Prs3d_DatumAspect.hxx>
17 #include <Prs3d_LineAspect.hxx>
18 #include <V3d_View.hxx>
19 #include <Visual3d_View.hxx>
20
21 #ifdef WIN32
22 #include <WNT_Window.hxx>
23 #else
24 #include <Xw_Window.hxx>
25 #endif
26
27 XGUI_Viewer::InteractionStyle2StatesMap XGUI_Viewer::myStateMap;
28 XGUI_Viewer::InteractionStyle2ButtonsMap XGUI_Viewer::myButtonMap;
29 static bool isInitialized = false;
30
31 /*!
32  Creates viewer 3d [ static ]
33  */
34 Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CString displayName,
35                                 const Standard_CString domain, const Standard_Real viewSize,
36                                 const V3d_TypeOfOrientation viewProjection,
37                                 const Standard_Boolean computedMode,
38                                 const Standard_Boolean defaultComputedMode)
39 {
40   static Handle(Graphic3d_GraphicDriver) aGraphicDriver;
41   if (aGraphicDriver.IsNull()) {
42     Handle(Aspect_DisplayConnection) aDisplayConnection;
43 #ifndef WIN32
44     aDisplayConnection = new Aspect_DisplayConnection( displayName );
45 #else
46     aDisplayConnection = new Aspect_DisplayConnection();
47 #endif
48     aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
49   }
50
51   return new V3d_Viewer(aGraphicDriver, name, domain, viewSize, viewProjection, Quantity_NOC_GRAY30,
52                         V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, computedMode, defaultComputedMode,
53                         V3d_TEX_NONE);
54 }
55
56 // VSR: Uncomment below line to allow texture background support in OCC viewer
57 #define OCC_ENABLE_TEXTURED_BACKGROUND
58
59 /*!
60  Get data for supported background modes: gradient types, identifiers and supported image formats
61  */
62 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
63 {
64   gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT")
65       << tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT")
66       << tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT")
67       << tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
68   idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
69       << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
70       << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
71 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
72   txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
73 #endif
74   return tr("BG_IMAGE_FILES");
75 }
76
77 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
78     : QObject(theParent), myMainWindow(theParent), myPreselectionEnabled(true), mySelectionEnabled(
79         true), myMultiSelectionEnabled(true), myIsRelative(true), myInteractionStyle(
80         XGUI::STANDARD), myTrihedronSize(100)
81 {
82   if (!isInitialized) {
83     isInitialized = true;
84
85     // standard interaction style
86     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::ControlModifier;
87     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ZOOM] = Qt::LeftButton;
88
89     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::PAN] = Qt::ControlModifier;
90     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::PAN] = Qt::MidButton;
91
92     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::ControlModifier;
93     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::ROTATE] = Qt::RightButton;
94
95     XGUI_Viewer::myStateMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::ControlModifier;
96     XGUI_Viewer::myButtonMap[XGUI::STANDARD][XGUI::FIT_AREA] = Qt::RightButton;
97
98     // "key free" interaction style
99     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::NoModifier;
100     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ZOOM] = Qt::RightButton;
101
102     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::NoModifier;
103     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::PAN] = Qt::MidButton;
104
105     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
106     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
107
108     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
109     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
110   }
111
112   // init CasCade viewers
113   myV3dViewer = CreateViewer(TCollection_ExtendedString("Viewer3d").ToExtString(), "", "", 1000.0,
114                              V3d_XposYnegZpos, Standard_True, Standard_True);
115   myV3dViewer->SetDefaultLights();
116
117   // init selector
118   myAISContext = new AIS_InteractiveContext(myV3dViewer);
119   myAISContext->SelectionColor(Quantity_NOC_WHITE);
120
121   // display isoline on planar faces (box for ex.)
122   myAISContext->IsoOnPlane(true);
123
124   if (DisplayTrihedron) {
125     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
126     myTrihedron = new AIS_Trihedron(anAxis);
127     myTrihedron->SetInfiniteState( Standard_True);
128
129     Quantity_Color Col(193 / 255., 205 / 255., 193 / 255., Quantity_TOC_RGB);
130     myTrihedron->SetArrowColor(Col.Name());
131     myTrihedron->SetSize(myTrihedronSize);
132     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
133     if (drawer->HasDatumAspect()) {
134       Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
135       daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
136       daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
137       daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
138     }
139   }
140   // set zooming style to standard
141   //myZoomingStyle = 0;
142 }
143
144 XGUI_Viewer::~XGUI_Viewer(void)
145 {
146   myAISContext.Nullify();
147   myV3dViewer.Nullify();
148 }
149
150 QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
151 {
152   // create view frame
153   XGUI_ViewWindow* view = new XGUI_ViewWindow(this, theType);
154   // get main view window (created by view frame)
155   //OCCViewer_ViewWindow* vw = view->getView(OCCViewer_ViewFrame::MAIN_VIEW);
156   // initialize main view window
157   //initView( vw );
158   // set default background for view window
159   //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
160   //// connect signal from viewport
161   connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
162   connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
163
164   view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::green, Qt::blue));
165
166   QMdiArea* aMDI = myMainWindow->mdiArea();
167   QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
168   aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
169   aWnd->show();
170   return aWnd;
171 }
172
173 /*! Sets hot button
174  *\param theOper - hot operation
175  *\param theState - adding state to state map operations.
176  *\param theButton - adding state to button map operations.
177  */
178 void XGUI_Viewer::setHotButton(XGUI::InteractionStyle theInteractionStyle,
179                                XGUI::HotOperation theOper, Qt::KeyboardModifiers theState,
180                                Qt::MouseButtons theButton)
181 {
182   myStateMap[theInteractionStyle][theOper] = theState;
183   myButtonMap[theInteractionStyle][theOper] = theButton;
184 }
185
186 /*! Gets hot button for operation \a theOper.
187  *\param theOper - input hot operation
188  *\param theState - output state from state map operations.
189  *\param theButton - output state from button map operations.
190  */
191 void XGUI_Viewer::getHotButton(XGUI::InteractionStyle theInteractionStyle,
192                                XGUI::HotOperation theOper, Qt::KeyboardModifiers& theState,
193                                Qt::MouseButtons& theButton)
194 {
195   theState = myStateMap[theInteractionStyle][theOper];
196   theButton = myButtonMap[theInteractionStyle][theOper];
197 }
198
199 /*!
200  Changes visibility of trihedron to opposite
201  */
202 void XGUI_Viewer::toggleTrihedron()
203 {
204   setTrihedronShown(!isTrihedronVisible());
205 }
206
207 /*!
208  \return true if trihedron is visible
209  */
210 bool XGUI_Viewer::isTrihedronVisible() const
211 {
212   return !myTrihedron.IsNull() && !myAISContext.IsNull() && myAISContext->IsDisplayed(myTrihedron);
213 }
214
215 /*!
216  Sets visibility state of trihedron
217  \param on - new state
218  */
219
220 void XGUI_Viewer::setTrihedronShown(const bool on)
221 {
222   if (myTrihedron.IsNull())
223     return;
224
225   if (on) {
226     myAISContext->Display(myTrihedron);
227     myAISContext->Deactivate(myTrihedron);
228   } else {
229     myAISContext->Erase(myTrihedron);
230   }
231 }
232
233 /*!
234  \return trihedron size
235  */
236 double XGUI_Viewer::trihedronSize() const
237 {
238   double sz = 0;
239   if (!myTrihedron.IsNull())
240     sz = myTrihedron->Size();
241   return sz;
242 }
243
244 /*!
245  Changes trihedron size
246  \param sz - new size
247  */
248 void XGUI_Viewer::setTrihedronSize(const double sz, bool isRelative)
249 {
250   if (myTrihedronSize != sz || isRelative != myIsRelative) {
251     myTrihedronSize = sz;
252     myIsRelative = isRelative;
253     updateTrihedron();
254   }
255 }
256
257 /*! 
258  * Update the size of the trihedron
259  */
260 void XGUI_Viewer::updateTrihedron()
261 {
262   if (myTrihedron.IsNull())
263     return;
264
265   if (myIsRelative) {
266     double newSz, oldSz;
267
268     if (computeTrihedronSize(newSz, oldSz))
269       myTrihedron->SetSize(newSz);
270
271   } else if (myTrihedron->Size() != myTrihedronSize) {
272     myTrihedron->SetSize(myTrihedronSize);
273   }
274 }
275
276 /*!
277  Get new and current trihedron size corresponding to the current model size
278  */
279 bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
280 {
281   theNewSize = 100;
282   theSize = 100;
283
284   //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
285   Handle(V3d_Viewer) viewer = v3dViewer();
286   viewer->InitActiveViews();
287   if (!viewer->MoreActiveViews())
288     return false;
289
290   Handle(V3d_View) view3d = viewer->ActiveView();
291   //SRN: END of fix
292
293   if (view3d.IsNull())
294     return false;
295
296   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
297   double aMaxSide;
298
299   view3d->View()->MinMaxValues(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
300
301   if (Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() || Xmax == RealLast()
302       || Ymax == RealLast() || Zmax == RealLast())
303     return false;
304
305   aMaxSide = Xmax - Xmin;
306   if (aMaxSide < Ymax - Ymin)
307     aMaxSide = Ymax - Ymin;
308   if (aMaxSide < Zmax - Zmin)
309     aMaxSide = Zmax - Zmin;
310
311   // IPAL21687
312   // The boundary box of the view may be initialized but nullified
313   // (case of infinite objects)
314   if (aMaxSide < Precision::Confusion())
315     return false;
316
317   static float EPS = 5.0E-3;
318   theSize = trihedron()->Size();
319   //theNewSize = aMaxSide*aSizeInPercents / 100.0;
320
321   return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
322 }
323
324 void XGUI_Viewer::onViewClosed()
325 {
326   Standard_Integer aViewsNb = 0;
327   for(myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews();
328       myV3dViewer->NextActiveViews())
329     ++aViewsNb;
330   if (aViewsNb < 2) {
331     //clean up presentations before last view is closed
332     myAISContext->RemoveAll(Standard_False);
333   }
334 }
335
336 void XGUI_Viewer::onViewMapped()
337 {
338   setTrihedronShown(true);
339 }
340