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_Object.h>
9 #include <Model_Events.h>
11 #include <Event_Loop.h>
17 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
18 : QAbstractItemModel(theParent)
20 // Find Document object
21 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
22 myDocument = aMgr->currentDocument();
24 // Register in event loop
25 Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
27 // Create a top part of data tree model
28 myModel = new XGUI_TopDataModel(myDocument, this);
32 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
38 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
41 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
42 if (myPartModels.size() != aNbParts) { // resize internal models
43 while (myPartModels.size() > aNbParts) {
44 delete myPartModels.last();
45 myPartModels.removeLast();
47 while (myPartModels.size() < aNbParts) {
48 myPartModels.append(new XGUI_PartDataModel(myDocument, this));
50 for (int i = 0; i < myPartModels.size(); i++)
51 myPartModels.at(i)->setPartId(i);
57 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
59 if (!theIndex.isValid())
61 return toSourceModel(theIndex).data(theRole);
65 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
70 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
72 if (!theParent.isValid())
73 return myModel->rowCount(theParent) + myPartModels.size();
75 QModelIndex aParent = toSourceModel(theParent);
76 return aParent.model()->rowCount(aParent);
79 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
84 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
87 if (!theParent.isValid()) {
88 int aOffs = myModel->rowCount();
90 aIndex = myModel->index(theRow, theColumn, theParent);
92 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
94 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
96 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
97 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
99 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
105 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
107 QModelIndex aParent = toSourceModel(theIndex);
108 aParent = aParent.model()->parent(aParent);
109 if (aParent.isValid())
110 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
115 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
117 if (!theParent.isValid())
119 return rowCount(theParent) > 0;
123 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
125 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
130 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
132 QList<QModelIndex*>::const_iterator aIt;
133 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
134 QModelIndex* aIndex = (*aIt);
135 if ((*aIndex) == theIndex)
141 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
143 QModelIndex* aIndexPtr = findModelIndex(theIndex);
145 aIndexPtr = new QModelIndex(theIndex);
146 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
147 that->myIndexes.append(aIndexPtr);
152 void XGUI_DocumentDataModel::clearModelIndexes()
154 QList<QModelIndex*>::const_iterator aIt;
155 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)
160 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
162 QModelIndex aIndex = toSourceModel(theIndex);
163 const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
164 return aModel->feature(aIndex);