Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/visu.git] / src / VISU_I / VISU_TimeAnimation.cxx
index b9f713c22dbe8b2782fc54493699c60cd139372e..66b97cd5cc67bc3f896279dd674013c74634a7a5 100644 (file)
 #include "VISU_ScalarMap_i.hh"
 #include "VISU_IsoSurfaces_i.hh"
 #include "VISU_DeformedShape_i.hh"
+#include "VISU_ScalarMapOnDeformedShape_i.hh"
 #include "VISU_CutPlanes_i.hh"
+#include "VISU_Plot3D_i.hh"
 #include "VISU_CutLines_i.hh"
 #include "VISU_Vectors_i.hh"
 #include "VISU_StreamLines_i.hh"
+#include "VISU_GaussPoints_i.hh"
 #include "VISU_ViewManager_i.hh"
 #include "VISU_View_i.hh"
 
 
 #include "SALOMEDSClient_AttributeComment.hxx"
 #include "SALOMEDSClient_AttributeName.hxx"
+#include "CASCatch.hxx"
 
 #include <qpixmap.h>
+#include <qimage.h>
+#include <qstrlist.h>
+#include <qdir.h>
 
 using namespace std;
 
 
-//************************************************************************
+//------------------------------------------------------------------------
 VISU_TimeAnimation::VISU_TimeAnimation (_PTR(Study) theStudy,
                                         VISU::View3D_ptr theView3D)
 {
@@ -58,33 +65,59 @@ VISU_TimeAnimation::VISU_TimeAnimation (_PTR(Study) theStudy,
 
   if (!CORBA::is_nil(theView3D)) {
     VISU::View3D_i* pView = dynamic_cast<VISU::View3D_i*>(GetServant(theView3D).in());
-    //QAD_StudyFrame* aStudyFrame = pView->GetStudyFrame();
-    //myView = VISU::GetViewFrame(aStudyFrame);
-    SUIT_ViewWindow* aVW = pView->myViewWindow;
-    myView = VISU::GetViewWindow(aVW);
+    SUIT_ViewWindow* aVW = pView->GetViewWindow();
+    myView = dynamic_cast<SVTK_ViewWindow*>(aVW);
+    connect( myView, SIGNAL( destroyed() ), this, SLOT( onViewDeleted() ) );
   }
 
-  myMaxVal = 0;
-  myMinVal = 0;
+  myTimeMinVal = 0;
+  myTimeMaxVal = 0;
   myTimeMin = 0;
   myTimeMax = 0;
   myLastError = "";
   myCycling = false;
 
   myAnimEntry = "";
+
+  myDumpPath = "";
+  myAVIMaker = "jpeg2yuv";
 }
 
 
-//************************************************************************
+//------------------------------------------------------------------------
 VISU_TimeAnimation::~VISU_TimeAnimation()
 {
+  if (!myView) {
+    MESSAGE("Viewer is not defined for animation");
+    return;
+  }
+
   for (int i = 0; i < getNbFields(); i++) {
     clearData(myFieldsLst[i]);
   }
-}
-
 
-//************************************************************************
+  /* Terminates the execution of the thread. 
+   * The thread may or may not be terminated immediately, 
+   * depending on the operating system's scheduling policies. 
+   *  
+   * Use QThread::wait() after terminate() for synchronous termination.
+   *
+   * When the thread is terminated, all threads waiting for the the thread to finish will be woken up.
+   * 
+   * Warning: This function is dangerous, and its use is discouraged. 
+   * The thread can be terminated at any point in its code path. 
+   * Threads can be terminated while modifying data. 
+   * There is no chance for the thread to cleanup after itself, 
+   * unlock any held mutexes, etc. In short, use this function only if absolutely necessary. 
+   */
+  myDumpPath = "";
+  QThread::wait(100);
+  QThread::terminate();
+  QThread::wait(400);
+}
+
+
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::addField (_PTR(SObject) theField)
 {
   FieldData aNewData;
@@ -107,39 +140,27 @@ void VISU_TimeAnimation::addField (_PTR(SObject) theField)
   }
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::addField (SALOMEDS::SObject_ptr theField)
 {
-  FieldData aNewData;
-  _PTR(SObject) aField = VISU::GetClientSObject(theField, myStudy);
-  aNewData.myField = aField;
-  aNewData.myNbFrames = 0;
-  aNewData.myPrsType = VISU::TSCALARMAP;
-  VISU::Storable::TRestoringMap aMap = getMapOfValue(aNewData.myField);
-  aNewData.myNbTimes = VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
-  myFieldsLst.append(aNewData);
-
-  //find Min/Max timestamps
-  if ((myTimeMin == 0) && (myTimeMax == 0)) {
-    _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aField);
-    anIter->Next(); // First is reference on support
-    myTimeMin = getTimeValue(anIter->Value());
-    for(; anIter->More(); anIter->Next()) {
-      myTimeMax = getTimeValue(anIter->Value());
-    }
-  }
+  SALOMEDS::SObject_var theFieldDup = SALOMEDS::SObject::_duplicate(theField);
+  _PTR(SObject) aField = VISU::GetClientSObject(theFieldDup, myStudy);
+  addField(aField);
 }
 
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::clearData(FieldData& theData) {
+  if (!myView) {
+    MESSAGE("Viewer is not defined for animation");
+    return;
+  }
   theData.myTiming.clear();
   vtkRenderer* aRen = myView->getRenderer();
   if (!theData.myActors.empty()) {
     for (int i = 0, iEnd = theData.myActors.size(); i < iEnd; i++) {
       if (theData.myActors[i] != 0) {
        theData.myActors[i]->RemoveFromRender(aRen);
-       theData.myActors[i]->Delete();
       }
     }
     theData.myActors.clear();
@@ -156,136 +177,196 @@ void VISU_TimeAnimation::clearData(FieldData& theData) {
 }
 
 
-//************************************************************************
+namespace
+{
+  //------------------------------------------------------------------------
+  template<class TPrs3d>
+  void
+  GeneratePresentations(_PTR(Study) theStudy,
+                       FieldData& theData,
+                       VISU::Result_i* theResult,
+                       bool theIsRangeDefined,
+                       CORBA::Double theTimeMin,
+                       CORBA::Double theTimeMax)
+  {
+    double aMin = VTK_LARGE_FLOAT, aMax = -VTK_LARGE_FLOAT;
+
+    _PTR(ChildIterator) anIter = theStudy->NewChildIterator(theData.myField);
+    anIter->Next(); // First is reference on support
+
+    long aFrameId = 0;
+    for(; anIter->More(); anIter->Next()){
+      if (aFrameId == theData.myNbTimes) {
+       MESSAGE("There are extra timestamps in field");
+       return;
+      }
+      _PTR(SObject) aTimeStamp = anIter->Value();
+      if(!aTimeStamp) 
+       continue;
+
+      theData.myTiming[aFrameId] = VISU_TimeAnimation::getTimeValue(aTimeStamp);
+      if (theIsRangeDefined) {
+       if (theData.myTiming[aFrameId] < theTimeMin) 
+         continue;
+       if (theData.myTiming[aFrameId] > theTimeMax) 
+         break;
+      }
+
+      VISU::Storable::TRestoringMap aTimeMap = VISU_TimeAnimation::getMapOfValue(aTimeStamp);
+      QString aMeshName = VISU::Storable::FindValue(aTimeMap,"myMeshName");
+      VISU::Entity anEntity = (VISU::Entity) VISU::Storable::FindValue(aTimeMap,"myEntityId").toInt();
+      QString aFieldName = VISU::Storable::FindValue(aTimeMap,"myFieldName");
+      int aTimeStampId = VISU::Storable::FindValue(aTimeMap,"myTimeStampId").toInt();
+      
+      bool anIsCreated = false;
+      TPrs3d* aPresent = new TPrs3d(theResult, false);
+      CASCatch_TRY{       
+       try{
+         if(aPresent->Create(aMeshName.latin1(),anEntity,aFieldName.latin1(),aTimeStampId)){
+           anIsCreated = true;
+           theData.myPrs[aFrameId++] = aPresent;
+           aMin = std::min(aPresent->GetMin(), aMin);
+           aMax = std::max(aPresent->GetMax(), aMax);
+         }
+       }catch(std::exception& exc){
+         INFOS("Follow exception was occured :\n"<<exc.what());
+       }catch(...){
+         INFOS("Unknown exception was occured!");
+       }
+      }CASCatch_CATCH(Standard_Failure) {
+       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+       INFOS("Follow signal was occured :\n"<<aFail->GetMessageString());
+      }
+      if(!anIsCreated)
+       aPresent->_remove_ref();
+    }
+    theData.myNbFrames = aFrameId;
+
+    if (theData.myPrsType != VISU::TGAUSSPOINTS){
+      int aRangeType = VISU::GetResourceMgr()->integerValue("VISU" , "scalar_range_type", 0);
+      if( aRangeType != 1 ){
+       for(long aFrameId = 0; aFrameId < theData.myNbFrames; aFrameId++) {
+         if (VISU::ScalarMap_i* aPrs = dynamic_cast<VISU::ScalarMap_i*>(theData.myPrs[aFrameId])){
+           aPrs->SetRange(aMin, aMax);
+           aPrs->SetOffset(theData.myOffset);
+         }
+       }
+      }
+      if (theData.myPrsType == VISU::TISOSURFACE)
+       for (long aFrameId = 0; aFrameId < theData.myNbFrames; aFrameId++)
+         if (VISU::IsoSurfaces_i* aPrs = dynamic_cast<VISU::IsoSurfaces_i*>(theData.myPrs[aFrameId]))
+           aPrs->SetSubRange(aMin, aMax);
+    }
+  }
+}
+  
 void VISU_TimeAnimation::generatePresentations(CORBA::Long theFieldNum) {
   FieldData& aData = myFieldsLst[theFieldNum];
 
   // Delete previous presentations
   clearData(aData);
 
-  VISU::Result_i* pResult = createPresent(aData.myField);
+  VISU::Result_i* aResult = createPresent(aData.myField);
   VISU::Storable::TRestoringMap aMap = getMapOfValue(aData.myField);
   aData.myNbFrames = aData.myNbTimes;
-    //VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
+  //VISU::Storable::FindValue(aMap,"myNbTimeStamps").toLong();
 
   aData.myPrs.resize(aData.myNbTimes,NULL);
   aData.myTiming.resize(aData.myNbTimes);
 
-  _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aData.myField);
-  _PTR(SObject) aTimeStamp;
-  anIter->Next(); // First is reference on support
-  long i = 0;
-  double aMin = VTK_LARGE_FLOAT, aMax = -VTK_LARGE_FLOAT;
-  for (; anIter->More(); anIter->Next()) {
-    if (i == aData.myNbTimes) {
-      MESSAGE("There are extra timestamps in field");
-      return;
-    }
-    aTimeStamp = anIter->Value();
-    if (!aTimeStamp) continue;
-
-    aData.myTiming[i] = getTimeValue(aTimeStamp);
-    if (isRangeDefined()) {
-      if (aData.myTiming[i] < myMinVal) continue;
-      if (aData.myTiming[i] > myMaxVal) break;
-    }
-
-    VISU::Storable::TRestoringMap aTimeMap = getMapOfValue(aTimeStamp);
-    QString aMeshName = VISU::Storable::FindValue(aTimeMap,"myMeshName");
-    VISU::Entity anEntity = (VISU::Entity) VISU::Storable::FindValue(aTimeMap,"myEntityId").toInt();
-    QString aFieldName = VISU::Storable::FindValue(aTimeMap,"myFieldName");
-    int aTimeStampId = VISU::Storable::FindValue(aTimeMap,"myTimeStampId").toInt();
-
-    switch (aData.myPrsType) {
-    case VISU::TSCALARMAP: // ScalarMap
-      {
-       VISU::ScalarMap_i* aPresent = new VISU::ScalarMap_i(pResult, false);
-       aPresent->Create(aMeshName.latin1(), anEntity,
-                        aFieldName.latin1(), aTimeStampId);
-       //VISU::ScalarMap_var aTmp = aPresent->_this();
-       //aPresent->_remove_ref();
-       aData.myPrs[i] = aPresent;
-      }
-      break;
-
-    case VISU::TISOSURFACE: // Iso Surfaces
-      {
-       VISU::IsoSurfaces_i* aPresent = new VISU::IsoSurfaces_i(pResult, false);
-       aPresent->Create(aMeshName.latin1(), anEntity,
-                        aFieldName.latin1(), aTimeStampId);
-       //VISU::IsoSurfaces_var aTmp = aPresent->_this();
-       //aPresent->_remove_ref();
-       aData.myPrs[i] = aPresent;
-      }
-      break;
-
-    case VISU::TCUTPLANES: // Cut Planes
-      {
-       VISU::CutPlanes_i* aPresent = new VISU::CutPlanes_i(pResult, false);
-       aPresent->Create(aMeshName.latin1(), anEntity,
-                        aFieldName.latin1(), aTimeStampId);
-       //VISU::CutPlanes_var aTmp = aPresent->_this();
-       //aPresent->_remove_ref();
-       aData.myPrs[i] = aPresent;
-      }
-      break;
-
-    case VISU::TDEFORMEDSHAPE: // Deformed Shape
-      {
-       VISU::DeformedShape_i* aPresent = new VISU::DeformedShape_i(pResult, false);
-       aPresent->Create(aMeshName.latin1(), anEntity,
-                        aFieldName.latin1(), aTimeStampId);
-       //VISU::DeformedShape_var aTmp = aPresent->_this();
-       //aPresent->_remove_ref();
-       aData.myPrs[i] = aPresent;
-      }
-      break;
-
-    case VISU::TVECTORS: // Vectors
-      {
-       VISU::Vectors_i* aPresent = new VISU::Vectors_i(pResult, false);
-       aPresent->Create(aMeshName.latin1(), anEntity,
-                        aFieldName.latin1(), aTimeStampId);
-       //VISU::Vectors_var aTmp = aPresent->_this();
-       //aPresent->_remove_ref();
-       aData.myPrs[i] = aPresent;
-      }
-      break;
-
-    case VISU::TSTREAMLINES: // Stream Lines
-      {
-       VISU::StreamLines_i* aPresent = new VISU::StreamLines_i(pResult, false);
-       aPresent->Create(aMeshName.latin1(), anEntity,
-                        aFieldName.latin1(), aTimeStampId);
-       //VISU::StreamLines_var aTmp = aPresent->_this();
-       //aPresent->_remove_ref();
-       aData.myPrs[i] = aPresent;
-      }
-      break;
-    }
-    if (aData.myPrs[i]->GetMin() < aMin) aMin = aData.myPrs[i]->GetMin();
-    if (aData.myPrs[i]->GetMax() > aMax) aMax = aData.myPrs[i]->GetMax();
-    i++;
-  }
-  aData.myNbFrames = i;
-  QString aFixRange = VISU::GetResourceMgr()->stringValue("Visu:SBImposeRange");
-  if (aFixRange.compare("true") != 0) {
-    for (i = 0; i < aData.myNbFrames; i++) {
-      aData.myPrs[i]->SetRange(aMin, aMax);
-      aData.myPrs[i]->SetOffset(aData.myOffset);
-    }
-    if (aData.myPrsType == VISU::TISOSURFACE)
-      for (i = 0; i < aData.myNbFrames; i++)
-       if (VISU::IsoSurfaces_i* aPrs = dynamic_cast<VISU::IsoSurfaces_i*>(aData.myPrs[i]))
-          aPrs->SetSubRange(aMin, aMax);
+  using namespace VISU;
+  switch (aData.myPrsType) {
+  case VISU::TSCALARMAP: // ScalarMap
+    GeneratePresentations<ScalarMap_i>(myStudy,
+                                      aData,
+                                      aResult,
+                                      isRangeDefined(),
+                                      myTimeMinVal,
+                                      myTimeMaxVal);
+    break;
+  case VISU::TISOSURFACE: // Iso Surfaces
+    GeneratePresentations<IsoSurfaces_i>(myStudy,
+                                        aData,
+                                        aResult,
+                                        isRangeDefined(),
+                                        myTimeMinVal,
+                                        myTimeMaxVal);
+    break;
+  case VISU::TCUTPLANES: // Cut Planes
+    GeneratePresentations<CutPlanes_i>(myStudy,
+                                      aData,
+                                      aResult,
+                                      isRangeDefined(),
+                                      myTimeMinVal,
+                                      myTimeMaxVal);
+    break;
+  case VISU::TCUTLINES: // Cut Lines
+    GeneratePresentations<CutLines_i>(myStudy,
+                                     aData,
+                                     aResult,
+                                     isRangeDefined(),
+                                     myTimeMinVal,
+                                     myTimeMaxVal);
+    break;
+  case VISU::TPLOT3D: // Plot3d
+    GeneratePresentations<Plot3D_i>(myStudy,
+                                   aData,
+                                   aResult,
+                                   isRangeDefined(),
+                                   myTimeMinVal,
+                                   myTimeMaxVal);
+    break;
+  case VISU::TDEFORMEDSHAPE: // Deformed Shape
+    GeneratePresentations<DeformedShape_i>(myStudy,
+                                          aData,
+                                          aResult,
+                                          isRangeDefined(),
+                                          myTimeMinVal,
+                                          myTimeMaxVal);
+    break;
+  case VISU::TVECTORS: // Vectors
+    GeneratePresentations<Vectors_i>(myStudy,
+                                    aData,
+                                    aResult,
+                                    isRangeDefined(),
+                                    myTimeMinVal,
+                                    myTimeMaxVal);
+    break;
+  case VISU::TSTREAMLINES: // Stream Lines
+    GeneratePresentations<StreamLines_i>(myStudy,
+                                        aData,
+                                        aResult,
+                                        isRangeDefined(),
+                                        myTimeMinVal,
+                                        myTimeMaxVal);
+    break;
+  case VISU::TGAUSSPOINTS: // Gauss Points
+    GeneratePresentations<GaussPoints_i>(myStudy,
+                                        aData,
+                                        aResult,
+                                        isRangeDefined(),
+                                        myTimeMinVal,
+                                        myTimeMaxVal);
+    break;
+  case VISU::TSCALARMAPONDEFORMEDSHAPE: // Scalar map on deformed shape
+    GeneratePresentations<ScalarMapOnDeformedShape_i>(myStudy,
+                                                     aData,
+                                                     aResult,
+                                                     isRangeDefined(),
+                                                     myTimeMinVal,
+                                                     myTimeMaxVal);
+    break;
+  default:
+    MESSAGE("Not implemented for this presentation type: " << aData.myPrsType);
+    return;
   }
 }
 
 
-//************************************************************************
+//------------------------------------------------------------------------
 CORBA::Boolean VISU_TimeAnimation::generateFrames() {
   if (!myView) {
-    MESSAGE("Viewer is nod defined for animation");
+    MESSAGE("Viewer is not defined for animation");
     return false;
   }
 
@@ -321,10 +402,10 @@ CORBA::Boolean VISU_TimeAnimation::generateFrames() {
   return aNoError;
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::clearView() {
   if (!myView) {
-    MESSAGE("Viewer is nod defined for animation");
+    MESSAGE("Viewer is not defined for animation");
     return;
   }
   vtkRenderer* aRen = myView->getRenderer();
@@ -334,7 +415,6 @@ void VISU_TimeAnimation::clearView() {
       for (int i = 0, iEnd = aData.myActors.size(); i < iEnd; i++) {
        if (aData.myActors[i] != 0) {
          aData.myActors[i]->RemoveFromRender(aRen);
-         aData.myActors[i]->Delete();
        }
       }
       aData.myActors.clear();
@@ -343,12 +423,12 @@ void VISU_TimeAnimation::clearView() {
   VISU::RepaintView(myView);
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::stopAnimation() {
   myIsActive = false;
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::startAnimation() {
   if (!myIsActive) {
     myIsActive = true;
@@ -356,8 +436,12 @@ void VISU_TimeAnimation::startAnimation() {
   }
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::nextFrame() {
+  if (!myView) {
+    MESSAGE("Viewer is not defined for animation");
+    return;
+  }
   stopAnimation();
   if (myFrame < (myFieldsLst[0].myNbFrames-1)) {
     int i;
@@ -375,8 +459,12 @@ void VISU_TimeAnimation::nextFrame() {
   }
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::prevFrame() {
+  if (!myView) {
+    MESSAGE("Viewer is not defined for animation");
+    return;
+  }
   stopAnimation();
   if (myFrame > 0) {
     int i;
@@ -394,8 +482,12 @@ void VISU_TimeAnimation::prevFrame() {
   }
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::firstFrame() {
+  if (!myView) {
+    MESSAGE("Viewer is not defined for animation");
+    return;
+  }
   stopAnimation();
   int i;
   for (i = 0; i < getNbFields(); i++)
@@ -413,8 +505,12 @@ void VISU_TimeAnimation::firstFrame() {
   }
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::lastFrame() {
+  if (!myView) {
+    MESSAGE("Viewer is not defined for animation");
+    return;
+  }
   stopAnimation();
   int i;
   for (i = 0; i < getNbFields(); i++)
@@ -431,9 +527,13 @@ void VISU_TimeAnimation::lastFrame() {
 }
 
 
-//************************************************************************
+//------------------------------------------------------------------------
 // For Batchmode using
 void VISU_TimeAnimation::gotoFrame(CORBA::Long theFrame) {
+  if (!myView) {
+    MESSAGE("Viewer is not defined for animation");
+    return;
+  }
   if ((theFrame < 0) || (theFrame > (getNbFrames()-1)))
     return;
   stopAnimation();
@@ -457,36 +557,43 @@ void VISU_TimeAnimation::gotoFrame(CORBA::Long theFrame) {
 }
 
 
-//************************************************************************
-VISU::ScalarMap_ptr VISU_TimeAnimation::getPresentation(CORBA::Long theField, CORBA::Long theFrame) {
+//------------------------------------------------------------------------
+VISU::ColoredPrs3d_ptr VISU_TimeAnimation::getPresentation(CORBA::Long theField, CORBA::Long theFrame) {
   if ((theField > getNbFields()) || (theField < 0))
-    return VISU::ScalarMap::_nil();
+    return VISU::ColoredPrs3d::_nil();
   if ((theFrame < 0) || (theFrame > (myFieldsLst[theField].myNbFrames - 1)))
-    return VISU::ScalarMap::_nil();
+    return VISU::ColoredPrs3d::_nil();
   return myFieldsLst[theField].myPrs[theFrame]->_this();
 }
 
 
-//************************************************************************
+//------------------------------------------------------------------------
 CORBA::Long VISU_TimeAnimation::getNbFrames() {
   return (getNbFields() > 0)? myFieldsLst[0].myNbFrames : 0;
 }
 
 
-//************************************************************************
-void VISU_TimeAnimation::run() {
+//------------------------------------------------------------------------
+void VISU_TimeAnimation::run()
+{
   if (!myView) {
-    MESSAGE("Viewer is nod defined for animation");
+    MESSAGE("Viewer is not defined for animation");
     return;
   }
-  double k=1;
-  double aOneVal;
-  bool isDumping = !myDumpPath.isEmpty();
-  aOneVal = (myFieldsLst[0].myNbFrames > 2) ?
-    myFieldsLst[0].myTiming[1] - myFieldsLst[0].myTiming[0] : 1;
+  double k = 1;
+  bool   isDumping = !myDumpPath.isEmpty();
+  double aOneVal = 1;
+  if (myFieldsLst[0].myNbFrames > 2)
+    aOneVal = myFieldsLst[0].myTiming[1] - myFieldsLst[0].myTiming[0];
+  myFileIndex = 0;
+  int aNbFiles = 0;
+  QValueList<int> anIndexList;
+
   qApp->lock();
   while (myIsActive) {
     emit frameChanged(myFrame, myFieldsLst[0].myTiming[myFrame]);
+    if(!(myFieldsLst[0].myField))
+      break;
     for (int i = 0; i < getNbFields(); i++) {
       FieldData& aData = myFieldsLst[i];
       if (myFrame > 0) {
@@ -501,43 +608,93 @@ void VISU_TimeAnimation::run() {
       }
     }
     myView->Repaint(false);
-    if (isDumping) {
-      QPixmap px = QPixmap::grabWindow(myView->winId());
-      QString aFile(myDumpPath);
-      QString aName = QString("%1").arg(myFieldsLst[0].myTiming[myFrame]);
-      int aPos = -1;
-      while ((aPos = aName.find(".")) > -1 )
-       aName.replace(aPos, 1, "_");
-      aFile += aName;
-      aFile += ".jpeg";
-      px.save(aFile, "JPEG");
+
+    k = 1;
+    if (myProportional) {
+      switch (myFrame) {
+      case 0:
+        break;
+      case 1:
+        if (myFieldsLst[0].myNbFrames > 2)
+          k = (myFieldsLst[0].myTiming[myFrame+1] -
+               myFieldsLst[0].myTiming[myFrame]) / aOneVal;
+        break;
+      default:
+        if (myFrame < (myFieldsLst[0].myNbFrames - 1))
+          k = (myFieldsLst[0].myTiming[myFrame+1] -
+               myFieldsLst[0].myTiming[myFrame]) / aOneVal;
+      }
     }
-    if (!isDumping) {
-      if (myProportional) {
+    int delay = (int)(1000. * k / mySpeed);
+    isDumping = !myDumpPath.isEmpty();
+    if (delay < 1 && isDumping) {
+      // We must unlock mutex for some time before grabbing to allow view updating
+      delay = 1;
+    }
+    qApp->unlock();
+    msleep(delay);
+    qApp->lock();
+
+    if (isDumping) {
+      // We must unlock mutex for some time before grabbing to allow view updating
+      qApp->unlock();
+      msleep(delay);
+      qApp->lock();
+      if(!(myFieldsLst[0].myField)) // break, if field was deleted.
+       break;
+      if (myDumpFormat.compare("AVI") != 0) {
+       QString aFile(myDumpPath);
+       QString aName = QString("%1").arg(myFieldsLst[0].myTiming[myFrame]);
+       int aPos = -1;
+       while ((aPos = aName.find(".")) > -1 )
+         aName.replace(aPos, 1, "_");
+       aFile += aName;
+       aFile += ".";
+       aFile += myDumpFormat.lower();
+       myView->dumpViewToFormat(aFile,myDumpFormat);
+      } else {
+       QFileInfo aFileInfo(myDumpPath);
+       QString aDirPath = aFileInfo.dirPath(true);
+       QString aBaseName = aFileInfo.fileName();
+
        switch (myFrame) {
-       case 0:
-         k=1;
+       case 0: 
          break;
        case 1:
-         if (myFieldsLst[0].myNbFrames > 2)
-           k = (myFieldsLst[0].myTiming[myFrame+1] - myFieldsLst[0].myTiming[myFrame])/aOneVal;
-         else
-           k = 1;
+         myFileIndex += 5;
          break;
        default:
-         k = (myFrame < (myFieldsLst[0].myNbFrames-1))?
-           (myFieldsLst[0].myTiming[myFrame+1] - myFieldsLst[0].myTiming[myFrame])/aOneVal : 1;
+         if (myProportional) {
+           double p = (myFieldsLst[0].myTiming[myFrame] -
+                       myFieldsLst[0].myTiming[myFrame-1]) / aOneVal;
+           myFileIndex += (long) (5*p);
+         } else {
+           myFileIndex += 5;
+         }
        }
-      } else
-       k = 1;
-      qApp->unlock();
-      msleep((int)(1000.*k/mySpeed));
-      qApp->lock();
-    } else {
-      qApp->unlock();
-      msleep(100);
-      qApp->lock();
+
+       QString aFile = aDirPath + QDir::separator() + aBaseName;
+       aFile += "_";
+       aFile += QString("%1").arg(myFileIndex).rightJustify(8, '0');
+       aFile += ".jpeg";
+
+       /* check image size is divisable 16
+       myView->dumpViewToFormat(aFile,"JPEG");
+       */
+       SUIT_ViewWindow* aView = myView;
+       QImage img = aView->dumpView();
+       if (!img.isNull()) {
+         int width = img.width(); width = (width/16)*16;
+         int height = img.height(); height = (height/16)*16;
+         QImage copy = img.copy(0, 0, width, height);
+         if (copy.save(aFile, "JPEG")) {
+           anIndexList.append(myFileIndex);
+           aNbFiles++;
+         }
+       }
+      }
     }
+
     if (!myIsActive) break;
 
     myFrame++;
@@ -550,12 +707,64 @@ void VISU_TimeAnimation::run() {
        myFrame = 0;
     }
   }
+
+  // make AVI file if need
+  if (isDumping && myDumpFormat.compare("AVI") == 0) {
+    double aFPS = 17.3 * mySpeed;
+
+    QFileInfo aFileInfo(myDumpPath);
+    QString aDirPath = aFileInfo.dirPath(true);
+    QString aBaseName = aFileInfo.fileName();
+
+    // add missing files
+    if (anIndexList.count() > 1) {
+      QString aFFile = aDirPath + QDir::separator() + aBaseName;
+      aFFile += QString("_%1.jpeg");
+      int aStartIndex = anIndexList[0], anEndIndex;
+      for (int i = 1; i < anIndexList.count(); i++) {
+       anEndIndex = anIndexList[i];
+       QString aCurFile = aFFile.arg(QString::number(aStartIndex).rightJustify(8, '0'));
+       QStringList aCommands;
+       for (int j = aStartIndex+1; j < anEndIndex; j++) {
+         QString aFile = aFFile.arg(QString::number(j).rightJustify(8, '0'));
+         aCommands.append(QString("ln -s %1 %2").arg(aCurFile).arg(aFile));
+       }
+       system(aCommands.join(" ; \\\n").latin1());
+       aStartIndex = anEndIndex;
+      }
+    }
+
+    // make AVI file
+    QString aPattern = aDirPath + QDir::separator() + aBaseName;
+    aPattern += "_\%08d.jpeg";
+
+    QString aCmd = myAVIMaker;
+    aCmd += " -I p";
+    aCmd += " -v 0";
+    aCmd += QString(" -f %1").arg(aFPS);
+    // aCmd += QString(" -n %1").arg(aNbFiles);
+    aCmd += QString(" -n %1").arg(myFileIndex+1);
+    aCmd += QString(" -j %1").arg(aPattern);
+    aCmd += " | yuv2lav";
+    aCmd += QString(" -o %1").arg(myDumpPath);
+    system(aCmd.latin1());
+
+    // remove temporary jpeg files
+    aCmd = "( ";
+    aCmd += QString("cd %1").arg(aDirPath);
+    aCmd += "; ls";
+    aCmd += QString(" | egrep '%1_[0-9]*.jpeg'").arg(aBaseName);
+    aCmd += " | xargs rm";
+    aCmd += " )";
+    system(aCmd.latin1());
+  }
+
   emit stopped();
   qApp->unlock();
   QThread::exit();
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 VISU::Result_i* VISU_TimeAnimation::createPresent (_PTR(SObject) theField)
 {
   _PTR(SObject) aSObj = theField->GetFather();
@@ -566,7 +775,7 @@ VISU::Result_i* VISU_TimeAnimation::createPresent (_PTR(SObject) theField)
   return dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 VISU::Storable::TRestoringMap VISU_TimeAnimation::getMapOfValue (_PTR(SObject) theSObject)
 {
   VISU::Storable::TRestoringMap aMap;
@@ -582,24 +791,55 @@ VISU::Storable::TRestoringMap VISU_TimeAnimation::getMapOfValue (_PTR(SObject) t
   return aMap;
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 double VISU_TimeAnimation::getTimeValue (_PTR(SObject) theTimeStamp)
 {
   _PTR(GenericAttribute) anAttr;
   if (theTimeStamp->FindAttribute(anAttr, "AttributeName")) {
     _PTR(AttributeName) aName (anAttr);
     QString aNameString (aName->Value().c_str());
-    return aNameString.toDouble();
+    int time_len = aNameString.find(',');
+    if (time_len > -1)
+      return aNameString.left(time_len).toDouble();
+    else
+      return aNameString.toDouble();
   }
   return -1.0;
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::setSpeed(CORBA::Long theSpeed)
 {
   mySpeed = (theSpeed<1)? 1 : theSpeed;
 }
 
+std::string VISU_TimeAnimation::setDumpFormat(const char* theFormat)
+{
+  myDumpFormat = theFormat;
+  QStrList aDumpFormats = QImageIO::outputFormats();
+  if (myDumpFormat.isEmpty() || 
+      (aDumpFormats.find(theFormat) < 0 && myDumpFormat.compare("AVI") != 0)) {
+    if (aDumpFormats.find("JPEG"))
+      myDumpFormat = "JPEG";
+    else
+      myDumpFormat = aDumpFormats.at(0);
+  }
+  return myDumpFormat.latin1();
+}
+
+//------------------------------------------------------------------------
+bool VISU_TimeAnimation::checkAVIMaker() const
+{
+  QStrList aDumpFormats = QImageIO::outputFormats();
+  if (aDumpFormats.find("JPEG") < 0) return false;
+
+  QString aCmd("which ");
+  aCmd += myAVIMaker;
+  aCmd += " >& /dev/null";
+  int iErr = system(aCmd.latin1());
+  return (iErr == 0);
+}
+
 //************************************************************************
 int VISU_TimeAnimation::myNBAnimations = 0;
 QString VISU_TimeAnimation::GenerateName()
@@ -607,7 +847,7 @@ QString VISU_TimeAnimation::GenerateName()
   return VISU::GenerateName("Animation", myNBAnimations++);
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 std::string GetPresentationComment (VISU::VISUType thePrsType)
 {
   std::string aPrsCmt;
@@ -621,6 +861,12 @@ std::string GetPresentationComment (VISU::VISUType thePrsType)
   case VISU::TCUTPLANES:
     aPrsCmt = VISU::CutPlanes_i::myComment;
     break;
+  case VISU::TCUTLINES:
+    aPrsCmt = VISU::CutLines_i::myComment;
+    break;
+  case VISU::TPLOT3D:
+    aPrsCmt = VISU::Plot3D_i::myComment;
+    break;
   case VISU::TDEFORMEDSHAPE:
     aPrsCmt = VISU::DeformedShape_i::myComment;
     break;
@@ -630,6 +876,12 @@ std::string GetPresentationComment (VISU::VISUType thePrsType)
   case VISU::TSTREAMLINES:
     aPrsCmt = VISU::StreamLines_i::myComment;
     break;
+  case VISU::TGAUSSPOINTS:
+    aPrsCmt = VISU::GaussPoints_i::myComment;
+    break;
+  case VISU::TSCALARMAPONDEFORMEDSHAPE:
+    aPrsCmt = VISU::ScalarMapOnDeformedShape_i::myComment;
+    break;
   default:
     aPrsCmt = "Unknown presentation";
     break;
@@ -637,7 +889,7 @@ std::string GetPresentationComment (VISU::VISUType thePrsType)
   return aPrsCmt;
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 SALOMEDS::SObject_ptr VISU_TimeAnimation::publishInStudy()
 {
   if (myStudy->GetProperties()->IsLocked())
@@ -649,8 +901,8 @@ SALOMEDS::SObject_ptr VISU_TimeAnimation::publishInStudy()
   std::string aSComponentEntry = aSComponent->GetID();
 
   QString aComment;
-  aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
-                   VISU::TANIMATION,myMinVal,myMaxVal);
+  aComment.sprintf("myComment=ANIMATION;myType=%d;myTimeMinVal=%g;myTimeMaxVal=%g",
+                   VISU::TANIMATION,myTimeMinVal,myTimeMaxVal);
 
   string anEntry = VISU::CreateAttributes(myStudy,aSComponentEntry.c_str(),"","",
                                           GenerateName(),"",aComment,true);
@@ -659,31 +911,31 @@ SALOMEDS::SObject_ptr VISU_TimeAnimation::publishInStudy()
 
   for (int i = 0; i < getNbFields(); i++) {
     FieldData& aData = myFieldsLst[i];
+
     _PTR(SObject) newObj = aStudyBuilder->NewObject(aAnimSObject);
     aStudyBuilder->Addreference(newObj, aData.myField);
+
     if (aData.myPrs.empty()) {
-      VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
-                             GetPresentationComment(aData.myPrsType).c_str(),"","",true);
-    } else {
-      ostringstream strOut;
-      aData.myPrs[0]->ToStream(strOut);
-      string aPrsComment = strOut.str();
-      VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
-                             aData.myPrs[0]->GetComment(),"",aPrsComment.c_str(),true);
+      generatePresentations(i);
     }
+    ostringstream strOut;
+    aData.myPrs[0]->ToStream(strOut);
+    string aPrsComment = strOut.str();
+    VISU::CreateAttributes(myStudy, newObj->GetID().c_str(),"","",
+                           aData.myPrs[0]->GetComment(),"",aPrsComment.c_str(),true);
   }
   aStudyBuilder->CommitCommand();
 
   return VISU::GetSObject(aAnimSObject);
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::saveAnimation()
 {
-  if (myStudy->GetProperties()->IsLocked())  return;
+  if (myStudy->GetProperties()->IsLocked()) return;
   if (myAnimEntry.isEmpty()) return;
 
-  _PTR(SObject) aAnimSObject = myStudy->FindObjectID(myAnimEntry);
+  _PTR(SObject) aAnimSObject = myStudy->FindObjectID(myAnimEntry.latin1());
   if (!aAnimSObject) return;
 
   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
@@ -692,35 +944,34 @@ void VISU_TimeAnimation::saveAnimation()
   std::string aSComponentEntry = aSComponent->GetID();
 
   QString aComment;
-  aComment.sprintf("myComment=ANIMATION;myType=%d;myMinVal=%g;myMaxVal=%g",
-                   VISU::TANIMATION,myMinVal,myMaxVal);
+  aComment.sprintf("myComment=ANIMATION;myType=%d;myTimeMinVal=%g;myTimeMaxVal=%g",
+                   VISU::TANIMATION,myTimeMinVal,myTimeMaxVal);
 
   _PTR(GenericAttribute) anAttr;
   anAttr = aStudyBuilder->FindOrCreateAttribute(aAnimSObject, "AttributeComment");
   _PTR(AttributeComment) aCmnt (anAttr);
-  aCmnt->SetValue(aComment);
+  aCmnt->SetValue(aComment.latin1());
 
   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
-  int i;
-  for (i = 0, anIter->Init(); anIter->More(); anIter->Next(), i++) {
+  int i = 0, nbf = getNbFields();
+  for (anIter->Init(); anIter->More(); anIter->Next(), i++) {
+    if (i >= nbf) break; // it must not be
     FieldData& aData = myFieldsLst[i];
 
+    // Get presentation name and comment
+    if (aData.myPrs.empty()) {
+      generatePresentations(i);
+    }
+    ostringstream strOut;
+    aData.myPrs[0]->ToStream(strOut);
+    string aPrsComment = strOut.str();
+    string aPrsNameTxt = aData.myPrs[0]->GetComment();
+
+    // Save in study
     _PTR(SObject) aRefObj = anIter->Value();
     _PTR(ChildIterator) anPrsIter = myStudy->NewChildIterator(aRefObj);
     anPrsIter->Init();
 
-    string aPrsComment, aPrsNameTxt;
-    if (aData.myPrs.empty()) {
-      aPrsComment = "";
-      aPrsNameTxt = GetPresentationComment(aData.myPrsType);
-    } else {
-      ostringstream strOut;
-      aData.myPrs[0]->ToStream(strOut);
-      aPrsComment = strOut.str();
-
-      aPrsNameTxt = aData.myPrs[0]->GetComment();
-    }
-
     if (anPrsIter->More()) {
       _PTR(SObject) aPrsObj = anPrsIter->Value();
       anAttr = aStudyBuilder->FindOrCreateAttribute(aPrsObj, "AttributeComment");
@@ -739,7 +990,7 @@ void VISU_TimeAnimation::saveAnimation()
   aStudyBuilder->CommitCommand();
 }
 
-//************************************************************************
+//------------------------------------------------------------------------
 void VISU_TimeAnimation::restoreFromStudy(SALOMEDS::SObject_ptr theField)
 {
   _PTR(SObject) aAnimSObject = VISU::GetClientSObject(theField, myStudy);
@@ -760,8 +1011,8 @@ void VISU_TimeAnimation::restoreFromStudy(_PTR(SObject) theField)
   VISU::Storable::StrToMap(strIn,aMap);
   bool isExist;
 
-  myMinVal = VISU::Storable::FindValue(aMap,"myMinVal",&isExist).toDouble();
-  myMaxVal = VISU::Storable::FindValue(aMap,"myMaxVal",&isExist).toDouble();
+  myTimeMinVal = VISU::Storable::FindValue(aMap,"myTimeMinVal",&isExist).toDouble();
+  myTimeMaxVal = VISU::Storable::FindValue(aMap,"myTimeMaxVal",&isExist).toDouble();
 
   _PTR(ChildIterator) anIter = myStudy->NewChildIterator(aAnimSObject);
   for (anIter->Init(); anIter->More(); anIter->Next()) {
@@ -787,12 +1038,20 @@ void VISU_TimeAnimation::restoreFromStudy(_PTR(SObject) theField)
       aData.myPrsType = VISU::TISOSURFACE;
     else if (strName == VISU::CutPlanes_i::myComment.c_str())
       aData.myPrsType = VISU::TCUTPLANES;
+    else if (strName == VISU::CutLines_i::myComment.c_str())
+      aData.myPrsType = VISU::TCUTLINES;
+    else if (strName == VISU::Plot3D_i::myComment.c_str())
+      aData.myPrsType = VISU::TPLOT3D;
     else if (strName == VISU::DeformedShape_i::myComment.c_str())
       aData.myPrsType = VISU::TDEFORMEDSHAPE;
     else if (strName == VISU::Vectors_i::myComment.c_str())
       aData.myPrsType = VISU::TVECTORS;
     else if (strName == VISU::StreamLines_i::myComment.c_str())
       aData.myPrsType = VISU::TSTREAMLINES;
+    else if (strName == VISU::GaussPoints_i::myComment.c_str())
+      aData.myPrsType = VISU::TGAUSSPOINTS;
+    else if (strName == VISU::ScalarMapOnDeformedShape_i::myComment.c_str())
+      aData.myPrsType = VISU::TSCALARMAPONDEFORMEDSHAPE;
     else
       continue;
     generatePresentations(getNbFields()-1);
@@ -809,13 +1068,19 @@ void VISU_TimeAnimation::restoreFromStudy(_PTR(SObject) theField)
     }
     aData.myPrs[0]->GetOffset(aData.myOffset);
     for (int i = 1; i < aData.myNbFrames; i++) {
-      aData.myPrs[i]->SameAs(aData.myPrs[0]);
+      //jfa 03.08.2005:aData.myPrs[i]->SameAs(aData.myPrs[0]);
+      aData.myPrs[i]->SameAsParams(aData.myPrs[0]);//jfa 03.08.2005
     }
   }
   string aStr = aAnimSObject->GetID();
   myAnimEntry = aStr.c_str();
 }
 
+void VISU_TimeAnimation::onViewDeleted()
+{
+  myView = 0;
+}
+
 
 //========================================================================
 //========================================================================
@@ -953,7 +1218,7 @@ CORBA::Long VISU_TimeAnimation_i::getCurrentFrame()
   return myAnim->getCurrentFrame();
 }
 
-VISU::ScalarMap_ptr VISU_TimeAnimation_i::getPresentation
+VISU::ColoredPrs3d_ptr VISU_TimeAnimation_i::getPresentation
                     (CORBA::Long theField, CORBA::Long theFrame)
 {
   return myAnim->getPresentation(theField,theFrame);
@@ -1011,6 +1276,12 @@ void VISU_TimeAnimation_i::dumpTo (const char* thePath)
   myAnim->dumpTo(thePath);
 }
 
+char* VISU_TimeAnimation_i::setDumpFormat (const char* theFormat)
+{
+  string aDumpFormat = myAnim->setDumpFormat(theFormat);
+  return CORBA::string_dup(aDumpFormat.c_str());
+}
+
 CORBA::Boolean VISU_TimeAnimation_i::isCycling()
 {
   return myAnim->isCycling();