Salome HOME
Minor changes with classes renaming and virtual destructors declarations.
[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_Data.h>
9 #include <Model_Events.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   // Find Document object
21   std::shared_ptr<ModelAPI_PluginManager> aMgr = ModelAPI_PluginManager::get();
22   myDocument = aMgr->currentDocument();
23
24   // Register in event loop
25   Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_CREATED));
26   Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
27
28   // Create a top part of data tree model
29   myModel = new XGUI_TopDataModel(myDocument, this);
30 }
31
32
33 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
34 {
35   clearModelIndexes();
36 }
37
38
39 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
40 {
41   beginResetModel();
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();
47     }
48     while (myPartModels.size() < aNbParts) {
49       myPartModels.append(new XGUI_PartDataModel(myDocument, this));
50     }
51     for (int i = 0; i < myPartModels.size(); i++)
52       myPartModels.at(i)->setPartId(i);
53   }
54   clearModelIndexes();
55   endResetModel();
56 }
57
58 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
59 {
60   if (!theIndex.isValid())
61     return QVariant();
62   return toSourceModel(theIndex).data(theRole);
63 }
64
65
66 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
67 {
68   return QVariant();
69 }
70
71 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
72 {
73   if (!theParent.isValid()) 
74     return myModel->rowCount(theParent) + myPartModels.size();
75
76   QModelIndex aParent = toSourceModel(theParent);
77   return aParent.model()->rowCount(aParent);
78 }
79
80 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
81 {
82   return 1;
83 }
84
85 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
86 {
87   QModelIndex aIndex;
88   if (!theParent.isValid()) {
89     int aOffs = myModel->rowCount();
90     if (theRow < aOffs) 
91       aIndex = myModel->index(theRow, theColumn, theParent);
92     else
93       aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
94
95     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
96   } else {
97     QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
98     aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
99
100     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
101   }
102   return aIndex;
103 }
104
105
106 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
107 {
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));
112   return aParent;
113 }
114
115
116 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
117 {
118   if (!theParent.isValid())
119     return true;
120   return rowCount(theParent) > 0;
121 }
122
123
124 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
125 {
126   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
127   return (*aIndexPtr);
128 }
129
130
131 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
132 {
133   QList<QModelIndex*>::const_iterator aIt;
134   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
135     QModelIndex* aIndex = (*aIt);
136     if ((*aIndex) == theIndex)
137       return aIndex;
138   }
139   return 0;
140 }
141
142 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
143 {
144   QModelIndex* aIndexPtr = findModelIndex(theIndex);
145   if (!aIndexPtr) {
146     aIndexPtr = new QModelIndex(theIndex);
147     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
148     that->myIndexes.append(aIndexPtr);
149   }
150   return aIndexPtr;
151 }
152
153 void XGUI_DocumentDataModel::clearModelIndexes()
154 {
155   QList<QModelIndex*>::const_iterator aIt;
156   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
157     delete (*aIt);
158   myIndexes.clear();
159 }
160
161 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
162 {
163   QModelIndex aIndex = toSourceModel(theIndex);
164   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
165   return aModel->feature(aIndex);
166 }