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