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 // 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(this);
29 myModel->setDocument(myDocument);
33 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
39 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
42 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
43 if (myPartModels.size() != aNbParts) { // resize internal models
44 while (myPartModels.size() > aNbParts) {
45 delete myPartModels.last();
46 myPartModels.removeLast();
48 while (myPartModels.size() < aNbParts) {
49 myPartModels.append(new XGUI_PartDataModel(this));
51 for (int i = 0; i < myPartModels.size(); i++)
52 myPartModels.at(i)->setDocument(myDocument, i);
58 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
60 if (!theIndex.isValid())
62 return toSourceModel(theIndex).data(theRole);
66 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
71 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
73 if (!theParent.isValid())
74 return myModel->rowCount(theParent) + myPartModels.size();
76 QModelIndex aParent = toSourceModel(theParent);
77 return aParent.model()->rowCount(aParent);
80 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
85 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
88 if (!theParent.isValid()) {
89 int aOffs = myModel->rowCount();
91 aIndex = myModel->index(theRow, theColumn, theParent);
93 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
95 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
97 QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
98 aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
100 aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
106 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
108 QModelIndex aParent = toSourceModel(theIndex);
109 aParent = aParent.model()->parent(aParent);
110 if (aParent.isValid())
111 return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
116 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
118 if (!theParent.isValid())
120 return rowCount(theParent) > 0;
124 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
126 QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
131 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
133 QList<QModelIndex*>::const_iterator aIt;
134 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
135 QModelIndex* aIndex = (*aIt);
136 if ((*aIndex) == theIndex)
142 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
144 QModelIndex* aIndexPtr = findModelIndex(theIndex);
146 aIndexPtr = new QModelIndex(theIndex);
147 XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
148 that->myIndexes.append(aIndexPtr);
153 void XGUI_DocumentDataModel::clearModelIndexes()
155 QList<QModelIndex*>::const_iterator aIt;
156 for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt)