Salome HOME
079986bc5134d7e36d7850132776ee27a9639101
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.cpp
1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
3
4 #include <ModelAPI_PluginManager.h>
5 #include <ModelAPI_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Data.h>
8 #include <Model_Events.h>
9
10 #include <Events_Loop.h>
11
12
13 #include <QIcon>
14 #include <QString>
15
16
17 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
18   : QAbstractItemModel(theParent)
19 {
20   // Find Document object
21   boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
22   myDocument = aMgr->currentDocument();
23
24   // Register in event loop
25   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
26   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
27   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
28
29   // Create a top part of data tree model
30   myModel = new XGUI_TopDataModel(myDocument, this);
31 }
32
33
34 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
35 {
36   clearModelIndexes();
37 }
38
39
40 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
41 {
42   // Created object event *******************
43   if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
44     const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
45     boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
46     boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
47
48     if (aDoc == myDocument) {  // If root objects
49       if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
50         // Add a new part
51         int aStart = myPartModels.size() + 1;
52         XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
53         aModel->setPartId(myPartModels.count());
54         myPartModels.append(aModel);
55         insertRow(aStart, partFolderNode());
56       } else { // Update top groups (other except parts
57         QModelIndex aIndex = myModel->findParent(aFeature);
58         int aStart = myModel->rowCount(aIndex) - 1;
59         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
60         insertRow(aStart, aIndex);
61       }
62     } else { // if sub-objects of first level nodes
63       XGUI_PartModel* aPartModel = 0;
64       QList<XGUI_PartModel*>::const_iterator aIt;
65       for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
66         if ((*aIt)->hasDocument(aDoc)) {
67           aPartModel = (*aIt);
68           break;
69         }
70       }
71       if (aPartModel) {
72         QModelIndex aIndex = aPartModel->findParent(aFeature);
73         int aStart = aPartModel->rowCount(aIndex) - 1;
74         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
75         insertRow(aStart, aIndex);
76       }
77     }
78
79   // Deleted object event ***********************
80   } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
81     const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
82     boost::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
83
84     if (aDoc == myDocument) {  // If root objects
85       if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
86         int aStart = myPartModels.size();
87         removeSubModel(myPartModels.size() - 1);
88         removeRow(aStart - 1, partFolderNode());
89       } else { // Update top groups (other except parts
90         QModelIndex aIndex = myModel->findGroup(aUpdMsg->group());
91         int aStart = myModel->rowCount(aIndex);
92         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
93         removeRow(aStart - 1, aIndex);
94       }
95     } else {
96       XGUI_PartModel* aPartModel = 0;
97       QList<XGUI_PartModel*>::const_iterator aIt;
98       for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
99         if ((*aIt)->hasDocument(aDoc)) {
100           aPartModel = (*aIt);
101           break;
102         }
103       }
104       if (aPartModel) {
105         QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group());
106         int aStart = aPartModel->rowCount(aIndex);
107         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
108         removeRow(aStart - 1, aIndex);
109       }
110     }
111
112   // Deleted object event ***********************
113   } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_UPDATED) {
114     const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
115     boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
116     boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
117     
118     QModelIndex aIndex;
119     emit dataChanged(aIndex, aIndex);
120
121   // Reset whole tree **************************
122   } else {  
123     beginResetModel();
124     int aNbParts = myDocument->size(PARTS_GROUP);
125     if (myPartModels.size() != aNbParts) { // resize internal models
126       while (myPartModels.size() > aNbParts) {
127         delete myPartModels.last();
128         myPartModels.removeLast();
129       }
130       while (myPartModels.size() < aNbParts) {
131         myPartModels.append(new XGUI_PartDataModel(myDocument, this));
132       }
133       for (int i = 0; i < myPartModels.size(); i++)
134         myPartModels.at(i)->setPartId(i);
135     }
136     clearModelIndexes();
137     endResetModel();
138   }
139 }
140
141 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
142 {
143   if (!theIndex.isValid())
144     return QVariant();
145   switch (theIndex.internalId()) {
146   case PartsFolder:
147     switch (theRole) {
148     case Qt::DisplayRole:
149       return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
150     case Qt::DecorationRole:
151       return QIcon(":pictures/constr_folder.png");
152     case Qt::ToolTipRole:
153       return tr("Parts folder");
154     default:
155       return QVariant();
156     }
157     break;
158   case HistoryNode:
159     {
160       int aOffset = historyOffset();
161       FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
162       switch (theRole) {
163       case Qt::DisplayRole:
164         if (aFeature)
165           return aFeature->data()->getName().c_str();
166         else 
167           return QVariant();
168       case Qt::DecorationRole:
169         {
170           std::string aType = aFeature->getKind();
171           if (aType.compare("Point") == 0)
172             return QIcon(":pictures/point_ico.png");
173           if (aType.compare("Part") == 0)
174             return QIcon(":pictures/part_ico.png");
175           if (aType.compare("Sketch") == 0)
176             return QIcon(":icons/sketch.png");
177         }
178       case Qt::ToolTipRole:
179         return tr("Feature object");
180       default:
181         return QVariant();
182       }
183     }
184     break;
185   }
186   QModelIndex aParent = theIndex.parent();
187   if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
188     return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
189   }
190   return toSourceModelIndex(theIndex).data(theRole);
191 }
192
193
194 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
195 {
196   return QVariant();
197 }
198
199 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
200 {
201   if (!theParent.isValid()) {
202     // Size of external models
203     int aVal = historyOffset();
204     // Plus history size
205     aVal += myDocument->size(FEATURES_GROUP);
206     return aVal;
207   }
208   if (theParent.internalId() == PartsFolder) {
209     return myPartModels.size();
210   }
211   if (theParent.internalId() == HistoryNode) {
212     return 0;
213   }
214   QModelIndex aParent = toSourceModelIndex(theParent);
215   if (!isSubModel(aParent.model())) 
216     return 0;
217
218   return aParent.model()->rowCount(aParent);
219 }
220
221 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
222 {
223   return 1;
224 }
225
226 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
227 {
228   QModelIndex aIndex;
229   if (!theParent.isValid()) {
230     int aOffs = myModel->rowCount();
231     if (theRow < aOffs) {
232       aIndex = myModel->index(theRow, theColumn, theParent);
233       aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
234     } else {
235       if (theRow == aOffs)  // Create Parts node
236         aIndex = partFolderNode();
237       else // create history node
238         aIndex = createIndex(theRow, theColumn, HistoryNode);
239     }
240   } else {
241     if (theParent.internalId() == PartsFolder) {
242       aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
243     } else {
244       QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
245       aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
246     }
247     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
248   }
249   return aIndex;
250 }
251
252
253 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
254 {
255   if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
256     return QModelIndex();
257
258   QModelIndex aIndex = toSourceModelIndex(theIndex);
259   const QAbstractItemModel* aModel = aIndex.model();
260   if (!isSubModel(aModel)) 
261     return QModelIndex();
262
263   if (isPartSubModel(aModel)) {
264     if (!aModel->parent(aIndex).isValid()) {
265       return partFolderNode();
266     }
267   }
268
269   aIndex = aModel->parent(aIndex);
270   if (aIndex.isValid())
271     return createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
272   return aIndex;
273 }
274
275
276 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
277 {
278   if (!theParent.isValid())
279     return true;
280   return rowCount(theParent) > 0;
281 }
282
283
284 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
285 {
286   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
287   return (*aIndexPtr);
288 }
289
290
291 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
292 {
293   QList<QModelIndex*>::const_iterator aIt;
294   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
295     QModelIndex* aIndex = (*aIt);
296     if ((*aIndex) == theIndex)
297       return aIndex;
298   }
299   return 0;
300 }
301
302 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
303 {
304   QModelIndex* aIndexPtr = findModelIndex(theIndex);
305   if (!aIndexPtr) {
306     aIndexPtr = new QModelIndex(theIndex);
307     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
308     that->myIndexes.append(aIndexPtr);
309   }
310   return aIndexPtr;
311 }
312
313 void XGUI_DocumentDataModel::clearModelIndexes()
314 {
315   QList<QModelIndex*>::const_iterator aIt;
316   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
317     delete (*aIt);
318   myIndexes.clear();
319 }
320
321 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
322 {
323   if (theIndex.internalId() == PartsFolder)
324     return FeaturePtr();
325   if (theIndex.internalId() == HistoryNode) {
326       int aOffset = historyOffset();
327       return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
328   }
329   QModelIndex aIndex = toSourceModelIndex(theIndex);
330   if (!isSubModel(aIndex.model())) 
331     return FeaturePtr();
332
333   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
334   return aModel->feature(aIndex);
335 }
336
337 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
338 {
339   beginInsertRows(theParent, theRow, theRow + theCount - 1);
340   //endInsertRows();
341
342   // Update history
343   QModelIndex aRoot;
344   int aRow = rowCount(aRoot);
345   beginInsertRows(aRoot, aRow, aRow);
346   endInsertRows();
347
348   return true;
349 }
350
351 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
352 {
353   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
354   endRemoveRows();
355   return true;
356 }
357
358
359 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
360 {
361   XGUI_PartModel* aModel = myPartModels.at(theModelId);
362   QIntList aToRemove;
363   for (int i = 0; i < myIndexes.size(); i++) {
364     if (myIndexes.at(i)->model() == aModel)
365       aToRemove.append(i);
366   }
367   int aId;
368   while(aToRemove.size() > 0) {
369     aId = aToRemove.last();
370     delete myIndexes.at(aId);
371     myIndexes.removeAt(aId);
372     aToRemove.removeLast();
373   }
374   delete aModel;
375   myPartModels.removeAt(theModelId);
376 }
377
378 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
379 {
380   if (theModel == myModel)
381     return true;
382   return isPartSubModel(theModel);
383 }
384
385 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
386 {
387   return myPartModels.contains((XGUI_PartModel*)theModel);
388 }
389
390 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
391 {
392   int aPos = myModel->rowCount(QModelIndex());
393   return createIndex(aPos, columnCount() - 1, PartsFolder);
394 }
395
396 int XGUI_DocumentDataModel::historyOffset() const
397 {
398   // Nb of rows of top model + Parts folder
399   return myModel->rowCount(QModelIndex()) + 1;
400 }