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 // Find Document object
29 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
30 myDocument = aMgr->currentDocument();
32 // Register in event loop
33 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
34 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
35 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
37 // Create a top part of data tree model
38 myModel = new XGUI_TopDataModel(myDocument, this);
39 myModel->setItemsColor(ACTIVE_COLOR);
43 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
49 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
51 // Created object event *******************
52 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
53 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
54 std::set<FeaturePtr> aFeatures = aUpdMsg->features();
56 std::set<FeaturePtr>::const_iterator aIt;
57 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
58 FeaturePtr aFeature = (*aIt);
59 DocumentPtr aDoc = aFeature->document();
60 if (aDoc == myDocument) { // If root objects
61 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
63 int aStart = myPartModels.size();
64 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
65 aModel->setPartId(myPartModels.count());
66 myPartModels.append(aModel);
67 insertRow(aStart, partFolderNode());
68 } else { // Update top groups (other except parts
69 QModelIndex aIndex = myModel->findParent(aFeature);
70 int aStart = myModel->rowCount(aIndex) - 1;
71 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
72 insertRow(aStart, aIndex);
74 } else { // if sub-objects of first level nodes
75 XGUI_PartModel* aPartModel = 0;
76 QList<XGUI_PartModel*>::const_iterator aIt;
77 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
78 if ((*aIt)->hasDocument(aDoc)) {
84 QModelIndex aIndex = aPartModel->findParent(aFeature);
85 int aStart = aPartModel->rowCount(aIndex) - 1;
86 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
87 insertRow(aStart, aIndex);
91 // Deleted object event ***********************
92 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED)) {
93 const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
94 DocumentPtr aDoc = aUpdMsg->document();
95 std::set<std::string> aGroups = aUpdMsg->groups();
97 std::set<std::string>::const_iterator aIt;
98 for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
99 std::string aGroup = (*aIt);
100 if (aDoc == myDocument) { // If root objects
101 if (aGroup.compare(PARTS_GROUP) == 0) { // Updsate only Parts group
102 int aStart = myPartModels.size() - 1;
103 removeSubModel(aStart);
104 removeRow(aStart, partFolderNode());
105 if (myActivePart && (!isPartSubModel(myActivePart))) {
107 myActivePartIndex = QModelIndex();
108 myModel->setItemsColor(ACTIVE_COLOR);
110 } else { // Update top groups (other except parts
111 QModelIndex aIndex = myModel->findGroup(aGroup);
112 int aStart = myModel->rowCount(aIndex);
113 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
114 removeRow(aStart, aIndex);
117 XGUI_PartModel* aPartModel = 0;
118 QList<XGUI_PartModel*>::const_iterator aIt;
119 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
120 if ((*aIt)->hasDocument(aDoc)) {
126 QModelIndex aIndex = aPartModel->findGroup(aGroup);
127 int aStart = aPartModel->rowCount(aIndex);
128 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
129 removeRow(aStart, aIndex);
133 // Deleted object event ***********************
134 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED)) {
135 //const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
136 //FeaturePtr aFeature = aUpdMsg->feature();
137 //DocumentPtr aDoc = aFeature->document();
139 // TODO: Identify the necessary index by the modified feature
141 emit dataChanged(aIndex, aIndex);
143 // Reset whole tree **************************
146 int aNbParts = myDocument->size(PARTS_GROUP);
147 if (myPartModels.size() != aNbParts) { // resize internal models
148 while (myPartModels.size() > aNbParts) {
149 delete myPartModels.last();
150 myPartModels.removeLast();
152 while (myPartModels.size() < aNbParts) {
153 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
155 for (int i = 0; i < myPartModels.size(); i++)
156 myPartModels.at(i)->setPartId(i);
163 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
165 if (!theIndex.isValid())
167 switch (theIndex.internalId()) {
170 case Qt::DisplayRole:
171 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
172 case Qt::DecorationRole:
173 return QIcon(":pictures/constr_folder.png");
174 case Qt::ToolTipRole:
175 return tr("Parts folder");
176 case Qt::ForegroundRole:
178 return QBrush(PASSIVE_COLOR);
180 return QBrush(ACTIVE_COLOR);
187 int aOffset = historyOffset();
188 FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
192 case Qt::DisplayRole:
194 return aFeature->data()->getName().c_str();
197 case Qt::DecorationRole:
198 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
199 case Qt::ToolTipRole:
200 return tr("Feature object");
201 case Qt::ForegroundRole:
203 return QBrush(PASSIVE_COLOR);
205 return QBrush(ACTIVE_COLOR);
212 QModelIndex aParent = theIndex.parent();
213 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
214 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
216 return toSourceModelIndex(theIndex)->data(theRole);
220 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
225 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
227 if (!theParent.isValid()) {
228 // Size of external models
229 int aVal = historyOffset();
231 aVal += myDocument->size(FEATURES_GROUP);
234 if (theParent.internalId() == PartsFolder) {
235 return myPartModels.size();
237 if (theParent.internalId() == HistoryNode) {
240 QModelIndex* aParent = toSourceModelIndex(theParent);
241 const QAbstractItemModel* aModel = aParent->model();
242 if (!isSubModel(aModel))
245 /*if (isPartSubModel(aModel)) {
246 if (aModel != myActivePart)
249 return aModel->rowCount(*aParent);
252 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
257 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
260 if (!theParent.isValid()) {
261 int aOffs = myModel->rowCount();
262 if (theRow < aOffs) {
263 aIndex = myModel->index(theRow, theColumn, theParent);
264 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
266 if (theRow == aOffs) // Create Parts node
267 aIndex = partFolderNode();
268 else // create history node
269 aIndex = createIndex(theRow, theColumn, HistoryNode);
272 if (theParent.internalId() == PartsFolder) {
273 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
275 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
276 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
278 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
284 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
286 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
287 return QModelIndex();
289 QModelIndex* aIndex = toSourceModelIndex(theIndex);
290 const QAbstractItemModel* aModel = aIndex->model();
291 if (!isSubModel(aModel))
292 return QModelIndex();
294 if (isPartSubModel(aModel)) {
295 if (!aModel->parent(*aIndex).isValid()) {
296 return partFolderNode();
300 QModelIndex aIndex1 = aModel->parent(*aIndex);
301 if (aIndex1.isValid())
302 return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
307 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
309 if (!theParent.isValid())
311 return rowCount(theParent) > 0;
315 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
317 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
322 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
324 QList<QModelIndex*>::const_iterator aIt;
325 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
326 QModelIndex* aIndex = (*aIt);
327 if ((*aIndex) == theIndex)
333 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
335 QModelIndex* aIndexPtr = findModelIndex(theIndex);
337 aIndexPtr = new QModelIndex(theIndex);
338 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
339 that->myIndexes.append(aIndexPtr);
344 void XGUI_DocumentDataModel::clearModelIndexes()
346 QList<QModelIndex*>::const_iterator aIt;
347 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
352 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
354 if (theIndex.internalId() == PartsFolder)
356 if (theIndex.internalId() == HistoryNode) {
357 int aOffset = historyOffset();
358 return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
360 QModelIndex* aIndex = toSourceModelIndex(theIndex);
361 if (!isSubModel(aIndex->model()))
364 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
365 return aModel->feature(*aIndex);
368 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
370 beginInsertRows(theParent, theRow, theRow + theCount - 1);
375 int aRow = rowCount(aRoot);
376 beginInsertRows(aRoot, aRow, aRow);
382 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
384 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
390 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
392 XGUI_PartModel* aModel = myPartModels.at(theModelId);
394 for (int i = 0; i < myIndexes.size(); i++) {
395 if (myIndexes.at(i)->model() == aModel)
399 while(aToRemove.size() > 0) {
400 aId = aToRemove.last();
401 delete myIndexes.at(aId);
402 myIndexes.removeAt(aId);
403 aToRemove.removeLast();
406 myPartModels.removeAt(theModelId);
409 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
411 if (theModel == myModel)
413 return isPartSubModel(theModel);
416 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
418 return myPartModels.contains((XGUI_PartModel*)theModel);
421 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
423 int aPos = myModel->rowCount(QModelIndex());
424 return createIndex(aPos, columnCount() - 1, PartsFolder);
427 int XGUI_DocumentDataModel::historyOffset() const
429 // Nb of rows of top model + Parts folder
430 return myModel->rowCount(QModelIndex()) + 1;
433 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
435 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
438 QModelIndex* aIndex = toSourceModelIndex(theIndex);
442 const QAbstractItemModel* aModel = aIndex->model();
444 if (isPartSubModel(aModel)) {
445 // if this is root node (Part item index)
446 if (!aIndex->parent().isValid()) {
447 if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
449 if (myActivePart == aModel) {
451 myActivePartIndex = QModelIndex();
453 myActivePart = (XGUI_PartModel*)aModel;
454 myActivePartIndex = theIndex;
458 myActivePart->setItemsColor(ACTIVE_COLOR);
459 myModel->setItemsColor(PASSIVE_COLOR);
461 myModel->setItemsColor(ACTIVE_COLOR);
468 FeaturePtr XGUI_DocumentDataModel::activePart() const
471 return myActivePart->part();
475 void XGUI_DocumentDataModel::deactivatePart()
478 myActivePart->setItemsColor(PASSIVE_COLOR);
480 myActivePartIndex = QModelIndex();
481 myModel->setItemsColor(ACTIVE_COLOR);
484 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
486 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
487 if (feature(theIndex)) {
488 aFlags |= Qt::ItemIsEditable;
493 QModelIndex XGUI_DocumentDataModel::partIndex(const FeaturePtr& theFeature) const
495 FeaturePtr aFeature = theFeature;
496 if (XGUI_Tools::isModelObject(aFeature)) {
497 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
498 aFeature = aObject->featureRef();
501 XGUI_PartModel* aModel = 0;
502 foreach (XGUI_PartModel* aPartModel, myPartModels) {
504 if (aPartModel->part() == aFeature) {
510 return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
512 return QModelIndex();