]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_DocumentDataModel.cpp
Salome HOME
719b6f466d2dcb19e14eb6c49dc3a50d0e4bf253
[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       if (!aFeature)
163         return QVariant();
164       switch (theRole) {
165       case Qt::DisplayRole:
166         if (aFeature)
167           return aFeature->data()->getName().c_str();
168         else 
169           return QVariant();
170       case Qt::DecorationRole:
171         {
172           std::string aType = aFeature->getKind();
173           if (aType.compare("Point") == 0)
174             return QIcon(":pictures/point_ico.png");
175           if (aType.compare("Part") == 0)
176             return QIcon(":pictures/part_ico.png");
177           if (aType.compare("Sketch") == 0)
178             return QIcon(":icons/sketch.png");
179         }
180       case Qt::ToolTipRole:
181         return tr("Feature object");
182       default:
183         return QVariant();
184       }
185     }
186     break;
187   }
188   QModelIndex aParent = theIndex.parent();
189   if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
190     return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
191   }
192   return toSourceModelIndex(theIndex).data(theRole);
193 }
194
195
196 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
197 {
198   return QVariant();
199 }
200
201 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
202 {
203   if (!theParent.isValid()) {
204     // Size of external models
205     int aVal = historyOffset();
206     // Plus history size
207     aVal += myDocument->size(FEATURES_GROUP);
208     return aVal;
209   }
210   if (theParent.internalId() == PartsFolder) {
211     return myPartModels.size();
212   }
213   if (theParent.internalId() == HistoryNode) {
214     return 0;
215   }
216   QModelIndex aParent = toSourceModelIndex(theParent);
217   if (!isSubModel(aParent.model())) 
218     return 0;
219
220   return aParent.model()->rowCount(aParent);
221 }
222
223 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
224 {
225   return 1;
226 }
227
228 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
229 {
230   QModelIndex aIndex;
231   if (!theParent.isValid()) {
232     int aOffs = myModel->rowCount();
233     if (theRow < aOffs) {
234       aIndex = myModel->index(theRow, theColumn, theParent);
235       aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
236     } else {
237       if (theRow == aOffs)  // Create Parts node
238         aIndex = partFolderNode();
239       else // create history node
240         aIndex = createIndex(theRow, theColumn, HistoryNode);
241     }
242   } else {
243     if (theParent.internalId() == PartsFolder) {
244       aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
245     } else {
246       QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
247       aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
248     }
249     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
250   }
251   return aIndex;
252 }
253
254
255 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
256 {
257   if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
258     return QModelIndex();
259
260   QModelIndex aIndex = toSourceModelIndex(theIndex);
261   const QAbstractItemModel* aModel = aIndex.model();
262   if (!isSubModel(aModel)) 
263     return QModelIndex();
264
265   if (isPartSubModel(aModel)) {
266     if (!aModel->parent(aIndex).isValid()) {
267       return partFolderNode();
268     }
269   }
270
271   aIndex = aModel->parent(aIndex);
272   if (aIndex.isValid())
273     return createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
274   return aIndex;
275 }
276
277
278 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
279 {
280   if (!theParent.isValid())
281     return true;
282   return rowCount(theParent) > 0;
283 }
284
285
286 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
287 {
288   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
289   return (*aIndexPtr);
290 }
291
292
293 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
294 {
295   QList<QModelIndex*>::const_iterator aIt;
296   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
297     QModelIndex* aIndex = (*aIt);
298     if ((*aIndex) == theIndex)
299       return aIndex;
300   }
301   return 0;
302 }
303
304 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
305 {
306   QModelIndex* aIndexPtr = findModelIndex(theIndex);
307   if (!aIndexPtr) {
308     aIndexPtr = new QModelIndex(theIndex);
309     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
310     that->myIndexes.append(aIndexPtr);
311   }
312   return aIndexPtr;
313 }
314
315 void XGUI_DocumentDataModel::clearModelIndexes()
316 {
317   QList<QModelIndex*>::const_iterator aIt;
318   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
319     delete (*aIt);
320   myIndexes.clear();
321 }
322
323 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
324 {
325   if (theIndex.internalId() == PartsFolder)
326     return FeaturePtr();
327   if (theIndex.internalId() == HistoryNode) {
328       int aOffset = historyOffset();
329       return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
330   }
331   QModelIndex aIndex = toSourceModelIndex(theIndex);
332   if (!isSubModel(aIndex.model())) 
333     return FeaturePtr();
334
335   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
336   return aModel->feature(aIndex);
337 }
338
339 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
340 {
341   beginInsertRows(theParent, theRow, theRow + theCount - 1);
342   //endInsertRows();
343
344   // Update history
345   QModelIndex aRoot;
346   int aRow = rowCount(aRoot);
347   beginInsertRows(aRoot, aRow, aRow);
348   endInsertRows();
349
350   return true;
351 }
352
353 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
354 {
355   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
356   endRemoveRows();
357   return true;
358 }
359
360
361 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
362 {
363   XGUI_PartModel* aModel = myPartModels.at(theModelId);
364   QIntList aToRemove;
365   for (int i = 0; i < myIndexes.size(); i++) {
366     if (myIndexes.at(i)->model() == aModel)
367       aToRemove.append(i);
368   }
369   int aId;
370   while(aToRemove.size() > 0) {
371     aId = aToRemove.last();
372     delete myIndexes.at(aId);
373     myIndexes.removeAt(aId);
374     aToRemove.removeLast();
375   }
376   delete aModel;
377   myPartModels.removeAt(theModelId);
378 }
379
380 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
381 {
382   if (theModel == myModel)
383     return true;
384   return isPartSubModel(theModel);
385 }
386
387 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
388 {
389   return myPartModels.contains((XGUI_PartModel*)theModel);
390 }
391
392 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
393 {
394   int aPos = myModel->rowCount(QModelIndex());
395   return createIndex(aPos, columnCount() - 1, PartsFolder);
396 }
397
398 int XGUI_DocumentDataModel::historyOffset() const
399 {
400   // Nb of rows of top model + Parts folder
401   return myModel->rowCount(QModelIndex()) + 1;
402 }