Salome HOME
Bug with shown scroll buttons in main menu is corrected
[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   // 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_UPDATED));
26
27   // Create a top part of data tree model
28   myModel = new XGUI_TopDataModel(myDocument, this);
29 }
30
31
32 XGUI_DocumentDataModel::~XGUI_DocumentDataModel()
33 {
34   clearModelIndexes();
35 }
36
37
38 void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
39 {
40   beginResetModel();
41   int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
42   if (myPartModels.size() != aNbParts) { // resize internal models
43     while (myPartModels.size() > aNbParts) {
44       delete myPartModels.last();
45       myPartModels.removeLast();
46     }
47     while (myPartModels.size() < aNbParts) {
48       myPartModels.append(new XGUI_PartDataModel(myDocument, this));
49     }
50     for (int i = 0; i < myPartModels.size(); i++)
51       myPartModels.at(i)->setPartId(i);
52   }
53   clearModelIndexes();
54   endResetModel();
55 }
56
57 QVariant XGUI_DocumentDataModel::data(const QModelIndex& theIndex, int theRole) const
58 {
59   if (!theIndex.isValid())
60     return QVariant();
61   return toSourceModel(theIndex).data(theRole);
62 }
63
64
65 QVariant XGUI_DocumentDataModel::headerData(int theSection, Qt::Orientation theOrient, int theRole) const
66 {
67   return QVariant();
68 }
69
70 int XGUI_DocumentDataModel::rowCount(const QModelIndex& theParent) const
71 {
72   if (!theParent.isValid()) 
73     return myModel->rowCount(theParent) + myPartModels.size();
74
75   QModelIndex aParent = toSourceModel(theParent);
76   return aParent.model()->rowCount(aParent);
77 }
78
79 int XGUI_DocumentDataModel::columnCount(const QModelIndex& theParent) const
80 {
81   return 1;
82 }
83
84 QModelIndex XGUI_DocumentDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
85 {
86   QModelIndex aIndex;
87   if (!theParent.isValid()) {
88     int aOffs = myModel->rowCount();
89     if (theRow < aOffs) 
90       aIndex = myModel->index(theRow, theColumn, theParent);
91     else
92       aIndex = myPartModels.at(theRow - aOffs)->index(theRow - aOffs, theColumn, theParent);
93
94     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
95   } else {
96     QModelIndex* aParent = (QModelIndex*)theParent.internalPointer();
97     aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
98
99     aIndex = createIndex(theRow, theColumn, (void*)getModelIndex(aIndex));
100   }
101   return aIndex;
102 }
103
104
105 QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
106 {
107   QModelIndex aParent = toSourceModel(theIndex);
108   aParent = aParent.model()->parent(aParent);
109   if (aParent.isValid())
110     return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
111   return aParent;
112 }
113
114
115 bool XGUI_DocumentDataModel::hasChildren(const QModelIndex& theParent) const
116 {
117   if (!theParent.isValid())
118     return true;
119   return rowCount(theParent) > 0;
120 }
121
122
123 QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
124 {
125   QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
126   return (*aIndexPtr);
127 }
128
129
130 QModelIndex* XGUI_DocumentDataModel::findModelIndex(const QModelIndex& theIndex) const
131 {
132   QList<QModelIndex*>::const_iterator aIt;
133   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) {
134     QModelIndex* aIndex = (*aIt);
135     if ((*aIndex) == theIndex)
136       return aIndex;
137   }
138   return 0;
139 }
140
141 QModelIndex* XGUI_DocumentDataModel::getModelIndex(const QModelIndex& theIndex) const
142 {
143   QModelIndex* aIndexPtr = findModelIndex(theIndex);
144   if (!aIndexPtr) {
145     aIndexPtr = new QModelIndex(theIndex);
146     XGUI_DocumentDataModel* that = (XGUI_DocumentDataModel*) this;
147     that->myIndexes.append(aIndexPtr);
148   }
149   return aIndexPtr;
150 }
151
152 void XGUI_DocumentDataModel::clearModelIndexes()
153 {
154   QList<QModelIndex*>::const_iterator aIt;
155   for (aIt = myIndexes.constBegin(); aIt != myIndexes.constEnd(); ++aIt) 
156     delete (*aIt);
157   myIndexes.clear();
158 }
159
160 FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
161 {
162   QModelIndex aIndex = toSourceModel(theIndex);
163   const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
164   return aModel->feature(aIndex);
165 }