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 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
76 insertRow(aStart, aIndex);
78 } else { // if sub-objects of first level nodes
79 XGUI_PartModel* aPartModel = 0;
80 QList<XGUI_PartModel*>::const_iterator aIt;
81 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
82 if ((*aIt)->hasDocument(aDoc)) {
88 QModelIndex aIndex = aPartModel->findParent(aObject);
89 int aStart = aPartModel->rowCount(aIndex); // check this index
90 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
91 insertRow(aStart, aIndex);
95 // Deleted object event ***********************
96 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
97 const ModelAPI_ObjectDeletedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
98 DocumentPtr aDoc = aUpdMsg->document();
99 std::set<std::string> aGroups = aUpdMsg->groups();
101 std::set<std::string>::const_iterator aIt;
102 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
103 std::string aGroup = (*aIt);
104 if (aDoc == aRootDoc) { // If root objects
105 if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
106 int aStart = myPartModels.size() - 1;
107 removeSubModel(aStart);
108 removeRow(aStart, partFolderNode());
109 if (myActivePart && (!isPartSubModel(myActivePart))) {
111 myActivePartIndex = QModelIndex();
112 myModel->setItemsColor(ACTIVE_COLOR);
114 } else { // Update top groups (other except parts
115 QModelIndex aIndex = myModel->findGroup(aGroup);
116 int aStart = myModel->rowCount(aIndex);
117 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
118 removeRow(aStart, aIndex);
121 XGUI_PartModel* aPartModel = 0;
122 QList<XGUI_PartModel*>::const_iterator aIt;
123 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
124 if ((*aIt)->hasDocument(aDoc)) {
130 QModelIndex aIndex = aPartModel->findGroup(aGroup);
131 int aStart = aPartModel->rowCount(aIndex);
132 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
133 removeRow(aStart, aIndex);
137 // Deleted object event ***********************
138 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
139 //const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
140 //ObjectPtr aFeature = aUpdMsg->feature();
141 //DocumentPtr aDoc = aFeature->document();
143 // TODO: Identify the necessary index by the modified feature
145 emit dataChanged(aIndex, aIndex);
147 // Reset whole tree **************************
153 void XGUI_DocumentDataModel::rebuildDataTree()
155 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
160 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
161 if (myPartModels.size() != aNbParts) { // resize internal models
162 while (myPartModels.size() > aNbParts) {
163 delete myPartModels.last();
164 myPartModels.removeLast();
166 while (myPartModels.size() < aNbParts) {
167 myPartModels.append(new XGUI_PartDataModel(this));
169 for (int i = 0; i < myPartModels.size(); i++)
170 myPartModels.at(i)->setPartId(i);
175 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
177 if (!theIndex.isValid())
179 switch (theIndex.internalId()) {
182 case Qt::DisplayRole:
183 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
184 case Qt::DecorationRole:
185 return QIcon(":pictures/constr_folder.png");
186 case Qt::ToolTipRole:
187 return tr("Parts folder");
188 case Qt::ForegroundRole:
190 return QBrush(PASSIVE_COLOR);
192 return QBrush(ACTIVE_COLOR);
199 int aOffset = historyOffset();
200 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
201 ObjectPtr aObj = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
202 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
206 case Qt::DisplayRole:
208 return aFeature->data()->name().c_str();
211 case Qt::DecorationRole:
212 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
213 case Qt::ToolTipRole:
214 return tr("Feature object");
215 case Qt::ForegroundRole:
217 return QBrush(PASSIVE_COLOR);
219 return QBrush(ACTIVE_COLOR);
226 QModelIndex aParent = theIndex.parent();
227 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
228 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
230 return toSourceModelIndex(theIndex)->data(theRole);
234 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
239 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
241 if (!theParent.isValid()) {
242 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
243 // Size of external models
244 int aVal = historyOffset();
246 aVal += aRootDoc->size(ModelAPI_Feature::group());
249 if (theParent.internalId() == PartsFolder) {
250 int aSize = myPartModels.size();
251 return myPartModels.size();
253 if (theParent.internalId() == HistoryNode) {
256 QModelIndex* aParent = toSourceModelIndex(theParent);
257 const QAbstractItemModel* aModel = aParent->model();
258 if (!isSubModel(aModel))
261 /*if (isPartSubModel(aModel)) {
262 if (aModel != myActivePart)
265 return aModel->rowCount(*aParent);
268 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
273 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
276 if (!theParent.isValid()) {
277 int aOffs = myModel->rowCount();
278 if (theRow < aOffs) {
279 aIndex = myModel->index(theRow, theColumn, theParent);
280 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
282 if (theRow == aOffs) // Create Parts node
283 aIndex = partFolderNode();
284 else // create history node
285 aIndex = createIndex(theRow, theColumn, HistoryNode);
288 if (theParent.internalId() == PartsFolder) {
289 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
291 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
292 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
294 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
300 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
302 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
303 return QModelIndex();
305 QModelIndex* aIndex = toSourceModelIndex(theIndex);
306 const QAbstractItemModel* aModel = aIndex->model();
307 if (!isSubModel(aModel))
308 return QModelIndex();
310 if (isPartSubModel(aModel)) {
311 if (!aModel->parent(*aIndex).isValid()) {
312 return partFolderNode();
316 QModelIndex aIndex1 = aModel->parent(*aIndex);
317 if (aIndex1.isValid())
318 return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
323 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
325 if (!theParent.isValid())
327 return rowCount(theParent) > 0;
331 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
333 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
338 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
340 QList<QModelIndex*>::const_iterator aIt;
341 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
342 QModelIndex* aIndex = (*aIt);
343 if ((*aIndex) == theIndex)
349 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
351 QModelIndex* aIndexPtr = findModelIndex(theIndex);
353 aIndexPtr = new QModelIndex(theIndex);
354 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
355 that->myIndexes.append(aIndexPtr);
360 void XGUI_DocumentDataModel::clearModelIndexes()
362 QList<QModelIndex*>::const_iterator aIt;
363 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
368 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
370 if (theIndex.internalId() == PartsFolder)
372 if (theIndex.internalId() == HistoryNode) {
373 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
374 int aOffset = historyOffset();
375 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
377 QModelIndex* aIndex = toSourceModelIndex(theIndex);
378 if (!isSubModel(aIndex->model()))
381 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
382 return aModel->object(*aIndex);
385 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
387 beginInsertRows(theParent, theRow, theRow + theCount - 1);
392 int aRow = rowCount(aRoot);
393 beginInsertRows(aRoot, aRow, aRow);
399 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
401 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
407 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
409 XGUI_PartModel* aModel = myPartModels.at(theModelId);
411 for (int i = 0; i < myIndexes.size(); i++) {
412 if (myIndexes.at(i)->model() == aModel)
416 while(aToRemove.size() > 0) {
417 aId = aToRemove.last();
418 delete myIndexes.at(aId);
419 myIndexes.removeAt(aId);
420 aToRemove.removeLast();
423 myPartModels.removeAt(theModelId);
426 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
428 if (theModel == myModel)
430 return isPartSubModel(theModel);
433 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
435 return myPartModels.contains((XGUI_PartModel*)theModel);
438 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
440 int aPos = myModel->rowCount(QModelIndex());
441 return createIndex(aPos, columnCount() - 1, PartsFolder);
444 int XGUI_DocumentDataModel::historyOffset() const
446 // Nb of rows of top model + Parts folder
447 return myModel->rowCount(QModelIndex()) + 1;
450 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
452 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
455 QModelIndex* aIndex = toSourceModelIndex(theIndex);
459 const QAbstractItemModel* aModel = aIndex->model();
461 if (isPartSubModel(aModel)) {
462 // if this is root node (Part item index)
463 if (!aIndex->parent().isValid()) {
464 if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
466 if (myActivePart == aModel) {
468 myActivePartIndex = QModelIndex();
470 myActivePart = (XGUI_PartModel*)aModel;
471 myActivePartIndex = theIndex;
475 myActivePart->setItemsColor(ACTIVE_COLOR);
476 myModel->setItemsColor(PASSIVE_COLOR);
478 myModel->setItemsColor(ACTIVE_COLOR);
485 ResultPartPtr XGUI_DocumentDataModel::activePart() const
488 return myActivePart->part();
489 return ResultPartPtr();
492 void XGUI_DocumentDataModel::deactivatePart()
495 myActivePart->setItemsColor(PASSIVE_COLOR);
497 myActivePartIndex = QModelIndex();
498 myModel->setItemsColor(ACTIVE_COLOR);
501 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
503 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
504 if (object(theIndex)) {
505 aFlags |= Qt::ItemIsEditable;
510 QModelIndex XGUI_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
513 XGUI_PartModel* aModel = 0;
514 foreach (XGUI_PartModel* aPartModel, myPartModels) {
516 if (aPartModel->part() == theObject) {
522 return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
524 return QModelIndex();
527 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
529 // Check that this feature belongs to root document
530 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
531 DocumentPtr aDoc = theObject->document();
532 if (aDoc == aRootDoc) {
533 // This feature belongs to histrory or top model
534 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
537 int aNb = aRootDoc->size(ModelAPI_Feature::group());
538 for (aId = 0; aId < aNb; aId++) {
539 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
543 return index(aId + historyOffset(), 0, QModelIndex());
545 QModelIndex aIndex = myModel->objectIndex(theObject);
546 return aIndex.isValid()?
547 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
551 XGUI_PartModel* aPartModel = 0;
552 foreach(XGUI_PartModel* aModel, myPartModels) {
553 if (aModel->hasDocument(aDoc)) {
559 QModelIndex aIndex = aPartModel->objectIndex(theObject);
560 return aIndex.isValid()?
561 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
565 return QModelIndex();