1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef ModuleBase_ITreeNode_H
22 #define ModuleBase_ITreeNode_H
24 #include "ModuleBase.h"
25 #include "ModuleBase_Definitions.h"
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Document.h>
35 class ModuleBase_ITreeNode;
36 class ModuleBase_IWorkshop;
38 typedef QList<ModuleBase_ITreeNode*> QTreeNodesList;
40 class ModuleBase_ITreeNode
43 /// Default constructor
44 ModuleBase_ITreeNode(ModuleBase_ITreeNode* theParent = 0) : myParent(theParent) {}
46 /// Returns the node representation according to theRole.
47 virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
49 /// Returns properties flag of the item
50 virtual Qt::ItemFlags flags(int theColumn) const { return Qt::ItemIsSelectable | Qt::ItemIsEnabled; }
52 /// Returns parent node of the current node
53 ModuleBase_ITreeNode* parent() const { return myParent; }
55 /// Returns list of the node children
56 QTreeNodesList children() const { return myChildren; }
58 /// Returns a children node according to given row (index)
59 ModuleBase_ITreeNode* subNode(int theRow) const
61 if ((theRow > -1) && (theRow < myChildren.length()))
62 return myChildren.at(theRow);
66 /// Finds a node which contains the referenced object
67 /// \param theObj an object to find
68 /// \param allLevels if true then all sub-trees will be processed
69 ModuleBase_ITreeNode* subNode(const ObjectPtr& theObj, bool allLevels = true) const
71 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
72 if (aNode->object() == theObj)
75 ModuleBase_ITreeNode* aSubNode = aNode->subNode(theObj, allLevels);
83 /// Returns true if the given node is found within children
84 /// \param theNode a node to find
85 /// \param allLevels if true then all sub-trees will be processed
86 bool hasSubNode(ModuleBase_ITreeNode* theNode, bool allLevels = true) const
88 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
92 if (aNode->hasSubNode(theNode))
99 /// Returns number of children
100 int childrenCount() const { return myChildren.length(); }
102 int nodeRow(ModuleBase_ITreeNode* theNode) const { return myChildren.indexOf(theNode); }
104 /// Returns object referenced by the node (can be null)
105 virtual ObjectPtr object() const { return ObjectPtr(); }
107 /// Updates all sub-nodes of the node (checks whole sub-tree)
108 virtual void update() {}
110 /// Process creation of objects.
111 /// \param theObjects a list of created objects
112 /// \return a list of nodes which corresponds to the created objects
113 virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) { return QTreeNodesList(); }
115 /// Process deletion of objects.
116 /// \param theDoc a document where objects were deleted
117 /// \param theGroup a name of group where objects were deleted
118 /// \return a list of parents where nodes were deleted
119 virtual QTreeNodesList objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
120 { return QTreeNodesList(); }
122 /// Returns workshop object. Has to be reimplemented in a root node
123 virtual ModuleBase_IWorkshop* workshop() const { return parent()->workshop(); }
125 /// Returns document object of the sub-tree. Has to be reimplemented in sub-tree root object
126 virtual DocumentPtr document() const { return parent()->document(); }
128 /// Returns a node which belongs to the given document and contains objects of the given group
129 /// \param theDoc a document
130 /// \param theGroup a name of objects group
131 /// \return a parent node if it is found
132 virtual ModuleBase_ITreeNode* findParent(const DocumentPtr& theDoc, QString theGroup)
135 /// Returns root node of a data tree of the given document
136 /// \param theDoc a document
137 /// \return a tree node which is a root of the document structure
138 virtual ModuleBase_ITreeNode* findRoot(const DocumentPtr& theDoc)
140 if (document() == theDoc)
142 ModuleBase_ITreeNode* aRoot;
143 foreach(ModuleBase_ITreeNode* aNode, myChildren) {
144 aRoot = aNode->findRoot(theDoc);
152 ModuleBase_ITreeNode* myParent; //!< Parent of the node
153 QTreeNodesList myChildren; //!< Children of the node