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