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()
47 void XGUI_DocumentDataModel::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
49 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
51 // Created object event *******************
52 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
53 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
54 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
55 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
57 std::set<ObjectPtr>::const_iterator aIt;
58 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
59 ObjectPtr aObject = (*aIt);
60 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
61 if (aFeature && (!aFeature->isInHistory()))
64 DocumentPtr aDoc = aObject->document();
65 if (aDoc == aRootDoc) { // If root objects
66 if (aObject->groupName() == ModelAPI_ResultPart::group()) { // Update only Parts group
68 int aStart = myPartModels.size();
69 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
70 aModel->setPartId(myPartModels.count());
71 myPartModels.append(aModel);
72 insertRow(aStart, partFolderNode());
73 } else { // Update top groups (other except parts
74 QModelIndex aIndex = myModel->findParent(aObject);
75 int aStart = myModel->rowCount(aIndex) - 1;
78 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
79 insertRow(aStart, aIndex);
81 } else { // if sub-objects of first level nodes
82 XGUI_PartModel* aPartModel = 0;
83 QList<XGUI_PartModel*>::const_iterator aIt;
84 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
85 if ((*aIt)->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 QList<XGUI_PartModel*>::const_iterator aIt;
127 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
128 if ((*aIt)->hasDocument(aDoc)) {
134 QModelIndex aIndex = aPartModel->findGroup(aGroup);
135 int aStart = aPartModel->rowCount(aIndex);
136 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
137 removeRow(aStart, aIndex);
141 // Deleted object event ***********************
142 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
143 //boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg = boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
144 //ObjectPtr aFeature = aUpdMsg->feature();
145 //DocumentPtr aDoc = aFeature->document();
147 // TODO: Identify the necessary index by the modified feature
149 emit dataChanged(aIndex, aIndex);
151 // Reset whole tree **************************
157 void XGUI_DocumentDataModel::rebuildDataTree()
159 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
164 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
165 if (myPartModels.size() != aNbParts) { // resize internal models
166 while (myPartModels.size() > aNbParts) {
167 delete myPartModels.last();
168 myPartModels.removeLast();
170 while (myPartModels.size() < aNbParts) {
171 myPartModels.append(new XGUI_PartDataModel(this));
173 for (int i = 0; i < myPartModels.size(); i++)
174 myPartModels.at(i)->setPartId(i);
179 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
181 if (!theIndex.isValid())
183 switch (theIndex.internalId()) {
186 case Qt::DisplayRole:
187 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
188 case Qt::DecorationRole:
189 return QIcon(":pictures/constr_folder.png");
190 case Qt::ToolTipRole:
191 return tr("Parts folder");
192 case Qt::ForegroundRole:
194 return QBrush(PASSIVE_COLOR);
196 return QBrush(ACTIVE_COLOR);
203 int aOffset = historyOffset();
204 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
205 ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
206 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
210 case Qt::DisplayRole:
212 return aFeature->data()->name().c_str();
215 case Qt::DecorationRole:
216 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
217 case Qt::ToolTipRole:
218 return tr("Feature object");
219 case Qt::ForegroundRole:
221 return QBrush(PASSIVE_COLOR);
223 return QBrush(ACTIVE_COLOR);
230 QModelIndex aParent = theIndex.parent();
231 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
232 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
234 return toSourceModelIndex(theIndex)->data(theRole);
237 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient,
243 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
245 if (!theParent.isValid()) {
246 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
247 // Size of external models
248 int aVal = historyOffset();
250 aVal += aRootDoc->size(ModelAPI_Feature::group());
253 if (theParent.internalId() == PartsFolder) {
254 int aSize = myPartModels.size();
255 return myPartModels.size();
257 if (theParent.internalId() == HistoryNode) {
260 QModelIndex* aParent = toSourceModelIndex(theParent);
261 const QAbstractItemModel* aModel = aParent->model();
262 if (!isSubModel(aModel))
265 /*if (isPartSubModel(aModel)) {
266 if (aModel != myActivePart)
269 return aModel->rowCount(*aParent);
272 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
277 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn,
278 const QModelIndex& theParent) const
281 if (!theParent.isValid()) {
282 int aOffs = myModel->rowCount();
283 if (theRow < aOffs) {
284 aIndex = myModel->index(theRow, theColumn, theParent);
285 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
287 if (theRow == aOffs) // Create Parts node
288 aIndex = partFolderNode();
290 // create history node
291 aIndex = createIndex(theRow, theColumn, HistoryNode);
294 if (theParent.internalId() == PartsFolder) {
295 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
297 QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
298 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
300 aIndex = createIndex(theRow, theColumn, (void*) getModelIndex(aIndex));
305 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
307 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
308 return QModelIndex();
310 QModelIndex* aIndex = toSourceModelIndex(theIndex);
311 const QAbstractItemModel* aModel = aIndex->model();
312 if (!isSubModel(aModel))
313 return QModelIndex();
315 if (isPartSubModel(aModel)) {
316 if (!aModel->parent(*aIndex).isValid()) {
317 return partFolderNode();
321 QModelIndex aIndex1 = aModel->parent(*aIndex);
322 if (aIndex1.isValid())
323 return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
327 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
329 if (!theParent.isValid())
331 return rowCount(theParent) > 0;
334 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
336 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
340 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
342 QList<QModelIndex*>::const_iterator aIt;
343 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
344 QModelIndex* aIndex = (*aIt);
345 if ((*aIndex) == theIndex)
351 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
353 QModelIndex* aIndexPtr = findModelIndex(theIndex);
355 aIndexPtr = new QModelIndex(theIndex);
356 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
357 that->myIndexes.append(aIndexPtr);
362 void XGUI_DocumentDataModel::clearModelIndexes()
364 QList<QModelIndex*>::const_iterator aIt;
365 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
370 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
372 if (theIndex.internalId() == PartsFolder)
374 if (theIndex.internalId() == HistoryNode) {
375 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
376 int aOffset = historyOffset();
377 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
379 QModelIndex* aIndex = toSourceModelIndex(theIndex);
380 if (!isSubModel(aIndex->model()))
383 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
384 return aModel->object(*aIndex);
387 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
389 beginInsertRows(theParent, theRow, theRow + theCount - 1);
394 int aRow = rowCount(aRoot);
395 beginInsertRows(aRoot, aRow, aRow);
401 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
403 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
408 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
410 XGUI_PartModel* aModel = myPartModels.at(theModelId);
412 for (int i = 0; i < myIndexes.size(); i++) {
413 if (myIndexes.at(i)->model() == aModel)
417 while (aToRemove.size() > 0) {
418 aId = aToRemove.last();
419 delete myIndexes.at(aId);
420 myIndexes.removeAt(aId);
421 aToRemove.removeLast();
424 myPartModels.removeAt(theModelId);
427 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
429 if (theModel == myModel)
431 return isPartSubModel(theModel);
434 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
436 return myPartModels.contains((XGUI_PartModel*) theModel);
439 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
441 int aPos = myModel->rowCount(QModelIndex());
442 return createIndex(aPos, columnCount() - 1, PartsFolder);
445 int XGUI_DocumentDataModel::historyOffset() const
447 // Nb of rows of top model + Parts folder
448 return myModel->rowCount(QModelIndex()) + 1;
451 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
453 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
456 QModelIndex* aIndex = toSourceModelIndex(theIndex);
460 const QAbstractItemModel* aModel = aIndex->model();
462 if (isPartSubModel(aModel)) {
463 // if this is root node (Part item index)
464 if (!aIndex->parent().isValid()) {
466 myActivePart->setItemsColor(PASSIVE_COLOR);
468 if (myActivePart == aModel) {
470 myActivePartIndex = QModelIndex();
472 myActivePart = (XGUI_PartModel*)aModel;
473 myActivePartIndex = theIndex;
477 myActivePart->setItemsColor(ACTIVE_COLOR);
478 myModel->setItemsColor(PASSIVE_COLOR);
480 myModel->setItemsColor(ACTIVE_COLOR);
487 ResultPartPtr XGUI_DocumentDataModel::activePart() const
490 return myActivePart->part();
491 return ResultPartPtr();
494 void XGUI_DocumentDataModel::deactivatePart()
497 myActivePart->setItemsColor(PASSIVE_COLOR);
499 myActivePartIndex = QModelIndex();
500 myModel->setItemsColor(ACTIVE_COLOR);
503 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
505 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
506 if (object(theIndex)) {
507 aFlags |= Qt::ItemIsEditable;
512 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
515 XGUI_PartModel* aModel = 0;
516 foreach (XGUI_PartModel* aPartModel, myPartModels)
519 if (aPartModel->part() == theObject) {
525 return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
527 return QModelIndex();
530 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
532 // Check that this feature belongs to root document
533 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
534 DocumentPtr aDoc = theObject->document();
535 if (aDoc == aRootDoc) {
536 // This feature belongs to histrory or top model
537 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
540 int aNb = aRootDoc->size(ModelAPI_Feature::group());
541 for (aId = 0; aId < aNb; aId++) {
542 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
546 return index(aId + historyOffset(), 0, QModelIndex());
548 QModelIndex aIndex = myModel->objectIndex(theObject);
551 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
555 XGUI_PartModel* aPartModel = 0;
556 foreach(XGUI_PartModel* aModel, myPartModels)
558 if (aModel->hasDocument(aDoc)) {
564 QModelIndex aIndex = aPartModel->objectIndex(theObject);
567 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
571 return QModelIndex();