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 <ModuleBase_IconFactory.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_ResultParameter.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_ResultPart.h>
17 #include <Config_FeatureMessage.h>
19 #include <Events_Loop.h>
20 #include <Events_Error.h>
25 #define ACTIVE_COLOR QColor(0,72,140)
26 #define PASSIVE_COLOR Qt::black
28 /// Returns ResultPart object if the given object is a Part feature
29 /// Otherwise returns NULL
30 ResultPartPtr getPartResult(ModelAPI_Object* theObj)
32 ModelAPI_Feature* aFeature = dynamic_cast<ModelAPI_Feature*>(theObj);
34 ResultPtr aRes = aFeature->firstResult();
35 if (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group())) {
36 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRes);
39 return ResultPartPtr();
42 /// Returns pointer on document if the given object is document object
43 ModelAPI_Document* getSubDocument(void* theObj)
45 ModelAPI_Document* aDoc = dynamic_cast<ModelAPI_Document*>((ModelAPI_Entity*)theObj);
52 // Constructor *************************************************
53 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)
55 myXMLReader.readAll();
57 Events_Loop* aLoop = Events_Loop::loop();
58 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
59 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
62 //******************************************************
63 void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
65 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
66 std::string aRootType = myXMLReader.rootType();
67 std::string aSubType = myXMLReader.subType();
68 int aNbFolders = myXMLReader.rootFoldersNumber();
69 int aNbSubFolders = myXMLReader.subFoldersNumber();
71 // Created object event *******************
72 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
73 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
74 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
75 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
77 std::set<ObjectPtr>::const_iterator aIt;
79 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
80 ObjectPtr aObject = (*aIt);
81 aObjType = aObject->groupName();
82 DocumentPtr aDoc = aObject->document();
83 if (aDoc == aRootDoc) {
84 int aRow = aRootDoc->size(aObjType) - 1;
85 if (aObjType == aRootType) {
86 insertRow(aRow + aNbFolders + 1);
88 int aFolderId = myXMLReader.rootFolderId(aObjType);
89 if (aFolderId != -1) {
90 insertRow(aRow, createIndex(aFolderId, 0, -1));
94 // Object created in sub-document
95 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
96 if (aDocRoot.isValid()) {
97 int aRow = aDoc->size(aObjType) - 1;
98 if (aObjType == aSubType) {
99 insertRow(aRow + aNbSubFolders, aDocRoot);
101 int aFolderId = myXMLReader.subFolderId(aObjType);
102 if (aFolderId != -1) {
103 insertRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
109 Events_Error::send("Problem with Data Model definition of sub-document");
114 // Deleted object event ***********************
115 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
116 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
117 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
118 DocumentPtr aDoc = aUpdMsg->document();
119 std::set<std::string> aGroups = aUpdMsg->groups();
120 std::set<std::string>::const_iterator aIt;
121 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
122 std::string aGroup = (*aIt);
123 if (aDoc == aRootDoc) { // If root objects
124 int aRow = aRootDoc->size(aGroup);
125 if (aGroup == aRootType) {
126 removeRow(aRow + aNbFolders);
128 int aFolderId = myXMLReader.rootFolderId(aGroup);
129 if (aFolderId != -1) {
130 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
131 removeRow(aRow, aFolderIndex);
139 //******************************************************
140 void XGUI_DataModel::clear()
145 //******************************************************
146 void XGUI_DataModel::rebuildDataTree()
151 //******************************************************
152 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
154 if (theIndex.internalId() < 0) // this is a folder
156 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
157 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
160 // We can not create the ObjectPtr directly because the pointer will be deleted
161 // with deletion of the ObjectPtr because its counter become to 0.
162 DocumentPtr aDoc = aObj->document();
163 std::string aType = aObj->groupName();
166 for (int i = 0; i < aDoc->size(aType); i++) {
167 aObjPtr = aDoc->object(aType, i);
168 if (aObjPtr.get() == aObj)
174 //******************************************************
175 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
177 std::string aType = theObject->groupName();
178 DocumentPtr aDoc = theObject->document();
179 int aRow = aDoc->index(theObject);
181 return QModelIndex();
183 SessionPtr aSession = ModelAPI_Session::get();
184 DocumentPtr aRootDoc = aSession->moduleDocument();
185 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
186 // The object from root document
187 aRow += myXMLReader.rootFoldersNumber();
188 } else if (myXMLReader.subType() == aType) {
189 // The object from sub document
190 aRow += myXMLReader.subFoldersNumber();
192 return createIndex(aRow, 0, theObject.get());
195 //******************************************************
196 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
198 SessionPtr aSession = ModelAPI_Session::get();
199 DocumentPtr aRootDoc = aSession->moduleDocument();
200 int aNbFolders = myXMLReader.rootFoldersNumber();
201 int theIndexRow = theIndex.row();
203 if ((theIndex.column() == 1) ) {
204 //if (theIndexRow >= aNbFolders) {
205 // if (theRole == Qt::DecorationRole) {
206 // return QIcon(":pictures/arrow.png");
212 int aParentId = theIndex.internalId();
213 if (aParentId == -1) { // root folders
215 case Qt::DisplayRole:
216 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
217 QString(" (%1)").arg(rowCount(theIndex));
218 case Qt::DecorationRole:
219 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
220 case Qt::ForegroundRole:
221 if (aRootDoc->isActive())
222 return QBrush(ACTIVE_COLOR);
224 return QBrush(PASSIVE_COLOR);
227 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
229 if (theRole == Qt::ForegroundRole) {
230 bool aIsActive = false;
232 aIsActive = aSubDoc->isActive();
234 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
235 aIsActive = aObj->document()->isActive();
238 return QBrush(ACTIVE_COLOR);
240 return QBrush(PASSIVE_COLOR);
243 if (aSubDoc) { // this is a folder of sub document
245 case Qt::DisplayRole:
246 return QString(myXMLReader.subFolderName(theIndexRow).c_str()) +
247 QString(" (%1)").arg(rowCount(theIndex));
248 case Qt::DecorationRole:
249 return QIcon(myXMLReader.subFolderIcon(theIndexRow).c_str());
252 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
254 case Qt::DisplayRole:
255 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
256 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
257 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
258 QString aVal = QString::number(aValueAttribute->value());
259 QString aTitle = QString(aObj->data()->name().c_str());
260 return aTitle + " = " + aVal;
262 return aObj->data()->name().c_str();
263 case Qt::DecorationRole:
264 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
271 //******************************************************
272 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
277 //******************************************************
278 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
280 SessionPtr aSession = ModelAPI_Session::get();
281 if (!aSession->hasModuleDocument())
283 DocumentPtr aRootDoc = aSession->moduleDocument();
285 if (!theParent.isValid()) {
286 // Return number of items in root
287 int aNbFolders = myXMLReader.rootFoldersNumber();
289 std::string aType = myXMLReader.rootType();
291 aNbItems = aRootDoc->size(aType);
292 return aNbFolders + aNbItems;
295 int aId = theParent.internalId();
297 // this is a folder under root
298 int aParentPos = theParent.row();
299 std::string aType = myXMLReader.rootFolderType(aParentPos);
300 //qDebug("### %s = %i\n", aType.c_str(), aRootDoc->size(aType));
301 return aRootDoc->size(aType);
303 // It is an object which could have children
304 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
306 // a folder of sub-document
307 std::string aType = myXMLReader.subFolderType(theParent.row());
308 return aDoc->size(aType);
310 // Check for Part feature
311 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
312 ResultPartPtr aPartRes = getPartResult(aObj);
313 if (aPartRes.get()) {
314 DocumentPtr aSubDoc = aPartRes->partDoc();
315 int aNbSubFolders = myXMLReader.subFoldersNumber();
317 std::string aSubType = myXMLReader.subType();
318 if (!aSubType.empty())
319 aNbSubItems = aSubDoc->size(aSubType);
320 return aNbSubItems + aNbSubFolders;
327 //******************************************************
328 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
333 //******************************************************
334 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
336 SessionPtr aSession = ModelAPI_Session::get();
337 DocumentPtr aRootDoc = aSession->moduleDocument();
338 int aNbFolders = myXMLReader.rootFoldersNumber();
340 if (!theParent.isValid()) {
341 if (theRow < aNbFolders) // Return first level folder index
342 return createIndex(theRow, theColumn, -1);
343 else { // return object under root index
344 std::string aType = myXMLReader.rootType();
345 int aObjId = theRow - aNbFolders;
346 if (aObjId < aRootDoc->size(aType)) {
347 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
348 QModelIndex aIndex = objectIndex(aObj);
350 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
353 return QModelIndex();
356 int aId = theParent.internalId();
357 int aParentPos = theParent.row();
358 if (aId == -1) { // return object index inside of first level of folders
359 std::string aType = myXMLReader.rootFolderType(aParentPos);
360 if (theRow < aRootDoc->size(aType)) {
361 ObjectPtr aObj = aRootDoc->object(aType, theRow);
362 QModelIndex aIndex = objectIndex(aObj);
364 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
368 // It is an object which could have children
369 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
371 // It is a folder of sub-document
372 std::string aType = myXMLReader.subFolderType(aParentPos);
373 if (theRow < aDoc->size(aType)) {
374 ObjectPtr aObj = aDoc->object(aType, theRow);
375 QModelIndex aIndex = objectIndex(aObj);
377 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
381 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
383 // Check for Part feature
384 ResultPartPtr aPartRes = getPartResult(aParentObj);
385 if (aPartRes.get()) {
386 DocumentPtr aSubDoc = aPartRes->partDoc();
387 int aNbSubFolders = myXMLReader.subFoldersNumber();
388 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
389 return createIndex(theRow, theColumn, aSubDoc.get());
391 // this is an object under sub document root
392 std::string aType = myXMLReader.subType();
393 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
394 QModelIndex aIndex = objectIndex(aObj);
396 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
402 return QModelIndex();
405 //******************************************************
406 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
408 int aId = theIndex.internalId();
409 if (aId != -1) { // The object is not a root folder
410 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
412 // It is a folder of sub-document
413 return findDocumentRootIndex(aDoc);
415 ModelAPI_Object* aObj = (ModelAPI_Object*) theIndex.internalPointer();
416 std::string aType = aObj->groupName();
417 SessionPtr aSession = ModelAPI_Session::get();
418 DocumentPtr aRootDoc = aSession->moduleDocument();
419 DocumentPtr aSubDoc = aObj->document();
420 if (aSubDoc == aRootDoc) {
421 if (aType == myXMLReader.rootType())
422 return QModelIndex();
424 // return first level of folder index
425 int aFolderId = myXMLReader.rootFolderId(aType);
426 // Items in a one row must have the same parent
427 return createIndex(aFolderId, 0, -1);
430 if (aType == myXMLReader.subType())
431 return findDocumentRootIndex(aSubDoc.get());
433 // return first level of folder index
434 int aFolderId = myXMLReader.subFolderId(aType);
435 // Items in a one row must have the same parent
436 return createIndex(aFolderId, 0, aSubDoc.get());
440 return QModelIndex();
443 //******************************************************
444 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
446 int aNbFolders = myXMLReader.rootFoldersNumber();
447 if (!theParent.isValid() && aNbFolders)
449 if (theParent.internalId() == -1) {
450 std::string aType = myXMLReader.rootFolderType(theParent.row());
451 if (!aType.empty()) {
452 SessionPtr aSession = ModelAPI_Session::get();
453 DocumentPtr aRootDoc = aSession->moduleDocument();
454 return aRootDoc->size(aType) > 0;
457 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
459 // a folder of sub-document
460 std::string aType = myXMLReader.subFolderType(theParent.row());
461 return aDoc->size(aType) > 0;
463 // Check that it could be an object with children
464 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
466 // Check for Part feature
467 ResultPartPtr aPartRes = getPartResult(aObj);
475 //******************************************************
476 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
478 beginInsertRows(theParent, theRow, theRow + theCount - 1);
484 //******************************************************
485 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
487 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
492 //******************************************************
493 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
495 Qt::ItemFlags aFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
496 if (theIndex.internalId() > -1) {
497 aFlags |= Qt::ItemIsEditable;
502 //******************************************************
503 QModelIndex XGUI_DataModel::findDocumentRootIndex(ModelAPI_Document* theDoc) const
505 SessionPtr aSession = ModelAPI_Session::get();
506 DocumentPtr aRootDoc = aSession->moduleDocument();
507 if (myXMLReader.isAttachToResult()) { // If document is attached to result
508 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
510 ResultPartPtr aPartRes;
511 for (int i = 0; i < aNb; i++) {
512 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
513 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
514 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
516 if (myXMLReader.rootType() == ModelAPI_Feature::group())
517 aRow += myXMLReader.rootFoldersNumber();
518 return createIndex(aRow, 0, aObj.get());
521 } else { // If document is attached to feature
522 int aNb = aRootDoc->size(ModelAPI_Feature::group());
524 ResultPartPtr aPartRes;
525 for (int i = 0; i < aNb; i++) {
526 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
527 aPartRes = getPartResult(aObj.get());
528 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
530 if (myXMLReader.rootType() == ModelAPI_Feature::group())
531 aRow += myXMLReader.rootFoldersNumber();
532 return createIndex(aRow, 0, aObj.get());
536 return QModelIndex();
539 //******************************************************
540 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
542 SessionPtr aSession = ModelAPI_Session::get();
543 DocumentPtr aRootDoc = aSession->moduleDocument();
544 if (theDoc == aRootDoc)
545 return QModelIndex();
547 return findDocumentRootIndex(theDoc.get());