1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_DataModel.h
4 // Created: 21 Jul 2015
5 // Author: Vitaly SMETANNIKOV
8 #ifndef XGUI_DataModel_H
9 #define XGUI_DataModel_H
12 #include <ModuleBase_Definitions.h>
13 #include <ModelAPI_Object.h>
14 #include <ModelAPI_Document.h>
15 #include <Events_Listener.h>
17 #include <QAbstractItemModel>
19 class Config_DataModelReader;
21 /**\class XGUI_DataModel
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
32 class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener
37 /// \param theParent a parent object
38 XGUI_DataModel(QObject* theParent);
41 virtual ~XGUI_DataModel();
43 /// Event Listener method
44 /// \param theMessage an event message
45 virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
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;
51 //! Returns index of the object
52 //! \param theObject object to find
53 virtual QModelIndex objectIndex(const ObjectPtr theObject) const;
55 //! Clear internal data
59 virtual void rebuildDataTree();
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;
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;
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;
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;
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 =
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;
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;
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());
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());
114 /// Returns the item flags for the given index.
115 /// \param theIndex a model index
116 virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
118 /// Returns an index which is root of the given document
119 /// \param theDoc a document
120 QModelIndex documentRootIndex(DocumentPtr theDoc) const;
122 /// Returns last history object index
123 virtual QModelIndex lastHistoryIndex() const;
125 /// Initialises XML data model reader. It must be initialised before DataModel using.
126 void setXMLReader(Config_DataModelReader* theReader) { myXMLReader = theReader; }
128 /// Do not processing anymore events of model loop
129 //bool blockEventsProcessing(const bool theState);
132 /// Signal about tree had been rebuilt
136 /// Find a root index which contains objects of the given document
137 /// \param theDoc the document object
138 QModelIndex findDocumentRootIndex(const ModelAPI_Document* theDoc) const;
140 /// Returns number of folders in document.
141 /// Considered folders which has to be shown only if they are not empty.
142 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
143 int foldersCount(ModelAPI_Document* theDoc = 0) const;
145 /// Retrurns indexes of folders which can not be shown because they are empty
146 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
147 QIntList missedFolderIndexes(ModelAPI_Document* theDoc = 0) const;
149 /// Returns Id (row) of a folder taking into consideration
150 /// folders which can not be shown non empty
151 /// \param theType Type of the folder
152 /// \param theDoc a document which contains this folder
153 int folderId(std::string theType, ModelAPI_Document* theDoc = 0) const;
155 /// Removes a row from branch of tree
156 /// \param theStart - start row to update indexes
157 /// \param theSize - number of indexes in the folder
158 /// \param theParent - index of parent folder
159 void rebuildBranch(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
162 /// Returns list of folders types which can not be shown empty
163 /// \param fromRoot - root document flag
164 QStringList listOfShowNotEmptyFolders(bool fromRoot = true) const;
166 Config_DataModelReader* myXMLReader;
168 //bool myIsEventsProcessingBlocked;