SHAPER  9.12.0
ModuleBase_ITreeNode.h
1 // Copyright (C) 2014-2023 CEA, EDF
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 #ifdef _MSC_VER
35 #pragma warning(disable: 4100)
36 #endif
37 
40 
41 typedef QList<ModuleBase_ITreeNode*> QTreeNodesList;
42 
44 {
45 public:
46  enum VisibilityState {
47  NoneState,
48  Visible,
49  SemiVisible,
50  Hidden
51  };
52 
54  ModuleBase_ITreeNode(ModuleBase_ITreeNode* theParent = 0) : myParent(theParent) {}
55 
56  virtual ~ModuleBase_ITreeNode() { deleteChildren(); }
57 
58  virtual std::string type() const = 0;
59 
61  virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
62 
64  virtual Qt::ItemFlags flags(int theColumn) const {
65  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
66  }
67 
69  ModuleBase_ITreeNode* parent() const { return myParent; }
70 
72  QTreeNodesList children() const { return myChildren; }
73 
75  ModuleBase_ITreeNode* subNode(int theRow) const
76  {
77  if ((theRow > -1) && (theRow < myChildren.length()))
78  return myChildren.at(theRow);
79  return 0;
80  }
81 
85  ModuleBase_ITreeNode* subNode(const ObjectPtr& theObj, bool allLevels = true) const
86  {
87  foreach(ModuleBase_ITreeNode* aNode, myChildren) {
88  if (aNode->object() == theObj)
89  return aNode;
90  if (allLevels) {
91  ModuleBase_ITreeNode* aSubNode = aNode->subNode(theObj, allLevels);
92  if (aSubNode)
93  return aSubNode;
94  }
95  }
96  return 0;
97  }
98 
102  bool hasSubNode(ModuleBase_ITreeNode* theNode, bool allLevels = true) const
103  {
104  foreach(ModuleBase_ITreeNode* aNode, myChildren) {
105  if (aNode == theNode)
106  return true;
107  if (allLevels) {
108  if (aNode->hasSubNode(theNode))
109  return true;
110  }
111  }
112  return false;
113  }
114 
116  int childrenCount() const { return myChildren.length(); }
117 
118  int nodeRow(ModuleBase_ITreeNode* theNode) const { return myChildren.indexOf(theNode); }
119 
121  virtual ObjectPtr object() const { return ObjectPtr(); }
122 
124  virtual void update() {}
125 
129  virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) {
130  return QTreeNodesList();
131  }
132 
137  virtual QTreeNodesList objectsDeleted(const DocumentPtr& theDoc, const QString& theGroup)
138  { return QTreeNodesList(); }
139 
141  virtual ModuleBase_IWorkshop* workshop() const { return parent()->workshop(); }
142 
144  virtual DocumentPtr document() const { return parent()->document(); }
145 
150  virtual ModuleBase_ITreeNode* findParent(const DocumentPtr& theDoc, QString theGroup)
151  { return 0; }
152 
156  virtual ModuleBase_ITreeNode* findRoot(const DocumentPtr& theDoc)
157  {
158  if (document() == theDoc)
159  return this;
160  ModuleBase_ITreeNode* aRoot;
161  foreach(ModuleBase_ITreeNode* aNode, myChildren) {
162  aRoot = aNode->findRoot(theDoc);
163  if (aRoot)
164  return aRoot;
165  }
166  return 0;
167  }
168 
170  virtual VisibilityState visibilityState() const { return NoneState; }
171 
172 protected:
173 
175  virtual void deleteChildren()
176  {
177  while (myChildren.size()) {
178  ModuleBase_ITreeNode* aNode = myChildren.takeLast();
179  delete aNode;
180  }
181  }
182 
183  void sortChildren() {
184  if (myChildren.size() > 1) {
185  int i = 0;
186  ModuleBase_ITreeNode* aNode = 0;
187  ObjectPtr aObject;
188  int aIdx;
189  int aCount = 0;
190  int aSize = myChildren.size();
191  int aMaxCount = aSize * aSize;
192  int aShift = 0;
193  while (i < aSize) {
194  aCount++;
195  // To avoid unlimited cycling
196  if (aCount > aMaxCount)
197  break;
198 
199  aNode = myChildren.at(i);
200  aObject = aNode->object();
201  if (aObject.get()) {
202  aIdx = aObject->document()->index(aObject, true) + aShift;
203  if (aIdx != i) {
204  myChildren.removeAll(aNode);
205  myChildren.insert(aIdx, aNode);
206  i = 0;
207  continue;
208  }
209  }
210  else
211  aShift++;
212  i++;
213  }
214  }
215  }
216 
218  QTreeNodesList myChildren;
219 };
220 
221 #endif
Definition: ModuleBase_ITreeNode.h:44
ModuleBase_ITreeNode * subNode(int theRow) const
Returns a children node according to given row (index)
Definition: ModuleBase_ITreeNode.h:75
ModuleBase_ITreeNode(ModuleBase_ITreeNode *theParent=0)
Default constructor.
Definition: ModuleBase_ITreeNode.h:54
virtual VisibilityState visibilityState() const
Returns visibilitystate of the node in viewer 3d.
Definition: ModuleBase_ITreeNode.h:170
bool hasSubNode(ModuleBase_ITreeNode *theNode, bool allLevels=true) const
Returns true if the given node is found within children.
Definition: ModuleBase_ITreeNode.h:102
ModuleBase_ITreeNode * myParent
Parent of the node.
Definition: ModuleBase_ITreeNode.h:217
virtual void update()
Updates all sub-nodes of the node (checks whole sub-tree)
Definition: ModuleBase_ITreeNode.h:124
virtual ModuleBase_ITreeNode * findParent(const DocumentPtr &theDoc, QString theGroup)
Returns a node which belongs to the given document and contains objects of the given group.
Definition: ModuleBase_ITreeNode.h:150
virtual ObjectPtr object() const
Returns object referenced by the node (can be null)
Definition: ModuleBase_ITreeNode.h:121
virtual QTreeNodesList objectCreated(const QObjectPtrList &theObjects)
Process creation of objects.
Definition: ModuleBase_ITreeNode.h:129
QTreeNodesList children() const
Returns list of the node children.
Definition: ModuleBase_ITreeNode.h:72
ModuleBase_ITreeNode * subNode(const ObjectPtr &theObj, bool allLevels=true) const
Finds a node which contains the referenced object.
Definition: ModuleBase_ITreeNode.h:85
virtual Qt::ItemFlags flags(int theColumn) const
Returns properties flag of the item.
Definition: ModuleBase_ITreeNode.h:64
virtual QVariant data(int theColumn, int theRole) const
Returns the node representation according to theRole.
Definition: ModuleBase_ITreeNode.h:61
ModuleBase_ITreeNode * parent() const
Returns parent node of the current node.
Definition: ModuleBase_ITreeNode.h:69
virtual QTreeNodesList objectsDeleted(const DocumentPtr &theDoc, const QString &theGroup)
Process deletion of objects.
Definition: ModuleBase_ITreeNode.h:137
QTreeNodesList myChildren
Children of the node.
Definition: ModuleBase_ITreeNode.h:218
int childrenCount() const
Returns number of children.
Definition: ModuleBase_ITreeNode.h:116
virtual void deleteChildren()
deletes all children nodes (called in destructor.)
Definition: ModuleBase_ITreeNode.h:175
virtual ModuleBase_ITreeNode * findRoot(const DocumentPtr &theDoc)
Returns root node of a data tree of the given document.
Definition: ModuleBase_ITreeNode.h:156
virtual DocumentPtr document() const
Returns document object of the sub-tree. Has to be reimplemented in sub-tree root object.
Definition: ModuleBase_ITreeNode.h:144
virtual ModuleBase_IWorkshop * workshop() const
Returns workshop object. Has to be reimplemented in a root node.
Definition: ModuleBase_ITreeNode.h:141
Class which provides access to Workshop object services.
Definition: ModuleBase_IWorkshop.h:48