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>
11 #include <Events_Loop.h>
13 #include <Config_FeatureMessage.h>
20 #define ACTIVE_COLOR QColor(0,72,140)
21 #define PASSIVE_COLOR Qt::black
23 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
24 : QAbstractItemModel(theParent), myActivePart(0)
26 // Find Document object
27 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
28 myDocument = aMgr->currentDocument();
30 // Register in event loop
31 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
32 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
33 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
35 // Create a top part of data tree model
36 myModel = new XGUI_TopDataModel(myDocument, this);
37 myModel->setItemsColor(ACTIVE_COLOR);
41 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
47 void XGUI_DocumentDataModel::processEvent(const Events_Message* theMessage)
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 == myDocument) { // If root objects
59 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
61 int aStart = myPartModels.size() + 1;
62 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, 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 == myDocument) { // 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 } else { // Update top groups (other except parts
104 QModelIndex aIndex = myModel->findGroup(aGroup);
105 int aStart = myModel->rowCount(aIndex);
106 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
107 removeRow(aStart, aIndex);
110 XGUI_PartModel* aPartModel = 0;
111 QList<XGUI_PartModel*>::const_iterator aIt;
112 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
113 if ((*aIt)->hasDocument(aDoc)) {
119 QModelIndex aIndex = aPartModel->findGroup(aGroup);
120 int aStart = aPartModel->rowCount(aIndex);
121 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
122 removeRow(aStart, aIndex);
126 // Deleted object event ***********************
127 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED)) {
128 //const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
129 //FeaturePtr aFeature = aUpdMsg->feature();
130 //DocumentPtr aDoc = aFeature->document();
132 // TODO: Identify the necessary index by the modified feature
134 emit dataChanged(aIndex, aIndex);
136 // Reset whole tree **************************
139 int aNbParts = myDocument->size(PARTS_GROUP);
140 if (myPartModels.size() != aNbParts) { // resize internal models
141 while (myPartModels.size() > aNbParts) {
142 delete myPartModels.last();
143 myPartModels.removeLast();
145 while (myPartModels.size() < aNbParts) {
146 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
148 for (int i = 0; i < myPartModels.size(); i++)
149 myPartModels.at(i)->setPartId(i);
156 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
158 if (!theIndex.isValid())
160 switch (theIndex.internalId()) {
163 case Qt::DisplayRole:
164 return tr("Parts") + QString(" (%1)").arg(rowCount(theIndex));
165 case Qt::DecorationRole:
166 return QIcon(":pictures/constr_folder.png");
167 case Qt::ToolTipRole:
168 return tr("Parts folder");
169 case Qt::ForegroundRole:
171 return QBrush(PASSIVE_COLOR);
173 return QBrush(ACTIVE_COLOR);
180 int aOffset = historyOffset();
181 FeaturePtr aFeature = myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
185 case Qt::DisplayRole:
187 return aFeature->data()->getName().c_str();
190 case Qt::DecorationRole:
191 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
192 case Qt::ToolTipRole:
193 return tr("Feature object");
194 case Qt::ForegroundRole:
196 return QBrush(PASSIVE_COLOR);
198 return QBrush(ACTIVE_COLOR);
205 QModelIndex aParent = theIndex.parent();
206 if (aParent.isValid() && (aParent.internalId() == PartsFolder)) {
207 return myPartModels.at(theIndex.row())->data(QModelIndex(), theRole);
209 return toSourceModelIndex(theIndex)->data(theRole);
213 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
218 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
220 if (!theParent.isValid()) {
221 // Size of external models
222 int aVal = historyOffset();
224 aVal += myDocument->size(FEATURES_GROUP);
227 if (theParent.internalId() == PartsFolder) {
228 return myPartModels.size();
230 if (theParent.internalId() == HistoryNode) {
233 QModelIndex* aParent = toSourceModelIndex(theParent);
234 const QAbstractItemModel* aModel = aParent->model();
235 if (!isSubModel(aModel))
238 /*if (isPartSubModel(aModel)) {
239 if (aModel != myActivePart)
242 return aModel->rowCount(*aParent);
245 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
250 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
253 if (!theParent.isValid()) {
254 int aOffs = myModel->rowCount();
255 if (theRow < aOffs) {
256 aIndex = myModel->index(theRow, theColumn, theParent);
257 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
259 if (theRow == aOffs) // Create Parts node
260 aIndex = partFolderNode();
261 else // create history node
262 aIndex = createIndex(theRow, theColumn, HistoryNode);
265 if (theParent.internalId() == PartsFolder) {
266 aIndex = myPartModels.at(theRow)->index(0, theColumn, QModelIndex());
268 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
269 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
271 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
277 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
279 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
280 return QModelIndex();
282 QModelIndex* aIndex = toSourceModelIndex(theIndex);
283 const QAbstractItemModel* aModel = aIndex->model();
284 if (!isSubModel(aModel))
285 return QModelIndex();
287 if (isPartSubModel(aModel)) {
288 if (!aModel->parent(*aIndex).isValid()) {
289 return partFolderNode();
293 QModelIndex aIndex1 = aModel->parent(*aIndex);
294 if (aIndex1.isValid())
295 return createIndex(aIndex1.row(), aIndex1.column(), (void*)getModelIndex(aIndex1));
300 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
302 if (!theParent.isValid())
304 return rowCount(theParent) > 0;
308 QModelIndex* XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
310 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
315 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
317 QList<QModelIndex*>::const_iterator aIt;
318 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
319 QModelIndex* aIndex = (*aIt);
320 if ((*aIndex) == theIndex)
326 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
328 QModelIndex* aIndexPtr = findModelIndex(theIndex);
330 aIndexPtr = new QModelIndex(theIndex);
331 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
332 that->myIndexes.append(aIndexPtr);
337 void XGUI_DocumentDataModel::clearModelIndexes()
339 QList<QModelIndex*>::const_iterator aIt;
340 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
345 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
347 if (theIndex.internalId() == PartsFolder)
349 if (theIndex.internalId() == HistoryNode) {
350 int aOffset = historyOffset();
351 return myDocument->feature(FEATURES_GROUP, theIndex.row() - aOffset);
353 QModelIndex* aIndex = toSourceModelIndex(theIndex);
354 if (!isSubModel(aIndex->model()))
357 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex->model());
358 return aModel->feature(*aIndex);
361 bool XGUI_DocumentDataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
363 beginInsertRows(theParent, theRow, theRow + theCount - 1);
368 int aRow = rowCount(aRoot);
369 beginInsertRows(aRoot, aRow, aRow);
375 bool XGUI_DocumentDataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
377 beginRemoveRows(theParent, theRow, theRow + theCount - 1);
383 void XGUI_DocumentDataModel::removeSubModel(int theModelId)
385 XGUI_PartModel* aModel = myPartModels.at(theModelId);
387 for (int i = 0; i < myIndexes.size(); i++) {
388 if (myIndexes.at(i)->model() == aModel)
392 while(aToRemove.size() > 0) {
393 aId = aToRemove.last();
394 delete myIndexes.at(aId);
395 myIndexes.removeAt(aId);
396 aToRemove.removeLast();
399 myPartModels.removeAt(theModelId);
402 bool XGUI_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
404 if (theModel == myModel)
406 return isPartSubModel(theModel);
409 bool XGUI_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
411 return myPartModels.contains((XGUI_PartModel*)theModel);
414 QModelIndex XGUI_DocumentDataModel::partFolderNode() const
416 int aPos = myModel->rowCount(QModelIndex());
417 return createIndex(aPos, columnCount() - 1, PartsFolder);
420 int XGUI_DocumentDataModel::historyOffset() const
422 // Nb of rows of top model + Parts folder
423 return myModel->rowCount(QModelIndex()) + 1;
426 bool XGUI_DocumentDataModel::activatedIndex(const QModelIndex& theIndex)
428 if ((theIndex.internalId() == PartsFolder) || (theIndex.internalId() == HistoryNode))
431 QModelIndex* aIndex = toSourceModelIndex(theIndex);
435 const QAbstractItemModel* aModel = aIndex->model();
437 if (isPartSubModel(aModel)) {
438 // if this is root node (Part item index)
439 if (!aIndex->parent().isValid()) {
440 if (myActivePart) myActivePart->setItemsColor(PASSIVE_COLOR);
442 if (myActivePart == aModel) {
444 myActivePartIndex = QModelIndex();
446 myActivePart = (XGUI_PartModel*)aModel;
447 myActivePartIndex = theIndex;
451 myActivePart->setItemsColor(ACTIVE_COLOR);
452 myModel->setItemsColor(PASSIVE_COLOR);
454 myModel->setItemsColor(ACTIVE_COLOR);
461 FeaturePtr XGUI_DocumentDataModel::activePart() const
464 return myActivePart->part();
468 void XGUI_DocumentDataModel::deactivatePart()
471 myActivePart->setItemsColor(PASSIVE_COLOR);
473 myModel->setItemsColor(ACTIVE_COLOR);
476 Qt::ItemFlags XGUI_DocumentDataModel::flags(const QModelIndex& theIndex) const
478 Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex);
479 if (feature(theIndex)) {
480 aFlags |= Qt::ItemIsEditable;
485 QModelIndex XGUI_DocumentDataModel::partIndex(const FeaturePtr& theFeature) const
488 XGUI_PartModel* aModel = 0;
489 foreach (XGUI_PartModel* aPartModel, myPartModels) {
491 if (aPartModel->part() == theFeature) {
497 return createIndex(aRow, 0, (void*)getModelIndex(aModel->index(0, 0, QModelIndex())));
499 return QModelIndex();