1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
3 #include "XGUI_Workshop.h"
4 #include "XGUI_Tools.h"
6 #include <ModelAPI_Session.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultPart.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Object.h>
14 #include <Events_Loop.h>
16 #include <Config_FeatureMessage.h>
24 #define ACTIVE_COLOR QColor(0,72,140)
25 #define PASSIVE_COLOR Qt::black
27 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
28 : QAbstractItemModel(theParent),
31 // Register in event loop
32 //Events_Loop* aLoop = Events_Loop::loop();
33 //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
34 //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
35 //aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
37 // Create a top part of data tree model
38 myModel = new XGUI_TopDataModel(this);
39 myModel->setItemsColor(ACTIVE_COLOR);
42 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
48 void XGUI_DocumentDataModel::processEvent(const std::shared_ptr<Events_Message>& theMessage)
50 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
52 // Created object event *******************
53 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
54 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
55 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
56 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
58 std::set<ObjectPtr>::const_iterator aIt;
59 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
60 ObjectPtr aObject = (*aIt);
61 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
62 if (aFeature && (!aFeature->isInHistory()))
65 DocumentPtr aDoc = aObject->document();
66 if (aDoc == aRootDoc) { // If root objects
67 if (aObject->groupName() == ModelAPI_ResultPart::group()) { // Update only Parts group
69 int aStart = myPartModels.size();
70 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
71 aModel->setPartId(myPartModels.count());
72 myPartModels.append(aModel);
73 insertRow(aStart, partFolderNode());
74 } else { // Update top groups (other except parts
75 QModelIndex aIndex = myModel->findParent(aObject);
76 int aStart = myModel->rowCount(aIndex) - 1;
79 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
80 insertRow(aStart, aIndex);
82 } else { // if sub-objects of first level nodes
83 XGUI_PartModel* aPartModel = 0;
84 foreach (XGUI_PartModel* aPart, myPartModels) {
85 if (aPart->hasDocument(aDoc)) {
91 QModelIndex aIndex = aPartModel->findParent(aObject);
92 int aStart = aPartModel->rowCount(aIndex); // check this index
93 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
94 insertRow(aStart, aIndex);
98 // Deleted object event ***********************
99 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
100 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
101 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
102 DocumentPtr aDoc = aUpdMsg->document();
103 std::set<std::string> aGroups = aUpdMsg->groups();
105 std::set<std::string>::const_iterator aIt;
106 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
107 std::string aGroup = (*aIt);
108 if (aDoc == aRootDoc) { // If root objects
109 if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
110 int aStart = myPartModels.size() - 1;
111 if (aStart >= 0) {// MPV: this could be reproduced on close
112 removeSubModel(aStart);
113 removeRow(aStart, partFolderNode());
114 if (myActivePart && (!isPartSubModel(myActivePart))) {
116 myActivePartIndex = QModelIndex();
117 myModel->setItemsColor(ACTIVE_COLOR);
120 } else { // Update top groups (other except parts
121 QModelIndex aIndex = myModel->findGroup(aGroup);
122 int aStart = myModel->rowCount(aIndex);
123 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
124 removeRow(aStart, aIndex);
127 XGUI_PartModel* aPartModel = 0;
128 foreach (XGUI_PartModel* aPart, myPartModels) {
129 if (aPart->hasDocument(aDoc)) {
135 QModelIndex aIndex = aPartModel->findGroup(aGroup);
136 int aStart = aPartModel->rowCount(aIndex);
137 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
138 removeRow(aStart, aIndex);
142 // Deleted object event ***********************
143 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
144 //std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg = std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
145 //ObjectPtr aFeature = aUpdMsg->feature();
146 //DocumentPtr aDoc = aFeature->document();
148 // TODO: Identify the necessary index by the modified feature
150 emit dataChanged(aIndex, aIndex);
152 // Reset whole tree **************************
158 void XGUI_DocumentDataModel::rebuildDataTree()
160 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
165 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
166 if (myPartModels.size() != aNbParts) { // resize internal models
167 while (myPartModels.size() > aNbParts) {
168 delete myPartModels.last();
169 myPartModels.removeLast();
171 while (myPartModels.size() < aNbParts) {
172 myPartModels.append(new XGUI_PartDataModel(this));
174 for (int i = 0; i < myPartModels.size(); i++)
175 myPartModels.at(i)->setPartId(i);
180 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
182 if (!theIndex.isValid())
184 switch (theIndex.internalId()) {
187 case Qt::DisplayRole:
188 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
189 case Qt::DecorationRole:
190 return QIcon(":pictures/constr_folder.png");
191 case Qt::ToolTipRole:
192 return tr("Parts folder");
193 case Qt::ForegroundRole:
195 return QBrush(PASSIVE_COLOR);
197 return QBrush(ACTIVE_COLOR);
204 int aOffset = historyOffset();
205 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
206 ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
207 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
211 case Qt::DisplayRole:
213 return aFeature->data()->name().c_str();
216 case Qt::DecorationRole:
217 return XGUI_Workshop::featureIcon(aFeature);
218 case Qt::ToolTipRole:
219 return tr("Feature object");
220 case Qt::ForegroundRole:
222 return QBrush(PASSIVE_COLOR);
224 return QBrush(ACTIVE_COLOR);
231 QModelIndex aParent = theIndex.parent();
232 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
233 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
235 return toSourceModelIndex(theIndex)->data(theRole);
238 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient,
244 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
246 if (!theParent.isValid()) {
247 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
248 // Size of external models
249 int aVal = historyOffset();
251 aVal += aRootDoc->size(ModelAPI_Feature::group());
254 if (theParent.internalId() == PartsFolder) {
255 int aSize = myPartModels.size();
256 return myPartModels.size();
258 if (theParent.internalId() == HistoryNode) {
261 QModelIndex* aParent = toSourceModelIndex(theParent);
262 const QAbstractItemModel* aModel = aParent->model();
263 if (!isSubModel(aModel))
266 /*if (isPartSubModel(aModel)) {
267 if (aModel != myActivePart)
270 return aModel->rowCount(*aParent);
273 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
278 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn,
279 const QModelIndex& theParent) const
282 if (!theParent.isValid()) {
283 int aOffs = myModel->rowCount();
284 if (theRow < aOffs) {
285 aIndex = myModel->index(theRow, theColumn, theParent);
286 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
288 if (theRow == aOffs) // Create Parts node
289 aIndex = partFolderNode();
291 // create history node
292 aIndex = createIndex(theRow, theColumn, HistoryNode);
295 if (theParent.internalId() == PartsFolder) {
296 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
298 QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
299 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
301 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
306 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
308 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
309 return QModelIndex();
311 QModelIndex* aIndex = toSourceModelIndex(theIndex);
312 const QAbstractItemModel* aModel = aIndex->model();
313 if (!isSubModel(aModel))
314 return QModelIndex();
316 if (isPartSubModel(aModel)) {
317 if (!aModel->parent(*aIndex).isValid()) {
318 return partFolderNode();
322 QModelIndex aIndex1 = aModel->parent(*aIndex);
323 if (aIndex1.isValid())
324 return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
328 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
330 if (!theParent.isValid())
332 return rowCount(theParent) > 0;
335 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
337 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
341 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
343 QList<QModelIndex*>::const_iterator aIt;
344 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
345 QModelIndex* aIndex = (*aIt);
346 if ((*aIndex) == theIndex)
352 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
354 QModelIndex* aIndexPtr = findModelIndex(theIndex);
356 aIndexPtr = new QModelIndex(theIndex);
357 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
358 that->myIndexes.append(aIndexPtr);
363 void XGUI_DocumentDataModel::clearModelIndexes()
365 foreach (QModelIndex* aIndex, myIndexes)
370 void XGUI_DocumentDataModel::clearSubModels()
372 foreach (XGUI_PartModel* aPart, myPartModels)
374 myPartModels.clear();
377 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
379 if (theIndex.internalId() == PartsFolder)
381 if (theIndex.internalId() == HistoryNode) {
382 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
383 int aOffset = historyOffset();
384 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
386 QModelIndex* aIndex = toSourceModelIndex(theIndex);
387 if (!isSubModel(aIndex->model()))
390 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
391 return aModel->object(*aIndex);
394 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
396 beginInsertRows(theParent, theRow, theRow + theCount - 1);
401 int aRow = rowCount(aRoot);
402 beginInsertRows(aRoot, aRow, aRow);
408 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
410 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
415 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
417 XGUI_PartModel* aModel = myPartModels.at(theModelId);
419 for (int i = 0; i < myIndexes.size(); i++) {
420 if (myIndexes.at(i)->model() == aModel)
424 while (aToRemove.size() > 0) {
425 aId = aToRemove.last();
426 delete myIndexes.at(aId);
427 myIndexes.removeAt(aId);
428 aToRemove.removeLast();
431 myPartModels.removeAt(theModelId);
434 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
436 if (theModel == myModel)
438 return isPartSubModel(theModel);
441 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
443 return myPartModels.contains((XGUI_PartModel*) theModel);
446 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
448 int aPos = myModel->rowCount(QModelIndex());
449 return createIndex(aPos, columnCount() - 1, PartsFolder);
452 int XGUI_DocumentDataModel::historyOffset() const
454 // Nb of rows of top model + Parts folder
455 return myModel->rowCount(QModelIndex()) + 1;
458 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
460 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
463 QModelIndex* aIndex = toSourceModelIndex(theIndex);
467 const QAbstractItemModel* aModel = aIndex->model();
469 if (isPartSubModel(aModel)) {
470 // if this is root node (Part item index)
471 if (!aIndex->parent().isValid()) {
473 myActivePart->setItemsColor(PASSIVE_COLOR);
475 if (myActivePart == aModel) {
477 myActivePartIndex = QModelIndex();
479 myActivePart = (XGUI_PartModel*)aModel;
480 myActivePartIndex = theIndex;
484 myActivePart->setItemsColor(ACTIVE_COLOR);
485 myModel->setItemsColor(PASSIVE_COLOR);
487 myModel->setItemsColor(ACTIVE_COLOR);
494 ResultPartPtr XGUI_DocumentDataModel::activePart() const
497 return myActivePart->part();
498 return ResultPartPtr();
501 void XGUI_DocumentDataModel::deactivatePart()
504 myActivePart->setItemsColor(PASSIVE_COLOR);
506 myActivePartIndex = QModelIndex();
507 myModel->setItemsColor(ACTIVE_COLOR);
510 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
512 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
513 if (object(theIndex)) {
514 aFlags |= Qt::ItemIsEditable;
519 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
522 XGUI_PartModel* aModel = 0;
523 foreach (XGUI_PartModel* aPartModel, myPartModels)
526 if (aPartModel->part() == theObject) {
532 return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
534 return QModelIndex();
537 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
539 // Check that this feature belongs to root document
540 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
541 DocumentPtr aDoc = theObject->document();
542 if (aDoc == aRootDoc) {
543 // This feature belongs to histrory or top model
544 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
547 int aNb = aRootDoc->size(ModelAPI_Feature::group());
548 for (aId = 0; aId < aNb; aId++) {
549 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
553 return index(aId + historyOffset(), 0, QModelIndex());
555 QModelIndex aIndex = myModel->objectIndex(theObject);
558 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
562 XGUI_PartModel* aPartModel = 0;
563 foreach(XGUI_PartModel* aModel, myPartModels)
565 if (aModel->hasDocument(aDoc)) {
571 QModelIndex aIndex = aPartModel->objectIndex(theObject);
574 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
578 return QModelIndex();
582 void XGUI_DocumentDataModel::clear()
587 myActivePartIndex = QModelIndex();
588 myModel->setItemsColor(ACTIVE_COLOR);