Salome HOME
Issue #273: Add copyright string
[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   return mySelector->viewer()->getViewer3d();
56 }
57
58 //**********************************************
59 Handle(V3d_View) NewGeom_SalomeViewer::activeView() const
60 {
61   OCCViewer_Viewer* aViewer = mySelector->viewer();
62   SUIT_ViewManager* aMgr = aViewer->getViewManager();
63   OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
64   return aWnd->getViewPort()->getView();
65 }
66
67 //**********************************************
68 void NewGeom_SalomeViewer::setSelector(NewGeom_OCCSelector* theSel)
69 {
70   if (mySelector) {
71     if (mySelector == theSel)
72       return;
73     else {
74       mySelector->viewer()->getViewManager()->disconnect(this);
75       OCCViewer_Viewer* aViewer = mySelector->viewer();
76       if (aViewer)
77         aViewer->disconnect(this);
78     }
79   }
80   mySelector = theSel;
81   if (!mySelector)
82     return;
83   OCCViewer_Viewer* aViewer = mySelector->viewer();
84   SUIT_ViewManager* aMgr = aViewer->getViewManager();
85
86   myView = new NewGeom_SalomeView(mySelector->viewer());
87
88   // TODO: Provide ModuleBase_IViewWindow interface
89   connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
90
91   connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewWindow*)), 
92           this, SLOT(onTryCloseView(SUIT_ViewWindow*)));
93   connect(aMgr, SIGNAL(deleteView(SUIT_ViewWindow*)), 
94           this, SLOT(onDeleteView(SUIT_ViewWindow*)));
95   connect(aMgr, SIGNAL(viewCreated(SUIT_ViewWindow*)), 
96           this, SLOT(onViewCreated(SUIT_ViewWindow*)));
97   connect(aMgr, SIGNAL(activated(SUIT_ViewWindow*)), 
98           this, SLOT(onActivated(SUIT_ViewWindow*)));
99
100   connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
101           SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
102   connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
103           SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
104   connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
105           SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
106   connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
107           SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
108
109   connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
110           SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
111   connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
112           SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
113
114   connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
115 }
116
117 //**********************************************
118 void NewGeom_SalomeViewer::onSelectionChanged()
119 {
120   // Selection event must be sent only after mouse release
121   myIsSelectionChanged = true;
122 }
123
124 //**********************************************
125 void NewGeom_SalomeViewer::onMousePress(SUIT_ViewWindow*, QMouseEvent* theEvent)
126 {
127   emit mousePress(myView, theEvent);
128 }
129
130 //**********************************************
131 void NewGeom_SalomeViewer::onMouseRelease(SUIT_ViewWindow*, QMouseEvent* theEvent)
132 {
133   emit mouseRelease(myView, theEvent);
134   if (myIsSelectionChanged) {
135     emit selectionChanged();
136     myIsSelectionChanged = false;
137   }
138 }
139
140 //**********************************************
141 void NewGeom_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent* theEvent)
142 {
143   emit mouseDoubleClick(myView, theEvent);
144 }
145
146 //**********************************************
147 void NewGeom_SalomeViewer::onMouseMove(SUIT_ViewWindow*, QMouseEvent* theEvent)
148 {
149   emit mouseMove(myView, theEvent);
150 }
151
152 //**********************************************
153 void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow*, QKeyEvent* theEvent)
154 {
155   emit keyPress(myView, theEvent);
156 }
157
158 //**********************************************
159 void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow*, QKeyEvent* theEvent)
160 {
161   emit keyRelease(myView, theEvent);
162 }
163
164 //**********************************************
165 void NewGeom_SalomeViewer::onTryCloseView(SUIT_ViewWindow*)
166 {
167   emit tryCloseView(myView);
168 }
169
170 //**********************************************
171 void NewGeom_SalomeViewer::onDeleteView(SUIT_ViewWindow*)
172 {
173   emit deleteView(myView);
174 }
175
176 //**********************************************
177 void NewGeom_SalomeViewer::onViewCreated(SUIT_ViewWindow*)
178 {
179   emit viewCreated(myView);
180 }
181
182 //**********************************************
183 void NewGeom_SalomeViewer::onActivated(SUIT_ViewWindow*)
184 {
185   emit activated(myView);
186 }
187
188 //**********************************************
189 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
190 {
191   mySelector->viewer()->enableSelection(isEnabled);
192   // there is a fix for a black-colored window 
193   // the viewer rubber band is valid if the values delta is less than 1
194   // TODO: remove this row after moving to SALOME 7.5
195   mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
196                                                       : SUIT_ViewModel::KEY_FREE);
197 }
198
199 //**********************************************
200 bool NewGeom_SalomeViewer::isSelectionEnabled() const
201 {
202   return mySelector->viewer()->isSelectionEnabled();
203 }
204
205 //**********************************************
206 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
207 {
208   mySelector->viewer()->enableMultiselection(isEnable);
209 }
210
211 //**********************************************
212 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
213 {
214   return mySelector->viewer()->isMultiSelectionEnabled();
215 }
216
217 //**********************************************
218 void NewGeom_SalomeViewer::fitAll()
219 {
220   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
221   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
222   if (aVFrame) {
223     aVFrame->onFitAll();
224   }
225 }
226
227 //**********************************************
228 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ)
229 {
230   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
231   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
232   if (aVFrame) {
233     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
234     if (!aView3d.IsNull()) {
235       aView3d->SetProj(theX, theY, theZ);
236       aView3d->FitAll(0.01, true, true);
237       aView3d->SetZSize(0.);
238     }
239   }
240 }
241
242 //***************************************
243 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
244 {
245   Handle(AIS_InteractiveContext) aContext = AISContext();
246   if (!aContext.IsNull()) {
247     aContext->AddFilter(theFilter);
248   }
249 }
250
251 //***************************************
252 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
253 {
254   Handle(AIS_InteractiveContext) aContext = AISContext();
255   if (!aContext.IsNull()) {
256     aContext->RemoveFilter(theFilter);
257   }
258 }
259
260 //***************************************
261 void NewGeom_SalomeViewer::clearSelectionFilters()
262 {
263   Handle(AIS_InteractiveContext) aContext = AISContext();
264   if (!aContext.IsNull()) {
265     aContext->RemoveFilters();
266   }
267 }