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 boost::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 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
55 boost::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 = boost::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 boost::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
101 boost::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 removeSubModel(aStart);
112 removeRow(aStart, partFolderNode());
113 if (myActivePart && (!isPartSubModel(myActivePart))) {
115 myActivePartIndex = QModelIndex();
116 myModel->setItemsColor(ACTIVE_COLOR);
118 } else { // Update top groups (other except parts
119 QModelIndex aIndex = myModel->findGroup(aGroup);
120 int aStart = myModel->rowCount(aIndex);
121 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
122 removeRow(aStart, aIndex);
125 XGUI_PartModel* aPartModel = 0;
126 foreach (XGUI_PartModel* aPart, myPartModels) {
127 if (aPart->hasDocument(aDoc)) {
133 QModelIndex aIndex = aPartModel->findGroup(aGroup);
134 int aStart = aPartModel->rowCount(aIndex);
135 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
136 removeRow(aStart, aIndex);
140 // Deleted object event ***********************
141 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
142 //boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg = boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
143 //ObjectPtr aFeature = aUpdMsg->feature();
144 //DocumentPtr aDoc = aFeature->document();
146 // TODO: Identify the necessary index by the modified feature
148 emit dataChanged(aIndex, aIndex);
150 // Reset whole tree **************************
156 void XGUI_DocumentDataModel::rebuildDataTree()
158 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
163 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
164 if (myPartModels.size() != aNbParts) { // resize internal models
165 while (myPartModels.size() > aNbParts) {
166 delete myPartModels.last();
167 myPartModels.removeLast();
169 while (myPartModels.size() < aNbParts) {
170 myPartModels.append(new XGUI_PartDataModel(this));
172 for (int i = 0; i < myPartModels.size(); i++)
173 myPartModels.at(i)->setPartId(i);
178 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
180 if (!theIndex.isValid())
182 switch (theIndex.internalId()) {
185 case Qt::DisplayRole:
186 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
187 case Qt::DecorationRole:
188 return QIcon(":pictures/constr_folder.png");
189 case Qt::ToolTipRole:
190 return tr("Parts folder");
191 case Qt::ForegroundRole:
193 return QBrush(PASSIVE_COLOR);
195 return QBrush(ACTIVE_COLOR);
202 int aOffset = historyOffset();
203 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
204 ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
205 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
209 case Qt::DisplayRole:
211 return aFeature->data()->name().c_str();
214 case Qt::DecorationRole:
215 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
216 case Qt::ToolTipRole:
217 return tr("Feature object");
218 case Qt::ForegroundRole:
220 return QBrush(PASSIVE_COLOR);
222 return QBrush(ACTIVE_COLOR);
229 QModelIndex aParent = theIndex.parent();
230 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
231 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
233 return toSourceModelIndex(theIndex)->data(theRole);
236 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient,
242 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
244 if (!theParent.isValid()) {
245 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
246 // Size of external models
247 int aVal = historyOffset();
249 aVal += aRootDoc->size(ModelAPI_Feature::group());
252 if (theParent.internalId() == PartsFolder) {
253 int aSize = myPartModels.size();
254 return myPartModels.size();
256 if (theParent.internalId() == HistoryNode) {
259 QModelIndex* aParent = toSourceModelIndex(theParent);
260 const QAbstractItemModel* aModel = aParent->model();
261 if (!isSubModel(aModel))
264 /*if (isPartSubModel(aModel)) {
265 if (aModel != myActivePart)
268 return aModel->rowCount(*aParent);
271 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
276 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn,
277 const QModelIndex& theParent) const
280 if (!theParent.isValid()) {
281 int aOffs = myModel->rowCount();
282 if (theRow < aOffs) {
283 aIndex = myModel->index(theRow, theColumn, theParent);
284 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
286 if (theRow == aOffs) // Create Parts node
287 aIndex = partFolderNode();
289 // create history node
290 aIndex = createIndex(theRow, theColumn, HistoryNode);
293 if (theParent.internalId() == PartsFolder) {
294 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
296 QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
297 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
299 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
304 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
306 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
307 return QModelIndex();
309 QModelIndex* aIndex = toSourceModelIndex(theIndex);
310 const QAbstractItemModel* aModel = aIndex->model();
311 if (!isSubModel(aModel))
312 return QModelIndex();
314 if (isPartSubModel(aModel)) {
315 if (!aModel->parent(*aIndex).isValid()) {
316 return partFolderNode();
320 QModelIndex aIndex1 = aModel->parent(*aIndex);
321 if (aIndex1.isValid())
322 return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
326 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
328 if (!theParent.isValid())
330 return rowCount(theParent) > 0;
333 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
335 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
339 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
341 QList<QModelIndex*>::const_iterator aIt;
342 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
343 QModelIndex* aIndex = (*aIt);
344 if ((*aIndex) == theIndex)
350 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
352 QModelIndex* aIndexPtr = findModelIndex(theIndex);
354 aIndexPtr = new QModelIndex(theIndex);
355 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
356 that->myIndexes.append(aIndexPtr);
361 void XGUI_DocumentDataModel::clearModelIndexes()
363 foreach (QModelIndex* aIndex, myIndexes)
368 void XGUI_DocumentDataModel::clearSubModels()
370 foreach (XGUI_PartModel* aPart, myPartModels)
372 myPartModels.clear();
375 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
377 if (theIndex.internalId() == PartsFolder)
379 if (theIndex.internalId() == HistoryNode) {
380 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
381 int aOffset = historyOffset();
382 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
384 QModelIndex* aIndex = toSourceModelIndex(theIndex);
385 if (!isSubModel(aIndex->model()))
388 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
389 return aModel->object(*aIndex);
392 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
394 beginInsertRows(theParent, theRow, theRow + theCount - 1);
399 int aRow = rowCount(aRoot);
400 beginInsertRows(aRoot, aRow, aRow);
406 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
408 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
413 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
415 XGUI_PartModel* aModel = myPartModels.at(theModelId);
417 for (int i = 0; i < myIndexes.size(); i++) {
418 if (myIndexes.at(i)->model() == aModel)
422 while (aToRemove.size() > 0) {
423 aId = aToRemove.last();
424 delete myIndexes.at(aId);
425 myIndexes.removeAt(aId);
426 aToRemove.removeLast();
429 myPartModels.removeAt(theModelId);
432 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
434 if (theModel == myModel)
436 return isPartSubModel(theModel);
439 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
441 return myPartModels.contains((XGUI_PartModel*) theModel);
444 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
446 int aPos = myModel->rowCount(QModelIndex());
447 return createIndex(aPos, columnCount() - 1, PartsFolder);
450 int XGUI_DocumentDataModel::historyOffset() const
452 // Nb of rows of top model + Parts folder
453 return myModel->rowCount(QModelIndex()) + 1;
456 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
458 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
461 QModelIndex* aIndex = toSourceModelIndex(theIndex);
465 const QAbstractItemModel* aModel = aIndex->model();
467 if (isPartSubModel(aModel)) {
468 // if this is root node (Part item index)
469 if (!aIndex->parent().isValid()) {
471 myActivePart->setItemsColor(PASSIVE_COLOR);
473 if (myActivePart == aModel) {
475 myActivePartIndex = QModelIndex();
477 myActivePart = (XGUI_PartModel*)aModel;
478 myActivePartIndex = theIndex;
482 myActivePart->setItemsColor(ACTIVE_COLOR);
483 myModel->setItemsColor(PASSIVE_COLOR);
485 myModel->setItemsColor(ACTIVE_COLOR);
492 ResultPartPtr XGUI_DocumentDataModel::activePart() const
495 return myActivePart->part();
496 return ResultPartPtr();
499 void XGUI_DocumentDataModel::deactivatePart()
502 myActivePart->setItemsColor(PASSIVE_COLOR);
504 myActivePartIndex = QModelIndex();
505 myModel->setItemsColor(ACTIVE_COLOR);
508 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
510 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
511 if (object(theIndex)) {
512 aFlags |= Qt::ItemIsEditable;
517 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
520 XGUI_PartModel* aModel = 0;
521 foreach (XGUI_PartModel* aPartModel, myPartModels)
524 if (aPartModel->part() == theObject) {
530 return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
532 return QModelIndex();
535 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
537 // Check that this feature belongs to root document
538 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
539 DocumentPtr aDoc = theObject->document();
540 if (aDoc == aRootDoc) {
541 // This feature belongs to histrory or top model
542 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
545 int aNb = aRootDoc->size(ModelAPI_Feature::group());
546 for (aId = 0; aId < aNb; aId++) {
547 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
551 return index(aId + historyOffset(), 0, QModelIndex());
553 QModelIndex aIndex = myModel->objectIndex(theObject);
556 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
560 XGUI_PartModel* aPartModel = 0;
561 foreach(XGUI_PartModel* aModel, myPartModels)
563 if (aModel->hasDocument(aDoc)) {
569 QModelIndex aIndex = aPartModel->objectIndex(theObject);
572 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
576 return QModelIndex();
580 void XGUI_DocumentDataModel::clear()
585 myActivePartIndex = QModelIndex();
586 myModel->setItemsColor(ACTIVE_COLOR);