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 <Model_Events.h>
11 #include <ModelAPI_Object.h>
13 #include <Events_Loop.h>
15 #include <Config_FeatureMessage.h>
22 #define ACTIVE_COLOR QColor(0,72,140)
23 #define PASSIVE_COLOR Qt::black
25 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
26 : QAbstractItemModel(theParent), myActivePart(0)
28 // Register in event loop
29 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
30 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
31 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
33 // Create a top part of data tree model
34 myModel = new XGUI_TopDataModel(this);
35 myModel->setItemsColor(ACTIVE_COLOR);
39 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
45 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
47 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
49 // Created object event *******************
50 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
51 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
52 std::set<FeaturePtr> aFeatures = aUpdMsg->features();
54 std::set<FeaturePtr>::const_iterator aIt;
55 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
56 FeaturePtr aFeature = (*aIt);
57 DocumentPtr aDoc = aFeature->document();
58 if (aDoc == aRootDoc) { // If root objects
59 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
61 int aStart = myPartModels.size();
62 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(this);
63 aModel->setPartId(myPartModels.count());
64 myPartModels.append(aModel);
65 insertRow(aStart, partFolderNode());
66 } else { // Update top groups (other except parts
67 QModelIndex aIndex = myModel->findParent(aFeature);
68 int aStart = myModel->rowCount(aIndex) - 1;
69 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
70 insertRow(aStart, aIndex);
72 } else { // if sub-objects of first level nodes
73 XGUI_PartModel* aPartModel = 0;
74 QList<XGUI_PartModel*>::const_iterator aIt;
75 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
76 if ((*aIt)->hasDocument(aDoc)) {
82 QModelIndex aIndex = aPartModel->findParent(aFeature);
83 int aStart = aPartModel->rowCount(aIndex) - 1;
84 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
85 insertRow(aStart, aIndex);
89 // Deleted object event ***********************
90 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED)) {
91 const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
92 DocumentPtr aDoc = aUpdMsg->document();
93 std::set<std::string> aGroups = aUpdMsg->groups();
95 std::set<std::string>::const_iterator aIt;
96 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
97 std::string aGroup = (*aIt);
98 if (aDoc == aRootDoc) { // If root objects
99 if (aGroup.compare(PARTS_GROUP) == 0) { // Updsate only Parts group
100 int aStart = myPartModels.size() - 1;
101 removeSubModel(aStart);
102 removeRow(aStart, partFolderNode());
103 if (myActivePart && (!isPartSubModel(myActivePart))) {
105 myActivePartIndex = QModelIndex();
106 myModel->setItemsColor(ACTIVE_COLOR);
108 } else { // Update top groups (other except parts
109 QModelIndex aIndex = myModel->findGroup(aGroup);
110 int aStart = myModel->rowCount(aIndex);
111 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
112 removeRow(aStart, aIndex);
115 XGUI_PartModel* aPartModel = 0;
116 QList<XGUI_PartModel*>::const_iterator aIt;
117 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
118 if ((*aIt)->hasDocument(aDoc)) {
124 QModelIndex aIndex = aPartModel->findGroup(aGroup);
125 int aStart = aPartModel->rowCount(aIndex);
126 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
127 removeRow(aStart, aIndex);
131 // Deleted object event ***********************
132 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED)) {
133 //const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
134 //FeaturePtr aFeature = aUpdMsg->feature();
135 //DocumentPtr aDoc = aFeature->document();
137 // TODO: Identify the necessary index by the modified feature
139 emit dataChanged(aIndex, aIndex);
141 // Reset whole tree **************************
147 void XGUI_DocumentDataModel::rebuildDataTree()
149 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
154 int aNbParts = aRootDoc->size(PARTS_GROUP);
155 if (myPartModels.size() != aNbParts) { // resize internal models
156 while (myPartModels.size() > aNbParts) {
157 delete myPartModels.last();
158 myPartModels.removeLast();
160 while (myPartModels.size() < aNbParts) {
161 myPartModels.append(new XGUI_PartDataModel(this));
163 for (int i = 0; i < myPartModels.size(); i++)
164 myPartModels.at(i)->setPartId(i);
169 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
171 if (!theIndex.isValid())
173 switch (theIndex.internalId()) {
176 case Qt::DisplayRole:
177 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
178 case Qt::DecorationRole:
179 return QIcon(":pictures/constr_folder.png");
180 case Qt::ToolTipRole:
181 return tr("Parts folder");
182 case Qt::ForegroundRole:
184 return QBrush(PASSIVE_COLOR);
186 return QBrush(ACTIVE_COLOR);
193 int aOffset = historyOffset();
194 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
195 FeaturePtr aFeature = aRootDoc->feature(FEATURES_GROUP, theIndex.row() - aOffset);
199 case Qt::DisplayRole:
201 return aFeature->data()->getName().c_str();
204 case Qt::DecorationRole:
205 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
206 case Qt::ToolTipRole:
207 return tr("Feature object");
208 case Qt::ForegroundRole:
210 return QBrush(PASSIVE_COLOR);
212 return QBrush(ACTIVE_COLOR);
219 QModelIndex aParent = theIndex.parent();
220 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
221 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
223 return toSourceModelIndex(theIndex)->data(theRole);
227 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
232 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
234 if (!theParent.isValid()) {
235 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
236 // Size of external models
237 int aVal = historyOffset();
239 aVal += aRootDoc->size(FEATURES_GROUP);
242 if (theParent.internalId() == PartsFolder) {
243 return myPartModels.size();
245 if (theParent.internalId() == HistoryNode) {
248 QModelIndex* aParent = toSourceModelIndex(theParent);
249 const QAbstractItemModel* aModel = aParent->model();
250 if (!isSubModel(aModel))
253 /*if (isPartSubModel(aModel)) {
254 if (aModel != myActivePart)
257 return aModel->rowCount(*aParent);
260 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
265 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
268 if (!theParent.isValid()) {
269 int aOffs = myModel->rowCount();
270 if (theRow < aOffs) {
271 aIndex = myModel->index(theRow, theColumn, theParent);
272 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
274 if (theRow == aOffs) // Create Parts node
275 aIndex = partFolderNode();
276 else // create history node
277 aIndex = createIndex(theRow, theColumn, HistoryNode);
280 if (theParent.internalId() == PartsFolder) {
281 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
283 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
284 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
286 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
292 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
294 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
295 return QModelIndex();
297 QModelIndex* aIndex = toSourceModelIndex(theIndex);
298 const QAbstractItemModel* aModel = aIndex->model();
299 if (!isSubModel(aModel))
300 return QModelIndex();
302 if (isPartSubModel(aModel)) {
303 if (!aModel->parent(*aIndex).isValid()) {
304 return partFolderNode();
308 QModelIndex aIndex1 = aModel->parent(*aIndex);
309 if (aIndex1.isValid())
310 return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
315 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
317 if (!theParent.isValid())
319 return rowCount(theParent) > 0;
323 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
325 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
330 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
332 QList<QModelIndex*>::const_iterator aIt;
333 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
334 QModelIndex* aIndex = (*aIt);
335 if ((*aIndex) == theIndex)
341 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
343 QModelIndex* aIndexPtr = findModelIndex(theIndex);
345 aIndexPtr = new QModelIndex(theIndex);
346 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
347 that->myIndexes.append(aIndexPtr);
352 void XGUI_DocumentDataModel::clearModelIndexes()
354 QList<QModelIndex*>::const_iterator aIt;
355 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
360 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
362 if (theIndex.internalId() == PartsFolder)
364 if (theIndex.internalId() == HistoryNode) {
365 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
366 int aOffset = historyOffset();
367 return aRootDoc->feature(FEATURES_GROUP, theIndex.row() - aOffset);
369 QModelIndex* aIndex = toSourceModelIndex(theIndex);
370 if (!isSubModel(aIndex->model()))
373 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
374 return aModel->feature(*aIndex);
377 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
379 beginInsertRows(theParent, theRow, theRow + theCount - 1);
384 int aRow = rowCount(aRoot);
385 beginInsertRows(aRoot, aRow, aRow);
391 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
393 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
399 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
401 XGUI_PartModel* aModel = myPartModels.at(theModelId);
403 for (int i = 0; i < myIndexes.size(); i++) {
404 if (myIndexes.at(i)->model() == aModel)
408 while(aToRemove.size() > 0) {
409 aId = aToRemove.last();
410 delete myIndexes.at(aId);
411 myIndexes.removeAt(aId);
412 aToRemove.removeLast();
415 myPartModels.removeAt(theModelId);
418 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
420 if (theModel == myModel)
422 return isPartSubModel(theModel);
425 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
427 return myPartModels.contains((XGUI_PartModel*)theModel);
430 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
432 int aPos = myModel->rowCount(QModelIndex());
433 return createIndex(aPos, columnCount() - 1, PartsFolder);
436 int XGUI_DocumentDataModel::historyOffset() const
438 // Nb of rows of top model + Parts folder
439 return myModel->rowCount(QModelIndex()) + 1;
442 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
444 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
447 QModelIndex* aIndex = toSourceModelIndex(theIndex);
451 const QAbstractItemModel* aModel = aIndex->model();
453 if (isPartSubModel(aModel)) {
454 // if this is root node (Part item index)
455 if (!aIndex->parent().isValid()) {
456 if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
458 if (myActivePart == aModel) {
460 myActivePartIndex = QModelIndex();
462 myActivePart = (XGUI_PartModel*)aModel;
463 myActivePartIndex = theIndex;
467 myActivePart->setItemsColor(ACTIVE_COLOR);
468 myModel->setItemsColor(PASSIVE_COLOR);
470 myModel->setItemsColor(ACTIVE_COLOR);
477 FeaturePtr XGUI_DocumentDataModel::activePart() const
480 return myActivePart->part();
484 void XGUI_DocumentDataModel::deactivatePart()
487 myActivePart->setItemsColor(PASSIVE_COLOR);
489 myActivePartIndex = QModelIndex();
490 myModel->setItemsColor(ACTIVE_COLOR);
493 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
495 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
496 if (feature(theIndex)) {
497 aFlags |= Qt::ItemIsEditable;
502 QModelIndex XGUI_DocumentDataModel::partIndex(const FeaturePtr& theFeature) const
504 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
507 XGUI_PartModel* aModel = 0;
508 foreach (XGUI_PartModel* aPartModel, myPartModels) {
510 if (aPartModel->part() == aFeature) {
516 return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
518 return QModelIndex();
521 QModelIndex XGUI_DocumentDataModel::featureIndex(const FeaturePtr theFeature) const
523 // Check that this feature belongs to root document
524 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
525 DocumentPtr aDoc = theFeature->document();
526 if (aDoc == aRootDoc) {
527 // This feature belongs to histrory or top model
528 if (theFeature->isInHistory()) {
530 for (aId = 0; aId < aRootDoc->size(FEATURES_GROUP); aId++) {
531 if (theFeature == aRootDoc->feature(FEATURES_GROUP, aId))
534 return index(aId + historyOffset(), 0, QModelIndex());
536 QModelIndex aIndex = myModel->featureIndex(theFeature);
537 return aIndex.isValid()?
538 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
542 XGUI_PartModel* aPartModel = 0;
543 foreach(XGUI_PartModel* aModel, myPartModels) {
544 if (aModel->hasDocument(aDoc)) {
550 QModelIndex aIndex = aPartModel->featureIndex(theFeature);
551 return aIndex.isValid()?
552 createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex)) :
556 return QModelIndex();