1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef ModuleBase_ITreeNode_H
21 #define ModuleBase_ITreeNode_H
23 #include "ModuleBase.h"
24 #include "ModuleBase_Definitions.h"
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Document.h>
35 #pragma warning(disable: 4100)
38 class ModuleBase_ITreeNode;
39 class ModuleBase_IWorkshop;
41 typedef QList<ModuleBase_ITreeNode*> QTreeNodesList;
43 class ModuleBase_ITreeNode
46 enum VisibilityState {
53 /// Default constructor
54 ModuleBase_ITreeNode(ModuleBase_ITreeNode* theParent = 0) : myParent(theParent) {}
56 virtual ~ModuleBase_ITreeNode() { deleteChildren(); }
58 virtual std::string type() const = 0;
60 /// Returns the node representation according to theRole.
61 virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
63 /// Returns properties flag of the item
64 virtual Qt::ItemFlags flags(int theColumn) const {
65 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
68 /// Returns parent node of the current node
69 ModuleBase_ITreeNode* parent() const { return myParent; }
71 /// Returns list of the node children
72 QTreeNodesList children() const { return myChildren; }
74 /// Returns a children node according to given row (index)
75 ModuleBase_ITreeNode* subNode(int theRow) const
77 if ((theRow > -1) && (theRow < myChildren.length()))
78 return myChildren.at(theRow);
82 /// Finds a node which contains the referenced object
83 /// \param theObj an object to find
84 /// \param allLevels if true then all sub-trees will be processed
85 ModuleBase_ITreeNode* subNode(const ObjectPtr& theObj, bool allLevels = true) const
87 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
88 if (aNode->object() == theObj)
91 ModuleBase_ITreeNode* aSubNode = aNode->subNode(theObj, allLevels);
99 /// Returns true if the given node is found within children
100 /// \param theNode a node to find
101 /// \param allLevels if true then all sub-trees will be processed
102 bool hasSubNode(ModuleBase_ITreeNode* theNode, bool allLevels = true) const
104 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
105 if (aNode == theNode)
108 if (aNode->hasSubNode(theNode))
115 /// Returns number of children
116 int childrenCount() const { return myChildren.length(); }
118 int nodeRow(ModuleBase_ITreeNode* theNode) const { return myChildren.indexOf(theNode); }
120 /// Returns object referenced by the node (can be null)
121 virtual ObjectPtr object() const { return ObjectPtr(); }
123 /// Updates all sub-nodes of the node (checks whole sub-tree)
124 virtual void update() {}
126 /// Process creation of objects.
127 /// \param theObjects a list of created objects
128 /// \return a list of nodes which corresponds to the created objects
129 virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) {
130 return QTreeNodesList();
133 /// Process deletion of objects.
134 /// \param theDoc a document where objects were deleted
135 /// \param theGroup a name of group where objects were deleted
136 /// \return a list of parents where nodes were deleted
137 virtual QTreeNodesList objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
138 { return QTreeNodesList(); }
140 /// Returns workshop object. Has to be reimplemented in a root node
141 virtual ModuleBase_IWorkshop* workshop() const { return parent()->workshop(); }
143 /// Returns document object of the sub-tree. Has to be reimplemented in sub-tree root object
144 virtual DocumentPtr document() const { return parent()->document(); }
146 /// Returns a node which belongs to the given document and contains objects of the given group
147 /// \param theDoc a document
148 /// \param theGroup a name of objects group
149 /// \return a parent node if it is found
150 virtual ModuleBase_ITreeNode* findParent(const DocumentPtr& theDoc, QString theGroup)
153 /// Returns root node of a data tree of the given document
154 /// \param theDoc a document
155 /// \return a tree node which is a root of the document structure
156 virtual ModuleBase_ITreeNode* findRoot(const DocumentPtr& theDoc)
158 if (document() == theDoc)
160 ModuleBase_ITreeNode* aRoot;
161 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
162 aRoot = aNode->findRoot(theDoc);
169 /// Returns visibilitystate of the node in viewer 3d
170 virtual VisibilityState visibilityState() const { return NoneState; }
174 /// deletes all children nodes (called in destructor.)
175 virtual void deleteChildren()
177 while (myChildren.size()) {
178 ModuleBase_ITreeNode* aNode = myChildren.takeLast();
183 void sortChildren() {
184 if (myChildren.size() > 1) {
186 ModuleBase_ITreeNode* aNode = 0;
190 int aSize = myChildren.size();
191 int aMaxCount = aSize * aSize;
195 // To avoid unlimited cycling
196 if (aCount > aMaxCount)
199 aNode = myChildren.at(i);
200 aObject = aNode->object();
202 aIdx = aObject->document()->index(aObject, true) + aShift;
204 myChildren.removeAll(aNode);
205 myChildren.insert(aIdx, aNode);
217 ModuleBase_ITreeNode* myParent; //!< Parent of the node
218 QTreeNodesList myChildren; //!< Children of the node