Salome HOME
Issue #324: Reset items of loaded parts
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_DocumentDataModel.h"
4 #include "XGUI_PartDataModel.h"
5 #include "XGUI_Workshop.h"
6 #include "XGUI_Tools.h"
7
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>
15
16 #include <Events_Loop.h>
17
18 #include <Config_FeatureMessage.h>
19
20 #include <QIcon>
21 #include <QString>
22 #include <QBrush>
23
24 #include <set>
25
26 #define ACTIVE_COLOR QColor(0,72,140)
27 #define PASSIVE_COLOR Qt::black
28
29 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
30     : QAbstractItemModel(theParent),
31       myActivePart(0)
32 {
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));
38
39   // Create a top part of data tree model
40   myModel = new XGUI_TopDataModel(this);
41   myModel->setItemsColor(ACTIVE_COLOR);
42 }
43
44 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
45 {
46   clearModelIndexes();
47   clearSubModels();
48 }
49
50 void XGUI_DocumentDataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
51 {
52   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
53
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();
59
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()))
65         continue;
66
67       DocumentPtr aDoc = aObject->document();
68       if (aDoc == aRootDoc) {  // If root objects
69         if (aObject->groupName() == ModelAPI_ResultPart::group()) {  // Update only Parts group
70             // Add a new part
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;
79           if (aStart < 0)
80             aStart = 0;
81           aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
82           insertRow(aStart, aIndex);
83         }
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)) {
88             aPartModel = aPart;
89             break;
90           }
91         }
92         if (aPartModel) {
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);
97         } else
98           reset();
99       }
100     }
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();
107
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))) {
118               myActivePart = 0;
119               myActivePartIndex = QModelIndex();
120               myModel->setItemsColor(ACTIVE_COLOR);
121             }
122           }
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);
128         }
129       } else {
130         XGUI_PartModel* aPartModel = 0;
131         foreach (XGUI_PartModel* aPart, myPartModels) {
132           if (aPart->hasDocument(aDoc)) {
133             aPartModel = aPart;
134             break;
135           }
136         }
137         if (aPartModel) {
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);
142         }
143       }
144     }
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();
150
151     // TODO: Identify the necessary index by the modified feature
152     QModelIndex aIndex;
153     emit dataChanged(aIndex, aIndex);
154
155     // Reset whole tree **************************
156   } else {
157     rebuildDataTree();
158   }
159 }
160
161 void XGUI_DocumentDataModel::rebuildDataTree()
162 {
163   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
164
165   beginResetModel();
166   clearModelIndexes();
167
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();
173     }
174     while (myPartModels.size() < aNbParts) {
175       myPartModels.append(new XGUI_PartDataModel(this));
176     }
177     for (int i = 0; i < myPartModels.size(); i++)
178       myPartModels.at(i)->setPartId(i);
179   }
180   endResetModel();
181 }
182
183 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
184 {
185   if (!theIndex.isValid())
186     return QVariant();
187   switch (theIndex.internalId()) {
188     case PartsFolder:
189       switch (theRole) {
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:
197           if (myActivePart)
198             return QBrush(PASSIVE_COLOR);
199             else
200             return QBrush(ACTIVE_COLOR);
201             default:
202             return QVariant();
203           }
204           break;
205           case HistoryNode:
206           {
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);
211             if (!aFeature)
212             return QVariant();
213             switch (theRole) {
214               case Qt::DisplayRole:
215               if (aFeature)
216               return aFeature->data()->name().c_str();
217               else
218               return QVariant();
219               case Qt::DecorationRole:
220               return XGUI_Workshop::featureIcon(aFeature);
221               case Qt::ToolTipRole:
222               return tr("Feature object");
223               case Qt::ForegroundRole:
224               if (myActivePart)
225               return QBrush(PASSIVE_COLOR);
226               else
227               return QBrush(ACTIVE_COLOR);
228               default:
229               return QVariant();
230             }
231           }
232           break;
233         }
234   QModelIndex aParent = theIndex.parent();
235   if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
236     return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
237   }
238   return toSourceModelIndex(theIndex)->data(theRole);
239 }
240
241 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient,
242                                             int theRole) const
243 {
244   return QVariant();
245 }
246
247 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
248 {
249   if (!theParent.isValid()) {
250     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
251     // Size of external models
252     int aVal = historyOffset();
253     // Plus history size
254     aVal += aRootDoc->size(ModelAPI_Feature::group());
255     return aVal;
256   }
257   if (theParent.internalId() == PartsFolder) {
258     int aSize = myPartModels.size();
259     return myPartModels.size();
260   }
261   if (theParent.internalId() == HistoryNode) {
262     return 0;
263   }
264   QModelIndex* aParent = toSourceModelIndex(theParent);
265   const QAbstractItemModel* aModel = aParent->model();
266   if (!isSubModel(aModel))
267     return 0;
268
269   /*if (isPartSubModel(aModel)) {
270    if (aModel != myActivePart)
271    return 0;
272    }*/
273   return aModel->rowCount(*aParent);
274 }
275
276 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
277 {
278   return 1;
279 }
280
281 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn,
282                                           const QModelIndex& theParent) const
283 {
284   QModelIndex aIndex;
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));
290     } else {
291       if (theRow == aOffs)  // Create Parts node
292         aIndex = partFolderNode();
293       else
294         // create history node
295         aIndex = createIndex(theRow, theColumn, HistoryNode);
296     }
297   } else {
298     if (theParent.internalId() == PartsFolder) {
299       aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
300     } else {
301       QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
302       aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
303     }
304     aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
305   }
306   return aIndex;
307 }
308
309 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
310 {
311   if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
312     return QModelIndex();
313
314   QModelIndex* aIndex = toSourceModelIndex(theIndex);
315   const QAbstractItemModel* aModel = aIndex->model();
316   if (!isSubModel(aModel))
317     return QModelIndex();
318
319   if (isPartSubModel(aModel)) {
320     if (!aModel->parent(*aIndex).isValid()) {
321       return partFolderNode();
322     }
323   }
324
325   QModelIndex aIndex1 = aModel->parent(*aIndex);
326   if (aIndex1.isValid())
327     return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
328   return aIndex1;
329 }
330
331 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
332 {
333   if (!theParent.isValid())
334     return true;
335   return rowCount(theParent) > 0;
336 }
337
338 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
339 {
340   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
341   return aIndexPtr;
342 }
343
344 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
345 {
346   QList<QModelIndex*>::const_iterator aIt;
347   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
348     QModelIndex* aIndex = (*aIt);
349     if ((*aIndex) == theIndex)
350       return aIndex;
351   }
352   return 0;
353 }
354
355 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
356 {
357   QModelIndex* aIndexPtr = findModelIndex(theIndex);
358   if (!aIndexPtr) {
359     aIndexPtr = new QModelIndex(theIndex);
360     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
361     that->myIndexes.append(aIndexPtr);
362   }
363   return aIndexPtr;
364 }
365
366 void XGUI_DocumentDataModel::clearModelIndexes()
367 {
368   foreach (QModelIndex* aIndex, myIndexes) 
369     delete aIndex;
370   myIndexes.clear();
371 }
372
373 void XGUI_DocumentDataModel::clearSubModels()
374 {
375   foreach (XGUI_PartModel* aPart, myPartModels) 
376     delete aPart;
377   myPartModels.clear();
378 }
379
380 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
381 {
382   if (theIndex.internalId() == PartsFolder)
383     return ObjectPtr();
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);
388   }
389   QModelIndex* aIndex = toSourceModelIndex(theIndex);
390   if (!isSubModel(aIndex->model()))
391     return ObjectPtr();
392
393   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
394   return aModel->object(*aIndex);
395 }
396
397 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
398 {
399   beginInsertRows(theParent, theRow, theRow + theCount - 1);
400   //endInsertRows();
401
402   // Update history
403   QModelIndex aRoot;
404   int aRow = rowCount(aRoot);
405   beginInsertRows(aRoot, aRow, aRow);
406   endInsertRows();
407
408   return true;
409 }
410
411 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
412 {
413   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
414   endRemoveRows();
415   return true;
416 }
417
418 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
419 {
420   XGUI_PartModel* aModel = myPartModels.at(theModelId);
421   QIntList aToRemove;
422   for (int i = 0; i < myIndexes.size(); i++) {
423     if (myIndexes.at(i)->model() == aModel)
424       aToRemove.append(i);
425   }
426   int aId;
427   while (aToRemove.size() > 0) {
428     aId = aToRemove.last();
429     delete myIndexes.at(aId);
430     myIndexes.removeAt(aId);
431     aToRemove.removeLast();
432   }
433   delete aModel;
434   myPartModels.removeAt(theModelId);
435 }
436
437 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
438 {
439   if (theModel == myModel)
440     return true;
441   return isPartSubModel(theModel);
442 }
443
444 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
445 {
446   return myPartModels.contains((XGUI_PartModel*) theModel);
447 }
448
449 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
450 {
451   int aPos = myModel->rowCount(QModelIndex());
452   return createIndex(aPos, columnCount() - 1, PartsFolder);
453 }
454
455 int XGUI_DocumentDataModel::historyOffset() const
456 {
457   // Nb of rows of top model + Parts folder
458   return myModel->rowCount(QModelIndex()) + 1;
459 }
460
461 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
462 {
463   if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
464     return false;
465
466   QModelIndex* aIndex = toSourceModelIndex(theIndex);
467   if (!aIndex)
468     return false;
469
470   const QAbstractItemModel* aModel = aIndex->model();
471
472   if (isPartSubModel(aModel)) {
473     // if this is root node (Part item index)
474     if (!aIndex->parent().isValid()) {
475       if (myActivePart)
476         myActivePart->setItemsColor(PASSIVE_COLOR);
477
478         if (myActivePart == aModel) {
479           myActivePart = 0;
480           myActivePartIndex = QModelIndex();
481         } else {
482           myActivePart = (XGUI_PartModel*)aModel;
483           myActivePartIndex = theIndex;
484         }
485
486         if (myActivePart) {
487           myActivePart->setItemsColor(ACTIVE_COLOR);
488           myModel->setItemsColor(PASSIVE_COLOR);
489         } else
490           myModel->setItemsColor(ACTIVE_COLOR);
491         return true;
492       }
493     }
494   return false;
495 }
496
497 ResultPartPtr XGUI_DocumentDataModel::activePart() const
498 {
499   if (myActivePart)
500     return myActivePart->part();
501   return ResultPartPtr();
502 }
503
504 void XGUI_DocumentDataModel::deactivatePart()
505 {
506   if (myActivePart)
507     myActivePart->setItemsColor(PASSIVE_COLOR);
508     myActivePart = 0;
509     myActivePartIndex = QModelIndex();
510     myModel->setItemsColor(ACTIVE_COLOR);
511   }
512
513 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
514 {
515   Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
516   if (object(theIndex)) {
517     aFlags |= Qt::ItemIsEditable;
518   }
519   return aFlags;
520 }
521
522 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
523 {
524   int aRow = -1;
525   XGUI_PartModel* aModel = 0;
526   foreach (XGUI_PartModel* aPartModel, myPartModels)
527   {
528     aRow++;
529     if (aPartModel->part() == theObject) {
530       aModel = aPartModel;
531       break;
532     }
533   }
534   if (aModel) {
535     return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
536   }
537   return QModelIndex();
538 }
539
540 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
541 {
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);
548     if (aFeature) {
549       int aId;
550       int aNb = aRootDoc->size(ModelAPI_Feature::group());
551       for (aId = 0; aId < aNb; aId++) {
552         if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
553           break;
554       }
555       if (aId < aNb)
556         return index(aId + historyOffset(), 0, QModelIndex());
557     } else {
558       QModelIndex aIndex = myModel->objectIndex(theObject);
559       return
560           aIndex.isValid() ?
561               createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
562               QModelIndex();
563     }
564   } else {
565     XGUI_PartModel* aPartModel = 0;
566     foreach(XGUI_PartModel* aModel, myPartModels)
567     {
568       if (aModel->hasDocument(aDoc)) {
569         aPartModel = aModel;
570         break;
571       }
572     }
573     if (aPartModel) {
574       QModelIndex aIndex = aPartModel->objectIndex(theObject);
575       return
576           aIndex.isValid() ?
577               createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
578               QModelIndex();
579     }
580   }
581   return QModelIndex();
582 }
583
584
585 void XGUI_DocumentDataModel::clear()
586 {
587   clearModelIndexes();
588   clearSubModels();
589   myActivePart = 0;
590   myActivePartIndex = QModelIndex();
591   myModel->setItemsColor(ACTIVE_COLOR);
592 }