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));
36 aLoop->registerListener(this, Events_Loop::eventByName("CurrentDocumentChanged"));
38 // Create a top part of data tree model
39 myModel = new XGUI_TopDataModel(this);
40 myModel->setItemsColor(ACTIVE_COLOR);
43 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
48 void XGUI_DocumentDataModel::processEvent(const 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 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
55 dynamic_cast<const 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 QList<XGUI_PartModel*>::const_iterator aIt;
85 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
86 if ((*aIt)->hasDocument(aDoc)) {
92 QModelIndex aIndex = aPartModel->findParent(aObject);
93 int aStart = aPartModel->rowCount(aIndex); // check this index
94 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
95 insertRow(aStart, aIndex);
99 // Deleted object event ***********************
100 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
101 const ModelAPI_ObjectDeletedMessage* aUpdMsg =
102 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
103 DocumentPtr aDoc = aUpdMsg->document();
104 std::set<std::string> aGroups = aUpdMsg->groups();
106 std::set<std::string>::const_iterator aIt;
107 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
108 std::string aGroup = (*aIt);
109 if (aDoc == aRootDoc) { // If root objects
110 if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
111 int aStart = myPartModels.size() - 1;
112 removeSubModel(aStart);
113 removeRow(aStart, partFolderNode());
114 if (myActivePart && (!isPartSubModel(myActivePart))) {
116 myActivePartIndex = QModelIndex();
117 myModel->setItemsColor(ACTIVE_COLOR);
119 } else { // Update top groups (other except parts
120 QModelIndex aIndex = myModel->findGroup(aGroup);
121 int aStart = myModel->rowCount(aIndex);
122 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
123 removeRow(aStart, aIndex);
126 XGUI_PartModel* aPartModel = 0;
127 QList<XGUI_PartModel*>::const_iterator aIt;
128 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
129 if ((*aIt)->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 //const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const 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 = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
211 case Qt::DisplayRole:
213 return aFeature->data()->name().c_str();
216 case Qt::DecorationRole:
217 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
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 QList<QModelIndex*>::const_iterator aIt;
366 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
371 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
373 if (theIndex.internalId() == PartsFolder)
375 if (theIndex.internalId() == HistoryNode) {
376 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
377 int aOffset = historyOffset();
378 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
380 QModelIndex* aIndex = toSourceModelIndex(theIndex);
381 if (!isSubModel(aIndex->model()))
384 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
385 return aModel->object(*aIndex);
388 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
390 beginInsertRows(theParent, theRow, theRow + theCount - 1);
395 int aRow = rowCount(aRoot);
396 beginInsertRows(aRoot, aRow, aRow);
402 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
404 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
409 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
411 XGUI_PartModel* aModel = myPartModels.at(theModelId);
413 for (int i = 0; i < myIndexes.size(); i++) {
414 if (myIndexes.at(i)->model() == aModel)
418 while (aToRemove.size() > 0) {
419 aId = aToRemove.last();
420 delete myIndexes.at(aId);
421 myIndexes.removeAt(aId);
422 aToRemove.removeLast();
425 myPartModels.removeAt(theModelId);
428 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
430 if (theModel == myModel)
432 return isPartSubModel(theModel);
435 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
437 return myPartModels.contains((XGUI_PartModel*) theModel);
440 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
442 int aPos = myModel->rowCount(QModelIndex());
443 return createIndex(aPos, columnCount() - 1, PartsFolder);
446 int XGUI_DocumentDataModel::historyOffset() const
448 // Nb of rows of top model + Parts folder
449 return myModel->rowCount(QModelIndex()) + 1;
452 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
454 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
457 QModelIndex* aIndex = toSourceModelIndex(theIndex);
461 const QAbstractItemModel* aModel = aIndex->model();
463 if (isPartSubModel(aModel)) {
464 // if this is root node (Part item index)
465 if (!aIndex->parent().isValid()) {
467 myActivePart->setItemsColor(PASSIVE_COLOR);
469 if (myActivePart == aModel) {
471 myActivePartIndex = QModelIndex();
473 myActivePart = (XGUI_PartModel*)aModel;
474 myActivePartIndex = theIndex;
478 myActivePart->setItemsColor(ACTIVE_COLOR);
479 myModel->setItemsColor(PASSIVE_COLOR);
481 myModel->setItemsColor(ACTIVE_COLOR);
488 ResultPartPtr XGUI_DocumentDataModel::activePart() const
491 return myActivePart->part();
492 return ResultPartPtr();
495 void XGUI_DocumentDataModel::deactivatePart()
498 myActivePart->setItemsColor(PASSIVE_COLOR);
500 myActivePartIndex = QModelIndex();
501 myModel->setItemsColor(ACTIVE_COLOR);
504 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
506 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
507 if (object(theIndex)) {
508 aFlags |= Qt::ItemIsEditable;
513 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
516 XGUI_PartModel* aModel = 0;
517 foreach (XGUI_PartModel* aPartModel, myPartModels)
520 if (aPartModel->part() == theObject) {
526 return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
528 return QModelIndex();
531 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
533 // Check that this feature belongs to root document
534 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
535 DocumentPtr aDoc = theObject->document();
536 if (aDoc == aRootDoc) {
537 // This feature belongs to histrory or top model
538 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
541 int aNb = aRootDoc->size(ModelAPI_Feature::group());
542 for (aId = 0; aId < aNb; aId++) {
543 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
547 return index(aId + historyOffset(), 0, QModelIndex());
549 QModelIndex aIndex = myModel->objectIndex(theObject);
552 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
556 XGUI_PartModel* aPartModel = 0;
557 foreach(XGUI_PartModel* aModel, myPartModels)
559 if (aModel->hasDocument(aDoc)) {
565 QModelIndex aIndex = aPartModel->objectIndex(theObject);
568 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
572 return QModelIndex();