Salome HOME
Minor changes with classes renaming and virtual destructors declarations.
[modules/shaper.git] / src / Model / Model_Document.cxx
index 8a3f0bf45c3419d5cab8f61f03194d6f50dacb87..0e976420c45652ec8dd63448b16428dce0790e84 100644 (file)
@@ -4,18 +4,21 @@
 
 #include <Model_Document.h>
 #include <ModelAPI_Feature.h>
-#include <Model_Object.h>
+#include <Model_Data.h>
 #include <Model_Application.h>
 #include <Model_PluginManager.h>
 #include <Model_Iterator.h>
+#include <Model_Events.h>
+#include <Event_Loop.h>
 
 #include <TDataStd_Integer.hxx>
 #include <TDataStd_Comment.hxx>
+#include <TDF_ChildIDIterator.hxx>
 
 static const int UNDO_LIMIT = 10; // number of possible undo operations
 
 static const int TAG_GENERAL = 1; // general properties tag
-static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (Root for Model_ObjectsMgr)
+static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (Root for Model_DatasMgr)
 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for Model_History)
 
 using namespace std;
@@ -101,117 +104,227 @@ bool Model_Document::save(const char* theFileName)
 
 void Model_Document::close()
 {
+  // close all subs
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    subDocument(*aSubIter)->close();
+  mySubs.clear();
+  // close this
   myDoc->Close();
+  Model_Application::getApplication()->deleteDocument(myID);
 }
 
 void Model_Document::startOperation()
 {
+  // new command for this
   myDoc->NewCommand();
+  // new command for all subs
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    subDocument(*aSubIter)->startOperation();
 }
 
 void Model_Document::finishOperation()
 {
-  myDoc->CommitCommand();
+  // returns false if delta is empty and no transaction was made
+  myIsEmptyTr[myTransactionsAfterSave] = !myDoc->CommitCommand();
   myTransactionsAfterSave++;
+  // finish for all subs
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    subDocument(*aSubIter)->finishOperation();
 }
 
 void Model_Document::abortOperation()
 {
   myDoc->AbortCommand();
+  // abort for all subs
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    subDocument(*aSubIter)->abortOperation();
 }
 
 bool Model_Document::isOperation()
 {
+  // operation is opened for all documents: no need to check subs
   return myDoc->HasOpenCommand() == Standard_True ;
 }
 
 bool Model_Document::isModified()
 {
-  return myTransactionsAfterSave != 0;
+  // is modified if at least one operation was commited and not undoed
+  return myTransactionsAfterSave > 0;
 }
 
 bool Model_Document::canUndo()
 {
-  return myDoc->GetAvailableUndos() > 0;
+  if (myDoc->GetAvailableUndos() > 0)
+    return true;
+  // check other subs contains operation that can be undoed
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    if (subDocument(*aSubIter)->canUndo())
+      return true;
+  return false;
 }
 
 void Model_Document::undo()
 {
-  myDoc->Undo();
   myTransactionsAfterSave--;
+  if (!myIsEmptyTr[myTransactionsAfterSave])
+    myDoc->Undo();
+  synchronizeFeatures();
+  // undo for all subs
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    subDocument(*aSubIter)->undo();
 }
 
 bool Model_Document::canRedo()
 {
-  return myDoc->GetAvailableRedos() > 0;
+  if (myDoc->GetAvailableRedos() > 0)
+    return true;
+  // check other subs contains operation that can be redoed
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    if (subDocument(*aSubIter)->canRedo())
+      return true;
+  return false;
 }
 
 void Model_Document::redo()
 {
-  myDoc->Redo();
+  if (!myIsEmptyTr[myTransactionsAfterSave])
+    myDoc->Redo();
   myTransactionsAfterSave++;
+  synchronizeFeatures();
+  // redo for all subs
+  set<string>::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++)
+    subDocument(*aSubIter)->redo();
 }
 
