]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_DocumentDataModel.cpp
Salome HOME
b2376b9aa2e79c9e091d6114acc7ebe31ed52d6a
[modules/shaper.git] / src / XGUI / XGUI_DocumentDataModel.cpp
1 #include "XGUI_DocumentDataModel.h"
2 #include "XGUI_PartDataModel.h"
3
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>
10
11 #include <Event_Loop.h>
12
13
14 #include <QIcon>
15
16
17 XGUI_DocumentDataModel::XGUI_DocumentDataModel(QObject* theParent)
18   : QAbstractItemModel(theParent)
19 {
20   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
21   myDocument = aMgr->currentDocument();
22
23   Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
24
25   myModel = new XGUI_TopDataModel(this);
26   myModel->setDocument(myDocument);
27 }
28
29
30 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
31 {
32 }
33
34
35 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
36 {
37   beginResetModel();
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();
43     }
44     while (myPartModels.size() < aNbParts) {
45       myPartModels.append(new XGUI_PartDataModel(this));
46     }
47     for (int i = 0; i < myPartModels.size(); i++)
48       myPartModels.at(i)->setDocument(myDocument, i);
49   }
50   endResetModel();
51 }
52
53 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
54 {
55   if (!theIndex.isValid())
56     return QVariant();
57   return toSourceModel(theIndex).data(theRole);
58 }
59
60
61 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
62 {
63   return QVariant();
64 }
65
66 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
67 {
68   if (!theParent.isValid()) 
69     return myModel->rowCount(theParent) + myPartModels.size();
70
71   return 0;
72 }
73
74 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
75 {
76   return 1;
77 }
78
79 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
80 {
81   QModelIndex aIndex;
82   if (!theParent.isValid()) {
83     int aOffs = myModel->rowCount();
84     if (theRow < aOffs) 
85       aIndex = myModel->index(theRow, theColumn, theParent);
86     else
87       aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
88     aIndex = createIndex(theRow, theColumn, aIndex.internalId());
89   }
90   return aIndex;
91 }
92
93
94 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
95 {
96   return QModelIndex();
97 }
98
99
100 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
101 {
102   if (!theParent.isValid())
103     return true;
104   return false;
105 }
106
107
108 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
109 {
110   int aRow = theProxy.row();
111   if (!theProxy.parent().isValid()) {
112     if (aRow < myModel->rowCount()) {
113       return myModel->index(aRow, 0);
114     } else {
115       int aOffs = aRow - myModel->rowCount();
116       return myPartModels.at(aOffs)->index(aOffs, 0);
117     }
118   } 
119   return QModelIndex();
120 }
121
122