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