1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
4 #include <ModelAPI_PluginManager.h>
5 #include <ModelAPI_Iterator.h>
6 #include <ModelAPI_Document.h>
7 #include <ModelAPI_Feature.h>
8 #include <ModelAPI_Data.h>
9 #include <Model_Events.h>
11 #include <Event_Loop.h>
18 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
19 : QAbstractItemModel(theParent)
21 // Find Document object
22 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
23 myDocument = aMgr->currentDocument();
25 // Register in event loop
26 Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_CREATED));
27 Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
28 Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_DELETED));
30 // Create a top part of data tree model
31 myModel = new XGUI_TopDataModel(myDocument, this);
35 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
41 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
43 // Created object event *******************
44 if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
45 const ModelAPI_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureUpdatedMessage*>(theMessage);
46 std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
47 std::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
49 if (aDoc == myDocument) { // If root objects
50 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
52 int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size() + 1;
53 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
54 aModel->setPartId(myPartModels.count());
55 myPartModels.append(aModel);
56 insertRows(QModelIndex(), aStart, aStart);
57 } else { // Update top groups (other except parts
58 QModelIndex aIndex = myModel->findParent(aFeature);
59 int aStart = myModel->rowCount(aIndex) - 1;
60 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
61 insertRows(aIndex, aStart, aStart);
63 } else { // if sub-objects of first level nodes
64 XGUI_PartModel* aPartModel = 0;
65 QList<XGUI_PartModel*>::const_iterator aIt;
66 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
67 if ((*aIt)->hasDocument(aDoc)) {
73 QModelIndex aIndex = aPartModel->findParent(aFeature);
74 int aStart = aPartModel->rowCount(aIndex) - 1;
75 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
76 insertRows(aIndex, aStart, aStart);
80 // Deteted object event ***********************
81 } else if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
82 const ModelAPI_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureDeletedMessage*>(theMessage);
83 std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
85 if (aDoc == myDocument) { // If root objects
86 int aStart = myPartModels.count() - 2;
87 delete myPartModels.last();
88 myPartModels.removeLast();
89 beginRemoveRows(QModelIndex(), aStart, aStart);
92 // Reset whole tree **************************
95 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
96 if (myPartModels.size() != aNbParts) { // resize internal models
97 while (myPartModels.size() > aNbParts) {
98 delete myPartModels.last();
99 myPartModels.removeLast();
101 while (myPartModels.size() < aNbParts) {
102 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
104 for (int i = 0; i < myPartModels.size(); i++)
105 myPartModels.at(i)->setPartId(i);
112 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
114 if (!theIndex.isValid())
116 return toSourceModelIndex(theIndex).data(theRole);
120 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
125 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
127 if (!theParent.isValid())
128 return myModel->rowCount(theParent) + myPartModels.size();
130 QModelIndex aParent = toSourceModelIndex(theParent);
131 return aParent.model()->rowCount(aParent);
134 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
139 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
142 if (!theParent.isValid()) {
143 int aOffs = myModel->rowCount();
145 aIndex = myModel->index(theRow, theColumn, theParent);
147 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
149 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
151 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
152 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
154 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
160 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
162 QModelIndex aParent = toSourceModelIndex(theIndex);
163 aParent = aParent.model()->parent(aParent);
164 if (aParent.isValid())
165 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
170 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
172 if (!theParent.isValid())
174 return rowCount(theParent) > 0;
178 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
180 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
185 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
187 QList<QModelIndex*>::const_iterator aIt;
188 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
189 QModelIndex* aIndex = (*aIt);
190 if ((*aIndex) == theIndex)
196 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
198 QModelIndex* aIndexPtr = findModelIndex(theIndex);
200 aIndexPtr = new QModelIndex(theIndex);
201 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
202 that->myIndexes.append(aIndexPtr);
207 void XGUI_DocumentDataModel::clearModelIndexes()
209 QList<QModelIndex*>::const_iterator aIt;
210 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
215 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
217 QModelIndex aIndex = toSourceModelIndex(theIndex);
218 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
219 return aModel->feature(aIndex);
222 void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd)
224 beginInsertRows(theParent, theStart, theEnd);
226 if (theStart == 0) // Update parent if this is a first child in order to update node decoration
227 emit dataChanged(theParent, theParent);