Salome HOME
Copyright update 2021
[modules/shaper.git] / src / XGUI / XGUI_DataModel.h
1 // Copyright (C) 2014-2021  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 XGUI_DataModel_H
21 #define XGUI_DataModel_H
22
23 #include "XGUI.h"
24 #include <ModuleBase_Definitions.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_Document.h>
27 #include <Events_Listener.h>
28
29 #include <QAbstractItemModel>
30
31 class XGUI_Workshop;
32 class ModuleBase_ITreeNode;
33
34 /**\class XGUI_DataModel
35  * \ingroup GUI
36  * \brief This is a data model for Object Browser (QTreeView).
37  * It uses XML file for definition of data tree.
38  * Some tips about organization of the model:
39  * - Non Valid Index - root index
40  * - An index with internal Id == -1 is a folder of root document
41  * - An index which contains internal pointer as ModelAPI_Object its the object
42  * - An index which contains internal pointer as ModelAPI_Document is 
43  *   a folder which belongs to this document
44  */
45 class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener
46 {
47 Q_OBJECT
48 public:
49   /// Constructor
50   /// \param theParent a parent object
51   XGUI_DataModel(QObject* theParent);
52
53   /// Destructor
54   virtual ~XGUI_DataModel();
55
56
57   void setRoot(ModuleBase_ITreeNode* theRoot) { myRoot = theRoot; }
58
59   ModuleBase_ITreeNode* root() const { return myRoot; }
60
61   /// Event Listener method
62   /// \param theMessage an event message
63   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
64
65   //! Returns an object by the given Model index.
66   //! Returns 0 if the given index is not index of an object
67   virtual ObjectPtr object(const QModelIndex& theIndex) const;
68
69   //! Returns index of the object
70   //! \param theObject object to find
71   virtual QModelIndex objectIndex(const ObjectPtr theObject, int theColumn = 1) const;
72
73   //! Clear internal data
74   virtual void clear();
75
76   //! Rebuild data tree
77   virtual void rebuildDataTree();
78
79   /// Returns the data stored under the given role for the item referred to by the index.
80   /// \param theIndex a model index
81   /// \param theRole a data role (see Qt::ItemDataRole)
82   virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
83
84   /// Returns the data for the given role and section in the header with the specified orientation.
85   /// \param theSection a section
86   /// \param theOrient an orientation
87   /// \param theRole a data role (see Qt::ItemDataRole)
88   virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
89                                   Qt::DisplayRole) const;
90
91   /// Returns the number of rows under the given parent. When the parent is valid it means that
92   /// rowCount is returning the number of children of parent.
93   /// \param theParent a parent model index
94   virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
95
96   /// Returns the number of columns for the children of the given parent.
97   /// It has a one column
98   /// \param theParent a parent model index
99   virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
100
101   /// Returns the index of the item in the model specified by the given row,
102   /// column and parent index.
103   /// \param theRow a row
104   /// \param theColumn a column
105   /// \param theParent a parent model index
106   virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
107                                 QModelIndex()) const;
108
109   /// Returns the parent of the model item with the given index.
110   /// If the item has no parent, an invalid QModelIndex is returned.
111   /// \param theIndex a model index
112   virtual QModelIndex parent(const QModelIndex& theIndex) const;
113
114   /// Returns true if parent has any children; otherwise returns false.
115   /// \param theParent a parent model index
116   virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
117
118   /// Inserts count rows into the model before the given row.
119   /// Items in the new row will be children of the item represented by the parent model index.
120   /// \param theRow a start row
121   /// \param theCount a nember of rows to insert
122   /// \param theParent a parent model index
123   virtual bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
124
125   /// Removes count rows starting with the given row under parent parent from the model.
126   /// \param theRow a start row
127   /// \param theCount a nember of rows to remove
128   /// \param theParent a parent model index
129   virtual bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
130
131   /// Returns the item flags for the given index.
132   /// \param theIndex a model index
133   virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
134
135   /// Returns an index which is root of the given document
136   /// \param theDoc a document
137   QModelIndex documentRootIndex(DocumentPtr theDoc, int theColumn = 1) const;
138
139   /// Returns last history object index
140   //virtual QModelIndex lastHistoryIndex() const;
141
142   /// Initialises XML data model reader. It must be initialised before DataModel using.
143   //void setXMLReader(Config_DataModelReader* theReader) { myXMLReader = theReader; }
144
145   /// Do not processing anymore events of model loop
146   //bool blockEventsProcessing(const bool theState);
147
148   /// Returns true if the data model item has Hidden visual state
149   /// \param theIndex a tree model item
150   /// \return boolean value
151   bool hasHiddenState(const QModelIndex& theIndex);
152
153   /// Returns true if the given index exists in data tree
154   /// \param theIndex an index to check
155   bool hasIndex(const QModelIndex& theIndex) const;
156
157   /// Returns true if the given node is a parto of the data tree
158   /// \param theNode a node to check
159   bool hasNode(ModuleBase_ITreeNode* theNode) const;
160
161   /// Returns document of the given index
162   /// \param theIndex an index to check
163   DocumentPtr document(const QModelIndex& theIndex) const;
164
165   /// Returns index for the node and column
166   /// \param theNode a node
167   /// \param thCol a column
168   QModelIndex getIndex(ModuleBase_ITreeNode* theNode, int thCol) const;
169
170 signals:
171   /// Signal send before tree rebuild
172   void beforeTreeRebuild();
173
174   /// Signal about tree had been rebuilt
175   void treeRebuilt();
176
177 private:
178   enum VisibilityState {
179     NoneState,
180     Visible,
181     SemiVisible,
182     Hidden };
183
184
185   QModelIndex getParentIndex(ModuleBase_ITreeNode* theNode, int thCol) const;
186
187   void updateSubTree(ModuleBase_ITreeNode* theParent);
188
189   /// Find a root index which contains objects of the given document
190   /// \param theDoc the document object
191   //QModelIndex findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn = 1) const;
192
193   /// Returns number of folders in document.
194   /// Considered folders which has to be shown only if they are not empty.
195   /// \param theDoc document which has to be checked. If 0 then Root document will be considered
196   //int foldersCount(ModelAPI_Document* theDoc = 0) const;
197
198   /// Retrurns indexes of folders which can not be shown because they are empty
199   /// \param theDoc document which has to be checked. If 0 then Root document will be considered
200   //QIntList missedFolderIndexes(ModelAPI_Document* theDoc = 0) const;
201
202   /// Returns Id (row) of a folder taking into consideration
203   /// folders which can not be shown non empty
204   /// \param theType Type of the folder
205   /// \param theDoc a document which contains this folder
206   //int folderId(std::string theType, ModelAPI_Document* theDoc = 0) const;
207
208   /// Removes a row from branch of tree
209   /// \param theStart - start row to update indexes
210   /// \param theSize - number of indexes in the folder
211   /// \param theParent - index of parent folder
212   //void rebuildBranch(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
213
214   /// Returns list of folders types which can not be shown empty
215   /// \param fromRoot - root document flag
216   //QStringList listOfShowNotEmptyFolders(bool fromRoot = true) const;
217
218   //int getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const;
219   //ObjectPtr getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const;
220
221   //VisibilityState getVisibilityState(const QModelIndex& theIndex) const;
222
223   //void addShownFolder(DocumentPtr theDoc, QString theFolder)
224   //{
225   //  if (!myShownFolders.contains(theDoc)) {
226   //    myShownFolders[theDoc] = QStringList();
227   //  }
228   //  myShownFolders[theDoc].append(theFolder);
229   //}
230
231   //void removeShownFolder(DocumentPtr theDoc, QString theFolder)
232   //{
233   //  if (myShownFolders.contains(theDoc)) {
234   //    myShownFolders[theDoc].removeAll(theFolder);
235   //    if (myShownFolders[theDoc].isEmpty())
236   //      myShownFolders.remove(theDoc);
237   //  }
238   //}
239
240   //bool hasShownFolder(DocumentPtr theDoc, QString theFolder) const
241   //{
242   //  if (myShownFolders.contains(theDoc))
243   //    return myShownFolders[theDoc].contains(theFolder);
244   //  return false;
245   //}
246
247   //Config_DataModelReader* myXMLReader;
248
249   XGUI_Workshop* myWorkshop;
250   QMap<DocumentPtr, QStringList> myShownFolders;
251   //bool myIsEventsProcessingBlocked;
252
253   ModuleBase_ITreeNode* myRoot;
254 };
255
256 #endif