1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
3 #include "XGUI_Workshop.h"
5 #include <ModelAPI_PluginManager.h>
6 #include <ModelAPI_Document.h>
7 #include <ModelAPI_Feature.h>
8 #include <ModelAPI_Data.h>
9 #include <Model_Events.h>
10 #include <ModelAPI_Object.h>
12 #include <Events_Loop.h>
14 #include <Config_FeatureMessage.h>
21 #define ACTIVE_COLOR QColor(0,72,140)
22 #define PASSIVE_COLOR Qt::black
24 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
25 : QAbstractItemModel(theParent), myActivePart(0)
27 // Find Document object
28 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
29 myDocument = aMgr->currentDocument();
31 // Register in event loop
32 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
33 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
34 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
36 // Create a top part of data tree model
37 myModel = new XGUI_TopDataModel(myDocument, this);
38 myModel->setItemsColor(ACTIVE_COLOR);
42 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
48 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
50 // Created object event *******************
51 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
52 const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
53 std::set<FeaturePtr> aFeatures = aUpdMsg->features();
55 std::set<FeaturePtr>::const_iterator aIt;
56 for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
57 FeaturePtr aFeature = (*aIt);
58 DocumentPtr aDoc = aFeature->document();
59 if (aDoc == myDocument) { // If root objects
60 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
62 int aStart = myPartModels.size();
63 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, 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_FEATURE_DELETED)) {
92 const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(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 == myDocument) { // If root objects
100 if (aGroup.compare(PARTS_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_FEATURE_UPDATED)) {
134 //const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
135 //FeaturePtr 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 **************************
145 int aNbParts = myDocument->size(PARTS_GROUP);
146 if (myPartModels.size() != aNbParts) { // resize internal models
147 while (myPartModels.size() > aNbParts) {
148 delete myPartModels.last();
149 myPartModels.removeLast();
151 while (myPartModels.size() < aNbParts) {
152 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
154 for (int i = 0; i < myPartModels.size(); i++)
155 myPartModels.at(i)->setPartId(i);
162 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
164 if (!theIndex.isValid())
166 switch (theIndex.internalId()) {
169 case Qt::DisplayRole:
170 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
171 case Qt::DecorationRole:
172 return QIcon(":pictures/constr_folder.png");
173 case Qt::ToolTipRole:
174 return tr("Parts folder");
175 case Qt::ForegroundRole:
177 return QBrush(PASSIVE_COLOR);
179 return QBrush(ACTIVE_COLOR);
186 int aOffset = historyOffset();
187 FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
191 case Qt::DisplayRole:
193 return aFeature->data()->getName().c_str();
196 case Qt::DecorationRole:
197 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
198 case Qt::ToolTipRole:
199 return tr("Feature object");
200 case Qt::ForegroundRole:
202 return QBrush(PASSIVE_COLOR);
204 return QBrush(ACTIVE_COLOR);
211 QModelIndex aParent = theIndex.parent();
212 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
213 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
215 return toSourceModelIndex(theIndex)->data(theRole);
219 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
224 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
226 if (!theParent.isValid()) {
227 // Size of external models
228 int aVal = historyOffset();
230 aVal += myDocument->size(FEATURES_GROUP);
233 if (theParent.internalId() == PartsFolder) {
234 return myPartModels.size();
236 if (theParent.internalId() == HistoryNode) {
239 QModelIndex* aParent = toSourceModelIndex(theParent);
240 const QAbstractItemModel* aModel = aParent->model();
241 if (!isSubModel(aModel))
244 /*if (isPartSubModel(aModel)) {
245 if (aModel != myActivePart)
248 return aModel->rowCount(*aParent);
251 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
256 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
259 if (!theParent.isValid()) {
260 int aOffs = myModel->rowCount();
261 if (theRow < aOffs) {
262 aIndex = myModel->index(theRow, theColumn, theParent);
263 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
265 if (theRow == aOffs) // Create Parts node
266 aIndex = partFolderNode();
267 else // create history node
268 aIndex = createIndex(theRow, theColumn, HistoryNode);
271 if (theParent.internalId() == PartsFolder) {
272 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
274 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
275 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
277 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
283 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
285 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
286 return QModelIndex();
288 QModelIndex* aIndex = toSourceModelIndex(theIndex);
289 const QAbstractItemModel* aModel = aIndex->model();
290 if (!isSubModel(aModel))
291 return QModelIndex();
293 if (isPartSubModel(aModel)) {
294 if (!aModel->parent(*aIndex).isValid()) {
295 return partFolderNode();
299 QModelIndex aIndex1 = aModel->parent(*aIndex);
300 if (aIndex1.isValid())
301 return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
306 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
308 if (!theParent.isValid())
310 return rowCount(theParent) > 0;
314 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
316 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
321 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
323 QList<QModelIndex*>::const_iterator aIt;
324 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
325 QModelIndex* aIndex = (*aIt);
326 if ((*aIndex) == theIndex)
332 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
334 QModelIndex* aIndexPtr = findModelIndex(theIndex);
336 aIndexPtr = new QModelIndex(theIndex);
337 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
338 that->myIndexes.append(aIndexPtr);
343 void XGUI_DocumentDataModel::clearModelIndexes()
345 QList<QModelIndex*>::const_iterator aIt;
346 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
351 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
353 if (theIndex.internalId() == PartsFolder)
355 if (theIndex.internalId() == HistoryNode) {
356 int aOffset = historyOffset();
357 return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
359 QModelIndex* aIndex = toSourceModelIndex(theIndex);
360 if (!isSubModel(aIndex->model()))
363 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
364 return aModel->feature(*aIndex);
367 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
369 beginInsertRows(theParent, theRow, theRow + theCount - 1);
374 int aRow = rowCount(aRoot);
375 beginInsertRows(aRoot, aRow, aRow);
381 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
383 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
389 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
391 XGUI_PartModel* aModel = myPartModels.at(theModelId);
393 for (int i = 0; i < myIndexes.size(); i++) {
394 if (myIndexes.at(i)->model() == aModel)
398 while(aToRemove.size() > 0) {
399 aId = aToRemove.last();
400 delete myIndexes.at(aId);
401 myIndexes.removeAt(aId);
402 aToRemove.removeLast();
405 myPartModels.removeAt(theModelId);
408 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
410 if (theModel == myModel)
412 return isPartSubModel(theModel);
415 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
417 return myPartModels.contains((XGUI_PartModel*)theModel);
420 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
422 int aPos = myModel->rowCount(QModelIndex());
423 return createIndex(aPos, columnCount() - 1, PartsFolder);
426 int XGUI_DocumentDataModel::historyOffset() const
428 // Nb of rows of top model + Parts folder
429 return myModel->rowCount(QModelIndex()) + 1;
432 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
434 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
437 QModelIndex* aIndex = toSourceModelIndex(theIndex);
441 const QAbstractItemModel* aModel = aIndex->model();
443 if (isPartSubModel(aModel)) {
444 // if this is root node (Part item index)
445 if (!aIndex->parent().isValid()) {
446 if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
448 if (myActivePart == aModel) {
450 myActivePartIndex = QModelIndex();
452 myActivePart = (XGUI_PartModel*)aModel;
453 myActivePartIndex = theIndex;
457 myActivePart->setItemsColor(ACTIVE_COLOR);
458 myModel->setItemsColor(PASSIVE_COLOR);
460 myModel->setItemsColor(ACTIVE_COLOR);
467 FeaturePtr XGUI_DocumentDataModel::activePart() const
470 return myActivePart->part();
474 void XGUI_DocumentDataModel::deactivatePart()
477 myActivePart->setItemsColor(PASSIVE_COLOR);
479 myActivePartIndex = QModelIndex();
480 myModel->setItemsColor(ACTIVE_COLOR);
483 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
485 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
486 if (feature(theIndex)) {
487 aFlags |= Qt::ItemIsEditable;
492 QModelIndex XGUI_DocumentDataModel::partIndex(const FeaturePtr& theFeature) const
494 FeaturePtr aFeature = theFeature;
495 if (!aFeature->data()) {
496 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
497 aFeature = aObject->featureRef();
500 XGUI_PartModel* aModel = 0;
501 foreach (XGUI_PartModel* aPartModel, myPartModels) {
503 if (aPartModel->part() == aFeature) {
509 return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
511 return QModelIndex();