Salome HOME
IMP 0017328: min and max scalar map of results given at gauss points. A fix for the...
[modules/visu.git] / src / PIPELINE / VISU_WidgetCtrl.cxx
1 //  Copyright (C) 2007-2008  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 //  SALOME VTKViewer : build VTK viewer into Salome desktop
23 //  File   : VVTK_WidgetCtrl.cxx
24 //  Author : Peter KURNEV
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "VISU_WidgetCtrl.hxx"
29 //
30 #include "VISU_ImplicitFunctionWidget.hxx"
31 #include "VISU_PlanesWidget.hxx"
32 #include "VISU_SphereWidget.hxx"
33 //
34 #include <algorithm>
35 //
36 #include <vtkObject.h>
37 #include <vtkImplicitFunction.h>
38 #include <vtkRenderWindowInteractor.h>
39 #include <vtkObjectFactory.h>
40 #include <vtkCallbackCommand.h>
41 #include <vtkCommand.h>
42 //
43 vtkCxxRevisionMacro(VISU_WidgetCtrl,"$Revision$");
44 vtkStandardNewMacro(VISU_WidgetCtrl);
45
46 //==================================================================
47 // function: VISU_WidgetCtrl
48 // purpose :
49 //==================================================================
50 VISU_WidgetCtrl::VISU_WidgetCtrl() 
51 {
52   myDummyWidget=NULL;
53   myDisableAll=0;
54   myNbWidgets=2;
55   myActiveIndex=-1;// 0 - PlanesWidget; 1 - SphereWidget
56   myPriority=0.;
57   //
58   myPlanesWidget=VISU_PlanesWidget::New();
59   mySphereWidget=VISU_SphereWidget::New();
60   //
61   myWidgets[0]=myPlanesWidget;
62   myWidgets[1]=mySphereWidget;
63   //
64   myEventCallbackCommand=vtkCallbackCommand::New();
65   myEventCallbackCommand->SetClientData(this); 
66   myEventCallbackCommand->SetCallback(VISU_WidgetCtrl::ProcessEvents);
67   //
68   int i;
69   for (i=0; i<myNbWidgets; ++i){
70     myWidgets[i]->AddObserver(vtkCommand::EnableEvent,
71                               myEventCallbackCommand,
72                               myPriority);
73     myWidgets[i]->AddObserver(vtkCommand::DisableEvent,
74                               myEventCallbackCommand,
75                               myPriority);
76     myWidgets[i]->AddObserver(vtkCommand::EndInteractionEvent,
77                               myEventCallbackCommand,
78                               myPriority);
79     myWidgets[i]->AddObserver(vtkCommand::StartInteractionEvent,
80                               myEventCallbackCommand,
81                               myPriority);
82     
83     myWidgets[i]->AddObserver(vtkCommand::InteractionEvent,
84                               myEventCallbackCommand,
85                               myPriority);
86   }
87 }
88 //==================================================================
89 // function: ~
90 // purpose :
91 //==================================================================
92 VISU_WidgetCtrl::~VISU_WidgetCtrl()
93 {  
94   
95   myPlanesWidget->Delete();
96   mySphereWidget->Delete();
97   //
98   for (int i=0; i<myNbWidgets; ++i){
99     myWidgets[i]->RemoveObserver(myEventCallbackCommand);
100   }
101   //
102   myEventCallbackCommand->Delete();
103 }
104 //==================================================================
105 // function: GetEnabled 
106 // purpose :
107 //==================================================================
108 int VISU_WidgetCtrl::GetEnabled()
109 {
110   if (HasActiveIndex()) {
111     return GetActiveWidget()->GetEnabled();
112   }
113   return 0;
114 }
115 //==================================================================
116 // function: SetEnabled
117 // purpose :
118 //==================================================================
119 void VISU_WidgetCtrl::SetEnabled(int theFlag)
120 {
121   int iFlag, i;
122   //
123   myDisableAll=0;
124   //
125   iFlag=GetEnabled();
126   if (iFlag==theFlag) {
127     return;
128   }
129   //
130   if (theFlag) {//enabling
131     if (HasActiveIndex()) {
132       for (i=0; i<myNbWidgets; ++i) {
133         iFlag=(i==myActiveIndex) ? 1 : 0;
134         myWidgets[i]->SetEnabled(iFlag);
135       }
136     }
137   }
138   else {//disabling
139     myDisableAll=1;
140     for (i=0; i<myNbWidgets; ++i) {
141       myWidgets[i]->SetEnabled(0);
142     }
143   }
144
145   Modified();
146 }
147 //==================================================================
148 // function: ProcessEvents
149 // purpose :
150 //==================================================================
151 void VISU_WidgetCtrl::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
152                                     unsigned long theEvent,
153                                     void* theClientData, 
154                                     void* vtkNotUsed(theCallData))
155 {
156   VISU_WidgetCtrl *aSelf = reinterpret_cast<VISU_WidgetCtrl*>(theClientData);
157   switch(theEvent){
158   case vtkCommand::DisableEvent:
159     if(aSelf->GetDisableAll())
160       aSelf->InvokeEvent(theEvent, NULL);
161   default:
162     aSelf->InvokeEvent(theEvent, NULL);
163     break;
164   }
165 }
166
167 //==================================================================
168 // function: GetNbWidgets
169 // purpose :
170 //==================================================================
171 int VISU_WidgetCtrl::GetNbWidgets()const 
172 {
173   return myNbWidgets;
174 }
175 //==================================================================
176 // function: GetDisableAll
177 // purpose :
178 //==================================================================
179 int VISU_WidgetCtrl::GetDisableAll()const
180 {
181   return myDisableAll;
182 }
183 //==================================================================
184 // function: SetActiveIndex
185 // purpose :
186 //==================================================================
187 void VISU_WidgetCtrl::SetActiveIndex(const int theIndex)
188 {
189   myActiveIndex=-1;
190   if (theIndex>=0 && theIndex<myNbWidgets){
191     myActiveIndex=theIndex;
192   }
193
194   Modified();
195 }
196 //==================================================================
197 // function: GetActiveIndex
198 // purpose :
199 //==================================================================
200 int VISU_WidgetCtrl::GetActiveIndex()const
201 {
202   return myActiveIndex;
203 }
204 //==================================================================
205 // function: HasActiveIndex
206 // purpose :
207 //==================================================================
208 bool VISU_WidgetCtrl::HasActiveIndex()const
209 {
210   return (myActiveIndex>=0 && myActiveIndex<myNbWidgets);
211 }
212 //==================================================================
213 // function: IsPlanesActive
214 // purpose :
215 //==================================================================
216 bool VISU_WidgetCtrl::IsPlanesActive()const
217 {
218   return (myActiveIndex==0);
219 }
220 //==================================================================
221 // function: IsSphereActive
222 // purpose :
223 //==================================================================
224 bool VISU_WidgetCtrl::IsSphereActive()const
225 {
226   return (myActiveIndex==1);
227 }
228 //==================================================================
229 // function: GetActiveWidget
230 // purpose :
231 //==================================================================
232 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetActiveWidget()
233 {
234   if (HasActiveIndex()){
235     return myWidgets[myActiveIndex];
236   }
237   return myDummyWidget;
238 }
239 //==================================================================
240 // function: GetWidget
241 // purpose :
242 //==================================================================
243 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetWidget(const int theIndex)
244 {
245   if (theIndex>=0 && theIndex<myNbWidgets) {
246     return myWidgets[theIndex];
247   }
248   return myDummyWidget;
249 }
250 //==================================================================
251 // function: GetPlanesWidget
252 // purpose :
253 //==================================================================
254 VISU_PlanesWidget* VISU_WidgetCtrl::GetPlanesWidget()
255 {
256   return myPlanesWidget;
257 }
258 //==================================================================
259 // function: GetSphereWidget
260 // purpose :
261 //==================================================================
262 VISU_SphereWidget* VISU_WidgetCtrl::GetSphereWidget()
263 {
264   return mySphereWidget;
265 }
266 //==================================================================
267 // function: PlaceWidget
268 // purpose :
269 //==================================================================
270 void VISU_WidgetCtrl::PlaceWidget(vtkFloatingPointType theBounds[6])
271 {
272   for (int i=0; i<myNbWidgets; ++i) { 
273     myWidgets[i]->PlaceWidget(theBounds);
274   }
275 }
276 //==================================================================
277 // function: SetInteractor
278 // purpose :
279 //==================================================================
280 void VISU_WidgetCtrl::SetInteractor(vtkRenderWindowInteractor* theRWI)
281 {
282   for (int i=0; i<myNbWidgets; ++i) {
283     myWidgets[i]->SetInteractor(theRWI);
284   }
285
286   Modified();
287 }
288 //==================================================================
289 // function: GetInteractor
290 // purpose :
291 //==================================================================
292 vtkRenderWindowInteractor* VISU_WidgetCtrl::GetInteractor()
293 {
294   return myWidgets[0]->GetInteractor();
295 }
296 //==================================================================
297 // function: SetPlaceFactor
298 // purpose :
299 //==================================================================
300 void VISU_WidgetCtrl::SetPlaceFactor(vtkFloatingPointType theFactor)
301 {
302   for (int i=0; i<myNbWidgets; ++i) {
303     myWidgets[i]->SetPlaceFactor(theFactor);
304   }
305
306   Modified();
307 }
308 //==================================================================
309 // function: GetPlaceFactor
310 // purpose :
311 //==================================================================
312 vtkFloatingPointType VISU_WidgetCtrl::GetPlaceFactor()
313 {
314   return myWidgets[0]->GetPlaceFactor();
315 }
316 //==================================================================
317 // function: ImplicitFunction
318 // purpose :
319 //==================================================================
320 vtkImplicitFunction* VISU_WidgetCtrl::ImplicitFunction()
321 {
322   return this;
323 }
324
325 //==================================================================
326 vtkFloatingPointType    
327 VISU_WidgetCtrl
328 ::EvaluateFunction(vtkFloatingPointType theX[3])
329 {
330   if(VISU_ImplicitFunctionWidget* aWidget = GetActiveWidget()){
331     if(vtkImplicitFunction* aFunction = aWidget->ImplicitFunction())
332       return aFunction->EvaluateFunction(theX[0],theX[1],theX[2]);
333   }
334   return 1.0;
335 }
336
337 //==================================================================
338 void    
339 VISU_WidgetCtrl
340 ::EvaluateGradient(vtkFloatingPointType theX[3], 
341                    vtkFloatingPointType theG[3])
342 {
343   theG[0] = theG[1] = theG[2] = 0.0;
344   if(VISU_ImplicitFunctionWidget* aWidget = GetActiveWidget()){
345     if(vtkImplicitFunction* aFunction = aWidget->ImplicitFunction())
346       aFunction->EvaluateGradient(theX,theG);
347   }
348 }
349
350 //==================================================================
351 unsigned long
352 VISU_WidgetCtrl
353 ::GetMTime()
354 {
355   unsigned long aTime = Superclass::GetMTime();
356
357   if(vtkImplicitFunction* aFunction = myPlanesWidget->ImplicitFunction())
358     aTime = std::max(aTime,aFunction->GetMTime());
359
360   if(vtkImplicitFunction* aFunction = mySphereWidget->ImplicitFunction())
361     aTime = std::max(aTime,aFunction->GetMTime());
362   
363   return aTime;
364 }