-void Model_Document::addFeature(
-  std::shared_ptr<ModelAPI_Feature> theFeature, const std::string theGroupID)
+shared_ptr<ModelAPI_Feature> Model_Document::addFeature(string theID)
 {
-  TDF_Label aGroupLab = groupLabel(theGroupID);
+  shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(theID);
+  if (aFeature) {
+    dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd())->addFeature(aFeature);
+  } else {
+    // TODO: generate error that feature is not created
+  }
+  return aFeature;
+}
+
+void Model_Document::addFeature(const std::shared_ptr<ModelAPI_Feature> theFeature)
+{
+  const std::string& aGroup = theFeature->getGroup();
+  TDF_Label aGroupLab = groupLabel(aGroup);
   TDF_Label anObjLab = aGroupLab.NewChild();
-  std::shared_ptr<Model_Object> aData(new Model_Object);
+  std::shared_ptr<Model_Data> aData(new Model_Data);
   aData->setLabel(anObjLab);
+  aData->setDocument(Model_Application::getApplication()->getDocument(myID));
   theFeature->setData(aData);
-  setUniqueName(theFeature, theGroupID);
+  setUniqueName(theFeature);
   theFeature->initAttributes();
+  // keep the feature ID to restore document later correctly
   TDataStd_Comment::Set(anObjLab, theFeature->getKind().c_str());
+  // put index of the feature in the group in the tree
+  TDataStd_Integer::Set(anObjLab, myFeatures[aGroup].size());
+  myFeatures[aGroup].push_back(theFeature);
+
+  // event: feature is added
+  static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_CREATED);
+  ModelAPI_FeatureUpdatedMessage aMsg(theFeature, anEvent);
+  Event_Loop::loop()->send(aMsg);
 }
 
