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