Salome HOME
ebae53b82849b7f98631299e36dc6841fe45dff3
[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         insertRows(partFolderNode(), aStart, aStart);
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         insertRows(aIndex, aStart, aStart);
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         insertRows(aIndex, aStart, aStart);
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         beginRemoveRows(partFolderNode(), aStart, aStart);
88         removeSubModel(myPartModels.size() - 1);
89         endRemoveRows();
90       } else { // Update top groups (other except parts
91         QModelIndex aIndex = myModel->findGroup(aUpdMsg->group());
92         int aStart = myModel->rowCount(aIndex);
93         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
94         beginRemoveRows(aIndex, aStart, aStart);
95         endRemoveRows();
96       }
97     } else {
98       XGUI_PartModel* aPartModel = 0;
99       QList<XGUI_PartModel*>::const_iterator aIt;
100       for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
101         if ((*aIt)->hasDocument(aDoc)) {
102           aPartModel = (*aIt);
103           break;
104         }
105       }
106       if (aPartModel) {
107         QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group());
108         int aStart = aPartModel->rowCount(aIndex);
109         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
110         beginRemoveRows(aIndex, aStart, aStart);
111         endRemoveRows();
112       }
113     }
114
115   // Deleted object event ***********************
116   } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_UPDATED) {
117     const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
118     boost::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
119     boost::shared_ptr<ModelAPI_Document> aDoc = aFeature->document();
120     
121     QModelIndex aIndex;
122     emit dataChanged(aIndex, aIndex);
123
124   // Reset whole tree **************************
125   } else {  
126     beginResetModel();
127     int aNbParts = myDocument->size(PARTS_GROUP);
128     if (myPartModels.size() != aNbParts) { // resize internal models
129       while (myPartModels.size() > aNbParts) {
130         delete myPartModels.last();
131         myPartModels.removeLast();
132       }
133       while (myPartModels.size() < aNbParts) {
134         myPartModels.append(new XGUI_PartDataModel(myDocument, this));
135       }
136       for (int i = 0; i < myPartModels.size(); i++)
137         myPartModels.at(i)->setPartId(i);
138     }
139     clearModelIndexes();
140     endResetModel();
141   }
142 }
143
144 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
145 {
146   if (!theIndex.isValid())
147     return QVariant();
148   if (theIndex.internalId() == 0){
149     switch (theRole) {
150     case Qt::DisplayRole:
151       return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
152     case Qt::DecorationRole:
153       return QIcon(":pictures/constr_folder.png");
154     case Qt::ToolTipRole:
155       return tr("Parts folder");
156     default:
157       return QVariant();
158     }
159   }
160   QModelIndex aParent = theIndex.parent();
161   if (aParent.isValid() && (aParent.internalId() == 0)) {
162     return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
163   }
164   return toSourceModelIndex(theIndex).data(theRole);
165 }
166
167
168 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
169 {
170   return QVariant();
171 }
172
173 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
174 {
175   if (!theParent.isValid()) {
176     int aVal = myModel->rowCount(theParent) + myPartModels.size();
177     return myModel->rowCount(theParent) + 1;//myPartModels.size();
178   }
179   if (theParent.internalId() == 0) {
180     return myPartModels.size();
181   }
182   QModelIndex aParent = toSourceModelIndex(theParent);
183   if (!isSubModel(aParent.model())) 
184     return 0;
185
186   return aParent.model()->rowCount(aParent);
187 }
188
189 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
190 {
191   return 1;
192 }
193
194 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
195 {
196   QModelIndex aIndex;
197   if (!theParent.isValid()) {
198     int aOffs = myModel->rowCount();
199     if (theRow < aOffs) {
200       aIndex = myModel->index(theRow, theColumn, theParent);
201       aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
202     } else {
203       // Create Parts node
204       aIndex = partFolderNode();
205     }
206   } else {
207     if (theParent.internalId() == 0) {
208       aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
209     } else {
210       QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
211       aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
212     }
213     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
214   }
215   return aIndex;
216 }
217
218
219 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
220 {
221   if (theIndex.internalId() == 0)
222     return QModelIndex();
223
224   QModelIndex aIndex = toSourceModelIndex(theIndex);
225   const QAbstractItemModel* aModel = aIndex.model();
226   if (!isSubModel(aModel)) 
227     return QModelIndex();
228
229   if (isPartSubModel(aModel)) {
230     if (!aModel->parent(aIndex).isValid()) {
231       return partFolderNode();
232     }
233   }
234
235   aIndex = aModel->parent(aIndex);
236   if (aIndex.isValid())
237     return createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
238   return aIndex;
239 }
240
241
242 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
243 {
244   if (!theParent.isValid())
245     return true;
246   return rowCount(theParent) > 0;
247 }
248
249
250 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
251 {
252   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
253   return (*aIndexPtr);
254 }
255
256
257 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
258 {
259   QList<QModelIndex*>::const_iterator aIt;
260   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
261     QModelIndex* aIndex = (*aIt);
262     if ((*aIndex) == theIndex)
263       return aIndex;
264   }
265   return 0;
266 }
267
268 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
269 {
270   QModelIndex* aIndexPtr = findModelIndex(theIndex);
271   if (!aIndexPtr) {
272     aIndexPtr = new QModelIndex(theIndex);
273     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
274     that->myIndexes.append(aIndexPtr);
275   }
276   return aIndexPtr;
277 }
278
279 void XGUI_DocumentDataModel::clearModelIndexes()
280 {
281   QList<QModelIndex*>::const_iterator aIt;
282   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
283     delete (*aIt);
284   myIndexes.clear();
285 }
286
287 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
288 {
289   if (theIndex.internalId() == 0)
290     return FeaturePtr();
291
292   QModelIndex aIndex = toSourceModelIndex(theIndex);
293   if (!isSubModel(aIndex.model())) 
294     return FeaturePtr();
295
296   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
297   return aModel->feature(aIndex);
298 }
299
300 void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd)
301 {
302   beginInsertRows(theParent, theStart, theEnd);
303   endInsertRows();
304   if (theStart == 0) // Update parent if this is a first child in order to update node decoration
305     emit dataChanged(theParent, theParent);
306 }
307
308 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
309 {
310   XGUI_PartModel* aModel = myPartModels.at(theModelId);
311   QIntList aToRemove;
312   for (int i = 0; i < myIndexes.size(); i++) {
313     if (myIndexes.at(i)->model() == aModel)
314       aToRemove.append(i);
315   }
316   int aId;
317   while(aToRemove.size() > 0) {
318     aId = aToRemove.last();
319     delete myIndexes.at(aId);
320     myIndexes.removeAt(aId);
321     aToRemove.removeLast();
322   }
323   delete aModel;
324   myPartModels.removeAt(theModelId);
325 }
326
327 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
328 {
329   if (theModel == myModel)
330     return true;
331   return isPartSubModel(theModel);
332 }
333
334 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
335 {
336   return myPartModels.contains((XGUI_PartModel*)theModel);
337 }
338
339 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
340 {
341   int aPos = myModel->rowCount(QModelIndex());
342   return createIndex(aPos, columnCount() - 1, 0);
343 }