Salome HOME
Fix compilation errors
[modules/visu.git] / src / VISU_I / VISU_TimeAnimation.cxx
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_TimeAnimation.cxx
6 //  Author : Vitaly SMETANNIKOV
7 //  Module : VISU
8
9 #include "VISU_TimeAnimation.h"
10
11 #include "VISUConfig.hh"
12
13 #include "VISU_Result_i.hh"
14 #include "VISU_Prs3d_i.hh"
15 #include "VISU_Mesh_i.hh"
16 #include "VISU_ScalarMap_i.hh"
17 #include "VISU_IsoSurfaces_i.hh"
18 #include "VISU_DeformedShape_i.hh"
19 #include "VISU_CutPlanes_i.hh"
20 #include "VISU_Plot3D_i.hh"
21 #include "VISU_CutLines_i.hh"
22 #include "VISU_Vectors_i.hh"
23 #include "VISU_StreamLines_i.hh"
24 #include "VISU_ViewManager_i.hh"
25 #include "VISU_View_i.hh"
26
27 #include "VISU_ScalarBarActor.hxx"
28 #include "VISU_Actor.h"
29
30 #include "SalomeApp_Study.h"
31
32 #include "SVTK_ViewWindow.h"
33
34 #include "SALOME_Event.hxx"
35
36 #include "SUIT_ResourceMgr.h"
37 #include "SUIT_Application.h"
38 #include "SUIT_Session.h"
39 #include "SUIT_Study.h"
40
41 #include "SALOMEDSClient_AttributeComment.hxx"
42 #include "SALOMEDSClient_AttributeName.hxx"
43
44 #include <qpixmap.h>
45
46 using namespace std;
47
48
49 //************************************************************************
50 VISU_TimeAnimation::VISU_TimeAnimation (_PTR(Study) theStudy,
51                                         VISU::View3D_ptr theView3D)
52 {
53   myStudy = theStudy;
54   myIsActive = false;
55   myFrame = 0;
56   mySpeed = 1;
57   myProportional = false;
58   myView = 0;
59
60   if (!CORBA::is_nil(theView3D)) {
61     VISU::View3D_i* pView = dynamic_cast<VISU::View3D_i*>(GetServant(theView3D).in());
62     //QAD_StudyFrame* aStudyFrame = pView->GetStudyFrame();
63     //myView = VISU::GetViewFrame(aStudyFrame);
64     SUIT_ViewWindow* aVW = pView->myViewWindow;
65     myView = VISU::GetViewWindow(aVW);
66   }
67
68   myMaxVal = 0;
69   myMinVal = 0;
70   myTimeMin = 0;
71   myTimeMax = 0;
72   myLastError = "";
73   myCycling = false;
74
75   myAnimEntry = "";
76 }
77
78
79 //************************************************************************
80 VISU_TimeAnimation::~VISU_TimeAnimation()
81 {
82   for (int i = 0; i < getNbFields(); i++) {
83     clearData(myFieldsLst[i]);
84   }
85 }
86
87
88 //************************************************************************
89 void VISU_TimeAnimation::addField (_PTR(SObject) theField)
90 {
91   FieldData aNewData;
92   aNewData.myField = theField;
93   aNewData.myNbFrames = 0;
94   aNewData.myPrsType = VISU::TSCALARMAP;
95   aNewData.myOffset[0] = aNewData.myOffset[1] = aNewData.myOffset[2] = 0;
96   VISU::Storable::TRestoringMap aMap = getMapOfValue(aNewData.myField);
97   aNewData.myNbTimes = VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
98   myFieldsLst.append(aNewData);
99
100   //find Min/Max timestamps
101   if ((myTimeMin == 0) && (myTimeMax == 0)) {
102     _PTR(ChildIterator) anIter = myStudy->NewChildIterator(theField);
103     anIter->Next(); // First is reference on support
104     myTimeMin = getTimeValue(anIter->Value());
105     for(; anIter->More(); anIter->Next()) {
106       myTimeMax = getTimeValue(anIter->Value());
107     }
108   }
109 }
110
111 //************************************************************************
112 void VISU_TimeAnimation::addField (SALOMEDS::SObject_ptr theField)
113 {
114   FieldData aNewData;
115   _PTR(SObject) aField = VISU::GetClientSObject(theField, myStudy);
116   aNewData.myField = aField;
117   aNewData.myNbFrames = 0;
118   aNewData.myPrsType = VISU::TSCALARMAP;
119   VISU::Storable::TRestoringMap aMap = getMapOfValue(aNewData.myField);
120   aNewData.myNbTimes = VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
121   myFieldsLst.append(aNewData);
122
123   //find Min/Max timestamps
124   if ((myTimeMin == 0) && (myTimeMax == 0)) {
125     _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aField);
126     anIter->Next(); // First is reference on support
127     myTimeMin = getTimeValue(anIter->Value());
128     for(; anIter->More(); anIter->Next()) {
129       myTimeMax = getTimeValue(anIter->Value());
130     }
131   }
132 }
133
134
135 //************************************************************************
136 void VISU_TimeAnimation::clearData(FieldData& theData) {
137   theData.myTiming.clear();
138   vtkRenderer* aRen = myView->getRenderer();
139   if (!theData.myActors.empty()) {
140     for (int i = 0, iEnd = theData.myActors.size(); i < iEnd; i++) {
141       if (theData.myActors[i] != 0) {
142         theData.myActors[i]->RemoveFromRender(aRen);
143         theData.myActors[i]->Delete();
144       }
145     }
146     theData.myActors.clear();
147   }
148   if (!theData.myPrs.empty()) {
149     for (int i = 0, iEnd = theData.myPrs.size(); i < iEnd; i++)
150       if (theData.myPrs[i] != 0) {
151         theData.myPrs[i]->_remove_ref();
152       }
153     theData.myPrs.clear();
154   }
155   theData.myNbFrames = 0;
156   myView->update();
157 }
158
159
160 //************************************************************************
161 void VISU_TimeAnimation::generatePresentations(CORBA::Long theFieldNum) {
162   FieldData& aData = myFieldsLst[theFieldNum];
163
164   // Delete previous presentations
165   clearData(aData);
166
167   VISU::Result_i* pResult = createPresent(aData.myField);
168   VISU::Storable::TRestoringMap aMap = getMapOfValue(aData.myField);
169   aData.myNbFrames = aData.myNbTimes;
170     //VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
171
172   aData.myPrs.resize(aData.myNbTimes,NULL);
173   aData.myTiming.resize(aData.myNbTimes);
174
175   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aData.myField);
176   _PTR(SObject) aTimeStamp;
177   anIter->Next(); // First is reference on support
178   long i = 0;
179   double aMin = VTK_LARGE_FLOAT, aMax = -VTK_LARGE_FLOAT;
180   for (; anIter->More(); anIter->Next()) {
181     if (i == aData.myNbTimes) {
182       MESSAGE("There are extra timestamps in field");
183       return;
184     }
185     aTimeStamp = anIter->Value();
186     if (!aTimeStamp) continue;
187
188     aData.myTiming[i] = getTimeValue(aTimeStamp);
189     if (isRangeDefined()) {
190       if (aData.myTiming[i] < myMinVal) continue;
191       if (aData.myTiming[i] > myMaxVal) break;
192     }
193
194     VISU::Storable::TRestoringMap aTimeMap = getMapOfValue(aTimeStamp);
195     QString aMeshName = VISU::Storable::FindValue(aTimeMap,"myMeshName");
196     VISU::Entity anEntity = (VISU::Entity) VISU::Storable::FindValue(aTimeMap,"myEntityId").toInt();
197     QString aFieldName = VISU::Storable::FindValue(aTimeMap,"myFieldName");
198     int aTimeStampId = VISU::Storable::FindValue(aTimeMap,"myTimeStampId").toInt();
199
200     switch (aData.myPrsType) {
201     case VISU::TSCALARMAP: // ScalarMap
202       {
203         VISU::ScalarMap_i* aPresent = new VISU::ScalarMap_i(pResult, false);
204         aPresent->Create(aMeshName.latin1(), anEntity,
205                          aFieldName.latin1(), aTimeStampId);
206         //VISU::ScalarMap_var aTmp = aPresent->_this();
207         //aPresent->_remove_ref();
208         aData.myPrs[i] = aPresent;
209       }
210       break;
211
212     case VISU::TISOSURFACE: // Iso Surfaces
213       {
214         VISU::IsoSurfaces_i* aPresent = new VISU::IsoSurfaces_i(pResult, false);
215         aPresent->Create(aMeshName.latin1(), anEntity,
216                          aFieldName.latin1(), aTimeStampId);
217         //VISU::IsoSurfaces_var aTmp = aPresent->_this();
218         //aPresent->_remove_ref();
219         aData.myPrs[i] = aPresent;
220       }
221       break;
222
223     case VISU::TCUTPLANES: // Cut Planes
224       {
225         VISU::CutPlanes_i* aPresent = new VISU::CutPlanes_i(pResult, false);
226         aPresent->Create(aMeshName.latin1(), anEntity,
227                          aFieldName.latin1(), aTimeStampId);
228         //VISU::CutPlanes_var aTmp = aPresent->_this();
229         //aPresent->_remove_ref();
230         aData.myPrs[i] = aPresent;
231       }
232       break;
233
234     case VISU::TPLOT3D: // Cut Planes
235       {
236         VISU::Plot3D_i* aPresent = new VISU::Plot3D_i (pResult, false);
237         aPresent->Create(aMeshName.latin1(), anEntity,
238                          aFieldName.latin1(), aTimeStampId);
239         aData.myPrs[i] = aPresent;
240       }
241       break;
242
243     case VISU::TDEFORMEDSHAPE: // Deformed Shape
244       {
245         VISU::DeformedShape_i* aPresent = new VISU::DeformedShape_i(pResult, false);
246         aPresent->Create(aMeshName.latin1(), anEntity,
247                          aFieldName.latin1(), aTimeStampId);
248         //VISU::DeformedShape_var aTmp = aPresent->_this();
249         //aPresent->_remove_ref();
250         aData.myPrs[i] = aPresent;
251       }
252       break;
253
254     case VISU::TVECTORS: // Vectors
255       {
256         VISU::Vectors_i* aPresent = new VISU::Vectors_i(pResult, false);
257         aPresent->Create(aMeshName.latin1(), anEntity,
258                          aFieldName.latin1(), aTimeStampId);
259         //VISU::Vectors_var aTmp = aPresent->_this();
260         //aPresent->_remove_ref();
261         aData.myPrs[i] = aPresent;
262       }
263       break;
264
265     case VISU::TSTREAMLINES: // Stream Lines
266       {
267         VISU::StreamLines_i* aPresent = new VISU::StreamLines_i(pResult, false);
268         aPresent->Create(aMeshName.latin1(), anEntity,
269                          aFieldName.latin1(), aTimeStampId);
270         //VISU::StreamLines_var aTmp = aPresent->_this();
271         //aPresent->_remove_ref();
272         aData.myPrs[i] = aPresent;
273       }
274       break;
275     default:
276       MESSAGE("Not implemented for this presentation type: " << aData.myPrsType);
277       return;
278     }
279     if (aData.myPrs[i]->GetMin() < aMin) aMin = aData.myPrs[i]->GetMin();
280     if (aData.myPrs[i]->GetMax() > aMax) aMax = aData.myPrs[i]->GetMax();
281     i++;
282   }
283   aData.myNbFrames = i;
284   QString aFixRange = VISU::GetResourceMgr()->stringValue("Visu:SBImposeRange");
285   if (aFixRange.compare("true") != 0) {
286     for (i = 0; i < aData.myNbFrames; i++) {
287       aData.myPrs[i]->SetRange(aMin, aMax);
288       aData.myPrs[i]->SetOffset(aData.myOffset);
289     }
290     if (aData.myPrsType == VISU::TISOSURFACE)
291       for (i = 0; i < aData.myNbFrames; i++)
292         if (VISU::IsoSurfaces_i* aPrs = dynamic_cast<VISU::IsoSurfaces_i*>(aData.myPrs[i]))
293            aPrs->SetSubRange(aMin, aMax);
294   }
295 }
296
297
298 //************************************************************************
299 CORBA::Boolean VISU_TimeAnimation::generateFrames() {
300   if (!myView) {
301     MESSAGE("Viewer is not defined for animation");
302     return false;
303   }
304
305   myLastError = QString("Frame(s) for ");
306   bool aNoError = true;
307
308   clearView();
309
310   for (int i = 0; i < getNbFields(); i++) {
311     FieldData& aData = myFieldsLst[i];
312     aData.myActors.resize(aData.myNbFrames,NULL);
313     for (long j = 0; j < aData.myNbFrames; j++) {
314       VISU_Actor* aActor = NULL;
315       try{
316         aData.myPrs[j]->SetOffset(aData.myOffset);
317         aActor = aData.myPrs[j]->CreateActor();
318         myView->AddActor(aActor);
319         if(j == 0)
320           aActor->VisibilityOn();
321         else
322           aActor->VisibilityOff();
323       }catch(...){ //catch(std::runtime_error& exc){
324         aNoError = false;
325         myLastError += QString("%1 ").arg(aData.myTiming[j]);
326       }
327       aData.myActors[j] = aActor;
328     }
329   }
330   myFrame = 0;
331   myLastError += QString(" timestamp(s) cannot be created.");
332   emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
333   myView->Repaint();
334   return aNoError;
335 }
336
337 //************************************************************************
338 void VISU_TimeAnimation::clearView() {
339   if (!myView) {
340     MESSAGE("Viewer is not defined for animation");
341     return;
342   }
343   vtkRenderer* aRen = myView->getRenderer();
344   for (int i = 0; i < getNbFields(); i++) {
345     FieldData& aData = myFieldsLst[i];
346     if (!aData.myActors.empty()) {
347       for (int i = 0, iEnd = aData.myActors.size(); i < iEnd; i++) {
348         if (aData.myActors[i] != 0) {
349           aData.myActors[i]->RemoveFromRender(aRen);
350           aData.myActors[i]->Delete();
351         }
352       }
353       aData.myActors.clear();
354     }
355   }
356   VISU::RepaintView(myView);
357 }
358
359 //************************************************************************
360 void VISU_TimeAnimation::stopAnimation() {
361   myIsActive = false;
362 }
363
364 //************************************************************************
365 void VISU_TimeAnimation::startAnimation() {
366   if (!myIsActive) {
367     myIsActive = true;
368     QThread::start();
369   }
370 }
371
372 //************************************************************************
373 void VISU_TimeAnimation::nextFrame() {
374   stopAnimation();
375   if (myFrame < (myFieldsLst[0].myNbFrames-1)) {
376     int i;
377     for (i = 0; i < getNbFields(); i++)
378       if (myFieldsLst[i].myActors[myFrame] != 0)
379         myFieldsLst[i].myActors[myFrame]->VisibilityOff();
380
381     myFrame++;
382     for (i = 0; i < getNbFields(); i++)
383       if (myFieldsLst[i].myActors[myFrame] != 0)
384         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
385
386     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
387     myView->Repaint();
388   }
389 }
390
391 //************************************************************************
392 void VISU_TimeAnimation::prevFrame() {
393   stopAnimation();
394   if (myFrame > 0) {
395     int i;
396     for (i = 0; i < getNbFields(); i++)
397       if (myFieldsLst[i].myActors[myFrame] != 0)
398         myFieldsLst[i].myActors[myFrame]->VisibilityOff();
399
400     myFrame--;
401     for (i = 0; i < getNbFields(); i++)
402       if (myFieldsLst[i].myActors[myFrame] != 0)
403         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
404
405     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
406     myView->Repaint();
407   }
408 }
409
410 //************************************************************************
411 void VISU_TimeAnimation::firstFrame() {
412   stopAnimation();
413   int i;
414   for (i = 0; i < getNbFields(); i++)
415     if(!myFieldsLst[i].myActors.empty())
416       if (myFieldsLst[i].myActors[myFrame] != 0)
417         myFieldsLst[i].myActors[myFrame]->VisibilityOff();
418   myFrame = 0;
419   for (i = 0; i < getNbFields(); i++)
420     if(!myFieldsLst[i].myActors.empty())
421       if (myFieldsLst[i].myActors[myFrame] != 0)
422         myFieldsLst[i].myActors[myFrame]->VisibilityOn();
423   if(!myFieldsLst[0].myTiming.empty()){
424     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
425     myView->Repaint();
426   }
427 }
428
429 //************************************************************************
430 void VISU_TimeAnimation::lastFrame() {
431   stopAnimation();
432   int i;
433   for (i = 0; i < getNbFields(); i++)
434    if (myFieldsLst[i].myActors[myFrame] != 0)
435       myFieldsLst[i].myActors[myFrame]->VisibilityOff();
436
437   myFrame = myFieldsLst[0].myNbFrames-1;
438   for (i = 0; i < getNbFields(); i++)
439     if (myFieldsLst[i].myActors[myFrame] != 0)
440       myFieldsLst[i].myActors[myFrame]->VisibilityOn();
441
442   emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
443   myView->Repaint();
444 }
445
446
447 //************************************************************************
448 // For Batchmode using
449 void VISU_TimeAnimation::gotoFrame(CORBA::Long theFrame) {
450   if ((theFrame < 0) || (theFrame > (getNbFrames()-1)))
451     return;
452   stopAnimation();
453   qApp->lock();
454   qApp->syncX();
455   int i;
456   for (i = 0; i < getNbFields(); i++)
457     if (myFieldsLst[i].myActors[myFrame] != 0)
458       myFieldsLst[i].myActors[myFrame]->VisibilityOff();
459
460   myFrame = theFrame;
461   for (i = 0; i < getNbFields(); i++)
462     if (myFieldsLst[i].myActors[myFrame] != 0)
463       myFieldsLst[i].myActors[myFrame]->VisibilityOn();
464
465   emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
466   myView->Repaint();
467   qApp->flushX();
468   qApp->processEvents(3);
469   qApp->unlock();
470 }
471
472
473 //************************************************************************
474 VISU::ScalarMap_ptr VISU_TimeAnimation::getPresentation(CORBA::Long theField, CORBA::Long theFrame) {
475   if ((theField > getNbFields()) || (theField < 0))
476     return VISU::ScalarMap::_nil();
477   if ((theFrame < 0) || (theFrame > (myFieldsLst[theField].myNbFrames - 1)))
478     return VISU::ScalarMap::_nil();
479   return myFieldsLst[theField].myPrs[theFrame]->_this();
480 }
481
482
483 //************************************************************************
484 CORBA::Long VISU_TimeAnimation::getNbFrames() {
485   return (getNbFields() > 0)? myFieldsLst[0].myNbFrames : 0;
486 }
487
488
489 //************************************************************************
490 void VISU_TimeAnimation::run()
491 {
492   if (!myView) {
493     MESSAGE("Viewer is not defined for animation");
494     return;
495   }
496   double k = 1;
497   bool   isDumping = !myDumpPath.isEmpty();
498   double aOneVal = 1;
499   if (myFieldsLst[0].myNbFrames > 2)
500     aOneVal = myFieldsLst[0].myTiming[1] - myFieldsLst[0].myTiming[0];
501   qApp->lock();
502   while (myIsActive) {
503     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
504     for (int i = 0; i < getNbFields(); i++) {
505       FieldData& aData = myFieldsLst[i];
506       if (myFrame > 0) {
507         if (aData.myActors[myFrame-1] != 0)
508           aData.myActors[myFrame-1]->VisibilityOff();
509       } else {
510         if (aData.myActors[aData.myNbFrames-1] != 0)
511           aData.myActors[aData.myNbFrames-1]->VisibilityOff();
512       }
513       if (aData.myActors[myFrame] != 0) {
514         aData.myActors[myFrame]->VisibilityOn();
515       }
516     }
517     myView->Repaint(false);
518
519     int delay = 100;
520     if (isDumping) {
521       QPixmap px = QPixmap::grabWindow(myView->winId());
522       QString aFile(myDumpPath);
523       QString aName = QString("%1").arg(myFieldsLst[0].myTiming[myFrame]);
524       int aPos = -1;
525       while ((aPos = aName.find(".")) > -1 )
526         aName.replace(aPos, 1, "_");
527       aFile += aName;
528       aFile += ".jpeg";
529       px.save(aFile, "JPEG");
530     } else {
531       k = 1;
532       if (myProportional) {
533         switch (myFrame) {
534         case 0:
535           break;
536         case 1:
537           if (myFieldsLst[0].myNbFrames > 2)
538             k = (myFieldsLst[0].myTiming[myFrame+1] -
539                  myFieldsLst[0].myTiming[myFrame]) / aOneVal;
540           break;
541         default:
542           if (myFrame < (myFieldsLst[0].myNbFrames - 1))
543             k = (myFieldsLst[0].myTiming[myFrame+1] -
544                  myFieldsLst[0].myTiming[myFrame]) / aOneVal;
545         }
546       }
547       delay = (int)(1000. * k / mySpeed);
548     }
549     qApp->unlock();
550     msleep(delay);
551     qApp->lock();
552
553     if (!myIsActive) break;
554
555     myFrame++;
556     if (myFrame == myFieldsLst[0].myNbFrames) {
557       if (!myCycling) {
558         myIsActive = false;
559         myFrame--;
560         break;
561       } else
562         myFrame = 0;
563     }
564   }
565   emit stopped();
566   qApp->unlock();
567   QThread::exit();
568 }
569
570 //************************************************************************
571 VISU::Result_i* VISU_TimeAnimation::createPresent (_PTR(SObject) theField)
572 {
573   _PTR(SObject) aSObj = theField->GetFather();
574   aSObj = aSObj->GetFather();
575   aSObj = aSObj->GetFather();
576   CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObj);
577   if (CORBA::is_nil(anObject)) return NULL;
578   return dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
579 }
580
581 //************************************************************************
582 VISU::Storable::TRestoringMap VISU_TimeAnimation::getMapOfValue (_PTR(SObject) theSObject)
583 {
584   VISU::Storable::TRestoringMap aMap;
585   if (theSObject) {
586     _PTR(GenericAttribute) anAttr;
587     if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
588       _PTR(AttributeComment) aComment (anAttr);
589       std::string aString = aComment->Value();
590       QString strIn (aString.c_str());
591       VISU::Storable::StrToMap(strIn, aMap);
592     }
593   }
594   return aMap;
595 }
596
597 //************************************************************************
598 double VISU_TimeAnimation::getTimeValue (_PTR(SObject) theTimeStamp)
599 {
600   _PTR(GenericAttribute) anAttr;
601   if (theTimeStamp->FindAttribute(anAttr, "AttributeName")) {
602     _PTR(AttributeName) aName (anAttr);
603     QString aNameString (aName->Value().c_str());
604     int time_len = aNameString.find(',');
605     if (time_len > -1)
606       return aNameString.left(time_len).toDouble();
607     else
608       return aNameString.toDouble();
609   }
610   return -1.0;
611 }
612
613 //************************************************************************
614 void VISU_TimeAnimation::setSpeed(CORBA::Long theSpeed)
615 {
616   mySpeed = (theSpeed<1)? 1 : theSpeed;
617 }
618
619 //************************************************************************
620 int VISU_TimeAnimation::myNBAnimations = 0;
621 QString VISU_TimeAnimation::GenerateName()
622 {
623   return VISU::GenerateName("Animation", myNBAnimations++);
624 }
625
626 //************************************************************************
627 std::string GetPresentationComment (VISU::VISUType thePrsType)
628 {
629   std::string aPrsCmt;
630   switch (thePrsType) {
631   case VISU::TSCALARMAP:
632     aPrsCmt = VISU::ScalarMap_i::myComment;
633     break;
634   case VISU::TISOSURFACE:
635     aPrsCmt = VISU::IsoSurfaces_i::myComment;
636     break;
637   case VISU::TCUTPLANES:
638     aPrsCmt = VISU::CutPlanes_i::myComment;
639     break;
640   case VISU::TPLOT3D:
641     aPrsCmt = VISU::Plot3D_i::myComment;
642     break;
643   case VISU::TDEFORMEDSHAPE:
644     aPrsCmt = VISU::DeformedShape_i::myComment;
645     break;
646   case VISU::TVECTORS:
647     aPrsCmt = VISU::Vectors_i::myComment;
648     break;
649   case VISU::TSTREAMLINES:
650     aPrsCmt = VISU::StreamLines_i::myComment;
651     break;
652   default:
653     aPrsCmt = "Unknown presentation";
654     break;
655   }
656   return aPrsCmt;
657 }
658
659 //************************************************************************
660 SALOMEDS::SObject_ptr VISU_TimeAnimation::publishInStudy()
661 {
662   if (myStudy->GetProperties()->IsLocked())
663     return SALOMEDS::SObject::_nil();
664
665   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
666   aStudyBuilder->NewCommand();  // There is a transaction
667   _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
668   std::string aSComponentEntry = aSComponent->GetID();
669
670   QString aComment;
671   aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
672                    VISU::TANIMATION,myMinVal,myMaxVal);
673
674   string anEntry = VISU::CreateAttributes(myStudy,aSComponentEntry.c_str(),"","",
675                                           GenerateName(),"",aComment,true);
676   myAnimEntry = anEntry.c_str();
677   _PTR(SObject) aAnimSObject = myStudy->FindObjectID(anEntry.c_str());
678
679   for (int i = 0; i < getNbFields(); i++) {
680     FieldData& aData = myFieldsLst[i];
681     _PTR(SObject) newObj = aStudyBuilder->NewObject(aAnimSObject);
682     aStudyBuilder->Addreference(newObj, aData.myField);
683     if (aData.myPrs.empty()) {
684       VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
685                              GetPresentationComment(aData.myPrsType).c_str(),"","",true);
686     } else {
687       ostringstream strOut;
688       aData.myPrs[0]->ToStream(strOut);
689       string aPrsComment = strOut.str();
690       VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
691                              aData.myPrs[0]->GetComment(),"",aPrsComment.c_str(),true);
692     }
693   }
694   aStudyBuilder->CommitCommand();
695
696   return VISU::GetSObject(aAnimSObject);
697 }
698
699 //************************************************************************
700 void VISU_TimeAnimation::saveAnimation()
701 {
702   if (myStudy->GetProperties()->IsLocked())  return;
703   if (myAnimEntry.isEmpty()) return;
704
705   _PTR(SObject) aAnimSObject = myStudy->FindObjectID(myAnimEntry.latin1());
706   if (!aAnimSObject) return;
707
708   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
709   aStudyBuilder->NewCommand();  // There is a transaction
710   _PTR(SComponent) aSComponent = VISU::ClientFindOrCreateVisuComponent(myStudy);
711   std::string aSComponentEntry = aSComponent->GetID();
712
713   QString aComment;
714   aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
715                    VISU::TANIMATION,myMinVal,myMaxVal);
716
717   _PTR(GenericAttribute) anAttr;
718   anAttr = aStudyBuilder->FindOrCreateAttribute(aAnimSObject, "AttributeComment");
719   _PTR(AttributeComment) aCmnt (anAttr);
720   aCmnt->SetValue(aComment.latin1());
721
722   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
723   int i;
724   for (i = 0, anIter->Init(); anIter->More(); anIter->Next(), i++) {
725     FieldData& aData = myFieldsLst[i];
726
727     _PTR(SObject) aRefObj = anIter->Value();
728     _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
729     anPrsIter->Init();
730
731     string aPrsComment, aPrsNameTxt;
732     if (aData.myPrs.empty()) {
733       aPrsComment = "";
734       aPrsNameTxt = GetPresentationComment(aData.myPrsType);
735     } else {
736       ostringstream strOut;
737       aData.myPrs[0]->ToStream(strOut);
738       aPrsComment = strOut.str();
739
740       aPrsNameTxt = aData.myPrs[0]->GetComment();
741     }
742
743     if (anPrsIter->More()) {
744       _PTR(SObject) aPrsObj = anPrsIter->Value();
745       anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeComment");
746       aCmnt = _PTR(AttributeComment)(anAttr);
747       aCmnt->SetValue(aPrsComment.c_str());
748
749       anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeName");
750       _PTR(AttributeName) aPrsName (anAttr);
751       aPrsName->SetValue(aPrsNameTxt);
752
753     } else {
754       VISU::CreateAttributes(myStudy, aRefObj->GetID().c_str(),"","",
755                              aPrsNameTxt.c_str(),"",aPrsComment.c_str(),true);
756     }
757   }
758   aStudyBuilder->CommitCommand();
759 }
760
761 //************************************************************************
762 void VISU_TimeAnimation::restoreFromStudy(SALOMEDS::SObject_ptr theField)
763 {
764   _PTR(SObject) aAnimSObject = VISU::GetClientSObject(theField, myStudy);
765   restoreFromStudy(aAnimSObject);
766 }
767
768 void VISU_TimeAnimation::restoreFromStudy(_PTR(SObject) theField)
769 {
770   _PTR(SObject) aAnimSObject = theField;
771
772   VISU::Storable::TRestoringMap aMap;
773   _PTR(GenericAttribute) anAttr;
774   if (!aAnimSObject->FindAttribute(anAttr, "AttributeComment")) return;
775
776   _PTR(AttributeComment) aComment (anAttr);
777   string aComm = aComment->Value();
778   QString strIn (aComm.c_str());
779   VISU::Storable::StrToMap(strIn,aMap);
780   bool isExist;
781
782   myMinVal = VISU::Storable::FindValue(aMap,"myMinVal",&isExist).toDouble();
783   myMaxVal = VISU::Storable::FindValue(aMap,"myMaxVal",&isExist).toDouble();
784
785   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
786   for (anIter->Init(); anIter->More(); anIter->Next()) {
787     _PTR(SObject) aRefObj = anIter->Value();
788     _PTR(SObject) aFieldObj;
789     if (!aRefObj->ReferencedObject(aFieldObj) ) continue;
790     addField(aFieldObj);
791     FieldData& aData = getFieldData(getNbFields()-1);
792
793     // Get Presentation object
794     _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
795     anPrsIter->Init();
796     if (!anPrsIter->More()) continue;
797     _PTR(SObject) aPrsObj = anPrsIter->Value();
798     if (!aPrsObj->FindAttribute(anAttr, "AttributeName")) continue;
799     _PTR(AttributeName) aName (anAttr);
800     string aStr = aName->Value();
801     QString strName (aStr.c_str());
802
803     if (strName == VISU::ScalarMap_i::myComment.c_str())
804       aData.myPrsType = VISU::TSCALARMAP;
805     else if (strName == VISU::IsoSurfaces_i::myComment.c_str())
806       aData.myPrsType = VISU::TISOSURFACE;
807     else if (strName == VISU::CutPlanes_i::myComment.c_str())
808       aData.myPrsType = VISU::TCUTPLANES;
809     else if (strName == VISU::Plot3D_i::myComment.c_str())
810       aData.myPrsType = VISU::TPLOT3D;
811     else if (strName == VISU::DeformedShape_i::myComment.c_str())
812       aData.myPrsType = VISU::TDEFORMEDSHAPE;
813     else if (strName == VISU::Vectors_i::myComment.c_str())
814       aData.myPrsType = VISU::TVECTORS;
815     else if (strName == VISU::StreamLines_i::myComment.c_str())
816       aData.myPrsType = VISU::TSTREAMLINES;
817     else
818       continue;
819     generatePresentations(getNbFields()-1);
820
821     if (!aPrsObj->FindAttribute(anAttr, "AttributeComment")) continue;
822     _PTR(AttributeComment) aPrsComment (anAttr);
823     string aPrsComm = aPrsComment->Value();
824     if (aPrsComm.length() > 0) {
825       QString strPrsIn (aPrsComm.c_str());
826       VISU::Storable::TRestoringMap aPrsMap;
827       VISU::Storable::StrToMap(strPrsIn,aPrsMap);
828
829       aData.myPrs[0]->Restore(aPrsMap);
830     }
831     aData.myPrs[0]->GetOffset(aData.myOffset);
832     for (int i = 1; i < aData.myNbFrames; i++) {
833       aData.myPrs[i]->SameAs(aData.myPrs[0]);
834     }
835   }
836   string aStr = aAnimSObject->GetID();
837   myAnimEntry = aStr.c_str();
838 }
839
840
841 //========================================================================
842 //========================================================================
843 //========================================================================
844 struct TNewAnimationEvent: public SALOME_Event
845 {
846   std::string myStudyName;
847   VISU::View3D_ptr myView3D;
848
849   typedef VISU_TimeAnimation* TResult;
850   TResult myResult;
851
852   TNewAnimationEvent (std::string theStudyName, VISU::View3D_ptr theView3D):
853     myStudyName(theStudyName),
854     myView3D(VISU::View3D::_duplicate(theView3D)),
855     myResult(NULL)
856   {}
857
858   virtual
859   void
860   Execute()
861   {
862     SUIT_Session* aSession = SUIT_Session::session();
863     QPtrList<SUIT_Application> anApplications = aSession->applications();
864     QPtrListIterator<SUIT_Application> anIter (anApplications);
865     while (SUIT_Application* anApp = anIter.current()) {
866       ++anIter;
867       if (SUIT_Study* aSStudy = anApp->activeStudy()) {
868         if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
869           if (_PTR(Study) aCStudy = aStudy->studyDS()) {
870             if (myStudyName == aCStudy->Name()) {
871               myResult = new VISU_TimeAnimation (aCStudy, myView3D);
872               break;
873             }
874           }
875         }
876       }
877     }
878   }
879 };
880
881 VISU_TimeAnimation_i::VISU_TimeAnimation_i (SALOMEDS::Study_ptr theStudy,
882                                             VISU::View3D_ptr theView3D)
883 {
884   std::string aStudyName = theStudy->Name();
885   myAnim = ProcessEvent(new TNewAnimationEvent (aStudyName, theView3D));
886 }
887
888 VISU_TimeAnimation_i::~VISU_TimeAnimation_i()
889 {
890   delete myAnim;
891 }
892
893 void VISU_TimeAnimation_i::addField (SALOMEDS::SObject_ptr theField)
894 {
895   myAnim->addField(theField);
896 }
897
898 CORBA::Boolean VISU_TimeAnimation_i::generateFrames()
899 {
900   return ProcessEvent(new TMemFunEvent<VISU_TimeAnimation,bool>
901                       (myAnim,&VISU_TimeAnimation::generateFrames));
902 }
903
904 void VISU_TimeAnimation_i::generatePresentations (CORBA::Long theFieldNum)
905 {
906   myAnim->generatePresentations(theFieldNum);
907 }
908
909 void VISU_TimeAnimation_i::clearView()
910 {
911   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
912                    (myAnim,&VISU_TimeAnimation::clearView));
913 }
914
915 void VISU_TimeAnimation_i::stopAnimation()
916 {
917   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
918                    (myAnim,&VISU_TimeAnimation::stopAnimation));
919 }
920
921 void VISU_TimeAnimation_i::startAnimation()
922 {
923   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
924                    (myAnim,&VISU_TimeAnimation::startAnimation));
925 }
926
927 void VISU_TimeAnimation_i::nextFrame()
928 {
929   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
930                    (myAnim,&VISU_TimeAnimation::nextFrame));
931 }
932
933 void VISU_TimeAnimation_i::prevFrame()
934 {
935   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
936                    (myAnim,&VISU_TimeAnimation::prevFrame));
937 }
938
939 void VISU_TimeAnimation_i::firstFrame()
940 {
941   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
942                    (myAnim,&VISU_TimeAnimation::firstFrame));
943 }
944
945 void VISU_TimeAnimation_i::lastFrame()
946 {
947   ProcessVoidEvent(new TVoidMemFunEvent<VISU_TimeAnimation>
948                    (myAnim,&VISU_TimeAnimation::lastFrame));
949 }
950
951 void VISU_TimeAnimation_i::gotoFrame(CORBA::Long theFrame)
952 {
953   ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_TimeAnimation,CORBA::Long>
954                    (myAnim,&VISU_TimeAnimation::gotoFrame,theFrame));
955 }
956
957 CORBA::Long VISU_TimeAnimation_i::getNbFields()
958 {
959   return myAnim->getNbFields();
960 }
961
962 CORBA::Long VISU_TimeAnimation_i::getNbFrames()
963 {
964   return myAnim->getNbFrames();
965 }
966
967 CORBA::Boolean VISU_TimeAnimation_i::isRunning()
968 {
969   return myAnim->isRunning();
970 }
971
972 CORBA::Long VISU_TimeAnimation_i::getCurrentFrame()
973 {
974   return myAnim->getCurrentFrame();
975 }
976
977 VISU::ScalarMap_ptr VISU_TimeAnimation_i::getPresentation
978                     (CORBA::Long theField, CORBA::Long theFrame)
979 {
980   return myAnim->getPresentation(theField,theFrame);
981 }
982
983 void VISU_TimeAnimation_i::setPresentationType (CORBA::Long theFieldNum,
984                                                 VISU::VISUType theType)
985 {
986   myAnim->setPresentationType(theFieldNum,theType);
987 }
988
989 VISU::VISUType VISU_TimeAnimation_i::getPresentationType (CORBA::Long theFieldNum)
990 {
991   return myAnim->getPresentationType(theFieldNum);
992 }
993
994 void VISU_TimeAnimation_i::setSpeed(CORBA::Long theSpeed)
995 {
996   myAnim->setSpeed(theSpeed);
997 }
998
999 CORBA::Long VISU_TimeAnimation_i::getSpeed()
1000 {
1001   return myAnim->getSpeed();
1002 }
1003
1004 CORBA::Boolean VISU_TimeAnimation_i::isProportional()
1005 {
1006   return myAnim->isProportional();
1007 }
1008
1009 void VISU_TimeAnimation_i::setAnimationRange (CORBA::Double theMin,
1010                                               CORBA::Double theMax)
1011 {
1012   myAnim->setAnimationRange(theMin,theMax);
1013 }
1014
1015 CORBA::Double VISU_TimeAnimation_i::getMinRange()
1016 {
1017   return myAnim->getMinRange();
1018 }
1019
1020 CORBA::Double VISU_TimeAnimation_i::getMaxRange()
1021 {
1022   return myAnim->getMaxRange();
1023 }
1024
1025 CORBA::Boolean VISU_TimeAnimation_i::isRangeDefined()
1026 {
1027   return myAnim->isRangeDefined();
1028 }
1029
1030 void VISU_TimeAnimation_i::dumpTo (const char* thePath)
1031 {
1032   myAnim->dumpTo(thePath);
1033 }
1034
1035 CORBA::Boolean VISU_TimeAnimation_i::isCycling()
1036 {
1037   return myAnim->isCycling();
1038 }
1039
1040 CORBA::Double VISU_TimeAnimation_i::getMinTime()
1041 {
1042   return myAnim->getMinTime();
1043 }
1044
1045 CORBA::Double VISU_TimeAnimation_i::getMaxTime()
1046 {
1047   return myAnim->getMaxTime();
1048 }
1049
1050 void VISU_TimeAnimation_i::setProportional (CORBA::Boolean theProp)
1051 {
1052   myAnim->setProportional(theProp);
1053 }
1054
1055 void VISU_TimeAnimation_i::setCycling (CORBA::Boolean theCycle)
1056 {
1057   myAnim->setCycling(theCycle);
1058 }
1059
1060 SALOMEDS::SObject_ptr VISU_TimeAnimation_i::publishInStudy()
1061 {
1062   return myAnim->publishInStudy();
1063 }
1064
1065 void VISU_TimeAnimation_i::restoreFromStudy(SALOMEDS::SObject_ptr theObj)
1066 {
1067   myAnim->restoreFromStudy(theObj);
1068 }
1069
1070 CORBA::Boolean VISU_TimeAnimation_i::isSavedInStudy()
1071 {
1072   return myAnim->isSavedInStudy();
1073 }
1074
1075 void VISU_TimeAnimation_i::saveAnimation()
1076 {
1077   myAnim->saveAnimation();
1078 }