1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
3 #include "XGUI_Workshop.h"
4 #include "XGUI_Tools.h"
6 #include <ModelAPI_PluginManager.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), myActivePart(0)
30 // Register in event loop
31 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
32 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
33 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
35 // Create a top part of data tree model
36 myModel = new XGUI_TopDataModel(this);
37 myModel->setItemsColor(ACTIVE_COLOR);
41 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
47 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
49 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
51 // Created object event *******************
52 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
53 const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
54 std::set<ObjectPtr> aObjects = aUpdMsg->objects();
56 std::set<ObjectPtr>::const_iterator aIt;
57 for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
58 ObjectPtr aObject = (*aIt);
59 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
60 if (aFeature && (!aFeature->isInHistory()))
63 DocumentPtr aDoc = aObject->document();
64 if (aDoc == aRootDoc) { // If root objects
65 if (aObject->groupName() == ModelAPI_ResultPart::group()) { // Update only Parts group
67 int aStart = myPartModels.size();
68 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
69 aModel->setPartId(myPartModels.count());
70 myPartModels.append(aModel);
71 insertRow(aStart, partFolderNode());
72 } else { // Update top groups (other except parts
73 QModelIndex aIndex = myModel->findParent(aObject);
74 int aStart = myModel->rowCount(aIndex) - 1;
75 if (aStart < 0) aStart = 0;
76 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
77 insertRow(aStart, aIndex);
79 } else { // if sub-objects of first level nodes
80 XGUI_PartModel* aPartModel = 0;
81 QList<XGUI_PartModel*>::const_iterator aIt;
82 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
83 if ((*aIt)->hasDocument(aDoc)) {
89 QModelIndex aIndex = aPartModel->findParent(aObject);
90 int aStart = aPartModel->rowCount(aIndex); // check this index
91 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
92 insertRow(aStart, aIndex);
96 // Deleted object event ***********************
97 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
98 const ModelAPI_ObjectDeletedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
99 DocumentPtr aDoc = aUpdMsg->document();
100 std::set<std::string> aGroups = aUpdMsg->groups();
102 std::set<std::string>::const_iterator aIt;
103 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
104 std::string aGroup = (*aIt);
105 if (aDoc == aRootDoc) { // If root objects
106 if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
107 int aStart = myPartModels.size() - 1;
108 removeSubModel(aStart);
109 removeRow(aStart, partFolderNode());
110 if (myActivePart && (!isPartSubModel(myActivePart))) {
112 myActivePartIndex = QModelIndex();
113 myModel->setItemsColor(ACTIVE_COLOR);
115 } else { // Update top groups (other except parts
116 QModelIndex aIndex = myModel->findGroup(aGroup);
117 int aStart = myModel->rowCount(aIndex);
118 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
119 removeRow(aStart, aIndex);
122 XGUI_PartModel* aPartModel = 0;
123 QList<XGUI_PartModel*>::const_iterator aIt;
124 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
125 if ((*aIt)->hasDocument(aDoc)) {
131 QModelIndex aIndex = aPartModel->findGroup(aGroup);
132 int aStart = aPartModel->rowCount(aIndex);
133 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
134 removeRow(aStart, aIndex);
138 // Deleted object event ***********************
139 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
140 //const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
141 //ObjectPtr aFeature = aUpdMsg->feature();
142 //DocumentPtr aDoc = aFeature->document();
144 // TODO: Identify the necessary index by the modified feature
146 emit dataChanged(aIndex, aIndex);
148 // Reset whole tree **************************
154 void XGUI_DocumentDataModel::rebuildDataTree()
156 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
161 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
162 if (myPartModels.size() != aNbParts) { // resize internal models
163 while (myPartModels.size() > aNbParts) {
164 delete myPartModels.last();
165 myPartModels.removeLast();
167 while (myPartModels.size() < aNbParts) {
168 myPartModels.append(new XGUI_PartDataModel(this));
170 for (int i = 0; i < myPartModels.size(); i++)
171 myPartModels.at(i)->setPartId(i);
176 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
178 if (!theIndex.isValid())
180 switch (theIndex.internalId()) {
183 case Qt::DisplayRole:
184 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
185 case Qt::DecorationRole:
186 return QIcon(":pictures/constr_folder.png");
187 case Qt::ToolTipRole:
188 return tr("Parts folder");
189 case Qt::ForegroundRole:
191 return QBrush(PASSIVE_COLOR);
193 return QBrush(ACTIVE_COLOR);
200 int aOffset = historyOffset();
201 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
202 ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
203 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
207 case Qt::DisplayRole:
209 return aFeature->data()->name().c_str();
212 case Qt::DecorationRole:
213 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
214 case Qt::ToolTipRole:
215 return tr("Feature object");
216 case Qt::ForegroundRole:
218 return QBrush(PASSIVE_COLOR);
220 return QBrush(ACTIVE_COLOR);
227 QModelIndex aParent = theIndex.parent();
228 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
229 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
231 return toSourceModelIndex(theIndex)->data(theRole);
235 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
240 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
242 if (!theParent.isValid()) {
243 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
244 // Size of external models
245 int aVal = historyOffset();
247 aVal += aRootDoc->size(ModelAPI_Feature::group());
250 if (theParent.internalId() == PartsFolder) {
251 int aSize = myPartModels.size();
252 return myPartModels.size();
254 if (theParent.internalId() == HistoryNode) {
257 QModelIndex* aParent = toSourceModelIndex(theParent);
258 const QAbstractItemModel* aModel = aParent->model();
259 if (!isSubModel(aModel))
262 /*if (isPartSubModel(aModel)) {
263 if (aModel != myActivePart)
266 return aModel->rowCount(*aParent);
269 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
274 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
277 if (!theParent.isValid()) {
278 int aOffs = myModel->rowCount();
279 if (theRow < aOffs) {
280 aIndex = myModel->index(theRow, theColumn, theParent);
281 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
283 if (theRow == aOffs) // Create Parts node
284 aIndex = partFolderNode();
285 else // create history node
286 aIndex = createIndex(theRow, theColumn, HistoryNode);
289 if (theParent.internalId() == PartsFolder) {
290 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
292 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
293 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
295 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
301 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
303 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
304 return QModelIndex();
306 QModelIndex* aIndex = toSourceModelIndex(theIndex);
307 const QAbstractItemModel* aModel = aIndex->model();
308 if (!isSubModel(aModel))
309 return QModelIndex();
311 if (isPartSubModel(aModel)) {
312 if (!aModel->parent(*aIndex).isValid()) {
313 return partFolderNode();
317 QModelIndex aIndex1 = aModel->parent(*aIndex);
318 if (aIndex1.isValid())
319 return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
324 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
326 if (!theParent.isValid())
328 return rowCount(theParent) > 0;
332 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
334 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 QList<QModelIndex*>::const_iterator aIt;
364 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
369 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
371 if (theIndex.internalId() == PartsFolder)
373 if (theIndex.internalId() == HistoryNode) {
374 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
375 int aOffset = historyOffset();
376 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
378 QModelIndex* aIndex = toSourceModelIndex(theIndex);
379 if (!isSubModel(aIndex->model()))
382 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
383 return aModel->object(*aIndex);
386 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
388 beginInsertRows(theParent, theRow, theRow + theCount - 1);
393 int aRow = rowCount(aRoot);
394 beginInsertRows(aRoot, aRow, aRow);
400 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
402 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()) {
465 if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
467 if (myActivePart == aModel) {
469 myActivePartIndex = QModelIndex();
471 myActivePart = (XGUI_PartModel*)aModel;
472 myActivePartIndex = theIndex;
476 myActivePart->setItemsColor(ACTIVE_COLOR);
477 myModel->setItemsColor(PASSIVE_COLOR);
479 myModel->setItemsColor(ACTIVE_COLOR);
486 ResultPartPtr XGUI_DocumentDataModel::activePart() const
489 return myActivePart->part();
490 return ResultPartPtr();
493 void XGUI_DocumentDataModel::deactivatePart()
496 myActivePart->setItemsColor(PASSIVE_COLOR);
498 myActivePartIndex = QModelIndex();
499 myModel->setItemsColor(ACTIVE_COLOR);
502 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
504 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
505 if (object(theIndex)) {
506 aFlags |= Qt::ItemIsEditable;
511 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
514 XGUI_PartModel* aModel = 0;
515 foreach (XGUI_PartModel* aPartModel, myPartModels) {
517 if (aPartModel->part() == theObject) {
523 return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
525 return QModelIndex();
528 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
530 // Check that this feature belongs to root document
531 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
532 DocumentPtr aDoc = theObject->document();
533 if (aDoc == aRootDoc) {
534 // This feature belongs to histrory or top model
535 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
538 int aNb = aRootDoc->size(ModelAPI_Feature::group());
539 for (aId = 0; aId < aNb; aId++) {
540 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
544 return index(aId + historyOffset(), 0, QModelIndex());
546 QModelIndex aIndex = myModel->objectIndex(theObject);
547 return aIndex.isValid()?
548 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
552 XGUI_PartModel* aPartModel = 0;
553 foreach(XGUI_PartModel* aModel, myPartModels) {
554 if (aModel->hasDocument(aDoc)) {
560 QModelIndex aIndex = aPartModel->objectIndex(theObject);
561 return aIndex.isValid()?
562 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
566 return QModelIndex();