Salome HOME
a13cca5357a171edde2b465434c8cc73421f0d28
[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_Iterator.h>
6 #include <ModelAPI_Document.h>
7 #include <ModelAPI_Feature.h>
8 #include <ModelAPI_Data.h>
9 #include <Model_Events.h>
10
11 #include <Event_Loop.h>
12
13
14 #include <QIcon>
15 #include <QString>
16
17
18 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
19   : QAbstractItemModel(theParent)
20 {
21   // Find Document object
22   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
23   myDocument = aMgr->currentDocument();
24
25   // Register in event loop
26   Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_CREATED));
27   Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
28   Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_DELETED));
29
30   // Create a top part of data tree model
31   myModel = new XGUI_TopDataModel(myDocument, this);
32 }
33
34
35 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
36 {
37   clearModelIndexes();
38 }
39
40
41 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
42 {
43   // Created object event *******************
44   if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
45     const ModelAPI_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureUpdatedMessage*>(theMessage);
46     std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
47     std::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
48
49     if (aDoc == myDocument) {  // If root objects
50       if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
51         // Add a new part
52         int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size();
53         XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
54         aModel->setPartId(myPartModels.count());
55         myPartModels.append(aModel);
56         insertRows(QModelIndex(), aStart, aStart);
57       } else { // Update top groups (other except parts
58         QModelIndex aIndex = myModel->findParent(aFeature);
59         int aStart = myModel->rowCount(aIndex);
60         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
61         insertRows(aIndex, aStart-1, aStart);
62       }
63     } else { // if sub-objects of first level nodes
64       XGUI_PartModel* aPartModel = 0;
65       QList<XGUI_PartModel*>::const_iterator aIt;
66       for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
67         if ((*aIt)->hasDocument(aDoc)) {
68           aPartModel = (*aIt);
69           break;
70         }
71       }
72       if (aPartModel) {
73         QModelIndex aIndex = aPartModel->findParent(aFeature);
74         int aStart = aPartModel->rowCount(aIndex);
75         aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
76         insertRows(aIndex, aStart-1, aStart);
77       }
78     }
79
80   // Deteted object event ***********************
81   } if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
82     const ModelAPI_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureDeletedMessage*>(theMessage);
83     std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
84
85     if (aDoc == myDocument) {  // If root objects
86       int aStart = myPartModels.count() - 1;
87       delete myPartModels.last();
88       myPartModels.removeLast();
89       beginRemoveRows(QModelIndex(), aStart, aStart);
90       endRemoveRows();
91     }
92   // Reset whole tree **************************
93   } else {  
94     beginResetModel();
95     int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
96     if (myPartModels.size() != aNbParts) { // resize internal models
97       while (myPartModels.size() > aNbParts) {
98         delete myPartModels.last();
99         myPartModels.removeLast();
100       }
101       while (myPartModels.size() < aNbParts) {
102         myPartModels.append(new XGUI_PartDataModel(myDocument, this));
103       }
104       for (int i = 0; i < myPartModels.size(); i++)
105         myPartModels.at(i)->setPartId(i);
106     }
107     clearModelIndexes();
108     endResetModel();
109   }
110 }
111
112 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
113 {
114   if (!theIndex.isValid())
115     return QVariant();
116   return toSourceModelIndex(theIndex).data(theRole);
117 }
118
119
120 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
121 {
122   return QVariant();
123 }
124
125 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
126 {
127   if (!theParent.isValid()) 
128     return myModel->rowCount(theParent) + myPartModels.size();
129
130   QModelIndex aParent = toSourceModelIndex(theParent);
131   return aParent.model()->rowCount(aParent);
132 }
133
134 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
135 {
136   return 1;
137 }
138
139 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
140 {
141   QModelIndex aIndex;
142   if (!theParent.isValid()) {
143     int aOffs = myModel->rowCount();
144     if (theRow < aOffs) 
145       aIndex = myModel->index(theRow, theColumn, theParent);
146     else
147       aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
148
149     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
150   } else {
151     QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
152     aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
153
154     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
155   }
156   return aIndex;
157 }
158
159
160 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
161 {
162   QModelIndex aParent = toSourceModelIndex(theIndex);
163   aParent = aParent.model()->parent(aParent);
164   if (aParent.isValid())
165     return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
166   return aParent;
167 }
168
169
170 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
171 {
172   if (!theParent.isValid())
173     return true;
174   return rowCount(theParent) > 0;
175 }
176
177
178 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
179 {
180   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
181   return (*aIndexPtr);
182 }
183
184
185 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
186 {
187   QList<QModelIndex*>::const_iterator aIt;
188   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
189     QModelIndex* aIndex = (*aIt);
190     if ((*aIndex) == theIndex)
191       return aIndex;
192   }
193   return 0;
194 }
195
196 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
197 {
198   QModelIndex* aIndexPtr = findModelIndex(theIndex);
199   if (!aIndexPtr) {
200     aIndexPtr = new QModelIndex(theIndex);
201     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
202     that->myIndexes.append(aIndexPtr);
203   }
204   return aIndexPtr;
205 }
206
207 void XGUI_DocumentDataModel::clearModelIndexes()
208 {
209   QList<QModelIndex*>::const_iterator aIt;
210   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
211     delete (*aIt);
212   myIndexes.clear();
213 }
214
215 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
216 {
217   QModelIndex aIndex = toSourceModelIndex(theIndex);
218   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
219   return aModel->feature(aIndex);
220 }
221
222 void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd)
223 {
224   beginInsertRows(theParent, theStart, theEnd);
225   endInsertRows();
226   if (theStart == 1) // Update parent if this is a first child in order to update node decoration
227     emit dataChanged(theParent, theParent);
228 }