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);
89 delete myPartModels.last();
90 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 return myModel->rowCount(theParent) + myPartModels.size();
155 QModelIndex aParent = toSourceModelIndex(theParent);
156 return aParent.model()->rowCount(aParent);
159 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
164 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
167 if (!theParent.isValid()) {
168 int aOffs = myModel->rowCount();
170 aIndex = myModel->index(theRow, theColumn, theParent);
172 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
174 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
176 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
177 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
179 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
185 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
187 QModelIndex aParent = toSourceModelIndex(theIndex);
188 aParent = aParent.model()->parent(aParent);
189 if (aParent.isValid())
190 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
195 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
197 if (!theParent.isValid())
199 return rowCount(theParent) > 0;
203 QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
205 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
210 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
212 QList<QModelIndex*>::const_iterator aIt;
213 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
214 QModelIndex* aIndex = (*aIt);
215 if ((*aIndex) == theIndex)
221 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
223 QModelIndex* aIndexPtr = findModelIndex(theIndex);
225 aIndexPtr = new QModelIndex(theIndex);
226 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
227 that->myIndexes.append(aIndexPtr);
232 void XGUI_DocumentDataModel::clearModelIndexes()
234 QList<QModelIndex*>::const_iterator aIt;
235 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
240 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
242 QModelIndex aIndex = toSourceModelIndex(theIndex);
243 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
244 return aModel->feature(aIndex);
247 void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd)
249 beginInsertRows(theParent, theStart, theEnd);
251 if (theStart == 0) // Update parent if this is a first child in order to update node decoration
252 emit dataChanged(theParent, theParent);