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 if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
87 int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size() - 1;
88 beginRemoveRows(QModelIndex(), aStart, aStart);
90 delete myPartModels.last();
91 myPartModels.removeLast();
92 } else { // Update top groups (other except parts
93 QModelIndex aIndex = myModel->findGroup(aUpdMsg->group());
94 int aStart = myModel->rowCount(aIndex);
95 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
96 beginRemoveRows(aIndex, aStart, aStart);
100 XGUI_PartModel* aPartModel = 0;
101 QList<XGUI_PartModel*>::const_iterator aIt;
102 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
103 if ((*aIt)->hasDocument(aDoc)) {
109 QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group());
110 int aStart = aPartModel->rowCount(aIndex);
111 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
112 beginRemoveRows(aIndex, aStart, aStart);
117 // Reset whole tree **************************
120 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
121 if (myPartModels.size() != aNbParts) { // resize internal models
122 while (myPartModels.size() > aNbParts) {
123 delete myPartModels.last();
124 myPartModels.removeLast();
126 while (myPartModels.size() < aNbParts) {
127 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
129 for (int i = 0; i < myPartModels.size(); i++)
130 myPartModels.at(i)->setPartId(i);
137 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
139 if (!theIndex.isValid())
141 return toSourceModelIndex(theIndex).data(theRole);
145 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
150 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
152 if (!theParent.isValid()) {
153 int aVal = myModel->rowCount(theParent) + myPartModels.size();
154 return myModel->rowCount(theParent) + myPartModels.size();
156 QModelIndex aParent = toSourceModelIndex(theParent);
157 return aParent.model()->rowCount(aParent);
160 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
165 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
168 if (!theParent.isValid()) {
169 int aOffs = myModel->rowCount();
171 aIndex = myModel->index(theRow, theColumn, theParent);
173 if (myPartModels.size() > 0) {
174 int aPos = theRow - aOffs;
175 if (aPos >= myPartModels.size())
177 aIndex = myPartModels.at(aPos)->index(aPos, theColumn, theParent);
181 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
183 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
184 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
186 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
192 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
194 QModelIndex aParent = toSourceModelIndex(theIndex);
195 const QAbstractItemModel* a = aParent.model();
196 aParent = aParent.model()->parent(aParent);
197 if (aParent.isValid())
198 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
203 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
205 if (!theParent.isValid())
207 return rowCount(theParent) > 0;
211 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
213 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
218 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
220 QList<QModelIndex*>::const_iterator aIt;
221 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
222 QModelIndex* aIndex = (*aIt);
223 if ((*aIndex) == theIndex)
229 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
231 QModelIndex* aIndexPtr = findModelIndex(theIndex);
233 aIndexPtr = new QModelIndex(theIndex);
234 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
235 that->myIndexes.append(aIndexPtr);
240 void XGUI_DocumentDataModel::clearModelIndexes()
242 QList<QModelIndex*>::const_iterator aIt;
243 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
248 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
250 QModelIndex aIndex = toSourceModelIndex(theIndex);
251 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
252 return aModel->feature(aIndex);
255 void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd)
257 beginInsertRows(theParent, theStart, theEnd);
259 if (theStart == 0) // Update parent if this is a first child in order to update node decoration
260 emit dataChanged(theParent, theParent);