Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / ModuleBase / ModuleBase_ITreeNode.h
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef ModuleBase_ITreeNode_H
21 #define ModuleBase_ITreeNode_H
22
23 #include "ModuleBase.h"
24 #include "ModuleBase_Definitions.h"
25
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Document.h>
28
29 #include <QList>
30 #include <QString>
31 #include <QIcon>
32 #include <QVariant>
33
34 class ModuleBase_ITreeNode;
35 class ModuleBase_IWorkshop;
36
37 typedef QList<ModuleBase_ITreeNode*> QTreeNodesList;
38
39 class ModuleBase_ITreeNode
40 {
41 public:
42   enum VisibilityState {
43     NoneState,
44     Visible,
45     SemiVisible,
46     Hidden
47   };
48
49   /// Default constructor
50   ModuleBase_ITreeNode(ModuleBase_ITreeNode* theParent = 0) : myParent(theParent) {}
51
52   virtual ~ModuleBase_ITreeNode() { deleteChildren(); }
53
54   virtual std::string type() const = 0;
55
56   /// Returns the node representation according to theRole.
57   virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
58
59   /// Returns properties flag of the item
60   virtual Qt::ItemFlags flags(int theColumn) const {
61     return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
62   }
63
64   /// Returns parent node of the current node
65   ModuleBase_ITreeNode* parent() const { return myParent; }
66
67   /// Returns list of the node children
68   QTreeNodesList children() const { return myChildren; }
69
70   /// Returns a children node according to given row (index)
71   ModuleBase_ITreeNode* subNode(int theRow) const
72   {
73     if ((theRow > -1) && (theRow < myChildren.length()))
74       return myChildren.at(theRow);
75     return 0;
76   }
77
78   /// Finds a node which contains the referenced object
79   /// \param theObj an object to find
80   /// \param allLevels if true then all sub-trees will be processed
81   ModuleBase_ITreeNode* subNode(const ObjectPtr& theObj, bool allLevels = true) const
82   {
83     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
84       if (aNode->object() == theObj)
85         return aNode;
86       if (allLevels) {
87         ModuleBase_ITreeNode* aSubNode = aNode->subNode(theObj, allLevels);
88         if (aSubNode)
89           return aSubNode;
90       }
91     }
92     return 0;
93   }
94
95   /// Returns true if the given node is found within children
96   /// \param theNode a node to find
97   /// \param allLevels if true then all sub-trees will be processed
98   bool hasSubNode(ModuleBase_ITreeNode* theNode, bool allLevels = true) const
99   {
100     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
101       if (aNode == theNode)
102         return true;
103       if (allLevels) {
104         if (aNode->hasSubNode(theNode))
105           return true;
106       }
107     }
108     return false;
109   }
110
111   /// Returns number of children
112   int childrenCount() const { return myChildren.length(); }
113
114   int nodeRow(ModuleBase_ITreeNode* theNode) const { return myChildren.indexOf(theNode); }
115
116   /// Returns object referenced by the node (can be null)
117   virtual ObjectPtr object() const { return ObjectPtr(); }
118
119   /// Updates all sub-nodes of the node (checks whole sub-tree)
120   virtual void update() {}
121
122   /// Process creation of objects.
123   /// \param theObjects a list of created objects
124   /// \return a list of nodes which corresponds to the created objects
125   virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) {
126     return QTreeNodesList();
127   }
128
129   /// Process deletion of objects.
130   /// \param theDoc a document where objects were deleted
131   /// \param theGroup a name of group where objects were deleted
132   /// \return a list of parents where nodes were deleted
133   virtual QTreeNodesList objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
134   { return QTreeNodesList(); }
135
136   /// Returns workshop object. Has to be reimplemented in a root node
137   virtual ModuleBase_IWorkshop* workshop() const { return parent()->workshop(); }
138
139   /// Returns document object of the sub-tree. Has to be reimplemented in sub-tree root object
140   virtual DocumentPtr document() const { return parent()->document(); }
141
142   /// Returns a node which belongs to the given document and contains objects of the given group
143   /// \param theDoc a document
144   /// \param theGroup a name of objects group
145   /// \return a parent node if it is found
146   virtual ModuleBase_ITreeNode* findParent(const DocumentPtr& theDoc, QString theGroup)
147   { return 0; }
148
149   /// Returns root node of a data tree of the given document
150   /// \param theDoc a document
151   /// \return a tree node which is a root of the document structure
152   virtual ModuleBase_ITreeNode* findRoot(const DocumentPtr& theDoc)
153   {
154     if (document() == theDoc)
155       return this;
156     ModuleBase_ITreeNode* aRoot;
157     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
158       aRoot = aNode->findRoot(theDoc);
159       if (aRoot)
160         return aRoot;
161     }
162     return 0;
163   }
164
165   /// Returns visibilitystate of the node in viewer 3d
166   virtual VisibilityState visibilityState() const { return NoneState; }
167
168 protected:
169
170   /// deletes all children nodes (called in destructor.)
171   virtual void deleteChildren()
172   {
173     while (myChildren.size()) {
174       ModuleBase_ITreeNode* aNode = myChildren.takeLast();
175       delete aNode;
176     }
177   }
178
179   void sortChildren() {
180     if (myChildren.size() > 1) {
181       int i = 0;
182       ModuleBase_ITreeNode* aNode = 0;
183       ObjectPtr aObject;
184       int aIdx;
185       int aCount = 0;
186       int aSize = myChildren.size();
187       int aMaxCount = aSize * aSize;
188       int aShift = 0;
189       while (i < aSize) {
190         aCount++;
191         // To avoid unlimited cycling
192         if (aCount > aMaxCount)
193           break;
194
195         aNode = myChildren.at(i);
196         aObject = aNode->object();
197         if (aObject.get()) {
198           aIdx = aObject->document()->index(aObject, true) + aShift;
199           if (aIdx != i) {
200             myChildren.removeAll(aNode);
201             myChildren.insert(aIdx, aNode);
202             i = 0;
203             continue;
204           }
205         }
206         else
207           aShift++;
208         i++;
209       }
210     }
211   }
212
213   ModuleBase_ITreeNode* myParent; //!< Parent of the node
214   QTreeNodesList myChildren; //!< Children of the node
215 };
216
217 #endif