initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / postProcessing / graphics / PVFoamReader / PVFoamReader / vtkPVFoamSelectTimeSet.cxx
blobfd948578da81a6a9df0553c6f80c6ef841baece3
1 /*=========================================================================
3 Program: ParaView
4 Module: $RCSfile: vtkPVFoamSelectTimeSet.cxx,v $
6 Copyright (c) Kitware, Inc.
7 All rights reserved.
8 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
14 =========================================================================*/
15 #include "vtkPVFoamSelectTimeSet.h"
17 #include "vtkDataArrayCollection.h"
18 #include "vtkFloatArray.h"
19 #include "vtkKWFrame.h"
20 #include "vtkKWLabel.h"
21 #include "vtkKWLabeledFrame.h"
22 #include "vtkKWMenu.h"
23 #include "vtkObjectFactory.h"
24 #include "vtkPVAnimationInterfaceEntry.h"
25 #include "vtkPVApplication.h"
26 #include "vtkPVProcessModule.h"
27 #include "vtkPVScalarListWidgetProperty.h"
28 #include "vtkPVSource.h"
29 #include "vtkPVXMLElement.h"
31 #include <vtkstd/string>
33 //-----------------------------------------------------------------------------
34 vtkStandardNewMacro(vtkPVFoamSelectTimeSet);
35 vtkCxxRevisionMacro(vtkPVFoamSelectTimeSet, "$Revision: 1.39 $");
37 //-----------------------------------------------------------------------------
38 int vtkDataArrayCollectionCommand(ClientData cd, Tcl_Interp *interp,
39 int argc, char *argv[]);
41 //-----------------------------------------------------------------------------
42 vtkPVFoamSelectTimeSet::vtkPVFoamSelectTimeSet()
44 this->LabeledFrame = vtkKWLabeledFrame::New();
45 this->LabeledFrame->SetParent(this);
47 this->TimeLabel = vtkKWLabel::New();
48 this->TimeLabel->SetParent(this->LabeledFrame->GetFrame());
50 this->TreeFrame = vtkKWWidget::New();
51 this->TreeFrame->SetParent(this->LabeledFrame->GetFrame());
53 this->Tree = vtkKWWidget::New();
54 this->Tree->SetParent(this->TreeFrame);
56 this->TimeValue = 0.0;
58 this->FrameLabel = 0;
60 this->TimeSets = vtkDataArrayCollection::New();
62 this->Property = 0;
64 this->SetCommand = 0;
65 this->ServerSideID.ID = 0;
68 //-----------------------------------------------------------------------------
69 vtkPVFoamSelectTimeSet::~vtkPVFoamSelectTimeSet()
71 this->LabeledFrame->Delete();
72 this->Tree->Delete();
73 this->TreeFrame->Delete();
74 this->TimeLabel->Delete();
75 this->SetFrameLabel(0);
76 this->TimeSets->Delete();
77 this->SetSetCommand(0);
78 if(this->ServerSideID.ID)
80 vtkPVProcessModule* pm = this->GetPVApplication()->GetProcessModule();
81 pm->DeleteStreamObject(this->ServerSideID);
82 pm->SendStream(vtkProcessModule::DATA_SERVER);
86 //-----------------------------------------------------------------------------
87 void vtkPVFoamSelectTimeSet::SetLabel(const char* label)
89 this->SetFrameLabel(label);
90 if (this->GetApplication())
92 this->LabeledFrame->SetLabel(label);
96 //-----------------------------------------------------------------------------
97 const char* vtkPVFoamSelectTimeSet::GetLabel()
99 return this->GetFrameLabel();
102 //-----------------------------------------------------------------------------
103 void vtkPVFoamSelectTimeSet::Create(vtkKWApplication *pvApp)
105 // Call the superclass to create the widget and set the appropriate flags
107 if (!this->vtkKWWidget::Create(pvApp, "frame", "-bd 2 -relief flat"))
109 vtkErrorMacro("Failed creating widget " << this->GetClassName());
110 return;
113 // For getting the widget in a script.
114 if ((this->TraceNameState == vtkPVWidget::Uninitialized ||
115 this->TraceNameState == vtkPVWidget::Default) )
117 this->SetTraceName("FoamSelectTimeSet");
118 this->SetTraceNameState(vtkPVWidget::SelfInitialized);
121 this->LabeledFrame->Create(this->GetApplication(), 0);
122 if (this->FrameLabel)
124 this->LabeledFrame->SetLabel(this->FrameLabel);
126 this->TimeLabel->Create(this->GetApplication(), "");
128 char label[32];
129 sprintf(label, "Time value: %12.5e", 0.0);
130 this->TimeLabel->SetLabel(label);
131 this->Script("pack %s", this->TimeLabel->GetWidgetName());
133 this->TreeFrame->Create(this->GetApplication(), "ScrolledWindow",
134 "-relief sunken -bd 2");
136 this->Tree->Create(this->GetApplication(), "Tree",
137 "-background white -bd 0 -width 15 -padx 2 "
138 "-redraw 1 -relief flat -selectbackground red");
139 this->Script("%s bindText <ButtonPress-1> {%s SetTimeValueCallback}",
140 this->Tree->GetWidgetName(), this->GetTclName());
141 this->Script("%s setwidget %s", this->TreeFrame->GetWidgetName(),
142 this->Tree->GetWidgetName());
144 this->Script("pack %s -expand t -fill x", this->TreeFrame->GetWidgetName());
146 this->Script("pack %s -side top -expand t -fill x",
147 this->LabeledFrame->GetWidgetName());
151 //-----------------------------------------------------------------------------
152 void vtkPVFoamSelectTimeSet::SetTimeValue(float time)
154 if (this->TimeValue != time ||
155 !this->TimeLabel->GetLabel() ||
156 !strcmp(this->TimeLabel->GetLabel(), "No timesets available."))
158 this->TimeValue = time;
160 char label[32];
161 sprintf(label, "Time value: %12.5e", time);
162 this->TimeLabel->SetLabel(label);
163 this->Modified();
167 //-----------------------------------------------------------------------------
168 void vtkPVFoamSelectTimeSet::SetTimeValueCallback(const char* item)
170 if (this->TimeSets->GetNumberOfItems() == 0)
172 return;
175 if ( strncmp(item, "timeset", strlen("timeset")) == 0 )
177 this->Script("if [%s itemcget %s -open] "
178 "{%s closetree %s} else {%s opentree %s}",
179 this->Tree->GetWidgetName(), item,
180 this->Tree->GetWidgetName(), item,
181 this->Tree->GetWidgetName(), item);
182 return;
185 this->Script("%s selection set %s", this->Tree->GetWidgetName(),
186 item);
187 this->Script("%s itemcget %s -data", this->Tree->GetWidgetName(),
188 item);
189 const char* result = this->GetApplication()->GetMainInterp()->result;
190 if (result[0] == '\0')
192 return;
195 int index[2];
196 sscanf(result, "%d %d", &(index[0]), &(index[1]));
198 this->SetTimeSetsFromReader();
199 this->SetTimeValue(this->TimeSets->GetItem(index[0])->GetTuple1(index[1]));
200 this->ModifiedCallback();
203 //-----------------------------------------------------------------------------
204 void vtkPVFoamSelectTimeSet::AddRootNode(const char* name, const char* text)
206 if (!this->GetApplication())
208 return;
210 this->Script("%s insert end root %s -text {%s}", this->Tree->GetWidgetName(),
211 name, text);
214 //-----------------------------------------------------------------------------
215 void vtkPVFoamSelectTimeSet::AddChildNode(const char* parent, const char* name,
216 const char* text, const char* data)
218 if (!this->GetApplication())
220 return;
222 this->Script("%s insert end %s %s -text {%s} -data %s",
223 this->Tree->GetWidgetName(), parent, name, text, data);
227 //-----------------------------------------------------------------------------
228 void vtkPVFoamSelectTimeSet::SaveInBatchScript(ofstream *file)
230 *file << " [$pvTemp" << this->PVSource->GetVTKSourceID(0)
231 << " GetProperty " << this->SetCommand << "] SetElements1 "
232 << this->Property->GetScalar(0) << endl;
235 //-----------------------------------------------------------------------------
236 void vtkPVFoamSelectTimeSet::AcceptInternal(vtkClientServerID sourceID)
238 if (this->ModifiedFlag)
240 this->Script("%s selection get", this->Tree->GetWidgetName());
241 this->AddTraceEntry("$kw(%s) SetTimeValueCallback {%s}",
242 this->GetTclName(),
243 this->GetApplication()->GetMainInterp()->result);
246 this->Property->SetVTKSourceID(sourceID);
247 this->Property->SetScalars(1, &this->TimeValue);
248 this->Property->AcceptInternal();
250 this->ModifiedFlag = 0;
253 //---------------------------------------------------------------------------
254 void vtkPVFoamSelectTimeSet::Trace(ofstream *file)
256 if ( ! this->InitializeTrace(file))
258 return;
261 this->Script("%s selection get", this->Tree->GetWidgetName());
262 *file << "$kw(" << this->GetTclName() << ") SetTimeValueCallback {"
263 << this->GetApplication()->GetMainInterp()->result << "}" << endl;
267 //-----------------------------------------------------------------------------
268 void vtkPVFoamSelectTimeSet::ResetInternal()
270 if ( ! this->ModifiedFlag)
272 return;
275 // Command to update the UI.
276 if (!this->Tree)
278 return;
281 this->Script("%s delete [%s nodes root]", this->Tree->GetWidgetName(),
282 this->Tree->GetWidgetName());
284 this->SetTimeSetsFromReader();
286 int timeSetId=0;
287 char timeSetName[32];
288 char timeSetText[32];
290 char timeValueName[32];
291 char timeValueText[32];
292 char indices[32];
294 float actualTimeValue = this->Property->GetScalar(0);
295 int matchFound = 0;
297 this->ModifiedFlag = 0;
299 if (this->TimeSets->GetNumberOfItems() == 0)
301 this->Script("pack forget %s", this->TreeFrame->GetWidgetName());
302 this->TimeLabel->SetLabel("No timesets available.");
303 return;
305 else
307 this->SetTimeValue(actualTimeValue);
308 this->Script("pack %s -expand t -fill x", this->TreeFrame->GetWidgetName());
311 this->TimeSets->InitTraversal();
312 vtkDataArray* da;
313 while( (da=this->TimeSets->GetNextItem()) )
315 timeSetId++;
316 sprintf(timeSetName,"timeset%d", timeSetId);
317 sprintf(timeSetText,"Time Set %d", timeSetId);
318 this->AddRootNode(timeSetName, timeSetText);
320 vtkIdType tuple;
321 for(tuple=0; tuple<da->GetNumberOfTuples(); tuple++)
323 float timeValue = da->GetTuple1(tuple);
324 sprintf(timeValueName, "time%d_%-12.5e", timeSetId, timeValue);
325 sprintf(timeValueText, "%-12.5e", timeValue);
326 ostrstream str;
327 str << "{" << timeSetId-1 << " " << tuple << "}" << ends;
328 sprintf(indices, "%s", str.str());
329 str.rdbuf()->freeze(0);
330 this->AddChildNode(timeSetName, timeValueName, timeValueText, indices);
331 if (actualTimeValue == timeValue && !matchFound)
333 matchFound=1;
334 this->Script("%s selection set %s", this->Tree->GetWidgetName(),
335 timeValueName);
338 if (timeSetId == 1)
340 this->Script("%s opentree %s", this->Tree->GetWidgetName(),
341 timeSetName);
345 this->SetTimeValue(actualTimeValue);
348 //-----------------------------------------------------------------------------
349 void vtkPVFoamSelectTimeSet::AddAnimationScriptsToMenu(vtkKWMenu *menu,
350 vtkPVAnimationInterfaceEntry *ai)
352 char methodAndArgs[500];
354 sprintf(methodAndArgs, "AnimationMenuCallback %s", ai->GetTclName());
355 // I do not under stand why the trace name is used for the
356 // menu entry, but Berk must know.
357 menu->AddCommand(this->GetTraceName(), this, methodAndArgs, 0, "");
361 //-----------------------------------------------------------------------------
362 // What a pain. I need this method for tracing.
363 // Maybe the animation should call PVwidget methods and not vtk object methods.
364 void vtkPVFoamSelectTimeSet::AnimationMenuCallback(vtkPVAnimationInterfaceEntry *ai)
366 if (ai->InitializeTrace(NULL))
368 this->AddTraceEntry("$kw(%s) AnimationMenuCallback $kw(%s)",
369 this->GetTclName(), ai->GetTclName());
372 // I do not under stand why the trace name is used for the
373 // menu entry, but Berk must know.
374 ai->SetLabelAndScript(this->GetTraceName(), NULL, this->GetTraceName());
375 ai->SetCurrentProperty(this->Property);
376 ai->Update();
381 //-----------------------------------------------------------------------------
382 vtkPVFoamSelectTimeSet* vtkPVFoamSelectTimeSet::ClonePrototype(vtkPVSource* pvSource,
383 vtkArrayMap<vtkPVWidget*, vtkPVWidget*>* map)
385 vtkPVWidget* clone = this->ClonePrototypeInternal(pvSource, map);
386 return vtkPVFoamSelectTimeSet::SafeDownCast(clone);
389 //-----------------------------------------------------------------------------
390 void vtkPVFoamSelectTimeSet::CopyProperties(vtkPVWidget* clone,
391 vtkPVSource* pvSource,
392 vtkArrayMap<vtkPVWidget*, vtkPVWidget*>* map)
394 this->Superclass::CopyProperties(clone, pvSource, map);
395 vtkPVFoamSelectTimeSet* pvts = vtkPVFoamSelectTimeSet::SafeDownCast(clone);
396 if (pvts)
398 pvts->SetLabel(this->FrameLabel);
399 pvts->SetSetCommand(this->SetCommand);
401 else
403 vtkErrorMacro(
404 "Internal error. Could not downcast clone to PVFoamSelectTimeSet.");
408 //-----------------------------------------------------------------------------
409 int vtkPVFoamSelectTimeSet::ReadXMLAttributes(vtkPVXMLElement* element,
410 vtkPVXMLPackageParser* parser)
412 if(!this->Superclass::ReadXMLAttributes(element, parser)) { return 0; }
414 // Setup the Label.
415 const char* label = element->GetAttribute("label");
416 if(label)
418 this->SetLabel(label);
421 this->SetSetCommand(element->GetAttribute("set_command"));
423 return 1;
426 //-----------------------------------------------------------------------------
427 void vtkPVFoamSelectTimeSet::SetTimeSetsFromReader()
429 vtkPVProcessModule* pm = this->GetPVApplication()->GetProcessModule();
430 this->TimeSets->RemoveAllItems();
432 // Create the server-side helper if necessary.
433 if(!this->ServerSideID.ID)
435 this->ServerSideID = pm->NewStreamObject("vtkPVFoamServerSelectTimeSet");
436 pm->SendStream(vtkProcessModule::DATA_SERVER);
439 // Get the time sets from the reader on the server.
440 // Reader -> VTKSourceID (0). We assume that there is 1 VTKSource.
441 pm->GetStream() << vtkClientServerStream::Invoke
442 << this->ServerSideID << "GetTimeSets"
443 << this->PVSource->GetVTKSourceID(0)
444 << vtkClientServerStream::End;
445 pm->SendStream(vtkProcessModule::DATA_SERVER_ROOT);
446 vtkClientServerStream timeSets;
447 if(!pm->GetLastServerResult().GetArgument(0, 0, &timeSets))
449 vtkErrorMacro("Error getting time sets from server.");
450 return;
453 // There is one time set per message.
454 for(int m=0; m < timeSets.GetNumberOfMessages(); ++m)
456 // Each argument in the message is a time set entry.
457 vtkFloatArray* timeSet = vtkFloatArray::New();
458 int n = timeSets.GetNumberOfArguments(m);
459 timeSet->SetNumberOfTuples(n);
460 for(int i=0; i < n; ++i)
462 float value;
463 if(!timeSets.GetArgument(m, i, &value))
465 vtkErrorMacro("Error reading time set value.");
466 timeSet->Delete();
467 return;
469 timeSet->SetTuple1(i, value);
471 this->TimeSets->AddItem(timeSet);
472 timeSet->Delete();
475 if (this->Property->GetNumberOfScalars() == 0 &&
476 this->TimeSets->GetNumberOfItems() > 0)
478 vtkFloatArray *ts =
479 vtkFloatArray::SafeDownCast(this->TimeSets->GetItem(0));
480 this->Property->SetScalars(1, ts->GetPointer(0));
484 //----------------------------------------------------------------------------
485 void vtkPVFoamSelectTimeSet::SaveInBatchScriptForPart(ofstream *file,
486 vtkClientServerID sourceID)
488 if (sourceID.ID == 0)
490 vtkErrorMacro(<< this->GetClassName()
491 << " must not have SaveInBatchScript method.");
492 return;
495 *file << "\t" << "pvTemp" << sourceID
496 << " SetTimeValue " << this->GetTimeValue()
497 << endl;;
500 //-----------------------------------------------------------------------------
501 void vtkPVFoamSelectTimeSet::SetProperty(vtkPVWidgetProperty *prop)
503 this->Property = vtkPVScalarListWidgetProperty::SafeDownCast(prop);
504 if (this->Property)
506 int numScalars = 1;
507 this->Property->SetVTKCommands(1, &this->SetCommand, &numScalars);
511 //-----------------------------------------------------------------------------
512 vtkPVWidgetProperty* vtkPVFoamSelectTimeSet::GetProperty()
514 return this->Property;
517 //-----------------------------------------------------------------------------
518 vtkPVWidgetProperty* vtkPVFoamSelectTimeSet::CreateAppropriateProperty()
520 return vtkPVScalarListWidgetProperty::New();
523 //-----------------------------------------------------------------------------
524 void vtkPVFoamSelectTimeSet::PrintSelf(ostream& os, vtkIndent indent)
526 this->Superclass::PrintSelf(os, indent);
527 os << indent << "TimeValue: " << this->TimeValue << endl;
528 os << indent << "LabeledFrame: " << this->LabeledFrame << endl;
529 os << indent << "SetCommand: "
530 << (this->SetCommand ? this->SetCommand : "(none)") << endl;