-std::shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
+shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
 {
-  Handle(TDataStd_Comment) aFeatureID;
-  if (theLabel.FindAttribute(TDataStd_Comment::GetID(), aFeatureID)) {
-    string anID(TCollection_AsciiString(aFeatureID->Get()).ToCString());
-    std::shared_ptr<ModelAPI_Feature> aResult = Model_PluginManager::get()->createFeature(anID);
-    std::shared_ptr<Model_Object> aData(new Model_Object);
-    aData->setLabel(theLabel);
-    aResult->setData(aData);
-    aResult->initAttributes();
-    return aResult;
+  Handle(TDataStd_Integer) aFeatureIndex;
+  if (theLabel.FindAttribute(TDataStd_Integer::GetID(), aFeatureIndex)) {
+    Handle(TDataStd_Comment) aGroupID;
+    if (theLabel.Father().FindAttribute(TDataStd_Comment::GetID(), aGroupID)) {
+      string aGroup = TCollection_AsciiString(aGroupID->Get()).ToCString();
+      return myFeatures[aGroup][aFeatureIndex->Get()];
+    }
   }
   return std::shared_ptr<ModelAPI_Feature>(); // not found
 }
 
+int Model_Document::featureIndex(shared_ptr<ModelAPI_Feature> theFeature)
+{
+  if (theFeature->data()->document().get() != this) {
+    return theFeature->data()->document()->featureIndex(theFeature);
+  }
+  shared_ptr<Model_Data> aData = dynamic_pointer_cast<Model_Data>(theFeature->data());
+  Handle(TDataStd_Integer) aFeatureIndex;
+  if (aData->label().FindAttribute(TDataStd_Integer::GetID(), aFeatureIndex)) {
+    return aFeatureIndex->Get();
+  }
+  return -1; // not found
+}
+
 shared_ptr<ModelAPI_Document> Model_Document::subDocument(string theDocID)
 {
+  // just store sub-document identifier here to manage it later
+  if (mySubs.find(theDocID) == mySubs.end())
+    mySubs.insert(theDocID);
   return Model_Application::getApplication()->getDocument(theDocID);
 }
 
 shared_ptr<ModelAPI_Iterator> Model_Document::featuresIterator(const string theGroup)
 {
   shared_ptr<Model_Document> aThis(Model_Application::getApplication()->getDocument(myID));
+  // create an empty iterator for not existing group 
+  // (to avoidance of attributes management outside the transaction)
+  if (myGroups.find(theGroup) == myGroups.end())
+    return shared_ptr<ModelAPI_Iterator>(new Model_Iterator());
   return shared_ptr<ModelAPI_Iterator>(new Model_Iterator(aThis, groupLabel(theGroup)));
 }
 
+shared_ptr<ModelAPI_Feature> Model_Document::feature(const string& theGroupID, const int theIndex)
+{
+  // TODO: optimize this method
+  shared_ptr<ModelAPI_Iterator>  anIter = featuresIterator(theGroupID);
+  for(int a = 0; a != theIndex && anIter->more(); anIter->next()) a++;
+  return anIter->more() ? anIter->current() : shared_ptr<ModelAPI_Feature>();
+}
+
+const vector<string>& Model_Document::getGroups() const
+{
+  return myGroupsNames;
+}
+
 Model_Document::Model_Document(const std::string theID)
     : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
 {
   myDoc->SetUndoLimit(UNDO_LIMIT);
   myTransactionsAfterSave = 0;
+  // to avoid creation of tag outside of the transaction (by iterator, for example)
+  /*
+  if (!myDoc->Main().FindChild(TAG_OBJECTS).IsAttribute(TDF_TagSource::GetID()))
+    TDataStd_Comment::Set(myDoc->Main().FindChild(TAG_OBJECTS).NewChild(), "");
+    */
 }
 
 TDF_Label Model_Document::groupLabel(const string theGroup)
 {
   if (myGroups.find(theGroup) == myGroups.end()) {
     myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
+    myGroupsNames.push_back(theGroup);
+    // set to the group label the group idntifier to restore on "open"
+    TDataStd_Comment::Set(myGroups[theGroup], theGroup.c_str());
+    myFeatures[theGroup] = vector<shared_ptr<ModelAPI_Feature> >();
   }
   return myGroups[theGroup];
 }
 
 void Model_Document::setUniqueName(
-  shared_ptr<ModelAPI_Feature> theFeature, const string theGroupID)
+  shared_ptr<ModelAPI_Feature> theFeature)
 {
   // first count all objects of such kind to start with index = count + 1
   int aNumObjects = 0;
-  shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theGroupID);
+  shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theFeature->getGroup());
   for(; anIter->more(); anIter->next()) {
     if (anIter->currentKind() == theFeature->getKind())
       aNumObjects++;
@@ -221,15 +334,105 @@ void Model_Document::setUniqueName(
   aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
   string aName = aNameStream.str();
   // check this is unique, if not, increase index by 1
-  for(anIter = featuresIterator(theGroupID); anIter->more();) {
+  for(anIter = featuresIterator(theFeature->getGroup()); anIter->more();) {
     if (anIter->currentName() == aName) {
       aNumObjects++;
       stringstream aNameStream;
       aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
       // reinitialize iterator to make sure a new name is unique
-      anIter = featuresIterator(theGroupID);
+      anIter = featuresIterator(theFeature->getGroup());
     } else anIter->next();
   }
 
   theFeature->data()->setName(aName);
 }
+
+void Model_Document::synchronizeFeatures()
+{
+  // iterate groups labels
+  TDF_ChildIDIterator aGroupsIter(myDoc->Main().FindChild(TAG_OBJECTS),
+    TDataStd_Comment::GetID(), Standard_False);
+  vector<string>::iterator aGroupNamesIter = myGroupsNames.begin();
+  for(; aGroupsIter.More() && aGroupNamesIter != myGroupsNames.end();
+        aGroupsIter.Next(), aGroupNamesIter++) {
+    string aGroupName = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
+      aGroupsIter.Value())->Get()).ToCString();
+    if (*aGroupNamesIter != aGroupName) 
+      break; // all since there is a change this must be recreated from scratch
+  }
+  // delete all groups left after the data model groups iteration
+  while(aGroupNamesIter != myGroupsNames.end()) {
+    myFeatures.erase(*aGroupNamesIter);
+    myGroups.erase(*aGroupNamesIter);
+    aGroupNamesIter = myGroupsNames.erase(aGroupNamesIter);
+  }
+  // create new groups basing on the following data model update
+  for(; aGroupsIter.More(); aGroupsIter.Next()) {
+    string aGroupName = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
+      aGroupsIter.Value())->Get()).ToCString();
+    myGroupsNames.push_back(aGroupName);
+    myGroups[aGroupName] = aGroupsIter.Value()->Label();
+    myFeatures[aGroupName] = vector<shared_ptr<ModelAPI_Feature> >();
+  }
+  // update features group by group
+  aGroupsIter.Initialize(myDoc->Main().FindChild(TAG_OBJECTS),
+    TDataStd_Comment::GetID(), Standard_False);
+  for(; aGroupsIter.More(); aGroupsIter.Next()) {
+    string aGroupName = TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
+      aGroupsIter.Value())->Get()).ToCString();
+    // iterate features in internal container
+    vector<shared_ptr<ModelAPI_Feature> >& aFeatures = myFeatures[aGroupName];
+    vector<shared_ptr<ModelAPI_Feature> >::iterator aFIter = aFeatures.begin();
+    // and in parallel iterate labels of features
+    TDF_ChildIDIterator aFLabIter(
+      aGroupsIter.Value()->Label(), TDataStd_Comment::GetID(), Standard_False);
+    while(aFIter != aFeatures.end() || aFLabIter.More()) {
+      static const int INFINITE_TAG = MAXINT; // no label means that it exists somwhere in infinite
+      int aFeatureTag = INFINITE_TAG; 
+      if (aFIter != aFeatures.end()) { // existing tag for feature
+        shared_ptr<Model_Data> aData = dynamic_pointer_cast<Model_Data>((*aFIter)->data());
+        aFeatureTag = aData->label().Tag();
+      }
+      int aDSTag = INFINITE_TAG; 
+      if (aFLabIter.More()) { // next label in DS is existing
+        aDSTag = aFLabIter.Value()->Label().Tag();
+      }
+      if (aDSTag > aFeatureTag) { // feature is removed
+        aFIter = aFeatures.erase(aFIter);
+        // event: model is updated
+        ModelAPI_FeatureDeletedMessage aMsg(
+          Model_Application::getApplication()->getDocument(myID), aGroupName);
+        Event_Loop::loop()->send(aMsg);
+      } else if (aDSTag < aFeatureTag) { // a new feature is inserted
+        // create a feature
+        shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(
+          TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(
+          aFLabIter.Value())->Get()).ToCString());
+
+        std::shared_ptr<Model_Data> aData(new Model_Data);
+        TDF_Label aLab = aFLabIter.Value()->Label();
+        aData->setLabel(aLab);
+        aData->setDocument(Model_Application::getApplication()->getDocument(myID));
+        aFeature->setData(aData);
+        aFeature->initAttributes();
+        // event: model is updated
+        static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_CREATED);
+        ModelAPI_FeatureUpdatedMessage aMsg(aFeature, anEvent);
+        Event_Loop::loop()->send(aMsg);
+
+        if (aFIter == aFeatures.end()) {
+          aFeatures.push_back(aFeature);
+          aFIter = aFeatures.end();
+        } else {
+          aFIter++;
+          aFeatures.insert(aFIter, aFeature);
+        }
+        // feature for this label is added, so go to the next label
+        aFLabIter.Next();
+      } else { // nothing is changed, both iterators are incremented
+        aFIter++;
+        aFLabIter.Next();
+      }
+    }
+  }
+}