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);
100 // Deleted object event ***********************
101 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
102 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
103 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
104 DocumentPtr aDoc = aUpdMsg->document();
105 std::set<std::string> aGroups = aUpdMsg->groups();
107 std::set<std::string>::const_iterator aIt;
108 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
109 std::string aGroup = (*aIt);
110 if (aDoc == aRootDoc) { // If root objects
111 if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
112 int aStart = myPartModels.size() - 1;
113 if (aStart >= 0) {// MPV: this could be reproduced on close
114 removeSubModel(aStart);
115 removeRow(aStart, partFolderNode());
116 if (myActivePart && (!isPartSubModel(myActivePart))) {
118 myActivePartIndex = QModelIndex();
119 myModel->setItemsColor(ACTIVE_COLOR);
122 } else { // Update top groups (other except parts
123 QModelIndex aIndex = myModel->findGroup(aGroup);
124 int aStart = myModel->rowCount(aIndex);
125 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
126 removeRow(aStart, aIndex);
129 XGUI_PartModel* aPartModel = 0;
130 foreach (XGUI_PartModel* aPart, myPartModels) {
131 if (aPart->hasDocument(aDoc)) {
137 QModelIndex aIndex = aPartModel->findGroup(aGroup);
138 int aStart = aPartModel->rowCount(aIndex);
139 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
140 removeRow(aStart, aIndex);
144 // Deleted object event ***********************
145 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
146 //std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg = std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
147 //ObjectPtr aFeature = aUpdMsg->feature();
148 //DocumentPtr aDoc = aFeature->document();
150 // TODO: Identify the necessary index by the modified feature
152 emit dataChanged(aIndex, aIndex);
154 // Reset whole tree **************************
160 void XGUI_DocumentDataModel::rebuildDataTree()
162 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
167 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
168 if (myPartModels.size() != aNbParts) { // resize internal models
169 while (myPartModels.size() > aNbParts) {
170 delete myPartModels.last();
171 myPartModels.removeLast();
173 while (myPartModels.size() < aNbParts) {
174 myPartModels.append(new XGUI_PartDataModel(this));
176 for (int i = 0; i < myPartModels.size(); i++)
177 myPartModels.at(i)->setPartId(i);
182 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
184 if (!theIndex.isValid())
186 switch (theIndex.internalId()) {
189 case Qt::DisplayRole:
190 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
191 case Qt::DecorationRole:
192 return QIcon(":pictures/constr_folder.png");
193 case Qt::ToolTipRole:
194 return tr("Parts folder");
195 case Qt::ForegroundRole:
197 return QBrush(PASSIVE_COLOR);
199 return QBrush(ACTIVE_COLOR);
206 int aOffset = historyOffset();
207 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
208 ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
209 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
213 case Qt::DisplayRole:
215 return aFeature->data()->name().c_str();
218 case Qt::DecorationRole:
219 return XGUI_Workshop::featureIcon(aFeature);
220 case Qt::ToolTipRole:
221 return tr("Feature object");
222 case Qt::ForegroundRole:
224 return QBrush(PASSIVE_COLOR);
226 return QBrush(ACTIVE_COLOR);
233 QModelIndex aParent = theIndex.parent();
234 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
235 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
237 return toSourceModelIndex(theIndex)->data(theRole);
240 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient,
246 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
248 if (!theParent.isValid()) {
249 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
250 // Size of external models
251 int aVal = historyOffset();
253 aVal += aRootDoc->size(ModelAPI_Feature::group());
256 if (theParent.internalId() == PartsFolder) {
257 int aSize = myPartModels.size();
258 return myPartModels.size();
260 if (theParent.internalId() == HistoryNode) {
263 QModelIndex* aParent = toSourceModelIndex(theParent);
264 const QAbstractItemModel* aModel = aParent->model();
265 if (!isSubModel(aModel))
268 /*if (isPartSubModel(aModel)) {
269 if (aModel != myActivePart)
272 return aModel->rowCount(*aParent);
275 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
280 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn,
281 const QModelIndex& theParent) const
284 if (!theParent.isValid()) {
285 int aOffs = myModel->rowCount();
286 if (theRow < aOffs) {
287 aIndex = myModel->index(theRow, theColumn, theParent);
288 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
290 if (theRow == aOffs) // Create Parts node
291 aIndex = partFolderNode();
293 // create history node
294 aIndex = createIndex(theRow, theColumn, HistoryNode);
297 if (theParent.internalId() == PartsFolder) {
298 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
300 QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
301 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
303 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
308 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
310 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
311 return QModelIndex();
313 QModelIndex* aIndex = toSourceModelIndex(theIndex);
314 const QAbstractItemModel* aModel = aIndex->model();
315 if (!isSubModel(aModel))
316 return QModelIndex();
318 if (isPartSubModel(aModel)) {
319 if (!aModel->parent(*aIndex).isValid()) {
320 return partFolderNode();
324 QModelIndex aIndex1 = aModel->parent(*aIndex);
325 if (aIndex1.isValid())
326 return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
330 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
332 if (!theParent.isValid())
334 return rowCount(theParent) > 0;
337 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
339 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
343 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
345 QList<QModelIndex*>::const_iterator aIt;
346 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
347 QModelIndex* aIndex = (*aIt);
348 if ((*aIndex) == theIndex)
354 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
356 QModelIndex* aIndexPtr = findModelIndex(theIndex);
358 aIndexPtr = new QModelIndex(theIndex);
359 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
360 that->myIndexes.append(aIndexPtr);
365 void XGUI_DocumentDataModel::clearModelIndexes()
367 foreach (QModelIndex* aIndex, myIndexes)
372 void XGUI_DocumentDataModel::clearSubModels()
374 foreach (XGUI_PartModel* aPart, myPartModels)
376 myPartModels.clear();
379 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
381 if (theIndex.internalId() == PartsFolder)
383 if (theIndex.internalId() == HistoryNode) {
384 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
385 int aOffset = historyOffset();
386 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
388 QModelIndex* aIndex = toSourceModelIndex(theIndex);
389 if (!isSubModel(aIndex->model()))
392 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
393 return aModel->object(*aIndex);
396 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
398 beginInsertRows(theParent, theRow, theRow + theCount - 1);
403 int aRow = rowCount(aRoot);
404 beginInsertRows(aRoot, aRow, aRow);
410 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
412 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
417 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
419 XGUI_PartModel* aModel = myPartModels.at(theModelId);
421 for (int i = 0; i < myIndexes.size(); i++) {
422 if (myIndexes.at(i)->model() == aModel)
426 while (aToRemove.size() > 0) {
427 aId = aToRemove.last();
428 delete myIndexes.at(aId);
429 myIndexes.removeAt(aId);
430 aToRemove.removeLast();
433 myPartModels.removeAt(theModelId);
436 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
438 if (theModel == myModel)
440 return isPartSubModel(theModel);
443 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
445 return myPartModels.contains((XGUI_PartModel*) theModel);
448 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
450 int aPos = myModel->rowCount(QModelIndex());
451 return createIndex(aPos, columnCount() - 1, PartsFolder);
454 int XGUI_DocumentDataModel::historyOffset() const
456 // Nb of rows of top model + Parts folder
457 return myModel->rowCount(QModelIndex()) + 1;
460 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
462 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
465 QModelIndex* aIndex = toSourceModelIndex(theIndex);
469 const QAbstractItemModel* aModel = aIndex->model();
471 if (isPartSubModel(aModel)) {
472 // if this is root node (Part item index)
473 if (!aIndex->parent().isValid()) {
475 myActivePart->setItemsColor(PASSIVE_COLOR);
477 if (myActivePart == aModel) {
479 myActivePartIndex = QModelIndex();
481 myActivePart = (XGUI_PartModel*)aModel;
482 myActivePartIndex = theIndex;
486 myActivePart->setItemsColor(ACTIVE_COLOR);
487 myModel->setItemsColor(PASSIVE_COLOR);
489 myModel->setItemsColor(ACTIVE_COLOR);
496 ResultPartPtr XGUI_DocumentDataModel::activePart() const
499 return myActivePart->part();
500 return ResultPartPtr();
503 void XGUI_DocumentDataModel::deactivatePart()
506 myActivePart->setItemsColor(PASSIVE_COLOR);
508 myActivePartIndex = QModelIndex();
509 myModel->setItemsColor(ACTIVE_COLOR);
512 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
514 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
515 if (object(theIndex)) {
516 aFlags |= Qt::ItemIsEditable;
521 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
524 XGUI_PartModel* aModel = 0;
525 foreach (XGUI_PartModel* aPartModel, myPartModels)
528 if (aPartModel->part() == theObject) {
534 return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
536 return QModelIndex();
539 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
541 // Check that this feature belongs to root document
542 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
543 DocumentPtr aDoc = theObject->document();
544 if (aDoc == aRootDoc) {
545 // This feature belongs to histrory or top model
546 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
549 int aNb = aRootDoc->size(ModelAPI_Feature::group());
550 for (aId = 0; aId < aNb; aId++) {
551 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
555 return index(aId + historyOffset(), 0, QModelIndex());
557 QModelIndex aIndex = myModel->objectIndex(theObject);
560 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
564 XGUI_PartModel* aPartModel = 0;
565 foreach(XGUI_PartModel* aModel, myPartModels)
567 if (aModel->hasDocument(aDoc)) {
573 QModelIndex aIndex = aPartModel->objectIndex(theObject);
576 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
580 return QModelIndex();
584 void XGUI_DocumentDataModel::clear()
589 myActivePartIndex = QModelIndex();
590 myModel->setItemsColor(ACTIVE_COLOR);