1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SALOME VTKViewer : build VTK viewer into Salome desktop
23 // File : VVTK_WidgetCtrl.cxx
24 // Author : Peter KURNEV
28 #include "VISU_WidgetCtrl.hxx"
30 #include "VISU_ImplicitFunctionWidget.hxx"
31 #include "VISU_PlanesWidget.hxx"
32 #include "VISU_SphereWidget.hxx"
36 #include <vtkObject.h>
37 #include <vtkImplicitFunction.h>
38 #include <vtkRenderWindowInteractor.h>
39 #include <vtkObjectFactory.h>
40 #include <vtkCallbackCommand.h>
41 #include <vtkCommand.h>
43 vtkCxxRevisionMacro(VISU_WidgetCtrl,"$Revision$");
44 vtkStandardNewMacro(VISU_WidgetCtrl);
46 //==================================================================
47 // function: VISU_WidgetCtrl
49 //==================================================================
50 VISU_WidgetCtrl::VISU_WidgetCtrl()
55 myActiveIndex=-1;// 0 - PlanesWidget; 1 - SphereWidget
58 myPlanesWidget=VISU_PlanesWidget::New();
59 mySphereWidget=VISU_SphereWidget::New();
61 myWidgets[0]=myPlanesWidget;
62 myWidgets[1]=mySphereWidget;
64 myEventCallbackCommand=vtkCallbackCommand::New();
65 myEventCallbackCommand->SetClientData(this);
66 myEventCallbackCommand->SetCallback(VISU_WidgetCtrl::ProcessEvents);
69 for (i=0; i<myNbWidgets; ++i){
70 myWidgets[i]->AddObserver(vtkCommand::EnableEvent,
71 myEventCallbackCommand,
73 myWidgets[i]->AddObserver(vtkCommand::DisableEvent,
74 myEventCallbackCommand,
76 myWidgets[i]->AddObserver(vtkCommand::EndInteractionEvent,
77 myEventCallbackCommand,
79 myWidgets[i]->AddObserver(vtkCommand::StartInteractionEvent,
80 myEventCallbackCommand,
83 myWidgets[i]->AddObserver(vtkCommand::InteractionEvent,
84 myEventCallbackCommand,
88 //==================================================================
91 //==================================================================
92 VISU_WidgetCtrl::~VISU_WidgetCtrl()
95 myPlanesWidget->Delete();
96 mySphereWidget->Delete();
98 for (int i=0; i<myNbWidgets; ++i){
99 myWidgets[i]->RemoveObserver(myEventCallbackCommand);
102 myEventCallbackCommand->Delete();
104 //==================================================================
105 // function: GetEnabled
107 //==================================================================
108 int VISU_WidgetCtrl::GetEnabled()
110 if (HasActiveIndex()) {
111 return GetActiveWidget()->GetEnabled();
115 //==================================================================
116 // function: SetEnabled
118 //==================================================================
119 void VISU_WidgetCtrl::SetEnabled(int theFlag)
126 if (iFlag==theFlag) {
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);
140 for (i=0; i<myNbWidgets; ++i) {
141 myWidgets[i]->SetEnabled(0);
147 //==================================================================
148 // function: ProcessEvents
150 //==================================================================
151 void VISU_WidgetCtrl::ProcessEvents(vtkObject* vtkNotUsed(theObject),
152 unsigned long theEvent,
154 void* vtkNotUsed(theCallData))
156 VISU_WidgetCtrl *aSelf = reinterpret_cast<VISU_WidgetCtrl*>(theClientData);
158 case vtkCommand::DisableEvent:
159 if(aSelf->GetDisableAll())
160 aSelf->InvokeEvent(theEvent, NULL);
162 aSelf->InvokeEvent(theEvent, NULL);
167 //==================================================================
168 // function: GetNbWidgets
170 //==================================================================
171 int VISU_WidgetCtrl::GetNbWidgets()const
175 //==================================================================
176 // function: GetDisableAll
178 //==================================================================
179 int VISU_WidgetCtrl::GetDisableAll()const
183 //==================================================================
184 // function: SetActiveIndex
186 //==================================================================
187 void VISU_WidgetCtrl::SetActiveIndex(const int theIndex)
190 if (theIndex>=0 && theIndex<myNbWidgets){
191 myActiveIndex=theIndex;
196 //==================================================================
197 // function: GetActiveIndex
199 //==================================================================
200 int VISU_WidgetCtrl::GetActiveIndex()const
202 return myActiveIndex;
204 //==================================================================
205 // function: HasActiveIndex
207 //==================================================================
208 bool VISU_WidgetCtrl::HasActiveIndex()const
210 return (myActiveIndex>=0 && myActiveIndex<myNbWidgets);
212 //==================================================================
213 // function: IsPlanesActive
215 //==================================================================
216 bool VISU_WidgetCtrl::IsPlanesActive()const
218 return (myActiveIndex==0);
220 //==================================================================
221 // function: IsSphereActive
223 //==================================================================
224 bool VISU_WidgetCtrl::IsSphereActive()const
226 return (myActiveIndex==1);
228 //==================================================================
229 // function: GetActiveWidget
231 //==================================================================
232 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetActiveWidget()
234 if (HasActiveIndex()){
235 return myWidgets[myActiveIndex];
237 return myDummyWidget;
239 //==================================================================
240 // function: GetWidget
242 //==================================================================
243 VISU_ImplicitFunctionWidget* VISU_WidgetCtrl::GetWidget(const int theIndex)
245 if (theIndex>=0 && theIndex<myNbWidgets) {
246 return myWidgets[theIndex];
248 return myDummyWidget;
250 //==================================================================
251 // function: GetPlanesWidget
253 //==================================================================
254 VISU_PlanesWidget* VISU_WidgetCtrl::GetPlanesWidget()
256 return myPlanesWidget;
258 //==================================================================
259 // function: GetSphereWidget
261 //==================================================================
262 VISU_SphereWidget* VISU_WidgetCtrl::GetSphereWidget()
264 return mySphereWidget;
266 //==================================================================
267 // function: PlaceWidget
269 //==================================================================
270 void VISU_WidgetCtrl::PlaceWidget(vtkFloatingPointType theBounds[6])
272 for (int i=0; i<myNbWidgets; ++i) {
273 myWidgets[i]->PlaceWidget(theBounds);
276 //==================================================================
277 // function: SetInteractor
279 //==================================================================
280 void VISU_WidgetCtrl::SetInteractor(vtkRenderWindowInteractor* theRWI)
282 for (int i=0; i<myNbWidgets; ++i) {
283 myWidgets[i]->SetInteractor(theRWI);
288 //==================================================================
289 // function: GetInteractor
291 //==================================================================
292 vtkRenderWindowInteractor* VISU_WidgetCtrl::GetInteractor()
294 return myWidgets[0]->GetInteractor();
296 //==================================================================
297 // function: SetPlaceFactor
299 //==================================================================
300 void VISU_WidgetCtrl::SetPlaceFactor(vtkFloatingPointType theFactor)
302 for (int i=0; i<myNbWidgets; ++i) {
303 myWidgets[i]->SetPlaceFactor(theFactor);
308 //==================================================================
309 // function: GetPlaceFactor
311 //==================================================================
312 vtkFloatingPointType VISU_WidgetCtrl::GetPlaceFactor()
314 return myWidgets[0]->GetPlaceFactor();
316 //==================================================================
317 // function: ImplicitFunction
319 //==================================================================
320 vtkImplicitFunction* VISU_WidgetCtrl::ImplicitFunction()
325 //==================================================================
328 ::EvaluateFunction(vtkFloatingPointType theX[3])
330 if(VISU_ImplicitFunctionWidget* aWidget = GetActiveWidget()){
331 if(vtkImplicitFunction* aFunction = aWidget->ImplicitFunction())
332 return aFunction->EvaluateFunction(theX[0],theX[1],theX[2]);
337 //==================================================================
340 ::EvaluateGradient(vtkFloatingPointType theX[3],
341 vtkFloatingPointType theG[3])
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);
350 //==================================================================
355 unsigned long aTime = Superclass::GetMTime();
357 if(vtkImplicitFunction* aFunction = myPlanesWidget->ImplicitFunction())
358 aTime = std::max(aTime,aFunction->GetMTime());
360 if(vtkImplicitFunction* aFunction = mySphereWidget->ImplicitFunction())
361 aTime = std::max(aTime,aFunction->GetMTime());