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