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 <Model_Events.h>
12 #include <ModelAPI_Object.h>
14 #include <Events_Loop.h>
16 #include <Config_FeatureMessage.h>
23 #define ACTIVE_COLOR QColor(0,72,140)
24 #define PASSIVE_COLOR Qt::black
26 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
27 : QAbstractItemModel(theParent), myActivePart(0)
29 // Register in event loop
30 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
31 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
32 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
34 // Create a top part of data tree model
35 myModel = new XGUI_TopDataModel(this);
36 myModel->setItemsColor(ACTIVE_COLOR);
40 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 Model_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const Model_ObjectUpdatedMessage*>(theMessage);
53 std::set<ObjectPtr> aFeatures = aUpdMsg->features();
55 std::set<ObjectPtr>::const_iterator aIt;
56 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
57 ObjectPtr aFeature = (*aIt);
58 DocumentPtr aDoc = aFeature->document();
59 if (aDoc == aRootDoc) { // If root objects
60 if (aFeature->groupName().compare(ModelAPI_ResultPart::group()) == 0) { // Update only Parts group
62 int aStart = myPartModels.size();
63 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
64 aModel->setPartId(myPartModels.count());
65 myPartModels.append(aModel);
66 insertRow(aStart, partFolderNode());
67 } else { // Update top groups (other except parts
68 QModelIndex aIndex = myModel->findParent(aFeature);
69 int aStart = myModel->rowCount(aIndex) - 1;
70 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
71 insertRow(aStart, aIndex);
73 } else { // if sub-objects of first level nodes
74 XGUI_PartModel* aPartModel = 0;
75 QList<XGUI_PartModel*>::const_iterator aIt;
76 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
77 if ((*aIt)->hasDocument(aDoc)) {
83 QModelIndex aIndex = aPartModel->findParent(aFeature);
84 int aStart = aPartModel->rowCount(aIndex) - 1;
85 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
86 insertRow(aStart, aIndex);
90 // Deleted object event ***********************
91 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
92 const Model_ObjectDeletedMessage* aUpdMsg = dynamic_cast<const Model_ObjectDeletedMessage*>(theMessage);
93 DocumentPtr aDoc = aUpdMsg->document();
94 std::set<std::string> aGroups = aUpdMsg->groups();
96 std::set<std::string>::const_iterator aIt;
97 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
98 std::string aGroup = (*aIt);
99 if (aDoc == aRootDoc) { // If root objects
100 if (aGroup.compare(ModelAPI_ResultPart::group()) == 0) { // Updsate only Parts group
101 int aStart = myPartModels.size() - 1;
102 removeSubModel(aStart);
103 removeRow(aStart, partFolderNode());
104 if (myActivePart && (!isPartSubModel(myActivePart))) {
106 myActivePartIndex = QModelIndex();
107 myModel->setItemsColor(ACTIVE_COLOR);
109 } else { // Update top groups (other except parts
110 QModelIndex aIndex = myModel->findGroup(aGroup);
111 int aStart = myModel->rowCount(aIndex);
112 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
113 removeRow(aStart, aIndex);
116 XGUI_PartModel* aPartModel = 0;
117 QList<XGUI_PartModel*>::const_iterator aIt;
118 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
119 if ((*aIt)->hasDocument(aDoc)) {
125 QModelIndex aIndex = aPartModel->findGroup(aGroup);
126 int aStart = aPartModel->rowCount(aIndex);
127 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
128 removeRow(aStart, aIndex);
132 // Deleted object event ***********************
133 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
134 //const Model_ObjectUpdatedMessage* aUpdMsg = dynamic_cast<const Model_ObjectUpdatedMessage*>(theMessage);
135 //ObjectPtr aFeature = aUpdMsg->feature();
136 //DocumentPtr aDoc = aFeature->document();
138 // TODO: Identify the necessary index by the modified feature
140 emit dataChanged(aIndex, aIndex);
142 // Reset whole tree **************************
148 void XGUI_DocumentDataModel::rebuildDataTree()
150 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
155 int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
156 if (myPartModels.size() != aNbParts) { // resize internal models
157 while (myPartModels.size() > aNbParts) {
158 delete myPartModels.last();
159 myPartModels.removeLast();
161 while (myPartModels.size() < aNbParts) {
162 myPartModels.append(new XGUI_PartDataModel(this));
164 for (int i = 0; i < myPartModels.size(); i++)
165 myPartModels.at(i)->setPartId(i);
170 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
172 if (!theIndex.isValid())
174 switch (theIndex.internalId()) {
177 case Qt::DisplayRole:
178 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
179 case Qt::DecorationRole:
180 return QIcon(":pictures/constr_folder.png");
181 case Qt::ToolTipRole:
182 return tr("Parts folder");
183 case Qt::ForegroundRole:
185 return QBrush(PASSIVE_COLOR);
187 return QBrush(ACTIVE_COLOR);
194 int aOffset = historyOffset();
195 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
196 ObjectPtr aFeature = aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
200 case Qt::DisplayRole:
202 return aFeature->data()->name().c_str();
205 case Qt::DecorationRole:
206 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
207 case Qt::ToolTipRole:
208 return tr("Feature object");
209 case Qt::ForegroundRole:
211 return QBrush(PASSIVE_COLOR);
213 return QBrush(ACTIVE_COLOR);
220 QModelIndex aParent = theIndex.parent();
221 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
222 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
224 return toSourceModelIndex(theIndex)->data(theRole);
228 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
233 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
235 if (!theParent.isValid()) {
236 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
237 // Size of external models
238 int aVal = historyOffset();
240 aVal += aRootDoc->size(ModelAPI_Feature::group());
243 if (theParent.internalId() == PartsFolder) {
244 return myPartModels.size();
246 if (theParent.internalId() == HistoryNode) {
249 QModelIndex* aParent = toSourceModelIndex(theParent);
250 const QAbstractItemModel* aModel = aParent->model();
251 if (!isSubModel(aModel))
254 /*if (isPartSubModel(aModel)) {
255 if (aModel != myActivePart)
258 return aModel->rowCount(*aParent);
261 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
266 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
269 if (!theParent.isValid()) {
270 int aOffs = myModel->rowCount();
271 if (theRow < aOffs) {
272 aIndex = myModel->index(theRow, theColumn, theParent);
273 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
275 if (theRow == aOffs) // Create Parts node
276 aIndex = partFolderNode();
277 else // create history node
278 aIndex = createIndex(theRow, theColumn, HistoryNode);
281 if (theParent.internalId() == PartsFolder) {
282 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
284 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
285 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
287 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
293 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
295 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
296 return QModelIndex();
298 QModelIndex* aIndex = toSourceModelIndex(theIndex);
299 const QAbstractItemModel* aModel = aIndex->model();
300 if (!isSubModel(aModel))
301 return QModelIndex();
303 if (isPartSubModel(aModel)) {
304 if (!aModel->parent(*aIndex).isValid()) {
305 return partFolderNode();
309 QModelIndex aIndex1 = aModel->parent(*aIndex);
310 if (aIndex1.isValid())
311 return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
316 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
318 if (!theParent.isValid())
320 return rowCount(theParent) > 0;
324 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
326 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
331 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
333 QList<QModelIndex*>::const_iterator aIt;
334 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
335 QModelIndex* aIndex = (*aIt);
336 if ((*aIndex) == theIndex)
342 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
344 QModelIndex* aIndexPtr = findModelIndex(theIndex);
346 aIndexPtr = new QModelIndex(theIndex);
347 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
348 that->myIndexes.append(aIndexPtr);
353 void XGUI_DocumentDataModel::clearModelIndexes()
355 QList<QModelIndex*>::const_iterator aIt;
356 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
361 ObjectPtr XGUI_DocumentDataModel::object(const QModelIndex& theIndex) const
363 if (theIndex.internalId() == PartsFolder)
365 if (theIndex.internalId() == HistoryNode) {
366 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
367 int aOffset = historyOffset();
368 return aRootDoc->object(ModelAPI_Feature::group(), theIndex.row() - aOffset);
370 QModelIndex* aIndex = toSourceModelIndex(theIndex);
371 if (!isSubModel(aIndex->model()))
374 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
375 return aModel->feature(*aIndex);
378 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
380 beginInsertRows(theParent, theRow, theRow + theCount - 1);
385 int aRow = rowCount(aRoot);
386 beginInsertRows(aRoot, aRow, aRow);
392 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
394 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
400 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
402 XGUI_PartModel* aModel = myPartModels.at(theModelId);
404 for (int i = 0; i < myIndexes.size(); i++) {
405 if (myIndexes.at(i)->model() == aModel)
409 while(aToRemove.size() > 0) {
410 aId = aToRemove.last();
411 delete myIndexes.at(aId);
412 myIndexes.removeAt(aId);
413 aToRemove.removeLast();
416 myPartModels.removeAt(theModelId);
419 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
421 if (theModel == myModel)
423 return isPartSubModel(theModel);
426 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
428 return myPartModels.contains((XGUI_PartModel*)theModel);
431 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
433 int aPos = myModel->rowCount(QModelIndex());
434 return createIndex(aPos, columnCount() - 1, PartsFolder);
437 int XGUI_DocumentDataModel::historyOffset() const
439 // Nb of rows of top model + Parts folder
440 return myModel->rowCount(QModelIndex()) + 1;
443 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
445 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
448 QModelIndex* aIndex = toSourceModelIndex(theIndex);
452 const QAbstractItemModel* aModel = aIndex->model();
454 if (isPartSubModel(aModel)) {
455 // if this is root node (Part item index)
456 if (!aIndex->parent().isValid()) {
457 if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
459 if (myActivePart == aModel) {
461 myActivePartIndex = QModelIndex();
463 myActivePart = (XGUI_PartModel*)aModel;
464 myActivePartIndex = theIndex;
468 myActivePart->setItemsColor(ACTIVE_COLOR);
469 myModel->setItemsColor(PASSIVE_COLOR);
471 myModel->setItemsColor(ACTIVE_COLOR);
478 ObjectPtr XGUI_DocumentDataModel::activePart() const
481 return myActivePart->part();
485 void XGUI_DocumentDataModel::deactivatePart()
488 myActivePart->setItemsColor(PASSIVE_COLOR);
490 myActivePartIndex = QModelIndex();
491 myModel->setItemsColor(ACTIVE_COLOR);
494 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
496 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
497 if (object(theIndex)) {
498 aFlags |= Qt::ItemIsEditable;
503 QModelIndex XGUI_DocumentDataModel::partIndex(const ObjectPtr& theObject) const
506 XGUI_PartModel* aModel = 0;
507 foreach (XGUI_PartModel* aPartModel, myPartModels) {
509 if (aPartModel->part() == theObject) {
515 return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
517 return QModelIndex();
520 QModelIndex XGUI_DocumentDataModel::featureIndex(const ObjectPtr theObject) const
522 // Check that this feature belongs to root document
523 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
524 DocumentPtr aDoc = theObject->document();
525 if (aDoc == aRootDoc) {
526 // This feature belongs to histrory or top model
527 if (theObject->isInHistory()) {
529 for (aId = 0; aId < aRootDoc->size(ModelAPI_Feature::group()); aId++) {
530 if (theObject == aRootDoc->object(ModelAPI_Feature::group(), aId))
533 return index(aId + historyOffset(), 0, QModelIndex());
535 QModelIndex aIndex = myModel->featureIndex(theObject);
536 return aIndex.isValid()?
537 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
541 XGUI_PartModel* aPartModel = 0;
542 foreach(XGUI_PartModel* aModel, myPartModels) {
543 if (aModel->hasDocument(aDoc)) {
549 QModelIndex aIndex = aPartModel->featureIndex(theObject);
550 return aIndex.isValid()?
551 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
555 return QModelIndex();