1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: ModuleBase_IDocumentDataModel.cpp
4 // Created: 28 Apr 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "XGUI_DataModel.h"
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Events.h>
13 #include <Config_FeatureMessage.h>
15 #include <Events_Loop.h>
19 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)
21 myXMLReader.readAll();
23 Events_Loop* aLoop = Events_Loop::loop();
24 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
25 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
26 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
27 aLoop->registerListener(this, Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
30 //******************************************************
31 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
33 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
34 std::string aRootType = myXMLReader.rootType();
35 int aNbFolders = myXMLReader.rootFoldersNumber();
37 // Created object event *******************
38 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
39 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
40 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
41 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
43 std::set<ObjectPtr>::const_iterator aIt;
45 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
46 ObjectPtr aObject = (*aIt);
47 aObjType = aObject->groupName();
48 DocumentPtr aDoc = aObject->document();
49 if (aDoc == aRootDoc) {
50 int aRow = aRootDoc->size(aObjType) - 1;
52 if (aObjType == aRootType) {
53 insertRow(aRow + aNbFolders);
55 int aFolderId = myXMLReader.rootFolderId(aObjType);
56 if (aFolderId != -1) {
57 insertRow(aRow, createIndex(aFolderId, 0, -1));
66 //******************************************************
67 void XGUI_DataModel::clear()
72 //******************************************************
73 void XGUI_DataModel::rebuildDataTree()
78 //******************************************************
79 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
84 //******************************************************
85 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
87 //std::string aType = theObject->groupName();
88 //DocumentPtr aDoc = theObject->document();
89 //int aRow = aDoc->index(theObject);
92 //return createIndex(aRow, 0, theObject.get());
95 //******************************************************
96 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
98 SessionPtr aSession = ModelAPI_Session::get();
99 DocumentPtr aRootDoc = aSession->moduleDocument();
100 int aNbFolders = myXMLReader.rootFoldersNumber();
101 int theIndexRow = theIndex.row();
103 if ((theIndex.column() == 1) ) {
104 //if (theIndexRow >= aNbFolders) {
105 // if (theRole == Qt::DecorationRole) {
106 // return QIcon(":pictures/arrow.png");
112 int aParentRow = theIndex.internalId();
113 if (aParentRow == -1) { // First level of items
114 if (theIndexRow < aNbFolders) { // A folder
116 case Qt::DisplayRole:
117 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
118 QString(" (%1)").arg(rowCount(theIndex));
119 case Qt::DecorationRole:
120 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
123 ObjectPtr aObj = aRootDoc->object(myXMLReader.rootType(), theIndexRow - aNbFolders);
125 case Qt::DisplayRole:
126 return aObj->data()->name().c_str();
127 //case Qt::DecorationRole:
128 // return featureIcon(aFeature);
131 } else { // Content of folders
132 if (aParentRow < aNbFolders) {
133 std::string aType = myXMLReader.rootFolderType(aParentRow);
134 ObjectPtr aObj = aRootDoc->object(aType, theIndexRow);
136 case Qt::DisplayRole:
137 return aObj->data()->name().c_str();
144 //******************************************************
145 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
150 //******************************************************
151 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
153 SessionPtr aSession = ModelAPI_Session::get();
154 if (!aSession->hasModuleDocument())
156 DocumentPtr aRootDoc = aSession->moduleDocument();
157 int aNbFolders = myXMLReader.rootFoldersNumber();
160 std::string aType = myXMLReader.rootType();
162 aNbItems = aRootDoc->size(aType);
164 if (!theParent.isValid())
165 return aNbFolders + aNbItems;
167 int aParentPos = theParent.row();
168 if (aParentPos < aNbFolders) {
169 // This is number of items under folder
170 aType = myXMLReader.rootFolderType(aParentPos);
171 return aRootDoc->size(aType);
176 //******************************************************
177 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
182 //******************************************************
183 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
185 if (!theParent.isValid())
186 return createIndex(theRow, theColumn, -1);
188 return createIndex(theRow, theColumn, theParent.row());
191 //******************************************************
192 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
194 if (theIndex.isValid() && (theIndex.internalId() != -1)) {
195 return createIndex(theIndex.internalId(), theIndex.column(), -1);
197 return QModelIndex();
200 //******************************************************
201 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
203 int aNbFolders = myXMLReader.rootFoldersNumber();
204 if (!theParent.isValid() && aNbFolders)
206 if (theParent.internalId() == -1) {
207 if (theParent.row() < aNbFolders) {
208 std::string aType = myXMLReader.rootFolderType(theParent.row());
209 if (!aType.empty()) {
210 SessionPtr aSession = ModelAPI_Session::get();
211 DocumentPtr aRootDoc = aSession->moduleDocument();
212 return aRootDoc->size(aType) > 0;
219 //******************************************************
220 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
222 beginInsertRows(theParent, theRow, theRow + theCount - 1);
228 //******************************************************
229 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
231 beginRemoveRows(theParent, theRow, theRow + theCount - 1);