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) {
44 int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size();
45 beginInsertRows(QModelIndex(), aStart, aStart + 1);
46 XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
47 aModel->setPartId(myPartModels.count());
48 myPartModels.append(aModel);
53 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
54 if (myPartModels.size() != aNbParts) { // resize internal models
55 while (myPartModels.size() > aNbParts) {
56 delete myPartModels.last();
57 myPartModels.removeLast();
59 while (myPartModels.size() < aNbParts) {
60 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
62 for (int i = 0; i < myPartModels.size(); i++)
63 myPartModels.at(i)->setPartId(i);
70 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
72 if (!theIndex.isValid())
74 return toSourceModel(theIndex).data(theRole);
78 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
83 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
85 if (!theParent.isValid())
86 return myModel->rowCount(theParent) + myPartModels.size();
88 QModelIndex aParent = toSourceModel(theParent);
89 return aParent.model()->rowCount(aParent);
92 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
97 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
100 if (!theParent.isValid()) {
101 int aOffs = myModel->rowCount();
103 aIndex = myModel->index(theRow, theColumn, theParent);
105 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
107 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
109 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
110 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
112 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
118 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
120 QModelIndex aParent = toSourceModel(theIndex);
121 aParent = aParent.model()->parent(aParent);
122 if (aParent.isValid())
123 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
128 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
130 if (!theParent.isValid())
132 return rowCount(theParent) > 0;
136 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
138 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
143 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
145 QList<QModelIndex*>::const_iterator aIt;
146 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
147 QModelIndex* aIndex = (*aIt);
148 if ((*aIndex) == theIndex)
154 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
156 QModelIndex* aIndexPtr = findModelIndex(theIndex);
158 aIndexPtr = new QModelIndex(theIndex);
159 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
160 that->myIndexes.append(aIndexPtr);
165 void XGUI_DocumentDataModel::clearModelIndexes()
167 QList<QModelIndex*>::const_iterator aIt;
168 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
173 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
175 QModelIndex aIndex = toSourceModel(theIndex);
176 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
177 return aModel->feature(aIndex);