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