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;
34 /**\class XGUI_DataModel
36 * \brief This is a data model for Object Browser (QTreeView).
37 * It uses XML file for definition of data tree.
38 * Some tips about organization of the model:
39 * - Non Valid Index - root index
40 * - An index with internal Id == -1 is a folder of root document
41 * - An index which contains internal pointer as ModelAPI_Object its the object
42 * - An index which contains internal pointer as ModelAPI_Document is
43 * a folder which belongs to this document
45 class XGUI_EXPORT XGUI_DataModel : public QAbstractItemModel, public Events_Listener
50 /// \param theParent a parent object
51 XGUI_DataModel(QObject* theParent);
54 virtual ~XGUI_DataModel();
56 /// Event Listener method
57 /// \param theMessage an event message
58 virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
60 //! Returns an object by the given Model index.
61 //! Returns 0 if the given index is not index of an object
62 virtual ObjectPtr object(const QModelIndex& theIndex) const;
64 //! Returns index of the object
65 //! \param theObject object to find
66 virtual QModelIndex objectIndex(const ObjectPtr theObject) const;
68 //! Clear internal data
72 virtual void rebuildDataTree();
75 /// Returns the data stored under the given role for the item referred to by the index.
76 /// \param theIndex a model index
77 /// \param theRole a data role (see Qt::ItemDataRole)
78 virtual QVariant data(const QModelIndex& theIndex, int theRole) const;
80 /// Returns the data for the given role and section in the header with the specified orientation.
81 /// \param theSection a section
82 /// \param theOrient an orientation
83 /// \param theRole a data role (see Qt::ItemDataRole)
84 virtual QVariant headerData(int theSection, Qt::Orientation theOrient, int theRole =
85 Qt::DisplayRole) const;
87 /// Returns the number of rows under the given parent. When the parent is valid it means that
88 /// rowCount is returning the number of children of parent.
89 /// \param theParent a parent model index
90 virtual int rowCount(const QModelIndex& theParent = QModelIndex()) const;
92 /// Returns the number of columns for the children of the given parent.
93 /// It has a one column
94 /// \param theParent a parent model index
95 virtual int columnCount(const QModelIndex& theParent = QModelIndex()) const;
97 /// Returns the index of the item in the model specified by the given row,
98 /// column and parent index.
99 /// \param theRow a row
100 /// \param theColumn a column
101 /// \param theParent a parent model index
102 virtual QModelIndex index(int theRow, int theColumn, const QModelIndex &theParent =
103 QModelIndex()) const;
105 /// Returns the parent of the model item with the given index.
106 /// If the item has no parent, an invalid QModelIndex is returned.
107 /// \param theIndex a model index
108 virtual QModelIndex parent(const QModelIndex& theIndex) const;
110 /// Returns true if parent has any children; otherwise returns false.
111 /// \param theParent a parent model index
112 virtual bool hasChildren(const QModelIndex& theParent = QModelIndex()) const;
114 /// Inserts count rows into the model before the given row.
115 /// Items in the new row will be children of the item represented by the parent model index.
116 /// \param theRow a start row
117 /// \param theCount a nember of rows to insert
118 /// \param theParent a parent model index
119 virtual bool insertRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
121 /// Removes count rows starting with the given row under parent parent from the model.
122 /// \param theRow a start row
123 /// \param theCount a nember of rows to remove
124 /// \param theParent a parent model index
125 virtual bool removeRows(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
127 /// Returns the item flags for the given index.
128 /// \param theIndex a model index
129 virtual Qt::ItemFlags flags(const QModelIndex& theIndex) const;
131 /// Returns an index which is root of the given document
132 /// \param theDoc a document
133 QModelIndex documentRootIndex(DocumentPtr theDoc) const;
135 /// Returns last history object index
136 virtual QModelIndex lastHistoryIndex() const;
138 /// Initialises XML data model reader. It must be initialised before DataModel using.
139 void setXMLReader(Config_DataModelReader* theReader) { myXMLReader = theReader; }
141 /// Do not processing anymore events of model loop
142 //bool blockEventsProcessing(const bool theState);
145 /// Signal about tree had been rebuilt
149 /// Find a root index which contains objects of the given document
150 /// \param theDoc the document object
151 QModelIndex findDocumentRootIndex(const ModelAPI_Document* theDoc) const;
153 /// Returns number of folders in document.
154 /// Considered folders which has to be shown only if they are not empty.
155 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
156 int foldersCount(ModelAPI_Document* theDoc = 0) const;
158 /// Retrurns indexes of folders which can not be shown because they are empty
159 /// \param theDoc document which has to be checked. If 0 then Root document will be considered
160 QIntList missedFolderIndexes(ModelAPI_Document* theDoc = 0) const;
162 /// Returns Id (row) of a folder taking into consideration
163 /// folders which can not be shown non empty
164 /// \param theType Type of the folder
165 /// \param theDoc a document which contains this folder
166 int folderId(std::string theType, ModelAPI_Document* theDoc = 0) const;
168 /// Removes a row from branch of tree
169 /// \param theStart - start row to update indexes
170 /// \param theSize - number of indexes in the folder
171 /// \param theParent - index of parent folder
172 void rebuildBranch(int theRow, int theCount, const QModelIndex& theParent = QModelIndex());
175 /// Returns list of folders types which can not be shown empty
176 /// \param fromRoot - root document flag
177 QStringList listOfShowNotEmptyFolders(bool fromRoot = true) const;
179 void addShownFolder(DocumentPtr theDoc, QString theFolder)
181 if (!myShownFolders.contains(theDoc)) {
182 myShownFolders[theDoc] = QStringList();
184 myShownFolders[theDoc].append(theFolder);
187 void removeShownFolder(DocumentPtr theDoc, QString theFolder)
189 if (myShownFolders.contains(theDoc)) {
190 myShownFolders[theDoc].removeAll(theFolder);
191 if (myShownFolders[theDoc].isEmpty())
192 myShownFolders.remove(theDoc);
196 bool hasShownFolder(DocumentPtr theDoc, QString theFolder) const
198 if (myShownFolders.contains(theDoc))
199 return myShownFolders[theDoc].contains(theFolder);
203 Config_DataModelReader* myXMLReader;
205 QMap<DocumentPtr, QStringList> myShownFolders;
206 //bool myIsEventsProcessingBlocked;