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 = foldersCount();
70 // Created object event *******************
71 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
72 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
73 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
74 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
76 std::set<ObjectPtr>::const_iterator aIt;
78 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
79 ObjectPtr aObject = (*aIt);
80 aObjType = aObject->groupName();
81 DocumentPtr aDoc = aObject->document();
82 if (aDoc == aRootDoc) {
83 // Check that new folders could appear
84 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
85 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
86 if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
87 // Appears first object in folder which can not be shown empty
88 insertRow(myXMLReader.rootFolderId(aObjType));
91 int aRow = aRootDoc->size(aObjType) - 1;
92 if (aObjType == aRootType) {
93 insertRow(aRow + aNbFolders + 1);
95 int aFolderId = myXMLReader.rootFolderId(aObjType);
96 if (aFolderId != -1) {
97 insertRow(aRow, createIndex(aFolderId, 0, -1));
101 // Object created in sub-document
102 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
103 if (aDocRoot.isValid()) {
104 // Check that new folders could appear
105 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
106 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
107 if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
108 // Appears first object in folder which can not be shown empty
109 insertRow(myXMLReader.subFolderId(aObjType), aDocRoot);
111 int aRow = aDoc->size(aObjType) - 1;
112 int aNbSubFolders = foldersCount(aDoc.get());
113 if (aObjType == aSubType) {
114 // List of objects under document root
115 insertRow(aRow + aNbSubFolders, aDocRoot);
117 // List of objects under a folder
118 int aFolderId = myXMLReader.subFolderId(aObjType);
119 if (aFolderId != -1) {
120 insertRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
126 Events_Error::send("Problem with Data Model definition of sub-document");
131 // Deleted object event ***********************
132 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
133 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
134 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
135 DocumentPtr aDoc = aUpdMsg->document();
136 std::set<std::string> aGroups = aUpdMsg->groups();
137 std::set<std::string>::const_iterator aIt;
138 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
139 std::string aGroup = (*aIt);
140 if (aDoc == aRootDoc) { // If root objects
141 int aRow = aRootDoc->size(aGroup);
142 if (aGroup == aRootType) {
143 removeRow(aRow + aNbFolders);
145 int aFolderId = myXMLReader.rootFolderId(aGroup);
146 if (aFolderId != -1) {
147 QModelIndex aFolderIndex = createIndex(aFolderId, 0, -1);
148 removeRow(aRow, aFolderIndex);
151 // Check that some folders could erased
152 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
153 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
154 if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0))
155 // Appears first object in folder which can not be shown empty
156 removeRow(myXMLReader.rootFolderId(aGroup));
159 // Remove row for sub-document
160 QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
161 if (aDocRoot.isValid()) {
162 int aRow = aDoc->size(aGroup);
163 int aNbSubFolders = foldersCount(aDoc.get());
164 if (aGroup == aSubType) {
165 // List of objects under document root
166 removeRow(aRow + aNbSubFolders, aDocRoot);
168 // List of objects under a folder
169 int aFolderId = myXMLReader.subFolderId(aGroup);
170 if (aFolderId != -1) {
171 removeRow(aRow, createIndex(aFolderId, 0, aDoc.get()));
174 // Check that some folders could disappear
175 QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
176 foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
177 if ((aNotEmptyFolder.toStdString() == aGroup) && (aDoc->size(aGroup) == 1))
178 // Appears first object in folder which can not be shown empty
179 removeRow(myXMLReader.subFolderId(aGroup), aDocRoot);
184 Events_Error::send("Problem with Data Model definition of sub-document");
192 //******************************************************
193 void XGUI_DataModel::clear()
198 //******************************************************
199 void XGUI_DataModel::rebuildDataTree()
204 //******************************************************
205 ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
207 if (theIndex.internalId() < 0) // this is a folder
209 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
210 if (getSubDocument(aObj)) // the selected index is a folder of sub-document
213 // We can not create the ObjectPtr directly because the pointer will be deleted
214 // with deletion of the ObjectPtr because its counter become to 0.
215 DocumentPtr aDoc = aObj->document();
216 std::string aType = aObj->groupName();
219 for (int i = 0; i < aDoc->size(aType); i++) {
220 aObjPtr = aDoc->object(aType, i);
221 if (aObjPtr.get() == aObj)
227 //******************************************************
228 QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
230 std::string aType = theObject->groupName();
231 DocumentPtr aDoc = theObject->document();
232 int aRow = aDoc->index(theObject);
234 return QModelIndex();
236 SessionPtr aSession = ModelAPI_Session::get();
237 DocumentPtr aRootDoc = aSession->moduleDocument();
238 if (aDoc == aRootDoc && myXMLReader.rootType() == aType) {
239 // The object from root document
240 aRow += foldersCount();
241 } else if (myXMLReader.subType() == aType) {
242 // The object from sub document
243 aRow += foldersCount(aDoc.get());
245 return createIndex(aRow, 0, theObject.get());
248 //******************************************************
249 QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
251 SessionPtr aSession = ModelAPI_Session::get();
252 DocumentPtr aRootDoc = aSession->moduleDocument();
253 int aNbFolders = foldersCount();
254 int theIndexRow = theIndex.row();
256 if ((theIndex.column() == 1) ) {
257 //if (theIndexRow >= aNbFolders) {
258 // if (theRole == Qt::DecorationRole) {
259 // return QIcon(":pictures/arrow.png");
265 int aParentId = theIndex.internalId();
266 if (aParentId == -1) { // root folders
268 case Qt::DisplayRole:
269 return QString(myXMLReader.rootFolderName(theIndexRow).c_str()) +
270 QString(" (%1)").arg(rowCount(theIndex));
271 case Qt::DecorationRole:
272 return QIcon(myXMLReader.rootFolderIcon(theIndexRow).c_str());
273 case Qt::ForegroundRole:
274 if (aRootDoc->isActive())
275 return QBrush(ACTIVE_COLOR);
277 return QBrush(PASSIVE_COLOR);
280 ModelAPI_Document* aSubDoc = getSubDocument(theIndex.internalPointer());
282 if (theRole == Qt::ForegroundRole) {
283 bool aIsActive = false;
285 aIsActive = aSubDoc->isActive();
287 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
288 aIsActive = aObj->document()->isActive();
291 return QBrush(ACTIVE_COLOR);
293 return QBrush(PASSIVE_COLOR);
296 if (aSubDoc) { // this is a folder of sub document
298 case Qt::DisplayRole:
299 return QString(myXMLReader.subFolderName(theIndexRow).c_str()) +
300 QString(" (%1)").arg(rowCount(theIndex));
301 case Qt::DecorationRole:
302 return QIcon(myXMLReader.subFolderIcon(theIndexRow).c_str());
305 ModelAPI_Object* aObj = (ModelAPI_Object*)theIndex.internalPointer();
307 case Qt::DisplayRole:
308 if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
309 ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
310 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
311 QString aVal = QString::number(aValueAttribute->value());
312 QString aTitle = QString(aObj->data()->name().c_str());
313 return aTitle + " = " + aVal;
315 return aObj->data()->name().c_str();
316 case Qt::DecorationRole:
317 return ModuleBase_IconFactory::get()->getIcon(object(theIndex));
324 //******************************************************
325 QVariant XGUI_DataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
330 //******************************************************
331 int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
333 SessionPtr aSession = ModelAPI_Session::get();
334 if (!aSession->hasModuleDocument())
336 DocumentPtr aRootDoc = aSession->moduleDocument();
338 if (!theParent.isValid()) {
339 // Return number of items in root
340 int aNbFolders = foldersCount();
342 std::string aType = myXMLReader.rootType();
344 aNbItems = aRootDoc->size(aType);
345 return aNbFolders + aNbItems;
348 int aId = theParent.internalId();
350 // this is a folder under root
351 int aParentPos = theParent.row();
352 std::string aType = myXMLReader.rootFolderType(aParentPos);
353 //qDebug("### %s = %i\n", aType.c_str(), aRootDoc->size(aType));
354 return aRootDoc->size(aType);
356 // It is an object which could have children
357 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
359 // a folder of sub-document
360 std::string aType = myXMLReader.subFolderType(theParent.row());
361 return aDoc->size(aType);
363 // Check for Part feature
364 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
365 ResultPartPtr aPartRes = getPartResult(aObj);
366 if (aPartRes.get()) {
367 DocumentPtr aSubDoc = aPartRes->partDoc();
368 int aNbSubFolders = foldersCount(aSubDoc.get());
370 std::string aSubType = myXMLReader.subType();
371 if (!aSubType.empty())
372 aNbSubItems = aSubDoc->size(aSubType);
373 return aNbSubItems + aNbSubFolders;
380 //******************************************************
381 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
386 //******************************************************
387 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
389 SessionPtr aSession = ModelAPI_Session::get();
390 DocumentPtr aRootDoc = aSession->moduleDocument();
391 int aNbFolders = foldersCount();
393 if (!theParent.isValid()) {
394 if (theRow < aNbFolders) // Return first level folder index
395 return createIndex(theRow, theColumn, -1);
396 else { // return object under root index
397 std::string aType = myXMLReader.rootType();
398 int aObjId = theRow - aNbFolders;
399 if (aObjId < aRootDoc->size(aType)) {
400 ObjectPtr aObj = aRootDoc->object(aType, aObjId);
401 QModelIndex aIndex = objectIndex(aObj);
403 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
406 return QModelIndex();
409 int aId = theParent.internalId();
410 int aParentPos = theParent.row();
411 if (aId == -1) { // return object index inside of first level of folders
412 std::string aType = myXMLReader.rootFolderType(aParentPos);
413 if (theRow < aRootDoc->size(aType)) {
414 ObjectPtr aObj = aRootDoc->object(aType, theRow);
415 QModelIndex aIndex = objectIndex(aObj);
417 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
421 // It is an object which could have children
422 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
424 // It is a folder of sub-document
425 std::string aType = myXMLReader.subFolderType(aParentPos);
426 if (theRow < aDoc->size(aType)) {
427 ObjectPtr aObj = aDoc->object(aType, theRow);
428 QModelIndex aIndex = objectIndex(aObj);
430 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
434 ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
436 // Check for Part feature
437 ResultPartPtr aPartRes = getPartResult(aParentObj);
438 if (aPartRes.get()) {
439 DocumentPtr aSubDoc = aPartRes->partDoc();
440 int aNbSubFolders = foldersCount(aSubDoc.get());
441 if (theRow < aNbSubFolders) { // Create a Folder of sub-document
442 return createIndex(theRow, theColumn, aSubDoc.get());
444 // this is an object under sub document root
445 std::string aType = myXMLReader.subType();
446 ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
447 QModelIndex aIndex = objectIndex(aObj);
449 return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
455 return QModelIndex();
458 //******************************************************
459 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
461 int aId = theIndex.internalId();
462 if (aId != -1) { // The object is not a root folder
463 ModelAPI_Document* aDoc = getSubDocument(theIndex.internalPointer());
465 // It is a folder of sub-document
466 return findDocumentRootIndex(aDoc);
468 ModelAPI_Object* aObj = (ModelAPI_Object*) theIndex.internalPointer();
469 std::string aType = aObj->groupName();
470 SessionPtr aSession = ModelAPI_Session::get();
471 DocumentPtr aRootDoc = aSession->moduleDocument();
472 DocumentPtr aSubDoc = aObj->document();
473 if (aSubDoc == aRootDoc) {
474 if (aType == myXMLReader.rootType())
475 return QModelIndex();
477 // return first level of folder index
478 int aFolderId = myXMLReader.rootFolderId(aType);
479 // Items in a one row must have the same parent
480 return createIndex(aFolderId, 0, -1);
483 if (aType == myXMLReader.subType())
484 return findDocumentRootIndex(aSubDoc.get());
486 // return first level of folder index
487 int aFolderId = myXMLReader.subFolderId(aType);
488 // Items in a one row must have the same parent
489 return createIndex(aFolderId, 0, aSubDoc.get());
493 return QModelIndex();
496 //******************************************************
497 bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
499 if (!theParent.isValid()) {
500 int aNbFolders = foldersCount();
503 SessionPtr aSession = ModelAPI_Session::get();
504 DocumentPtr aRootDoc = aSession->moduleDocument();
505 return aRootDoc->size(myXMLReader.rootType()) > 0;
507 if (theParent.internalId() == -1) {
508 std::string aType = myXMLReader.rootFolderType(theParent.row());
509 if (!aType.empty()) {
510 SessionPtr aSession = ModelAPI_Session::get();
511 DocumentPtr aRootDoc = aSession->moduleDocument();
512 return aRootDoc->size(aType) > 0;
515 ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
517 // a folder of sub-document
518 std::string aType = myXMLReader.subFolderType(theParent.row());
519 return aDoc->size(aType) > 0;
521 // Check that it could be an object with children
522 ModelAPI_Object* aObj = (ModelAPI_Object*)theParent.internalPointer();
524 // Check for Part feature
525 ResultPartPtr aPartRes = getPartResult(aObj);
533 //******************************************************
534 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
536 beginInsertRows(theParent, theRow, theRow + theCount - 1);
542 //******************************************************
543 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
545 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
550 //******************************************************
551 Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
553 Qt::ItemFlags aFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
554 if (theIndex.internalId() > -1) {
555 aFlags |= Qt::ItemIsEditable;
560 //******************************************************
561 QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
563 SessionPtr aSession = ModelAPI_Session::get();
564 DocumentPtr aRootDoc = aSession->moduleDocument();
565 if (myXMLReader.isAttachToResult()) { // If document is attached to result
566 int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
568 ResultPartPtr aPartRes;
569 for (int i = 0; i < aNb; i++) {
570 aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
571 aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
572 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
574 if (myXMLReader.rootType() == ModelAPI_Feature::group())
575 aRow += foldersCount();
576 return createIndex(aRow, 0, aObj.get());
579 } else { // If document is attached to feature
580 int aNb = aRootDoc->size(ModelAPI_Feature::group());
582 ResultPartPtr aPartRes;
583 for (int i = 0; i < aNb; i++) {
584 aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
585 aPartRes = getPartResult(aObj.get());
586 if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
588 if (myXMLReader.rootType() == ModelAPI_Feature::group())
589 aRow += foldersCount();
590 return createIndex(aRow, 0, aObj.get());
594 return QModelIndex();
597 //******************************************************
598 QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
600 SessionPtr aSession = ModelAPI_Session::get();
601 DocumentPtr aRootDoc = aSession->moduleDocument();
602 if (theDoc == aRootDoc)
603 return QModelIndex();
605 return findDocumentRootIndex(theDoc.get());
608 //******************************************************
609 int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
612 SessionPtr aSession = ModelAPI_Session::get();
613 DocumentPtr aRootDoc = aSession->moduleDocument();
614 if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
615 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
616 if (myXMLReader.rootShowEmpty(i))
619 if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
624 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
625 if (myXMLReader.subShowEmpty(i))
628 if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
636 //******************************************************
637 QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
641 for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
642 if (!myXMLReader.rootShowEmpty(i))
643 aResult << myXMLReader.rootFolderType(i).c_str();
646 for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
647 if (!myXMLReader.subShowEmpty(i))
648 aResult << myXMLReader.subFolderType(i).c_str();