Salome HOME
Update copyright information
[modules/visu.git] / src / PIPELINE / VISU_WidgetCtrl.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME VTKViewer : build VTK viewer into Salome desktop
24 //  File   : VVTK_WidgetCtrl.cxx
25 //  Author : Peter KURNEV
26 //  Module : SALOME
27 //  $Header$
28 //
29 #include "VISU_WidgetCtrl.hxx"
30 //
31 #include "VISU_ImplicitFunctionWidget.hxx"
32 #include "VISU_PlanesWidget.hxx"
33 #include "VISU_SphereWidget.hxx"
34 //
35 #include <algorithm>
36 //
37 #include <vtkObject.h>
38 #include <vtkImplicitFunction.h>
39 #include <vtkRenderWindowInteractor.h>
40 #include <vtkObjectFactory.h>
41 #include <vtkCallbackCommand.h>
42 #include <vtkCommand.h>
43 //
44 vtkCxxRevisionMacro(VISU_WidgetCtrl,"$Revision$");
45 vtkStandardNewMacro(VISU_WidgetCtrl);
46
47 //==================================================================
48 // function: VISU_WidgetCtrl
49 // purpose :
50 //==================================================================
51 VISU_WidgetCtrl::VISU_WidgetCtrl() 
52 {
53   myDummyWidget=NULL;
54   myDisableAll=0;
55   myNbWidgets=2;
56   myActiveIndex=-1;// 0 - PlanesWidget; 1 - SphereWidget
57   myPriority=0.;
58   //
59   myPlanesWidget=VISU_PlanesWidget::New();
60   mySphereWidget=VISU_SphereWidget::New();
61   //
62   myWidgets[0]=myPlanesWidget;
63   myWidgets[1]=mySphereWidget;
64   //
65   myEventCallbackCommand=vtkCallbackCommand::New();
66   myEventCallbackCommand->SetClientData(this); 
67   myEventCallbackCommand->SetCallback(VISU_WidgetCtrl::ProcessEvents);
68   //
69   int i;
70   for (i=0; i<myNbWidgets; ++i){
71     myWidgets[i]->AddObserver(vtkCommand::EnableEvent,
72                               myEventCallbackCommand,
73                               myPriority);
74     myWidgets[i]->AddObserver(vtkCommand::DisableEvent,
75                               myEventCallbackCommand,
76                               myPriority);
77     myWidgets[i]->AddObserver(vtkCommand::EndInteractionEvent,
78                               myEventCallbackCommand,
79                               myPriority);
80     myWidgets[i]->AddObserver(vtkCommand::StartInteractionEvent,
81                               myEventCallbackCommand,
82                               myPriority);
83     
84     myWidgets[i]->AddObserver(vtkCommand::InteractionEvent,
85                               myEventCallbackCommand,
86                               myPriority);
87   }
88 }
89 //==================================================================
90 // function: ~
91 // purpose :
92 //==================================================================
93 VISU_WidgetCtrl::~VISU_WidgetCtrl()
94 {  
95   
96   myPlanesWidget->Delete();
97   mySphereWidget->Delete();
98   //
99   for (int i=0; i<myNbWidgets; ++i){
100     myWidgets[i]->RemoveObserver(myEventCallbackCommand);
101   }
102   //
103   myEventCallbackCommand->Delete();
104 }
105 //==================================================================
106 // function: GetEnabled 
107 // purpose :
108 //==================================================================
109 int VISU_WidgetCtrl::GetEnabled()
110 {
111   if (HasActiveIndex()) {
112     return GetActiveWidget()->GetEnabled();
113   }
114   return 0;
115 }
116 //==================================================================
117 // function: SetEnabled
118 // purpose :
119 //==================================================================
120 void VISU_WidgetCtrl::SetEnabled(int theFlag)
121 {
122   int iFlag, i;
123   //
124   myDisableAll=0;
125   //
126   iFlag=GetEnabled();
127   if (iFlag==theFlag) {
128     return;
129   }
130   //
131   if (theFlag) {//enabling
132     if (HasActiveIndex()) {
133       for (i=0; i<myNbWidgets; ++i) {
134         iFlag=(i==myActiveIndex) ? 1 : 0;
135         myWidgets[i]->SetEnabled(iFlag);
136       }
137     }
138   }
139   else {//disabling
140     myDisableAll=1;
141     for (i=0; i<myNbWidgets; ++i) {
142       myWidgets[i]->SetEnabled(0);
143     }
144   }
145
146   Modified();
147 }
148 //==================================================================
149 // function: ProcessEvents
150 // purpose :
151 //==================================================================
152 void VISU_WidgetCtrl::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
153                                     unsigned long theEvent,
154                                     void* theClientData, 
155                                     void* vtkNotUsed(theCallData))
156 {
157   VISU_WidgetCtrl *aSelf = reinterpret_cast<VISU_WidgetCtrl*>(theClientData);
158   switch(theEvent){
159   case vtkCommand::DisableEvent:
160     if(aSelf->GetDisableAll())
161       aSelf->InvokeEvent(theEvent, NULL);
162   default:
163     aSelf->InvokeEvent(theEvent, NULL);
164     break;
165   }
166 }
167
168 //==================================================================
169 // function: GetNbWidgets
170 // purpose :
171 //==================================================================
172 int VISU_WidgetCtrl::GetNbWidgets()const 
173 {
174   return myNbWidgets;
175 }
176 //==================================================================
177 // function: GetDisableAll
178 // purpose :
179 //==================================================================
180 int VISU_WidgetCtrl::GetDisableAll()const
181 {
182   return myDisableAll;
183 }
184 //==================================================================
185 // function: SetActiveIndex
186 // purpose :
187 //==================================================================
188 void VISU_WidgetCtrl::SetActiveIndex(const int theIndex)
189 {
190   myActiveIndex=-1;
191   if (theIndex>=0 && theIndex<myNbWidgets){
192     myActiveIndex=theIndex;
193   }
194
195   Modified();
196 }
197 //==================================================================
198 // function: GetActiveIndex
199 // purpose :
200 //==================================================================
201 int VISU_WidgetCtrl::GetActiveIndex()const
202 {
203   return myActiveIndex;
204 }
205 //==================================================================
206 // function: HasActiveIndex
207 // purpose :
208 //==================================================================
209 bool VISU_WidgetCtrl::HasActiveIndex()const
210 {
211   return (myActiveIndex>=0 && myActiveIndex<myNbWidgets);
212 }
213 //==================================================================
214 // function: IsPlanesActive
215 // purpose :
216 //==================================================================
217 bool VISU_WidgetCtrl::IsPlanesActive()const
218 {
219   return (myActiveIndex==0);
220 }
221 //==================================================================
222 // function: IsSphereActive
223 // purpose :
224 //==================================================================
225 bool VISU_WidgetCtrl::IsSphereActive()const
226 {
227   return (myActiveIndex==1);
228 }
229 //==================================================================
230 // function: GetActiveWidget
231 // purpose :
232 //==================================================================
233 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetActiveWidget()
234 {
235   if (HasActiveIndex()){
236     return myWidgets[myActiveIndex];
237   }
238   return myDummyWidget;
239 }
240 //==================================================================
241 // function: GetWidget
242 // purpose :
243 //==================================================================
244 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetWidget(const int theIndex)
245 {
246   if (theIndex>=0 && theIndex<myNbWidgets) {
247     return myWidgets[theIndex];
248   }
249   return myDummyWidget;
250 }
251 //==================================================================
252 // function: GetPlanesWidget
253 // purpose :
254 //==================================================================
255 VISU_PlanesWidget* VISU_WidgetCtrl::GetPlanesWidget()
256 {
257   return myPlanesWidget;
258 }
259 //==================================================================
260 // function: GetSphereWidget
261 // purpose :
262 //==================================================================
263 VISU_SphereWidget* VISU_WidgetCtrl::GetSphereWidget()
264 {
265   return mySphereWidget;
266 }
267 //==================================================================
268 // function: PlaceWidget
269 // purpose :
270 //==================================================================
271 void VISU_WidgetCtrl::PlaceWidget(vtkFloatingPointType theBounds[6])
272 {
273   for (int i=0; i<myNbWidgets; ++i) { 
274     myWidgets[i]->PlaceWidget(theBounds);
275   }
276 }
277 //==================================================================
278 // function: SetInteractor
279 // purpose :
280 //==================================================================
281 void VISU_WidgetCtrl::SetInteractor(vtkRenderWindowInteractor* theRWI)
282 {
283   for (int i=0; i<myNbWidgets; ++i) {
284     myWidgets[i]->SetInteractor(theRWI);
285   }
286
287   Modified();
288 }
289 //==================================================================
290 // function: GetInteractor
291 // purpose :
292 //==================================================================
293 vtkRenderWindowInteractor* VISU_WidgetCtrl::GetInteractor()
294 {
295   return myWidgets[0]->GetInteractor();
296 }
297 //==================================================================
298 // function: SetPlaceFactor
299 // purpose :
300 //==================================================================
301 void VISU_WidgetCtrl::SetPlaceFactor(vtkFloatingPointType theFactor)
302 {
303   for (int i=0; i<myNbWidgets; ++i) {
304     myWidgets[i]->SetPlaceFactor(theFactor);
305   }
306
307   Modified();
308 }
309 //==================================================================
310 // function: GetPlaceFactor
311 // purpose :
312 //==================================================================
313 vtkFloatingPointType VISU_WidgetCtrl::GetPlaceFactor()
314 {
315   return myWidgets[0]->GetPlaceFactor();
316 }
317 //==================================================================
318 // function: ImplicitFunction
319 // purpose :
320 //==================================================================
321 vtkImplicitFunction* VISU_WidgetCtrl::ImplicitFunction()
322 {
323   return this;
324 }
325
326 //==================================================================
327 vtkFloatingPointType    
328 VISU_WidgetCtrl
329 ::EvaluateFunction(vtkFloatingPointType theX[3])
330 {
331   if(VISU_ImplicitFunctionWidget* aWidget = GetActiveWidget()){
332     if(vtkImplicitFunction* aFunction = aWidget->ImplicitFunction())
333       return aFunction->EvaluateFunction(theX[0],theX[1],theX[2]);
334   }
335   return 1.0;
336 }
337
338 //==================================================================
339 void    
340 VISU_WidgetCtrl
341 ::EvaluateGradient(vtkFloatingPointType theX[3], 
342                    vtkFloatingPointType theG[3])
343 {
344   theG[0] = theG[1] = theG[2] = 0.0;
345   if(VISU_ImplicitFunctionWidget* aWidget = GetActiveWidget()){
346     if(vtkImplicitFunction* aFunction = aWidget->ImplicitFunction())
347       aFunction->EvaluateGradient(theX,theG);
348   }
349 }
350
351 //==================================================================
352 unsigned long
353 VISU_WidgetCtrl
354 ::GetMTime()
355 {
356   unsigned long aTime = Superclass::GetMTime();
357
358   if(vtkImplicitFunction* aFunction = myPlanesWidget->ImplicitFunction())
359     aTime = std::max(aTime,aFunction->GetMTime());
360
361   if(vtkImplicitFunction* aFunction = mySphereWidget->ImplicitFunction())
362     aTime = std::max(aTime,aFunction->GetMTime());
363   
364   return aTime;
365 }