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