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()
35 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
38 int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
39 if (myPartModels.size() != aNbParts) { // resize internal models
40 while (myPartModels.size() > aNbParts) {
41 delete myPartModels.last();
42 myPartModels.removeLast();
44 while (myPartModels.size() < aNbParts) {
45 myPartModels.append(new XGUI_PartDataModel(this));
47 for (int i = 0; i < myPartModels.size(); i++)
48 myPartModels.at(i)->setDocument(myDocument, i);
53 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
55 if (!theIndex.isValid())
57 return toSourceModel(theIndex).data(theRole);
61 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
66 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
68 if (!theParent.isValid())
69 return myModel->rowCount(theParent) + myPartModels.size();
74 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
79 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
82 if (!theParent.isValid()) {
83 int aOffs = myModel->rowCount();
85 aIndex = myModel->index(theRow, theColumn, theParent);
87 aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
88 aIndex = createIndex(theRow, theColumn, aIndex.internalId());
94 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
100 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
102 if (!theParent.isValid())
108 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
110 int aRow = theProxy.row();
111 if (!theProxy.parent().isValid()) {
112 if (aRow < myModel->rowCount()) {
113 return myModel->index(aRow, 0);
115 int aOffs = aRow - myModel->rowCount();
116 return myPartModels.at(aOffs)->index(aOffs, 0);
119 return QModelIndex();