1 // SALOME VTKViewer : build VTK viewer into Salome desktop
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : VVTK_WidgetCtrl.cxx
25 // Author : Peter KURNEV
29 #include "VISU_WidgetCtrl.hxx"
31 #include "VISU_ImplicitFunctionWidget.hxx"
32 #include "VISU_PlanesWidget.hxx"
33 #include "VISU_SphereWidget.hxx"
37 #include <vtkObject.h>
38 #include <vtkImplicitFunction.h>
39 #include <vtkRenderWindowInteractor.h>
40 #include <vtkObjectFactory.h>
41 #include <vtkCallbackCommand.h>
42 #include <vtkCommand.h>
44 vtkCxxRevisionMacro(VISU_WidgetCtrl,"$Revision$");
45 vtkStandardNewMacro(VISU_WidgetCtrl);
47 //==================================================================
48 // function: VISU_WidgetCtrl
50 //==================================================================
51 VISU_WidgetCtrl::VISU_WidgetCtrl()
56 myActiveIndex=-1;// 0 - PlanesWidget; 1 - SphereWidget
59 myPlanesWidget=VISU_PlanesWidget::New();
60 mySphereWidget=VISU_SphereWidget::New();
62 myWidgets[0]=myPlanesWidget;
63 myWidgets[1]=mySphereWidget;
65 myEventCallbackCommand=vtkCallbackCommand::New();
66 myEventCallbackCommand->SetClientData(this);
67 myEventCallbackCommand->SetCallback(VISU_WidgetCtrl::ProcessEvents);
70 for (i=0; i<myNbWidgets; ++i){
71 myWidgets[i]->AddObserver(vtkCommand::EnableEvent,
72 myEventCallbackCommand,
74 myWidgets[i]->AddObserver(vtkCommand::DisableEvent,
75 myEventCallbackCommand,
77 myWidgets[i]->AddObserver(vtkCommand::EndInteractionEvent,
78 myEventCallbackCommand,
80 myWidgets[i]->AddObserver(vtkCommand::StartInteractionEvent,
81 myEventCallbackCommand,
84 myWidgets[i]->AddObserver(vtkCommand::InteractionEvent,
85 myEventCallbackCommand,
89 //==================================================================
92 //==================================================================
93 VISU_WidgetCtrl::~VISU_WidgetCtrl()
96 myPlanesWidget->Delete();
97 mySphereWidget->Delete();
99 for (int i=0; i<myNbWidgets; ++i){
100 myWidgets[i]->RemoveObserver(myEventCallbackCommand);
103 myEventCallbackCommand->Delete();
105 //==================================================================
106 // function: GetEnabled
108 //==================================================================
109 int VISU_WidgetCtrl::GetEnabled()
111 if (HasActiveIndex()) {
112 return GetActiveWidget()->GetEnabled();
116 //==================================================================
117 // function: SetEnabled
119 //==================================================================
120 void VISU_WidgetCtrl::SetEnabled(int theFlag)
127 if (iFlag==theFlag) {
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);
141 for (i=0; i<myNbWidgets; ++i) {
142 myWidgets[i]->SetEnabled(0);
148 //==================================================================
149 // function: ProcessEvents
151 //==================================================================
152 void VISU_WidgetCtrl::ProcessEvents(vtkObject* vtkNotUsed(theObject),
153 unsigned long theEvent,
155 void* vtkNotUsed(theCallData))
157 VISU_WidgetCtrl *aSelf = reinterpret_cast<VISU_WidgetCtrl*>(theClientData);
159 case vtkCommand::DisableEvent:
160 if(aSelf->GetDisableAll())
161 aSelf->InvokeEvent(theEvent, NULL);
163 aSelf->InvokeEvent(theEvent, NULL);
168 //==================================================================
169 // function: GetNbWidgets
171 //==================================================================
172 int VISU_WidgetCtrl::GetNbWidgets()const
176 //==================================================================
177 // function: GetDisableAll
179 //==================================================================
180 int VISU_WidgetCtrl::GetDisableAll()const
184 //==================================================================
185 // function: SetActiveIndex
187 //==================================================================
188 void VISU_WidgetCtrl::SetActiveIndex(const int theIndex)
191 if (theIndex>=0 && theIndex<myNbWidgets){
192 myActiveIndex=theIndex;
197 //==================================================================
198 // function: GetActiveIndex
200 //==================================================================
201 int VISU_WidgetCtrl::GetActiveIndex()const
203 return myActiveIndex;
205 //==================================================================
206 // function: HasActiveIndex
208 //==================================================================
209 bool VISU_WidgetCtrl::HasActiveIndex()const
211 return (myActiveIndex>=0 && myActiveIndex<myNbWidgets);
213 //==================================================================
214 // function: IsPlanesActive
216 //==================================================================
217 bool VISU_WidgetCtrl::IsPlanesActive()const
219 return (myActiveIndex==0);
221 //==================================================================
222 // function: IsSphereActive
224 //==================================================================
225 bool VISU_WidgetCtrl::IsSphereActive()const
227 return (myActiveIndex==1);
229 //==================================================================
230 // function: GetActiveWidget
232 //==================================================================
233 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetActiveWidget()
235 if (HasActiveIndex()){
236 return myWidgets[myActiveIndex];
238 return myDummyWidget;
240 //==================================================================
241 // function: GetWidget
243 //==================================================================
244 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetWidget(const int theIndex)
246 if (theIndex>=0 && theIndex<myNbWidgets) {
247 return myWidgets[theIndex];
249 return myDummyWidget;
251 //==================================================================
252 // function: GetPlanesWidget
254 //==================================================================
255 VISU_PlanesWidget* VISU_WidgetCtrl::GetPlanesWidget()
257 return myPlanesWidget;
259 //==================================================================
260 // function: GetSphereWidget
262 //==================================================================
263 VISU_SphereWidget* VISU_WidgetCtrl::GetSphereWidget()
265 return mySphereWidget;
267 //==================================================================
268 // function: PlaceWidget
270 //==================================================================
271 void VISU_WidgetCtrl::PlaceWidget(vtkFloatingPointType theBounds[6])
273 for (int i=0; i<myNbWidgets; ++i) {
274 myWidgets[i]->PlaceWidget(theBounds);
277 //==================================================================
278 // function: SetInteractor
280 //==================================================================
281 void VISU_WidgetCtrl::SetInteractor(vtkRenderWindowInteractor* theRWI)
283 for (int i=0; i<myNbWidgets; ++i) {
284 myWidgets[i]->SetInteractor(theRWI);
289 //==================================================================
290 // function: GetInteractor
292 //==================================================================
293 vtkRenderWindowInteractor* VISU_WidgetCtrl::GetInteractor()
295 return myWidgets[0]->GetInteractor();
297 //==================================================================
298 // function: SetPlaceFactor
300 //==================================================================
301 void VISU_WidgetCtrl::SetPlaceFactor(vtkFloatingPointType theFactor)
303 for (int i=0; i<myNbWidgets; ++i) {
304 myWidgets[i]->SetPlaceFactor(theFactor);
309 //==================================================================
310 // function: GetPlaceFactor
312 //==================================================================
313 vtkFloatingPointType VISU_WidgetCtrl::GetPlaceFactor()
315 return myWidgets[0]->GetPlaceFactor();
317 //==================================================================
318 // function: ImplicitFunction
320 //==================================================================
321 vtkImplicitFunction* VISU_WidgetCtrl::ImplicitFunction()
326 //==================================================================
329 ::EvaluateFunction(vtkFloatingPointType theX[3])
331 if(VISU_ImplicitFunctionWidget* aWidget = GetActiveWidget()){
332 if(vtkImplicitFunction* aFunction = aWidget->ImplicitFunction())
333 return aFunction->EvaluateFunction(theX[0],theX[1],theX[2]);
338 //==================================================================
341 ::EvaluateGradient(vtkFloatingPointType theX[3],
342 vtkFloatingPointType theG[3])
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);
351 //==================================================================
356 unsigned long aTime = Superclass::GetMTime();
358 if(vtkImplicitFunction* aFunction = myPlanesWidget->ImplicitFunction())
359 aTime = std::max(aTime,aFunction->GetMTime());
361 if(vtkImplicitFunction* aFunction = mySphereWidget->ImplicitFunction())
362 aTime = std::max(aTime,aFunction->GetMTime());