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),
31 // Register in event loop
32 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
33 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
34 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
36 // Create a top part of data tree model
37 myModel = new XGUI_TopDataModel(this);
38 myModel->setItemsColor(ACTIVE_COLOR);
41 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
46 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
48 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
50 // Created object event *******************
51 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
52 const ModelAPI_ObjectUpdatedMessage* aUpdMsg =
53 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;
77 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
78 insertRow(aStart, aIndex);
80 } else { // if sub-objects of first level nodes
81 XGUI_PartModel* aPartModel = 0;
82 QList<XGUI_PartModel*>::const_iterator aIt;
83 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
84 if ((*aIt)->hasDocument(aDoc)) {
90 QModelIndex aIndex = aPartModel->findParent(aObject);
91 int aStart = aPartModel->rowCount(aIndex); // check this index
92 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
93 insertRow(aStart, aIndex);
97 // Deleted object event ***********************
98 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
99 const ModelAPI_ObjectDeletedMessage* aUpdMsg =
100 dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
101 DocumentPtr aDoc = aUpdMsg->document();
102 std::set<std::string> aGroups = aUpdMsg->groups();
104 std::set<std::string>::const_iterator aIt;
105 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
106 std::string aGroup = (*aIt);
107 if (aDoc == aRootDoc) { // If root objects
108 if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
109 int aStart = myPartModels.size() - 1;
110 removeSubModel(aStart);
111 removeRow(aStart, partFolderNode());
112 if (myActivePart && (!isPartSubModel(myActivePart))) {
114 myActivePartIndex = QModelIndex();
115 myModel->setItemsColor(ACTIVE_COLOR);
117 } else { // Update top groups (other except parts
118 QModelIndex aIndex = myModel->findGroup(aGroup);
119 int aStart = myModel->rowCount(aIndex);
120 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
121 removeRow(aStart, aIndex);
124 XGUI_PartModel* aPartModel = 0;
125 QList<XGUI_PartModel*>::const_iterator aIt;
126 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
127 if ((*aIt)->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 //const ModelAPI_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const 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_PluginManager::get()->rootDocument();
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_PluginManager::get()->rootDocument();
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_PluginManager::get()->rootDocument();
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 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);
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()) {
465 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)
518 if (aPartModel->part() == theObject) {
524 return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
526 return QModelIndex();
529 QModelIndex XGUI_DocumentDataModel::objectIndex(const ObjectPtr theObject) const
531 // Check that this feature belongs to root document
532 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
533 DocumentPtr aDoc = theObject->document();
534 if (aDoc == aRootDoc) {
535 // This feature belongs to histrory or top model
536 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
539 int aNb = aRootDoc->size(ModelAPI_Feature::group());
540 for (aId = 0; aId < aNb; aId++) {
541 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
545 return index(aId + historyOffset(), 0, QModelIndex());
547 QModelIndex aIndex = myModel->objectIndex(theObject);
550 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
554 XGUI_PartModel* aPartModel = 0;
555 foreach(XGUI_PartModel* aModel, myPartModels)
557 if (aModel->hasDocument(aDoc)) {
563 QModelIndex aIndex = aPartModel->objectIndex(theObject);
566 createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
570 return QModelIndex();