Salome HOME
c3c887ae8e71763f04ae01fdc30409815be02ff5
[modules/shaper.git] / src / NewGeom / NewGeom_SalomeViewer.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "NewGeom_SalomeViewer.h"
4 #include "NewGeom_OCCSelector.h"
5
6 #include <OCCViewer_ViewWindow.h>
7 #include <OCCViewer_ViewPort3d.h>
8 #include <OCCViewer_ViewFrame.h>
9
10 #include <SUIT_ViewManager.h>
11
12 #include <QMouseEvent>
13 #include <QContextMenuEvent>
14
15
16 Handle(V3d_View) NewGeom_SalomeView::v3dView() const
17 {
18   SUIT_ViewManager* aMgr = myViewer->getViewManager();
19   OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
20   Handle(V3d_View) aView = aWnd->getViewPort()->getView();
21   return aView;
22 }
23
24 //**********************************************
25 //**********************************************
26 //**********************************************
27
28
29
30 NewGeom_SalomeViewer::NewGeom_SalomeViewer(QObject* theParent)
31     : ModuleBase_IViewer(theParent),
32       mySelector(0), myView(0), myIsSelectionChanged(false)
33 {
34 }
35
36 NewGeom_SalomeViewer::~NewGeom_SalomeViewer()
37 {
38   if (myView)
39     delete myView;
40 }
41
42
43 //**********************************************
44 Handle(AIS_InteractiveContext) NewGeom_SalomeViewer::AISContext() const
45 {
46   if (mySelector && mySelector->viewer())
47     return mySelector->viewer()->getAISContext();
48   Handle(AIS_InteractiveContext) aNull;
49   return aNull;
50 }
51
52 //**********************************************
53 Handle(V3d_Viewer) NewGeom_SalomeViewer::v3dViewer() const
54 {
55   if (mySelector)
56     return mySelector->viewer()->getViewer3d();
57   return Handle(V3d_Viewer)();
58 }
59
60 //**********************************************
61 Handle(V3d_View) NewGeom_SalomeViewer::activeView() const
62 {
63   if (mySelector) {
64     OCCViewer_Viewer* aViewer = mySelector->viewer();
65     SUIT_ViewManager* aMgr = aViewer->getViewManager();
66     OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
67     return aWnd->getViewPort()->getView();
68   }
69   return Handle(V3d_View)();
70 }
71
72 //**********************************************
73 void NewGeom_SalomeViewer::setSelector(NewGeom_OCCSelector* theSel)
74 {
75   if (mySelector) {
76     if (mySelector == theSel)
77       return;
78     else {
79       mySelector->viewer()->getViewManager()->disconnect(this);
80       OCCViewer_Viewer* aViewer = mySelector->viewer();
81       if (aViewer)
82         aViewer->disconnect(this);
83     }
84   }
85   mySelector = theSel;
86   if (!mySelector)
87     return;
88   OCCViewer_Viewer* aViewer = mySelector->viewer();
89   SUIT_ViewManager* aMgr = aViewer->getViewManager();
90
91   myView = new NewGeom_SalomeView(mySelector->viewer());
92
93   // TODO: Provide ModuleBase_IViewWindow interface
94   connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
95
96   connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)), 
97           this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
98   connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)), 
99           this, SLOT(onDeleteView(SUIT_ViewWindow*)));
100   connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)), 
101           this, SLOT(onViewCreated(SUIT_ViewWindow*)));
102   connect(aMgr, SIGNAL(activated(SUIT_ViewWindow*)), 
103           this, SLOT(onActivated(SUIT_ViewWindow*)));
104
105   connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
106           SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
107   connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
108           SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
109   connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
110           SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
111   connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
112           SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
113
114   connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
115           SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
116   connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
117           SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
118
119   connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
120 }
121
122 //**********************************************
123 void NewGeom_SalomeViewer::onSelectionChanged()
124 {
125   // Selection event must be sent only after mouse release
126   myIsSelectionChanged = true;
127 }
128
129 //**********************************************
130 void NewGeom_SalomeViewer::onMousePress(SUIT_ViewWindow*, QMouseEvent* theEvent)
131 {
132   emit mousePress(myView, theEvent);
133 }
134
135 //**********************************************
136 void NewGeom_SalomeViewer::onMouseRelease(SUIT_ViewWindow*, QMouseEvent* theEvent)
137 {
138   emit mouseRelease(myView, theEvent);
139   if (myIsSelectionChanged) {
140     emit selectionChanged();
141     myIsSelectionChanged = false;
142   }
143 }
144
145 //**********************************************
146 void NewGeom_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent* theEvent)
147 {
148   emit mouseDoubleClick(myView, theEvent);
149 }
150
151 //**********************************************
152 void NewGeom_SalomeViewer::onMouseMove(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
153 {
154   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(theView);
155   Handle(AIS_InteractiveContext) aContext = AISContext();
156   if (aContext->HasDetected()) // Set focus to provide key events in the view
157     aViewWnd->getViewPort()->setFocus(Qt::MouseFocusReason);
158   emit mouseMove(myView, theEvent);
159 }
160
161 //**********************************************
162 bool NewGeom_SalomeViewer::canDragByMouse() const
163 {
164   OCCViewer_Viewer* aViewer = mySelector->viewer();
165   return (aViewer->interactionStyle() != 0);
166 }
167
168
169 //**********************************************
170 void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
171 {
172   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(theView);
173   Handle(AIS_InteractiveContext) aContext = AISContext();
174   Handle(V3d_View) aView = aViewWnd->getViewPort()->getView();
175
176   bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
177   if ((theEvent->key() == Qt::Key_N) && noModifiers) {
178     aContext->HilightNextDetected(aView);
179   } else if ((theEvent->key() == Qt::Key_P) && noModifiers) {
180     aContext->HilightPreviousDetected(aView);
181   }
182   emit keyPress(myView, theEvent);
183 }
184
185 //**********************************************
186 void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
187 {
188   emit keyRelease(myView, theEvent);
189 }
190
191 //**********************************************
192 void NewGeom_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
193 {
194   emit tryCloseView(myView);
195 }
196
197 //**********************************************
198 void NewGeom_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
199 {
200   emit deleteView(myView);
201 }
202
203 //**********************************************
204 void NewGeom_SalomeViewer::onViewCreated(SUIT_ViewWindow*)
205 {
206   emit viewCreated(myView);
207 }
208
209 //**********************************************
210 void NewGeom_SalomeViewer::onActivated(SUIT_ViewWindow*)
211 {
212   emit activated(myView);
213 }
214
215 //**********************************************
216 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
217 {
218   if (mySelector)
219     mySelector->viewer()->enableSelection(isEnabled);
220   // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
221   // After this is corrected, the first row should be recommented, the last - removed
222     //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
223     //                                                    : SUIT_ViewModel::KEY_FREE);
224 }
225
226 //**********************************************
227 bool NewGeom_SalomeViewer::isSelectionEnabled() const
228 {
229   if (mySelector)
230     return mySelector->viewer()->isSelectionEnabled();
231 }
232
233 //**********************************************
234 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
235 {
236   if (mySelector)
237     mySelector->viewer()->enableMultiselection(isEnable);
238 }
239
240 //**********************************************
241 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
242 {
243   if (mySelector)
244     return mySelector->viewer()->isMultiSelectionEnabled();
245   return false;
246 }
247
248 //**********************************************
249 void NewGeom_SalomeViewer::fitAll()
250 {
251   if (mySelector) {
252     SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
253     OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
254     if (aVFrame) {
255       aVFrame->onFitAll();
256     }
257   }
258 }
259
260 //**********************************************
261 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ)
262 {
263   if (!mySelector) 
264     return;
265
266   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
267   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
268   if (aVFrame) {
269     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
270     if (!aView3d.IsNull()) {
271       aView3d->SetProj(theX, theY, theZ);
272       aView3d->FitAll(0.01, true);
273       aView3d->SetZSize(0.);
274     }
275   }
276 }
277
278 //***************************************
279 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
280 {
281   Handle(AIS_InteractiveContext) aContext = AISContext();
282   if (!aContext.IsNull()) {
283     aContext->AddFilter(theFilter);
284   }
285 }
286
287 //***************************************
288 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
289 {
290   Handle(AIS_InteractiveContext) aContext = AISContext();
291   if (!aContext.IsNull()) {
292     aContext->RemoveFilter(theFilter);
293   }
294 }
295
296 //***************************************
297 void NewGeom_SalomeViewer::clearSelectionFilters()
298 {
299   Handle(AIS_InteractiveContext) aContext = AISContext();
300   if (!aContext.IsNull()) {
301     aContext->RemoveFilters();
302   }
303 }
304
305 //***************************************
306 void NewGeom_SalomeViewer::update()
307 {
308   Handle(AIS_InteractiveContext) aContext = AISContext();
309   if (!aContext.IsNull()) {
310     aContext->UpdateCurrentViewer();
311   }
312 }