Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / ModuleBase / ModuleBase_ITreeNode.h
index 9e33eeae5fc9d752bd844d13f27fc034084753fd..33b8d8a6450dcf922a08a571b33d391971a36f77 100644 (file)
@@ -40,14 +40,27 @@ typedef QList<ModuleBase_ITreeNode*> QTreeNodesList;
 class ModuleBase_ITreeNode
 {
 public:
+  enum VisibilityState {
+    NoneState,
+    Visible,
+    SemiVisible,
+    Hidden
+  };
+
   /// Default constructor
   ModuleBase_ITreeNode(ModuleBase_ITreeNode* theParent = 0) : myParent(theParent) {}
 
+  virtual ~ModuleBase_ITreeNode() { deleteChildren(); }
+
+  virtual std::string type() const = 0;
+
   /// Returns the node representation according to theRole.
   virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
 
   /// Returns properties flag of the item
-  virtual Qt::ItemFlags flags(int theColumn) const { return Qt::ItemIsSelectable | Qt::ItemIsEnabled; }
+  virtual Qt::ItemFlags flags(int theColumn) const {
+    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
+  }
 
   /// Returns parent node of the current node
   ModuleBase_ITreeNode* parent() const { return myParent; }
@@ -110,7 +123,9 @@ public:
   /// Process creation of objects.
   /// \param theObjects a list of created objects
   /// \return a list of nodes which corresponds to the created objects
-  virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) { return QTreeNodesList(); }
+  virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) {
+    return QTreeNodesList();
+  }
 
   /// Process deletion of objects.
   /// \param theDoc a document where objects were deleted
@@ -132,7 +147,70 @@ public:
   virtual ModuleBase_ITreeNode* findParent(const DocumentPtr& theDoc, QString theGroup)
   { return 0; }
 
+  /// Returns root node of a data tree of the given document
+  /// \param theDoc a document
+  /// \return a tree node which is a root of the document structure
+  virtual ModuleBase_ITreeNode* findRoot(const DocumentPtr& theDoc)
+  {
+    if (document() == theDoc)
+      return this;
+    ModuleBase_ITreeNode* aRoot;
+    foreach(ModuleBase_ITreeNode* aNode, myChildren) {
+      aRoot = aNode->findRoot(theDoc);
+      if (aRoot)
+        return aRoot;
+    }
+    return 0;
+  }
+
+  /// Returns visibilitystate of the node in viewer 3d
+  virtual VisibilityState visibilityState() const { return NoneState; }
+
 protected:
+
+  /// deletes all children nodes (called in destructor.)
+  virtual void deleteChildren()
+  {
+    while (myChildren.size()) {
+      ModuleBase_ITreeNode* aNode = myChildren.takeLast();
+      delete aNode;
+    }
+  }
+
+  void sortChildren() {
+    if (myChildren.size() > 1) {
+      int i = 0;
+      ModuleBase_ITreeNode* aNode = 0;
+      ObjectPtr aObject;
+      int aIdx;
+      int aCount = 0;
+      int aSize = myChildren.size();
+      int aMaxCount = aSize * aSize;
+      int aShift = 0;
+      while (i < aSize) {
+        aCount++;
+        // To avoid unlimited cycling
+        if (aCount > aMaxCount)
+          break;
+
+        aNode = myChildren.at(i);
+        aObject = aNode->object();
+        if (aObject.get()) {
+          aIdx = aObject->document()->index(aObject, true) + aShift;
+          if (aIdx != i) {
+            myChildren.removeAll(aNode);
+            myChildren.insert(aIdx, aNode);
+            i = 0;
+            continue;
+          }
+        }
+        else
+          aShift++;
+        i++;
+      }
+    }
+  }
+
   ModuleBase_ITreeNode* myParent; //!< Parent of the node
   QTreeNodesList myChildren; //!< Children of the node
 };