Salome HOME
Avoid unlimited cycling on nodes sorting
[modules/shaper.git] / src / ModuleBase / ModuleBase_ITreeNode.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef ModuleBase_ITreeNode_H
22 #define ModuleBase_ITreeNode_H
23
24 #include "ModuleBase.h"
25 #include "ModuleBase_Definitions.h"
26
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Document.h>
29
30 #include <QList>
31 #include <QString>
32 #include <QIcon>
33 #include <QVariant>
34
35 class ModuleBase_ITreeNode;
36 class ModuleBase_IWorkshop;
37
38 typedef QList<ModuleBase_ITreeNode*> QTreeNodesList;
39
40 class ModuleBase_ITreeNode
41 {
42 public:
43   enum VisibilityState {
44     NoneState,
45     Visible,
46     SemiVisible,
47     Hidden
48   };
49
50   /// Default constructor
51   ModuleBase_ITreeNode(ModuleBase_ITreeNode* theParent = 0) : myParent(theParent) {}
52
53   virtual ~ModuleBase_ITreeNode() { deleteChildren(); }
54
55   virtual std::string type() const = 0;
56
57   /// Returns the node representation according to theRole.
58   virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
59
60   /// Returns properties flag of the item
61   virtual Qt::ItemFlags flags(int theColumn) const {
62     return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
63   }
64
65   /// Returns parent node of the current node
66   ModuleBase_ITreeNode* parent() const { return myParent; }
67
68   /// Returns list of the node children
69   QTreeNodesList children() const { return myChildren; }
70
71   /// Returns a children node according to given row (index)
72   ModuleBase_ITreeNode* subNode(int theRow) const
73   {
74     if ((theRow > -1) && (theRow < myChildren.length()))
75       return myChildren.at(theRow);
76     return 0;
77   }
78
79   /// Finds a node which contains the referenced object
80   /// \param theObj an object to find
81   /// \param allLevels if true then all sub-trees will be processed
82   ModuleBase_ITreeNode* subNode(const ObjectPtr& theObj, bool allLevels = true) const
83   {
84     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
85       if (aNode->object() == theObj)
86         return aNode;
87       if (allLevels) {
88         ModuleBase_ITreeNode* aSubNode = aNode->subNode(theObj, allLevels);
89         if (aSubNode)
90           return aSubNode;
91       }
92     }
93     return 0;
94   }
95
96   /// Returns true if the given node is found within children
97   /// \param theNode a node to find
98   /// \param allLevels if true then all sub-trees will be processed
99   bool hasSubNode(ModuleBase_ITreeNode* theNode, bool allLevels = true) const
100   {
101     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
102       if (aNode == theNode)
103         return true;
104       if (allLevels) {
105         if (aNode->hasSubNode(theNode))
106           return true;
107       }
108     }
109     return false;
110   }
111
112   /// Returns number of children
113   int childrenCount() const { return myChildren.length(); }
114
115   int nodeRow(ModuleBase_ITreeNode* theNode) const { return myChildren.indexOf(theNode); }
116
117   /// Returns object referenced by the node (can be null)
118   virtual ObjectPtr object() const { return ObjectPtr(); }
119
120   /// Updates all sub-nodes of the node (checks whole sub-tree)
121   virtual void update() {}
122
123   /// Process creation of objects.
124   /// \param theObjects a list of created objects
125   /// \return a list of nodes which corresponds to the created objects
126   virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) {
127     return QTreeNodesList();
128   }
129
130   /// Process deletion of objects.
131   /// \param theDoc a document where objects were deleted
132   /// \param theGroup a name of group where objects were deleted
133   /// \return a list of parents where nodes were deleted
134   virtual QTreeNodesList objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
135   { return QTreeNodesList(); }
136
137   /// Returns workshop object. Has to be reimplemented in a root node
138   virtual ModuleBase_IWorkshop* workshop() const { return parent()->workshop(); }
139
140   /// Returns document object of the sub-tree. Has to be reimplemented in sub-tree root object
141   virtual DocumentPtr document() const { return parent()->document(); }
142
143   /// Returns a node which belongs to the given document and contains objects of the given group
144   /// \param theDoc a document
145   /// \param theGroup a name of objects group
146   /// \return a parent node if it is found
147   virtual ModuleBase_ITreeNode* findParent(const DocumentPtr& theDoc, QString theGroup)
148   { return 0; }
149
150   /// Returns root node of a data tree of the given document
151   /// \param theDoc a document
152   /// \return a tree node which is a root of the document structure
153   virtual ModuleBase_ITreeNode* findRoot(const DocumentPtr& theDoc)
154   {
155     if (document() == theDoc)
156       return this;
157     ModuleBase_ITreeNode* aRoot;
158     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
159       aRoot = aNode->findRoot(theDoc);
160       if (aRoot)
161         return aRoot;
162     }
163     return 0;
164   }
165
166   /// Returns visibilitystate of the node in viewer 3d
167   virtual VisibilityState visibilityState() const { return NoneState; }
168
169 protected:
170
171   /// deletes all children nodes (called in destructor.)
172   virtual void deleteChildren()
173   {
174     while (myChildren.size()) {
175       ModuleBase_ITreeNode* aNode = myChildren.takeLast();
176       delete aNode;
177     }
178   }
179
180   void sortChildren() {
181     if (myChildren.size() > 1) {
182       int i = 0;
183       ModuleBase_ITreeNode* aNode = 0;
184       ObjectPtr aObject;
185       int aIdx;
186       int aCount = 0;
187       int aSize = myChildren.size();
188       while (i < aSize) {
189         aCount++;
190         aNode = myChildren.at(i);
191         aObject = aNode->object();
192         if (aObject.get()) {
193           aIdx = aObject->document()->index(aObject, true);
194           if (aIdx != i) {
195             myChildren.removeAll(aNode);
196             myChildren.insert(aIdx, aNode);
197             i = 0;
198             continue;
199           }
200         }
201         i++;
202         // To avoid unlimited cycling
203         if (aCount > aSize * aSize)
204           break;
205       }
206     }
207   }
208
209   ModuleBase_ITreeNode* myParent; //!< Parent of the node
210   QTreeNodesList myChildren; //!< Children of the node
211 };
212
213 #endif