1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_DocumentDataModel.h"
4 #include "XGUI_PartDataModel.h"
5 #include "XGUI_Workshop.h"
6 #include "XGUI_Tools.h"
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Feature.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_ResultPart.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_Object.h>
16 #include <Events_Loop.h>
18 #include <Config_FeatureMessage.h>
26 #define ACTIVE_COLOR QColor(0,72,140)
27 #define PASSIVE_COLOR Qt::black
29 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
30 : QAbstractItemModel(theParent),
33 // Register in event loop
34 //Events_Loop* aLoop = Events_Loop::loop();
35 //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
36 //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
37 //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
39 // Create a top part of data tree model
40 myModel = new XGUI_TopDataModel(this);
41 myModel->setItemsColor(ACTIVE_COLOR);
44 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
50 void XGUI_DocumentDataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
52 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
54 // Created object event *******************
55 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
56 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
57 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
58 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
60 std::set<ObjectPtr>::const_iterator aIt;
61 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
62 ObjectPtr aObject = (*aIt);
63 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
64 if (aFeature && (!aFeature->isInHistory()))
67 DocumentPtr aDoc = aObject->document();
68 if (aDoc == aRootDoc) { // If root objects
69 if (aObject->groupName() == ModelAPI_ResultPart::group()) { // Update only Parts group
71 int aStart = myPartModels.size();
72 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
73 aModel->setPartId(myPartModels.count());
74 myPartModels.append(aModel);
75 insertRow(aStart, partFolderNode());
76 } else { // Update top groups (other except parts
77 QModelIndex aIndex = myModel->findParent(aObject);
78 int aStart = myModel->rowCount(aIndex) - 1;
81 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
82 insertRow(aStart, aIndex);
84 } else { // if sub-objects of first level nodes
85 XGUI_PartModel* aPartModel = 0;
86 foreach (XGUI_PartModel* aPart, myPartModels) {
87 if (aPart->hasDocument(aDoc)) {
93 QModelIndex aIndex = aPartModel->findParent(aObject);
94 int aStart = aPartModel->rowCount(aIndex); // check this index
95 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
96 insertRow(aStart, aIndex);
101 // Deleted object event ***********************
102 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
103 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
104 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
105 DocumentPtr aDoc = aUpdMsg->document();
106 std::set<std::string> aGroups = aUpdMsg->groups();
108 std::set<std::string>::const_iterator aIt;
109 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
110 std::string aGroup = (*aIt);
111 if (aDoc == aRootDoc) { // If root objects
112 if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
113 int aStart = myPartModels.size() - 1;
114 if (aStart >= 0) {// MPV: this could be reproduced on close
115 removeSubModel(aStart);
116 removeRow(aStart, partFolderNode());
117 if (myActivePart && (!isPartSubModel(myActivePart))) {
119 myActivePartIndex = QModelIndex();
120 myModel->setItemsColor(ACTIVE_COLOR);
123 } else { // Update top groups (other except parts
124 QModelIndex aIndex = myModel->findGroup(aGroup);
125 int aStart = myModel->rowCount(aIndex);
126 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
127 removeRow(aStart, aIndex);
130 XGUI_PartModel* aPartModel = 0;
131 foreach (XGUI_PartModel* aPart, myPartModels) {
132 if (aPart->hasDocument(aDoc)) {
138 QModelIndex aIndex = aPartModel->findGroup(aGroup);
139 int aStart = aPartModel->rowCount(aIndex);
140 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
141 removeRow(aStart, aIndex);
145 // Deleted object event ***********************
146 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
147 //std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg = std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
148 //ObjectPtr aFeature = aUpdMsg->feature();
149 //DocumentPtr aDoc = aFeature->document();
151 // TODO: Identify the necessary index by the modified feature
153 emit dataChanged(aIndex, aIndex);
155 // Reset whole tree **************************
161 void XGUI_DocumentDataModel::rebuildDataTree()
163 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
168 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
169 if (myPartModels.size() != aNbParts) { // resize internal models
170 while (myPartModels.size() > aNbParts) {
171 delete myPartModels.last();
172 myPartModels.removeLast();
174 while (myPartModels.size() < aNbParts) {
175 myPartModels.append(new XGUI_PartDataModel(this));
177 for (int i = 0; i < myPartModels.size(); i++)
178 myPartModels.at(i)->setPartId(i);
183 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
185 if (!theIndex.isValid())
187 switch (theIndex.internalId()) {
190 case Qt::DisplayRole:
191 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
192 case Qt::DecorationRole:
193 return QIcon(":pictures/constr_folder.png");
194 case Qt::ToolTipRole:
195 return tr("Parts folder");
196 case Qt::ForegroundRole:
198 return QBrush(PASSIVE_COLOR);
200 return QBrush(ACTIVE_COLOR);
207 int aOffset = historyOffset();
208 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
209 ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
210 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
214 case Qt::DisplayRole:
216 return aFeature->data()->name().c_str();
219 case Qt::DecorationRole:
220 return XGUI_Workshop::featureIcon(aFeature);
221 case Qt::ToolTipRole:
222 return tr("Feature object");
223 case Qt::ForegroundRole:
225 return QBrush(PASSIVE_COLOR);
227 return QBrush(ACTIVE_COLOR);
234 QModelIndex aParent = theIndex.parent();
235 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
236 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
238 return toSourceModelIndex(theIndex)->data(theRole);
241 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient,
247 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
249 if (!theParent.isValid()) {
250 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
251 // Size of external models
252 int aVal = historyOffset();
254 aVal += aRootDoc->size(ModelAPI_Feature::group());
257 if (theParent.internalId() == PartsFolder) {
258 int aSize = myPartModels.size();
259 return myPartModels.size();
261 if (theParent.internalId() == HistoryNode) {
264 QModelIndex* aParent = toSourceModelIndex(theParent);
265 const QAbstractItemModel* aModel = aParent->model();
266 if (!isSubModel(aModel))
269 /*if (isPartSubModel(aModel)) {
270 if (aModel != myActivePart)
273 return aModel->rowCount(*aParent);
276 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
281 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn,
282 const QModelIndex& theParent) const
285 if (!theParent.isValid()) {
286 int aOffs = myModel->rowCount();
287 if (theRow < aOffs) {
288 aIndex = myModel->index(theRow, theColumn, theParent);
289 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
291 if (theRow == aOffs) // Create Parts node
292 aIndex = partFolderNode();
294 // create history node
295 aIndex = createIndex(theRow, theColumn, HistoryNode);
298 if (theParent.internalId() == PartsFolder) {
299 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
301 QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
302 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
304 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
309 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
311 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
312 return QModelIndex();
314 QModelIndex* aIndex = toSourceModelIndex(theIndex);
315 const QAbstractItemModel* aModel = aIndex->model();
316 if (!isSubModel(aModel))
317 return QModelIndex();
319 if (isPartSubModel(aModel)) {
320 if (!aModel->parent(*aIndex).isValid()) {
321 return partFolderNode();
325 QModelIndex aIndex1 = aModel->parent(*aIndex);
326 if (aIndex1.isValid())
327 return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
331 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
333 if (!theParent.isValid())
335 return rowCount(theParent) > 0;
338 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
340 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
344 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
346 QList<QModelIndex*>::const_iterator aIt;
347 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
348 QModelIndex* aIndex = (*aIt);
349 if ((*aIndex) == theIndex)
355 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
357 QModelIndex* aIndexPtr = findModelIndex(theIndex);
359 aIndexPtr = new QModelIndex(theIndex);
360 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
361 that->myIndexes.append(aIndexPtr);
366 void XGUI_DocumentDataModel::clearModelIndexes()
368 foreach (QModelIndex* aIndex, myIndexes)
373 void XGUI_DocumentDataModel::clearSubModels()
375 foreach (XGUI_PartModel* aPart, myPartModels)
377 myPartModels.clear();
380 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
382 if (theIndex.internalId() == PartsFolder)
384 if (theIndex.internalId() == HistoryNode) {
385 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
386 int aOffset = historyOffset();
387 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
389 QModelIndex* aIndex = toSourceModelIndex(theIndex);
390 if (!isSubModel(aIndex->model()))
393 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
394 return aModel->object(*aIndex);
397 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
399 beginInsertRows(theParent, theRow, theRow + theCount - 1);
404 int aRow = rowCount(aRoot);
405 beginInsertRows(aRoot, aRow, aRow);
411 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
413 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
418 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
420 XGUI_PartModel* aModel = myPartModels.at(theModelId);
422 for (int i = 0; i < myIndexes.size(); i++) {
423 if (myIndexes.at(i)->model() == aModel)
427 while (aToRemove.size() > 0) {
428 aId = aToRemove.last();
429 delete myIndexes.at(aId);
430 myIndexes.removeAt(aId);
431 aToRemove.removeLast();
434 myPartModels.removeAt(theModelId);
437 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
439 if (theModel == myModel)
441 return isPartSubModel(theModel);
444 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
446 return myPartModels.contains((XGUI_PartModel*) theModel);
449 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
451 int aPos = myModel->rowCount(QModelIndex());
452 return createIndex(aPos, columnCount() - 1, PartsFolder);
455 int XGUI_DocumentDataModel::historyOffset() const
457 // Nb of rows of top model + Parts folder
458 return myModel->rowCount(QModelIndex()) + 1;
461 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
463 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
466 QModelIndex* aIndex = toSourceModelIndex(theIndex);
470 const QAbstractItemModel* aModel = aIndex->model();
472 if (isPartSubModel(aModel)) {
473 // if this is root node (Part item index)
474 if (!aIndex->parent().isValid()) {
476 myActivePart->setItemsColor(PASSIVE_COLOR);
478 if (myActivePart == aModel) {
480 myActivePartIndex = QModelIndex();
482 myActivePart = (XGUI_PartModel*)aModel;
483 myActivePartIndex = theIndex;
487 myActivePart->setItemsColor(ACTIVE_COLOR);
488 myModel->setItemsColor(PASSIVE_COLOR);
490 myModel->setItemsColor(ACTIVE_COLOR);
497 ResultPartPtr XGUI_DocumentDataModel::activePart() const
500 return myActivePart->part();
501 return ResultPartPtr();
504 void XGUI_DocumentDataModel::deactivatePart()
507 myActivePart->setItemsColor(PASSIVE_COLOR);
509 myActivePartIndex = QModelIndex();
510 myModel->setItemsColor(ACTIVE_COLOR);
513 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
515 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
516 if (object(theIndex)) {
517 aFlags |= Qt::ItemIsEditable;
522 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
525 XGUI_PartModel* aModel = 0;
526 foreach (XGUI_PartModel* aPartModel, myPartModels)
529 if (aPartModel->part() == theObject) {
535 return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
537 return QModelIndex();
540 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
542 // Check that this feature belongs to root document
543 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
544 DocumentPtr aDoc = theObject->document();
545 if (aDoc == aRootDoc) {
546 // This feature belongs to histrory or top model
547 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
550 int aNb = aRootDoc->size(ModelAPI_Feature::group());
551 for (aId = 0; aId < aNb; aId++) {
552 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
556 return index(aId + historyOffset(), 0, QModelIndex());
558 QModelIndex aIndex = myModel->objectIndex(theObject);
561 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
565 XGUI_PartModel* aPartModel = 0;
566 foreach(XGUI_PartModel* aModel, myPartModels)
568 if (aModel->hasDocument(aDoc)) {
574 QModelIndex aIndex = aPartModel->objectIndex(theObject);
577 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
581 return QModelIndex();
585 void XGUI_DocumentDataModel::clear()
590 myActivePartIndex = QModelIndex();
591 myModel->setItemsColor(ACTIVE_COLOR);