]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ITreeNode.h
Salome HOME
4ae92e32f798e290785a80a39055f913deaa3102
[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   /// Default constructor
44   ModuleBase_ITreeNode(ModuleBase_ITreeNode* theParent = 0) : myParent(theParent) {}
45
46   virtual ~ModuleBase_ITreeNode() { deleteChildren(); }
47
48   virtual std::string type() const = 0;
49
50   /// Returns the node representation according to theRole.
51   virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
52
53   /// Returns properties flag of the item
54   virtual Qt::ItemFlags flags(int theColumn) const { return Qt::ItemIsSelectable | Qt::ItemIsEnabled; }
55
56   /// Returns parent node of the current node
57   ModuleBase_ITreeNode* parent() const { return myParent; }
58
59   /// Returns list of the node children
60   QTreeNodesList children() const { return myChildren; }
61
62   /// Returns a children node according to given row (index)
63   ModuleBase_ITreeNode* subNode(int theRow) const
64   {
65     if ((theRow > -1) && (theRow < myChildren.length()))
66       return myChildren.at(theRow);
67     return 0;
68   }
69
70   /// Finds a node which contains the referenced object
71   /// \param theObj an object to find
72   /// \param allLevels if true then all sub-trees will be processed
73   ModuleBase_ITreeNode* subNode(const ObjectPtr& theObj, bool allLevels = true) const
74   {
75     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
76       if (aNode->object() == theObj)
77         return aNode;
78       if (allLevels) {
79         ModuleBase_ITreeNode* aSubNode = aNode->subNode(theObj, allLevels);
80         if (aSubNode)
81           return aSubNode;
82       }
83     }
84     return 0;
85   }
86
87   /// Returns true if the given node is found within children
88   /// \param theNode a node to find
89   /// \param allLevels if true then all sub-trees will be processed
90   bool hasSubNode(ModuleBase_ITreeNode* theNode, bool allLevels = true) const
91   {
92     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
93       if (aNode == theNode)
94         return true;
95       if (allLevels) {
96         if (aNode->hasSubNode(theNode))
97           return true;
98       }
99     }
100     return false;
101   }
102
103   /// Returns number of children
104   int childrenCount() const { return myChildren.length(); }
105
106   int nodeRow(ModuleBase_ITreeNode* theNode) const { return myChildren.indexOf(theNode); }
107
108   /// Returns object referenced by the node (can be null)
109   virtual ObjectPtr object() const { return ObjectPtr(); }
110
111   /// Updates all sub-nodes of the node (checks whole sub-tree)
112   virtual void update() {}
113
114   /// Process creation of objects.
115   /// \param theObjects a list of created objects
116   /// \return a list of nodes which corresponds to the created objects
117   virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) { return QTreeNodesList(); }
118
119   /// Process deletion of objects.
120   /// \param theDoc a document where objects were deleted
121   /// \param theGroup a name of group where objects were deleted
122   /// \return a list of parents where nodes were deleted
123   virtual QTreeNodesList objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
124   { return QTreeNodesList(); }
125
126   /// Returns workshop object. Has to be reimplemented in a root node
127   virtual ModuleBase_IWorkshop* workshop() const { return parent()->workshop(); }
128
129   /// Returns document object of the sub-tree. Has to be reimplemented in sub-tree root object
130   virtual DocumentPtr document() const { return parent()->document(); }
131
132   /// Returns a node which belongs to the given document and contains objects of the given group
133   /// \param theDoc a document
134   /// \param theGroup a name of objects group
135   /// \return a parent node if it is found
136   virtual ModuleBase_ITreeNode* findParent(const DocumentPtr& theDoc, QString theGroup)
137   { return 0; }
138
139   /// Returns root node of a data tree of the given document
140   /// \param theDoc a document
141   /// \return a tree node which is a root of the document structure
142   virtual ModuleBase_ITreeNode* findRoot(const DocumentPtr& theDoc)
143   {
144     if (document() == theDoc)
145       return this;
146     ModuleBase_ITreeNode* aRoot;
147     foreach(ModuleBase_ITreeNode* aNode, myChildren) {
148       aRoot = aNode->findRoot(theDoc);
149       if (aRoot)
150         return aRoot;
151     }
152     return 0;
153   }
154
155 protected:
156
157   /// deletes all children nodes (called in destructor.)
158   virtual void deleteChildren()
159   {
160     while (myChildren.size()) {
161       ModuleBase_ITreeNode* aNode = myChildren.takeLast();
162       delete aNode;
163     }
164   }
165
166   ModuleBase_ITreeNode* myParent; //!< Parent of the node
167   QTreeNodesList myChildren; //!< Children of the node
168 };
169
170 #endif