Salome HOME
d74163c715332aaa640744424b5ac46d05a98198
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.cpp
1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
3 #include "XGUI_Workshop.h"
4 #include "XGUI_Tools.h"
5
6 #include <ModelAPI_Session.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultPart.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Object.h>
13
14 #include <Events_Loop.h>
15
16 #include <Config_FeatureMessage.h>
17
18 #include <QIcon>
19 #include <QString>
20 #include <QBrush>
21
22 #include <set>
23
24 #define ACTIVE_COLOR QColor(0,72,140)
25 #define PASSIVE_COLOR Qt::black
26
27 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
28     : QAbstractItemModel(theParent),
29       myActivePart(0)
30 {
31   // Register in event loop
32   //Events_Loop* aLoop = Events_Loop::loop();
33   //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
34   //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
35   //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
36
37   // Create a top part of data tree model
38   myModel = new XGUI_TopDataModel(this);
39   myModel->setItemsColor(ACTIVE_COLOR);
40 }
41
42 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
43 {
44   clearModelIndexes();
45 }
46
47 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
48 {
49   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
50
51   // Created object event *******************
52   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
53     const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
54         dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
55     std::set<ObjectPtr> aObjects = aUpdMsg->objects();
56
57     std::set<ObjectPtr>::const_iterator aIt;
58     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
59       ObjectPtr aObject = (*aIt);
60       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
61       if (aFeature && (!aFeature->isInHistory()))
62         continue;
63
64       DocumentPtr aDoc = aObject->document();
65       if (aDoc == aRootDoc) {  // If root objects
66         if (aObject->groupName() == ModelAPI_ResultPart::group()) {  // Update only Parts group
67             // Add a new part
68           int aStart = myPartModels.size();
69           XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
70           aModel->setPartId(myPartModels.count());
71           myPartModels.append(aModel);
72           insertRow(aStart, partFolderNode());
73         } else {  // Update top groups (other except parts
74           QModelIndex aIndex = myModel->findParent(aObject);
75           int aStart = myModel->rowCount(aIndex) - 1;
76           if (aStart < 0)
77             aStart = 0;
78           aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
79           insertRow(aStart, aIndex);
80         }
81       } else {  // if sub-objects of first level nodes
82         XGUI_PartModel* aPartModel = 0;
83         QList<XGUI_PartModel*>::const_iterator aIt;
84         for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
85           if ((*aIt)->hasDocument(aDoc)) {
86             aPartModel = (*aIt);
87             break;
88           }
89         }
90         if (aPartModel) {
91           QModelIndex aIndex = aPartModel->findParent(aObject);
92           int aStart = aPartModel->rowCount(aIndex);  // check this index
93           aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
94           insertRow(aStart, aIndex);
95         }
96       }
97     }
98     // Deleted object event ***********************
99   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
100     const ModelAPI_ObjectDeletedMessage* aUpdMsg =
101         dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
102     DocumentPtr aDoc = aUpdMsg->document();
103     std::set<std::string> aGroups = aUpdMsg->groups();
104
105     std::set<std::string>::const_iterator aIt;
106     for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
107       std::string aGroup = (*aIt);
108       if (aDoc == aRootDoc) {  // If root objects
109         if (aGroup == ModelAPI_ResultPart::group()) {  // Update only Parts group
110           int aStart = myPartModels.size() - 1;
111           removeSubModel(aStart);
112           removeRow(aStart, partFolderNode());
113           if (myActivePart && (!isPartSubModel(myActivePart))) {
114             myActivePart = 0;
115             myActivePartIndex = QModelIndex();
116             myModel->setItemsColor(ACTIVE_COLOR);
117           }
118         } else {  // Update top groups (other except parts
119           QModelIndex aIndex = myModel->findGroup(aGroup);
120           int aStart = myModel->rowCount(aIndex);
121           aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
122           removeRow(aStart, aIndex);
123         }
124       } else {
125         XGUI_PartModel* aPartModel = 0;
126         QList<XGUI_PartModel*>::const_iterator aIt;
127         for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
128           if ((*aIt)->hasDocument(aDoc)) {
129             aPartModel = (*aIt);
130             break;
131           }
132         }
133         if (aPartModel) {
134           QModelIndex aIndex = aPartModel->findGroup(aGroup);
135           int aStart = aPartModel->rowCount(aIndex);
136           aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
137           removeRow(aStart, aIndex);
138         }
139       }
140     }
141     // Deleted object event ***********************
142   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
143     //const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
144     //ObjectPtr aFeature = aUpdMsg->feature();
145     //DocumentPtr aDoc = aFeature->document();
146
147     // TODO: Identify the necessary index by the modified feature
148     QModelIndex aIndex;
149     emit dataChanged(aIndex, aIndex);
150
151     // Reset whole tree **************************
152   } else {
153     rebuildDataTree();
154   }
155 }
156
157 void XGUI_DocumentDataModel::rebuildDataTree()
158 {
159   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
160
161   beginResetModel();
162   clearModelIndexes();
163
164   int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
165   if (myPartModels.size() != aNbParts) {  // resize internal models
166     while (myPartModels.size() > aNbParts) {
167       delete myPartModels.last();
168       myPartModels.removeLast();
169     }
170     while (myPartModels.size() < aNbParts) {
171       myPartModels.append(new XGUI_PartDataModel(this));
172     }
173     for (int i = 0; i < myPartModels.size(); i++)
174       myPartModels.at(i)->setPartId(i);
175   }
176   endResetModel();
177 }
178
179 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
180 {
181   if (!theIndex.isValid())
182     return QVariant();
183   switch (theIndex.internalId()) {
184     case PartsFolder:
185       switch (theRole) {
186         case Qt::DisplayRole:
187           return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
188         case Qt::DecorationRole:
189           return QIcon(":pictures/constr_folder.png");
190         case Qt::ToolTipRole:
191           return tr("Parts folder");
192         case Qt::ForegroundRole:
193           if (myActivePart)
194             return QBrush(PASSIVE_COLOR);
195             else
196             return QBrush(ACTIVE_COLOR);
197             default:
198             return QVariant();
199           }
200           break;
201           case HistoryNode:
202           {
203             int aOffset = historyOffset();
204             DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
205             ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
206             FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
207             if (!aFeature)
208             return QVariant();
209             switch (theRole) {
210               case Qt::DisplayRole:
211               if (aFeature)
212               return aFeature->data()->name().c_str();
213               else
214               return QVariant();
215               case Qt::DecorationRole:
216               return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
217               case Qt::ToolTipRole:
218               return tr("Feature object");
219               case Qt::ForegroundRole:
220               if (myActivePart)
221               return QBrush(PASSIVE_COLOR);
222               else
223               return QBrush(ACTIVE_COLOR);
224               default:
225               return QVariant();
226             }
227           }
228           break;
229         }
230   QModelIndex aParent = theIndex.parent();
231   if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
232     return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
233   }
234   return toSourceModelIndex(theIndex)->data(theRole);
235 }
236
237 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient,
238                                             int theRole) const
239 {
240   return QVariant();
241 }
242
243 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
244 {
245   if (!theParent.isValid()) {
246     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
247     // Size of external models
248     int aVal = historyOffset();
249     // Plus history size
250     aVal += aRootDoc->size(ModelAPI_Feature::group());
251     return aVal;
252   }
253   if (theParent.internalId() == PartsFolder) {
254     int aSize = myPartModels.size();
255     return myPartModels.size();
256   }
257   if (theParent.internalId() == HistoryNode) {
258     return 0;
259   }
260   QModelIndex* aParent = toSourceModelIndex(theParent);
261   const QAbstractItemModel* aModel = aParent->model();
262   if (!isSubModel(aModel))
263     return 0;
264
265   /*if (isPartSubModel(aModel)) {
266    if (aModel != myActivePart)
267    return 0;
268    }*/
269   return aModel->rowCount(*aParent);
270 }
271
272 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
273 {
274   return 1;
275 }
276
277 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn,
278                                           const QModelIndex& theParent) const
279 {
280   QModelIndex aIndex;
281   if (!theParent.isValid()) {
282     int aOffs = myModel->rowCount();
283     if (theRow < aOffs) {
284       aIndex = myModel->index(theRow, theColumn, theParent);
285       aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
286     } else {
287       if (theRow == aOffs)  // Create Parts node
288         aIndex = partFolderNode();
289       else
290         // create history node
291         aIndex = createIndex(theRow, theColumn, HistoryNode);
292     }
293   } else {
294     if (theParent.internalId() == PartsFolder) {
295       aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
296     } else {
297       QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
298       aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
299     }
300     aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
301   }
302   return aIndex;
303 }
304
305 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
306 {
307   if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
308     return QModelIndex();
309
310   QModelIndex* aIndex = toSourceModelIndex(theIndex);
311   const QAbstractItemModel* aModel = aIndex->model();
312   if (!isSubModel(aModel))
313     return QModelIndex();
314
315   if (isPartSubModel(aModel)) {
316     if (!aModel->parent(*aIndex).isValid()) {
317       return partFolderNode();
318     }
319   }
320
321   QModelIndex aIndex1 = aModel->parent(*aIndex);
322   if (aIndex1.isValid())
323     return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
324   return aIndex1;
325 }
326
327 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
328 {
329   if (!theParent.isValid())
330     return true;
331   return rowCount(theParent) > 0;
332 }
333
334 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
335 {
336   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
337   return aIndexPtr;
338 }
339
340 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
341 {
342   QList<QModelIndex*>::const_iterator aIt;
343   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
344     QModelIndex* aIndex = (*aIt);
345     if ((*aIndex) == theIndex)
346       return aIndex;
347   }
348   return 0;
349 }
350
351 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
352 {
353   QModelIndex* aIndexPtr = findModelIndex(theIndex);
354   if (!aIndexPtr) {
355     aIndexPtr = new QModelIndex(theIndex);
356     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
357     that->myIndexes.append(aIndexPtr);
358   }
359   return aIndexPtr;
360 }
361
362 void XGUI_DocumentDataModel::clearModelIndexes()
363 {
364   QList<QModelIndex*>::const_iterator aIt;
365   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
366     delete (*aIt);
367   myIndexes.clear();
368 }
369
370 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
371 {
372   if (theIndex.internalId() == PartsFolder)
373     return ObjectPtr();
374   if (theIndex.internalId() == HistoryNode) {
375     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
376     int aOffset = historyOffset();
377     return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
378   }
379   QModelIndex* aIndex = toSourceModelIndex(theIndex);
380   if (!isSubModel(aIndex->model()))
381     return ObjectPtr();
382
383   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
384   return aModel->object(*aIndex);
385 }
386
387 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
388 {
389   beginInsertRows(theParent, theRow, theRow + theCount - 1);
390   //endInsertRows();
391
392   // Update history
393   QModelIndex aRoot;
394   int aRow = rowCount(aRoot);
395   beginInsertRows(aRoot, aRow, aRow);
396   endInsertRows();
397
398   return true;
399 }
400
401 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
402 {
403   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
404   endRemoveRows();
405   return true;
406 }
407
408 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
409 {
410   XGUI_PartModel* aModel = myPartModels.at(theModelId);
411   QIntList aToRemove;
412   for (int i = 0; i < myIndexes.size(); i++) {
413     if (myIndexes.at(i)->model() == aModel)
414       aToRemove.append(i);
415   }
416   int aId;
417   while (aToRemove.size() > 0) {
418     aId = aToRemove.last();
419     delete myIndexes.at(aId);
420     myIndexes.removeAt(aId);
421     aToRemove.removeLast();
422   }
423   delete aModel;
424   myPartModels.removeAt(theModelId);
425 }
426
427 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
428 {
429   if (theModel == myModel)
430     return true;
431   return isPartSubModel(theModel);
432 }
433
434 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
435 {
436   return myPartModels.contains((XGUI_PartModel*) theModel);
437 }
438
439 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
440 {
441   int aPos = myModel->rowCount(QModelIndex());
442   return createIndex(aPos, columnCount() - 1, PartsFolder);
443 }
444
445 int XGUI_DocumentDataModel::historyOffset() const
446 {
447   // Nb of rows of top model + Parts folder
448   return myModel->rowCount(QModelIndex()) + 1;
449 }
450
451 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
452 {
453   if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
454     return false;
455
456   QModelIndex* aIndex = toSourceModelIndex(theIndex);
457   if (!aIndex)
458     return false;
459
460   const QAbstractItemModel* aModel = aIndex->model();
461
462   if (isPartSubModel(aModel)) {
463     // if this is root node (Part item index)
464     if (!aIndex->parent().isValid()) {
465       if (myActivePart)
466         myActivePart->setItemsColor(PASSIVE_COLOR);
467
468         if (myActivePart == aModel) {
469           myActivePart = 0;
470           myActivePartIndex = QModelIndex();
471         } else {
472           myActivePart = (XGUI_PartModel*)aModel;
473           myActivePartIndex = theIndex;
474         }
475
476         if (myActivePart) {
477           myActivePart->setItemsColor(ACTIVE_COLOR);
478           myModel->setItemsColor(PASSIVE_COLOR);
479         } else
480           myModel->setItemsColor(ACTIVE_COLOR);
481         return true;
482       }
483     }
484   return false;
485 }
486
487 ResultPartPtr XGUI_DocumentDataModel::activePart() const
488 {
489   if (myActivePart)
490     return myActivePart->part();
491   return ResultPartPtr();
492 }
493
494 void XGUI_DocumentDataModel::deactivatePart()
495 {
496   if (myActivePart)
497     myActivePart->setItemsColor(PASSIVE_COLOR);
498     myActivePart = 0;
499     myActivePartIndex = QModelIndex();
500     myModel->setItemsColor(ACTIVE_COLOR);
501   }
502
503 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
504 {
505   Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
506   if (object(theIndex)) {
507     aFlags |= Qt::ItemIsEditable;
508   }
509   return aFlags;
510 }
511
512 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
513 {
514   int aRow = -1;
515   XGUI_PartModel* aModel = 0;
516   foreach (XGUI_PartModel* aPartModel, myPartModels)
517   {
518     aRow++;
519     if (aPartModel->part() == theObject) {
520       aModel = aPartModel;
521       break;
522     }
523   }
524   if (aModel) {
525     return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
526   }
527   return QModelIndex();
528 }
529
530 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
531 {
532   // Check that this feature belongs to root document
533   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
534   DocumentPtr aDoc = theObject->document();
535   if (aDoc == aRootDoc) {
536     // This feature belongs to histrory or top model
537     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
538     if (aFeature) {
539       int aId;
540       int aNb = aRootDoc->size(ModelAPI_Feature::group());
541       for (aId = 0; aId < aNb; aId++) {
542         if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
543           break;
544       }
545       if (aId < aNb)
546         return index(aId + historyOffset(), 0, QModelIndex());
547     } else {
548       QModelIndex aIndex = myModel->objectIndex(theObject);
549       return
550           aIndex.isValid() ?
551               createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
552               QModelIndex();
553     }
554   } else {
555     XGUI_PartModel* aPartModel = 0;
556     foreach(XGUI_PartModel* aModel, myPartModels)
557     {
558       if (aModel->hasDocument(aDoc)) {
559         aPartModel = aModel;
560         break;
561       }
562     }
563     if (aPartModel) {
564       QModelIndex aIndex = aPartModel->objectIndex(theObject);
565       return
566           aIndex.isValid() ?
567               createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
568               QModelIndex();
569     }
570   }
571   return QModelIndex();
572 }