1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
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>
10 #include <Events_Loop.h>
17 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
18 : QAbstractItemModel(theParent)
20 // Find Document object
21 boost::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
22 myDocument = aMgr->currentDocument();
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));
29 // Create a top part of data tree model
30 myModel = new XGUI_TopDataModel(myDocument, this);
34 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
40 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
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();
48 if (aDoc == myDocument) { // If root objects
49 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
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);
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)) {
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);
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();
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);
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)) {
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);
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();
119 emit dataChanged(aIndex, aIndex);
121 // Reset whole tree **************************
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();
130 while (myPartModels.size() < aNbParts) {
131 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
133 for (int i = 0; i < myPartModels.size(); i++)
134 myPartModels.at(i)->setPartId(i);
141 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
143 if (!theIndex.isValid())
145 switch (theIndex.internalId()) {
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");
160 int aOffset = historyOffset();
161 FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
165 case Qt::DisplayRole:
167 return aFeature->data()->getName().c_str();
170 case Qt::DecorationRole:
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");
180 case Qt::ToolTipRole:
181 return tr("Feature object");
188 QModelIndex aParent = theIndex.parent();
189 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
190 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
192 return toSourceModelIndex(theIndex).data(theRole);
196 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
201 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
203 if (!theParent.isValid()) {
204 // Size of external models
205 int aVal = historyOffset();
207 aVal += myDocument->size(FEATURES_GROUP);
210 if (theParent.internalId() == PartsFolder) {
211 return myPartModels.size();
213 if (theParent.internalId() == HistoryNode) {
216 QModelIndex aParent = toSourceModelIndex(theParent);
217 if (!isSubModel(aParent.model()))
220 return aParent.model()->rowCount(aParent);
223 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
228 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
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));
237 if (theRow == aOffs) // Create Parts node
238 aIndex = partFolderNode();
239 else // create history node
240 aIndex = createIndex(theRow, theColumn, HistoryNode);
243 if (theParent.internalId() == PartsFolder) {
244 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
246 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
247 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
249 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
255 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
257 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
258 return QModelIndex();
260 QModelIndex aIndex = toSourceModelIndex(theIndex);
261 const QAbstractItemModel* aModel = aIndex.model();
262 if (!isSubModel(aModel))
263 return QModelIndex();
265 if (isPartSubModel(aModel)) {
266 if (!aModel->parent(aIndex).isValid()) {
267 return partFolderNode();
271 aIndex = aModel->parent(aIndex);
272 if (aIndex.isValid())
273 return createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
278 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
280 if (!theParent.isValid())
282 return rowCount(theParent) > 0;
286 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
288 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
293 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
295 QList<QModelIndex*>::const_iterator aIt;
296 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
297 QModelIndex* aIndex = (*aIt);
298 if ((*aIndex) == theIndex)
304 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
306 QModelIndex* aIndexPtr = findModelIndex(theIndex);
308 aIndexPtr = new QModelIndex(theIndex);
309 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
310 that->myIndexes.append(aIndexPtr);
315 void XGUI_DocumentDataModel::clearModelIndexes()
317 QList<QModelIndex*>::const_iterator aIt;
318 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
323 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
325 if (theIndex.internalId() == PartsFolder)
327 if (theIndex.internalId() == HistoryNode) {
328 int aOffset = historyOffset();
329 return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
331 QModelIndex aIndex = toSourceModelIndex(theIndex);
332 if (!isSubModel(aIndex.model()))
335 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
336 return aModel->feature(aIndex);
339 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
341 beginInsertRows(theParent, theRow, theRow + theCount - 1);
346 int aRow = rowCount(aRoot);
347 beginInsertRows(aRoot, aRow, aRow);
353 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
355 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
361 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
363 XGUI_PartModel* aModel = myPartModels.at(theModelId);
365 for (int i = 0; i < myIndexes.size(); i++) {
366 if (myIndexes.at(i)->model() == aModel)
370 while(aToRemove.size() > 0) {
371 aId = aToRemove.last();
372 delete myIndexes.at(aId);
373 myIndexes.removeAt(aId);
374 aToRemove.removeLast();
377 myPartModels.removeAt(theModelId);
380 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
382 if (theModel == myModel)
384 return isPartSubModel(theModel);
387 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
389 return myPartModels.contains((XGUI_PartModel*)theModel);
392 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
394 int aPos = myModel->rowCount(QModelIndex());
395 return createIndex(aPos, columnCount() - 1, PartsFolder);
398 int XGUI_DocumentDataModel::historyOffset() const
400 // Nb of rows of top model + Parts folder
401 return myModel->rowCount(QModelIndex()) + 1;