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 a folder which belongs to this document
31 class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener
36 /// \param theParent a parent object
37 XGUI_DataModel(QObject* theParent);
40 virtual ~XGUI_DataModel();
42 /// Event Listener method
43 /// \param theMessage an event message
44 virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
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;
50 //! Returns index of the object
51 //! \param theObject object to find
52 virtual QModelIndex objectIndex(const ObjectPtr theObject) const;
54 //! Clear internal data
58 virtual void rebuildDataTree();
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;
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;
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;
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;
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 =
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;
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;
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());
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());
112 /// Returns the item flags for the given index.
113 /// \param theIndex a model index
114 virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
116 /// Returns an index which is root of the given document
117 /// \param theDoc a document
118 QModelIndex documentRootIndex(DocumentPtr theDoc) const;
120 /// Returns last history object index
121 virtual QModelIndex lastHistoryIndex() const;
123 /// Initialises XML data model reader. It must be initialised before DataModel using.
124 void setXMLReader(Config_DataModelReader* theReader) { myXMLReader = theReader; }
127 /// Signal about tree had been rebuilt
131 /// Find a root index which contains objects of the given document
132 /// \param theDoc the document object
133 QModelIndex findDocumentRootIndex(const ModelAPI_Document* theDoc) const;
135 /// Returns number of folders in document. Considered folders which has to be shown only if they are not empty.
136 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
137 int foldersCount(ModelAPI_Document* theDoc = 0) const;
139 /// Retrurns indexes of folders which can not be shown because they are empty
140 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
141 QIntList missedFolderIndexes(ModelAPI_Document* theDoc = 0) const;
143 /// Returns Id (row) of a folder taking into consideration folders which can not be shown non empty
144 /// \param theType Type of the folder
145 /// \param theDoc a document which contains this folder
146 int folderId(std::string theType, ModelAPI_Document* theDoc = 0);
148 /// Removes a row from branch of tree
149 /// \param theStart - start row to update indexes
150 /// \param theSize - number of indexes in the folder
151 /// \param theParent - index of parent folder
152 void rebuildBranch(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
155 /// Returns list of folders types which can not be shown empty
156 /// \param fromRoot - root document flag
157 QStringList listOfShowNotEmptyFolders(bool fromRoot = true) const;
159 Config_DataModelReader* myXMLReader;