Salome HOME
Merge branch 'python_parametric_api' of https://git.salome-platform.org/git/modules...
[modules/shaper.git] / src / XGUI / XGUI_DataModel.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_DataModel.h
4 // Created:     21 Jul 2015
5 // Author:      Vitaly SMETANNIKOV
6
7
8 #ifndef XGUI_DataModel_H
9 #define XGUI_DataModel_H
10
11 #include "XGUI.h"
12 #include <ModuleBase_Definitions.h>
13 #include <ModelAPI_Object.h>
14 #include <ModelAPI_Document.h>
15 #include <Config_DataModelReader.h>
16 #include <Events_Listener.h>
17
18 #include <QAbstractItemModel>
19
20 /**\class XGUI_DataModel
21  * \ingroup GUI
22  * \brief This is a data model for Object Browser (QTreeView).
23  * It uses XML file for definition of data tree.
24  * Some tips about organization of the model:
25  * - Non Valid Index - root index
26  * - An index with internal Id == -1 is a folder of root document
27  * - An index which contains internal pointer as ModelAPI_Object its the object
28  * - An index which contains internal pointer as ModelAPI_Document is a folder which belongs to this document
29  */
30 class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener
31 {
32 Q_OBJECT
33 public:
34   /// Constructor
35   /// \param theParent a parent object
36   XGUI_DataModel(QObject* theParent);
37
38   /// Event Listener method
39   /// \param theMessage an event message
40   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
41
42   //! Returns an object by the given Model index.
43   //! Returns 0 if the given index is not index of an object
44   virtual ObjectPtr object(const QModelIndex& theIndex) const;
45
46   //! Returns index of the object
47   //! \param theObject object to find
48   virtual QModelIndex objectIndex(const ObjectPtr theObject) const;
49
50   //! Clear internal data
51   virtual void clear();
52
53   //! Rebuild data tree
54   virtual void rebuildDataTree();
55
56
57   /// Returns the data stored under the given role for the item referred to by the index.
58   /// \param theIndex a model index
59   /// \param theRole a data role (see Qt::ItemDataRole)
60   virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
61
62   /// Returns the data for the given role and section in the header with the specified orientation.
63   /// \param theSection a section
64   /// \param theOrient an orientation
65   /// \param theRole a data role (see Qt::ItemDataRole)
66   virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
67                                   Qt::DisplayRole) const;
68
69   /// Returns the number of rows under the given parent. When the parent is valid it means that 
70   /// rowCount is returning the number of children of parent.
71   /// \param theParent a parent model index
72   virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
73
74   /// Returns the number of columns for the children of the given parent.
75   /// It has a one column
76   /// \param theParent a parent model index
77   virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
78
79   /// Returns the index of the item in the model specified by the given row, column and parent index.
80   /// \param theRow a row
81   /// \param theColumn a column
82   /// \param theParent a parent model index
83   virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
84                                 QModelIndex()) const;
85
86   /// Returns the parent of the model item with the given index. 
87   /// If the item has no parent, an invalid QModelIndex is returned.
88   /// \param theIndex a model index
89   virtual QModelIndex parent(const QModelIndex& theIndex) const;
90
91   /// Returns true if parent has any children; otherwise returns false.
92   /// \param theParent a parent model index
93   virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
94
95   /// Inserts count rows into the model before the given row. 
96   /// Items in the new row will be children of the item represented by the parent model index.
97   /// \param theRow a start row
98   /// \param theCount a nember of rows to insert
99   /// \param theParent a parent model index
100   virtual bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
101
102   /// Removes count rows starting with the given row under parent parent from the model.
103   /// \param theRow a start row
104   /// \param theCount a nember of rows to remove
105   /// \param theParent a parent model index
106   virtual bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
107
108   /// Returns the item flags for the given index.
109   /// \param theIndex a model index
110   virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
111
112   /// Returns an index which is root of the given document
113   /// \param theDoc a document
114   QModelIndex documentRootIndex(DocumentPtr theDoc) const;
115
116   /// Returns last history object index
117   virtual QModelIndex lastHistoryIndex() const;
118
119 private:
120   /// Find a root index which contains objects of the given document
121   /// \param theDoc the document object
122   QModelIndex findDocumentRootIndex(const ModelAPI_Document* theDoc) const;
123
124   /// Returns number of folders in document. Considered folders which has to be shown only if they are not empty.
125   /// \param theDoc document which has to be checked. If 0 then Root document will be considered 
126   int foldersCount(ModelAPI_Document* theDoc = 0) const;
127
128   /// Retrurns indexes of folders which can not be shown because they are empty
129   /// \param theDoc document which has to be checked. If 0 then Root document will be considered 
130   QIntList missedFolderIndexes(ModelAPI_Document* theDoc = 0) const;
131
132   /// Returns Id (row) of a folder taking into consideration folders which can not be shown non empty
133   /// \param theType Type of the folder
134   /// \param theDoc a document which contains this folder
135   int folderId(std::string theType, ModelAPI_Document* theDoc = 0);
136
137   /// Removes a row from branch of tree
138   /// \param theStart - start row to update indexes
139   /// \param theSize - number of indexes in the folder
140   /// \param theParent - index of parent folder
141   void rebuildBranch(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
142
143
144   /// Returns list of folders types which can not be shown empty
145   /// \param fromRoot - root document flag
146   QStringList listOfShowNotEmptyFolders(bool fromRoot = true) const;
147
148   Config_DataModelReader myXMLReader;
149 };
150
151 #endif