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