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