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