Salome HOME
Merge remote-tracking branch 'origin/cbr/export_to_geom_via_xao'
[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
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   /// Event Listener method
58   /// \param theMessage an event message
59   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
60
61   //! Returns an object by the given Model index.
62   //! Returns 0 if the given index is not index of an object
63   virtual ObjectPtr object(const QModelIndex& theIndex) const;
64
65   //! Returns index of the object
66   //! \param theObject object to find
67   virtual QModelIndex objectIndex(const ObjectPtr theObject, int theColumn = 1) const;
68
69   //! Clear internal data
70   virtual void clear();
71
72   //! Rebuild data tree
73   virtual void rebuildDataTree();
74
75
76   /// Returns the data stored under the given role for the item referred to by the index.
77   /// \param theIndex a model index
78   /// \param theRole a data role (see Qt::ItemDataRole)
79   virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
80
81   /// Returns the data for the given role and section in the header with the specified orientation.
82   /// \param theSection a section
83   /// \param theOrient an orientation
84   /// \param theRole a data role (see Qt::ItemDataRole)
85   virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
86                                   Qt::DisplayRole) const;
87
88   /// Returns the number of rows under the given parent. When the parent is valid it means that
89   /// rowCount is returning the number of children of parent.
90   /// \param theParent a parent model index
91   virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
92
93   /// Returns the number of columns for the children of the given parent.
94   /// It has a one column
95   /// \param theParent a parent model index
96   virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
97
98   /// Returns the index of the item in the model specified by the given row,
99   /// column and parent index.
100   /// \param theRow a row
101   /// \param theColumn a column
102   /// \param theParent a parent model index
103   virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
104                                 QModelIndex()) const;
105
106   /// Returns the parent of the model item with the given index.
107   /// If the item has no parent, an invalid QModelIndex is returned.
108   /// \param theIndex a model index
109   virtual QModelIndex parent(const QModelIndex& theIndex) const;
110
111   /// Returns true if parent has any children; otherwise returns false.
112   /// \param theParent a parent model index
113   virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
114
115   /// Inserts count rows into the model before the given row.
116   /// Items in the new row will be children of the item represented by the parent model index.
117   /// \param theRow a start row
118   /// \param theCount a nember of rows to insert
119   /// \param theParent a parent model index
120   virtual bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
121
122   /// Removes count rows starting with the given row under parent parent from the model.
123   /// \param theRow a start row
124   /// \param theCount a nember of rows to remove
125   /// \param theParent a parent model index
126   virtual bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
127
128   /// Returns the item flags for the given index.
129   /// \param theIndex a model index
130   virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
131
132   /// Returns an index which is root of the given document
133   /// \param theDoc a document
134   QModelIndex documentRootIndex(DocumentPtr theDoc, int theColumn = 1) const;
135
136   /// Returns last history object index
137   virtual QModelIndex lastHistoryIndex() const;
138
139   /// Initialises XML data model reader. It must be initialised before DataModel using.
140   void setXMLReader(Config_DataModelReader* theReader) { myXMLReader = theReader; }
141
142   /// Do not processing anymore events of model loop
143   //bool blockEventsProcessing(const bool theState);
144
145 signals:
146   /// Signal about tree had been rebuilt
147   void treeRebuilt();
148
149 private:
150   enum VisibilityState {
151     NoneState,
152     Visible,
153     SemiVisible,
154     Hidden };
155
156   /// Find a root index which contains objects of the given document
157   /// \param theDoc the document object
158   QModelIndex findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn = 1) const;
159
160   /// Returns number of folders in document.
161   /// Considered folders which has to be shown only if they are not empty.
162   /// \param theDoc document which has to be checked. If 0 then Root document will be considered
163   int foldersCount(ModelAPI_Document* theDoc = 0) const;
164
165   /// Retrurns indexes of folders which can not be shown because they are empty
166   /// \param theDoc document which has to be checked. If 0 then Root document will be considered
167   QIntList missedFolderIndexes(ModelAPI_Document* theDoc = 0) const;
168
169   /// Returns Id (row) of a folder taking into consideration
170   /// folders which can not be shown non empty
171   /// \param theType Type of the folder
172   /// \param theDoc a document which contains this folder
173   int folderId(std::string theType, ModelAPI_Document* theDoc = 0) const;
174
175   /// Removes a row from branch of tree
176   /// \param theStart - start row to update indexes
177   /// \param theSize - number of indexes in the folder
178   /// \param theParent - index of parent folder
179   void rebuildBranch(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
180
181   /// Returns list of folders types which can not be shown empty
182   /// \param fromRoot - root document flag
183   QStringList listOfShowNotEmptyFolders(bool fromRoot = true) const;
184
185   int getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const;
186   ObjectPtr getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const;
187
188   VisibilityState getVisibilityState(const QModelIndex& theIndex) const;
189
190   void addShownFolder(DocumentPtr theDoc, QString theFolder)
191   {
192     if (!myShownFolders.contains(theDoc)) {
193       myShownFolders[theDoc] = QStringList();
194     }
195     myShownFolders[theDoc].append(theFolder);
196   }
197
198   void removeShownFolder(DocumentPtr theDoc, QString theFolder)
199   {
200     if (myShownFolders.contains(theDoc)) {
201       myShownFolders[theDoc].removeAll(theFolder);
202       if (myShownFolders[theDoc].isEmpty())
203         myShownFolders.remove(theDoc);
204     }
205   }
206
207   bool hasShownFolder(DocumentPtr theDoc, QString theFolder) const
208   {
209     if (myShownFolders.contains(theDoc))
210       return myShownFolders[theDoc].contains(theFolder);
211     return false;
212   }
213
214   Config_DataModelReader* myXMLReader;
215
216   XGUI_Workshop* myWorkshop;
217   QMap<DocumentPtr, QStringList> myShownFolders;
218   //bool myIsEventsProcessingBlocked;
219 };
220
221 #endif