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_Document.h>
11 #include <Event_Loop.h>
17 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
18 : QAbstractItemModel(theParent)
20 std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
21 myDocument = aMgr->currentDocument();
23 Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
25 myModel = new XGUI_TopDataModel(this);
26 myModel->setDocument(myDocument);
30 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
36 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
39 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
40 if (myPartModels.size() != aNbParts) { // resize internal models
41 while (myPartModels.size() > aNbParts) {
42 delete myPartModels.last();
43 myPartModels.removeLast();
45 while (myPartModels.size() < aNbParts) {
46 myPartModels.append(new XGUI_PartDataModel(this));
48 for (int i = 0; i < myPartModels.size(); i++)
49 myPartModels.at(i)->setDocument(myDocument, i);
55 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
57 if (!theIndex.isValid())
59 return toSourceModel(theIndex).data(theRole);
63 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
68 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
70 if (!theParent.isValid())
71 return myModel->rowCount(theParent) + myPartModels.size();
73 QModelIndex aParent = toSourceModel(theParent);
74 return aParent.model()->rowCount(aParent);
77 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
82 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
85 if (!theParent.isValid()) {
86 int aOffs = myModel->rowCount();
88 aIndex = myModel->index(theRow, theColumn, theParent);
90 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
92 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
94 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
95 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
97 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
103 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
105 QModelIndex aParent = toSourceModel(theIndex);
106 aParent = aParent.model()->parent(aParent);
107 if (aParent.isValid())
108 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
113 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
115 if (!theParent.isValid())
117 return rowCount(theParent) > 0;
121 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
123 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
128 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
130 QList<QModelIndex*>::const_iterator aIt;
131 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
132 QModelIndex* aIndex = (*aIt);
133 if ((*aIndex) == theIndex)
139 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
141 QModelIndex* aIndexPtr = findModelIndex(theIndex);
143 aIndexPtr = new QModelIndex(theIndex);
144 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
145 that->myIndexes.append(aIndexPtr);
150 void XGUI_DocumentDataModel::clearModelIndexes()
152 QList<QModelIndex*>::const_iterator aIt;
153 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)