Salome HOME
Merge branch 'master' of newgeom:newgeom
[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   clearModelIndexes();
33 }
34
35
36 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
37 {
38   beginResetModel();
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();
44     }
45     while (myPartModels.size() < aNbParts) {
46       myPartModels.append(new XGUI_PartDataModel(this));
47     }
48     for (int i = 0; i < myPartModels.size(); i++)
49       myPartModels.at(i)->setDocument(myDocument, i);
50   }
51   clearModelIndexes();
52   endResetModel();
53 }
54
55 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
56 {
57   if (!theIndex.isValid())
58     return QVariant();
59   return toSourceModel(theIndex).data(theRole);
60 }
61
62
63 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
64 {
65   return QVariant();
66 }
67
68 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
69 {
70   if (!theParent.isValid()) 
71     return myModel->rowCount(theParent) + myPartModels.size();
72
73   QModelIndex aParent = toSourceModel(theParent);
74   return aParent.model()->rowCount(aParent);
75 }
76
77 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
78 {
79   return 1;
80 }
81
82 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
83 {
84   QModelIndex aIndex;
85   if (!theParent.isValid()) {
86     int aOffs = myModel->rowCount();
87     if (theRow < aOffs) 
88       aIndex = myModel->index(theRow, theColumn, theParent);
89     else
90       aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
91
92     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
93   } else {
94     QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
95     aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
96
97     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
98   }
99   return aIndex;
100 }
101
102
103 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
104 {
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));
109   return aParent;
110 }
111
112
113 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
114 {
115   if (!theParent.isValid())
116     return true;
117   return rowCount(theParent) > 0;
118 }
119
120
121 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
122 {
123   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
124   return (*aIndexPtr);
125 }
126
127
128 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
129 {
130   QList<QModelIndex*>::const_iterator aIt;
131   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
132     QModelIndex* aIndex = (*aIt);
133     if ((*aIndex) == theIndex)
134       return aIndex;
135   }
136   return 0;
137 }
138
139 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
140 {
141   QModelIndex* aIndexPtr = findModelIndex(theIndex);
142   if (!aIndexPtr) {
143     aIndexPtr = new QModelIndex(theIndex);
144     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
145     that->myIndexes.append(aIndexPtr);
146   }
147   return aIndexPtr;
148 }
149
150 void XGUI_DocumentDataModel::clearModelIndexes()
151 {
152   QList<QModelIndex*>::const_iterator aIt;
153   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
154     delete (*aIt);
155   myIndexes.clear();
156 }