1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef XGUI_DataModel_H
22 #define XGUI_DataModel_H
25 #include <ModuleBase_Definitions.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Document.h>
28 #include <Events_Listener.h>
30 #include <QAbstractItemModel>
32 class Config_DataModelReader;
35 /**\class XGUI_DataModel
37 * \brief This is a data model for Object Browser (QTreeView).
38 * It uses XML file for definition of data tree.
39 * Some tips about organization of the model:
40 * - Non Valid Index - root index
41 * - An index with internal Id == -1 is a folder of root document
42 * - An index which contains internal pointer as ModelAPI_Object its the object
43 * - An index which contains internal pointer as ModelAPI_Document is
44 * a folder which belongs to this document
46 class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener
51 /// \param theParent a parent object
52 XGUI_DataModel(QObject* theParent);
55 virtual ~XGUI_DataModel();
57 /// Event Listener method
58 /// \param theMessage an event message
59 virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
61 //! Returns an object by the given Model index.
62 //! Returns 0 if the given index is not index of an object
63 virtual ObjectPtr object(const QModelIndex& theIndex) const;
65 //! Returns index of the object
66 //! \param theObject object to find
67 virtual QModelIndex objectIndex(const ObjectPtr theObject, int theColumn = 1) const;
69 //! Clear internal data
73 virtual void rebuildDataTree();
76 /// Returns the data stored under the given role for the item referred to by the index.
77 /// \param theIndex a model index
78 /// \param theRole a data role (see Qt::ItemDataRole)
79 virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
81 /// Returns the data for the given role and section in the header with the specified orientation.
82 /// \param theSection a section
83 /// \param theOrient an orientation
84 /// \param theRole a data role (see Qt::ItemDataRole)
85 virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
86 Qt::DisplayRole) const;
88 /// Returns the number of rows under the given parent. When the parent is valid it means that
89 /// rowCount is returning the number of children of parent.
90 /// \param theParent a parent model index
91 virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
93 /// Returns the number of columns for the children of the given parent.
94 /// It has a one column
95 /// \param theParent a parent model index
96 virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
98 /// Returns the index of the item in the model specified by the given row,
99 /// column and parent index.
100 /// \param theRow a row
101 /// \param theColumn a column
102 /// \param theParent a parent model index
103 virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
104 QModelIndex()) const;
106 /// Returns the parent of the model item with the given index.
107 /// If the item has no parent, an invalid QModelIndex is returned.
108 /// \param theIndex a model index
109 virtual QModelIndex parent(const QModelIndex& theIndex) const;
111 /// Returns true if parent has any children; otherwise returns false.
112 /// \param theParent a parent model index
113 virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
115 /// Inserts count rows into the model before the given row.
116 /// Items in the new row will be children of the item represented by the parent model index.
117 /// \param theRow a start row
118 /// \param theCount a nember of rows to insert
119 /// \param theParent a parent model index
120 virtual bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
122 /// Removes count rows starting with the given row under parent parent from the model.
123 /// \param theRow a start row
124 /// \param theCount a nember of rows to remove
125 /// \param theParent a parent model index
126 virtual bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
128 /// Returns the item flags for the given index.
129 /// \param theIndex a model index
130 virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
132 /// Returns an index which is root of the given document
133 /// \param theDoc a document
134 QModelIndex documentRootIndex(DocumentPtr theDoc, int theColumn = 1) const;
136 /// Returns last history object index
137 virtual QModelIndex lastHistoryIndex() const;
139 /// Initialises XML data model reader. It must be initialised before DataModel using.
140 void setXMLReader(Config_DataModelReader* theReader) { myXMLReader = theReader; }
142 /// Do not processing anymore events of model loop
143 //bool blockEventsProcessing(const bool theState);
146 /// Signal about tree had been rebuilt
150 enum VisibilityState {
156 /// Find a root index which contains objects of the given document
157 /// \param theDoc the document object
158 QModelIndex findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn = 1) const;
160 /// Returns number of folders in document.
161 /// Considered folders which has to be shown only if they are not empty.
162 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
163 int foldersCount(ModelAPI_Document* theDoc = 0) const;
165 /// Retrurns indexes of folders which can not be shown because they are empty
166 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
167 QIntList missedFolderIndexes(ModelAPI_Document* theDoc = 0) const;
169 /// Returns Id (row) of a folder taking into consideration
170 /// folders which can not be shown non empty
171 /// \param theType Type of the folder
172 /// \param theDoc a document which contains this folder
173 int folderId(std::string theType, ModelAPI_Document* theDoc = 0) const;
175 /// Removes a row from branch of tree
176 /// \param theStart - start row to update indexes
177 /// \param theSize - number of indexes in the folder
178 /// \param theParent - index of parent folder
179 void rebuildBranch(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
181 /// Returns list of folders types which can not be shown empty
182 /// \param fromRoot - root document flag
183 QStringList listOfShowNotEmptyFolders(bool fromRoot = true) const;
185 VisibilityState getVisibilityState(const QModelIndex& theIndex) const;
187 void addShownFolder(DocumentPtr theDoc, QString theFolder)
189 if (!myShownFolders.contains(theDoc)) {
190 myShownFolders[theDoc] = QStringList();
192 myShownFolders[theDoc].append(theFolder);
195 void removeShownFolder(DocumentPtr theDoc, QString theFolder)
197 if (myShownFolders.contains(theDoc)) {
198 myShownFolders[theDoc].removeAll(theFolder);
199 if (myShownFolders[theDoc].isEmpty())
200 myShownFolders.remove(theDoc);
204 bool hasShownFolder(DocumentPtr theDoc, QString theFolder) const
206 if (myShownFolders.contains(theDoc))
207 return myShownFolders[theDoc].contains(theFolder);
211 Config_DataModelReader* myXMLReader;
213 XGUI_Workshop* myWorkshop;
214 QMap<DocumentPtr, QStringList> myShownFolders;
215 //bool myIsEventsProcessingBlocked;