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));
29 // Create a top part of data tree model
30 myModel = new XGUI_TopDataModel(myDocument, this);
34 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
40 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
42 if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
43 const ModelAPI_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureUpdatedMessage*>(theMessage);
44 std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
45 std::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
47 if (aDoc == myDocument) {
48 if (aFeature->getGroup().compare(PARTS_GROUP) == 0) {
50 int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size();
51 beginInsertRows(QModelIndex(), aStart, aStart + 1);
52 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
53 aModel->setPartId(myPartModels.count());
54 myPartModels.append(aModel);
57 QModelIndex aIndex = myModel->findParent(aFeature);
58 int aStart = myModel->rowCount(aIndex);
59 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
60 beginInsertRows(aIndex, aStart-1, aStart);
62 if (aStart == 1) // Update parent if this is a first child in order to update node decoration
63 emit dataChanged(aIndex, aIndex);
66 XGUI_PartModel* aPartModel = 0;
67 QList<XGUI_PartModel*>::const_iterator aIt;
68 for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
69 if ((*aIt)->hasDocument(aDoc)) {
75 QModelIndex aIndex = aPartModel->findParent(aFeature);
76 int aStart = aPartModel->rowCount(aIndex);
77 aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
78 beginInsertRows(aIndex, aStart-1, aStart);
80 if (aStart == 1) // Update parent if this is a first child in order to update node decoration
81 emit dataChanged(aIndex, aIndex);
87 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
88 if (myPartModels.size() != aNbParts) { // resize internal models
89 while (myPartModels.size() > aNbParts) {
90 delete myPartModels.last();
91 myPartModels.removeLast();
93 while (myPartModels.size() < aNbParts) {
94 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
96 for (int i = 0; i < myPartModels.size(); i++)
97 myPartModels.at(i)->setPartId(i);
104 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
106 if (!theIndex.isValid())
108 return toSourceModel(theIndex).data(theRole);
112 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
117 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
119 if (!theParent.isValid())
120 return myModel->rowCount(theParent) + myPartModels.size();
122 QModelIndex aParent = toSourceModel(theParent);
123 return aParent.model()->rowCount(aParent);
126 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
131 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
134 if (!theParent.isValid()) {
135 int aOffs = myModel->rowCount();
137 aIndex = myModel->index(theRow, theColumn, theParent);
139 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
141 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
143 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
144 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
146 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
152 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
154 QModelIndex aParent = toSourceModel(theIndex);
155 aParent = aParent.model()->parent(aParent);
156 if (aParent.isValid())
157 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
162 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
164 if (!theParent.isValid())
166 return rowCount(theParent) > 0;
170 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
172 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
177 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
179 QList<QModelIndex*>::const_iterator aIt;
180 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
181 QModelIndex* aIndex = (*aIt);
182 if ((*aIndex) == theIndex)
188 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
190 QModelIndex* aIndexPtr = findModelIndex(theIndex);
192 aIndexPtr = new QModelIndex(theIndex);
193 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
194 that->myIndexes.append(aIndexPtr);
199 void XGUI_DocumentDataModel::clearModelIndexes()
201 QList<QModelIndex*>::const_iterator aIt;
202 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
207 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
209 QModelIndex aIndex = toSourceModel(theIndex);
210 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
211 return aModel->feature(aIndex);