]> SALOME platform Git repositories - modules/shaper.git/blob - src/NewGeom/NewGeom_SalomeViewer.cpp
Salome HOME
Issue #374:Prevent switching viewer interaction mode. Clear operations on module...
[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())
157     aViewWnd->getViewPort()->setFocus(Qt::MouseFocusReason);
158   emit mouseMove(myView, theEvent);
159 }
160
161 //**********************************************
162 void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
163 {
164   OCCViewer_ViewWindow* aViewWnd = dynamic_cast<OCCViewer_ViewWindow*>(theView);
165   Handle(AIS_InteractiveContext) aContext = AISContext();
166   Handle(V3d_View) aView = aViewWnd->getViewPort()->getView();
167
168   bool noModifiers = (theEvent->modifiers() == Qt::NoModifier);
169   if ((theEvent->key() == Qt::Key_N) && noModifiers) {
170     aContext->HilightNextDetected(aView);
171   } else if ((theEvent->key() == Qt::Key_P) && noModifiers) {
172     aContext->HilightPreviousDetected(aView);
173   }
174   emit keyPress(myView, theEvent);
175 }
176
177 //**********************************************
178 void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow* theView, QKeyEvent* theEvent)
179 {
180   emit keyRelease(myView, theEvent);
181 }
182
183 //**********************************************
184 void NewGeom_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
185 {
186   emit tryCloseView(myView);
187 }
188
189 //**********************************************
190 void NewGeom_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
191 {
192   emit deleteView(myView);
193 }
194
195 //**********************************************
196 void NewGeom_SalomeViewer::onViewCreated(SUIT_ViewWindow*)
197 {
198   emit viewCreated(myView);
199 }
200
201 //**********************************************
202 void NewGeom_SalomeViewer::onActivated(SUIT_ViewWindow*)
203 {
204   emit activated(myView);
205 }
206
207 //**********************************************
208 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
209 {
210   if (mySelector)
211     mySelector->viewer()->enableSelection(isEnabled);
212   // The enableSelection() in SALOME 7.5 cause of forced Viewer update(we have blinking)
213   // After this is corrected, the first row should be recommented, the last - removed
214     //mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
215     //                                                    : SUIT_ViewModel::KEY_FREE);
216 }
217
218 //**********************************************
219 bool NewGeom_SalomeViewer::isSelectionEnabled() const
220 {
221   if (mySelector)
222     return mySelector->viewer()->isSelectionEnabled();
223 }
224
225 //**********************************************
226 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
227 {
228   if (mySelector)
229     mySelector->viewer()->enableMultiselection(isEnable);
230 }
231
232 //**********************************************
233 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
234 {
235   if (mySelector)
236     return mySelector->viewer()->isMultiSelectionEnabled();
237   return false;
238 }
239
240 //**********************************************
241 void NewGeom_SalomeViewer::fitAll()
242 {
243   if (mySelector) {
244     SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
245     OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
246     if (aVFrame) {
247       aVFrame->onFitAll();
248     }
249   }
250 }
251
252 //**********************************************
253 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ)
254 {
255   if (!mySelector) 
256     return;
257
258   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
259   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
260   if (aVFrame) {
261     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
262     if (!aView3d.IsNull()) {
263       aView3d->SetProj(theX, theY, theZ);
264       aView3d->FitAll(0.01, true);
265       aView3d->SetZSize(0.);
266     }
267   }
268 }
269
270 //***************************************
271 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
272 {
273   Handle(AIS_InteractiveContext) aContext = AISContext();
274   if (!aContext.IsNull()) {
275     aContext->AddFilter(theFilter);
276   }
277 }
278
279 //***************************************
280 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
281 {
282   Handle(AIS_InteractiveContext) aContext = AISContext();
283   if (!aContext.IsNull()) {
284     aContext->RemoveFilter(theFilter);
285   }
286 }
287
288 //***************************************
289 void NewGeom_SalomeViewer::clearSelectionFilters()
290 {
291   Handle(AIS_InteractiveContext) aContext = AISContext();
292   if (!aContext.IsNull()) {
293     aContext->RemoveFilters();
294   }
295 }
296
297 //***************************************
298 void NewGeom_SalomeViewer::update()
299 {
300   Handle(AIS_InteractiveContext) aContext = AISContext();
301   if (!aContext.IsNull()) {
302     aContext->UpdateCurrentViewer();
303   }
304 }