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