Salome HOME
4f8423f467634fcd188ac81f492b49879251e2af
[modules/shaper.git] / src / XGUI / XGUI_PartDataModel.cpp
1 #include "XGUI_PartDataModel.h"
2
3 #include <ModelAPI_PluginManager.h>
4 #include <ModelAPI_Iterator.h>
5 #include <ModelAPI_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Object.h>
8 #include <ModelAPI_AttributeDocRef.h>
9
10 #include <QIcon>
11
12 XGUI_TopDataModel::XGUI_TopDataModel(const std::shared_ptr<ModelAPI_Document>& theDocument, QObject* theParent)
13   : XGUI_FeaturesModel(theDocument, theParent)
14 {
15 }
16   
17 XGUI_TopDataModel::~XGUI_TopDataModel()
18 {
19 }
20
21
22 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
23 {
24   switch (theRole) {
25   case Qt::DisplayRole:
26     // return a name
27     switch (theIndex.internalId()) {
28     case ParamsFolder:
29       return tr("Parameters");
30     case ParamObject:
31       {
32         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row());
33         return aFeature->data()->getName().c_str();
34       } 
35     case ConstructFolder:
36         return tr("Constructions");
37     case ConstructObject:
38       {
39         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row());
40         return aFeature->data()->getName().c_str();
41       }
42     }
43     break;
44
45   case Qt::DecorationRole:
46     // return an Icon
47     switch (theIndex.internalId()) {
48     case ParamsFolder:
49         return QIcon(":pictures/params_folder.png");
50     case ConstructFolder:
51         return QIcon(":pictures/constr_folder.png");
52     case ConstructObject:
53         return QIcon(":pictures/point_ico.png");
54     }
55     break;
56
57   case Qt::ToolTipRole:
58     // return Tooltip
59     break;
60   }
61   return QVariant();
62 }
63
64 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
65 {
66   return QVariant();
67 }
68
69 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
70 {
71   if (!theParent.isValid()) 
72     return 2;
73
74   if (theParent.internalId() == ParamsFolder)
75     return myDocument->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
76
77   if (theParent.internalId() == ConstructFolder)
78     return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
79
80   return 0;
81 }
82
83 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
84 {
85   return 1;
86 }
87
88 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
89 {
90   if (!theParent.isValid()) {
91     switch (theRow) {
92     case 0:
93       return createIndex(theRow, theColumn, (quintptr) ParamsFolder);
94     case 1:
95       return createIndex(theRow, theColumn, (quintptr) ConstructFolder);
96     }
97   } else {
98     if (theParent.internalId() == ParamsFolder)
99       return createIndex(theRow, theColumn, (quintptr) ParamObject);
100
101     if (theParent.internalId() == ConstructFolder)
102       return createIndex(theRow, theColumn, (quintptr) ConstructObject);
103   }
104   return QModelIndex();
105 }
106
107 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
108 {
109   int aId = (int)theIndex.internalId();
110   switch (aId) {
111   case ParamsFolder:
112   case ConstructFolder:
113     return QModelIndex();
114   case ParamObject:
115     return createIndex(0, 0, (quintptr) ParamsFolder);
116   case ConstructObject:
117     return createIndex(1, 0, (quintptr) ConstructFolder);
118   }
119   return QModelIndex();
120 }
121
122 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
123 {
124   return rowCount(theParent) > 0;
125 }
126
127 FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const
128 {
129   switch (theIndex.internalId()) {
130   case ParamsFolder:
131   case ConstructFolder:
132     return 0;
133   case ParamObject:
134     return myDocument->feature(PARAMETERS_GROUP, theIndex.row());
135   case ConstructObject:
136     return myDocument->feature(CONSTRUCTIONS_GROUP, theIndex.row());
137   }
138   return 0;
139 }
140
141
142 //******************************************************************
143 //******************************************************************
144 //******************************************************************
145 XGUI_PartDataModel::XGUI_PartDataModel(const std::shared_ptr<ModelAPI_Document>& theDocument, QObject* theParent)
146   : XGUI_PartModel(theDocument, theParent)
147 {
148 }
149
150
151 XGUI_PartDataModel::~XGUI_PartDataModel()
152 {
153 }
154
155 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
156 {
157   switch (theRole) {
158   case Qt::DisplayRole:
159     // return a name
160     switch (theIndex.internalId()) {
161     case MyRoot:
162       {
163         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
164         return aFeature->data()->getName().c_str();
165       }
166     case ParamsFolder:
167       return tr("Parameters");
168     case ConstructFolder:
169       return tr("Constructions");
170     case ParamObject:
171       {
172         std::shared_ptr<ModelAPI_Feature> aFeature = 
173           featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
174         return aFeature->data()->getName().c_str();
175       }
176     case ConstructObject:
177       {
178         std::shared_ptr<ModelAPI_Feature> aFeature = 
179           featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
180         return aFeature->data()->getName().c_str();
181       }
182     }
183     break;
184   case Qt::DecorationRole:
185     // return an Icon
186     switch (theIndex.internalId()) {
187     case MyRoot:
188       return QIcon(":pictures/part_ico.png");
189     case ParamsFolder:
190       return QIcon(":pictures/params_folder.png");
191     case ConstructFolder:
192       return QIcon(":pictures/constr_folder.png");
193     case ConstructObject:
194         return QIcon(":pictures/point_ico.png");
195     }
196    break;
197   case Qt::ToolTipRole:
198     // return Tooltip
199     break;
200   }
201   return QVariant();
202 }
203
204 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
205 {
206   return QVariant();
207 }
208
209 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
210 {
211   if (!parent.isValid()) 
212     if (myDocument->feature(PARTS_GROUP, myId))
213       return 1;
214     else 
215       return 0;
216   switch (parent.internalId()) {
217   case MyRoot:
218     return 2;
219   case ParamsFolder:
220     return featureDocument()->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
221   case ConstructFolder:
222     return featureDocument()->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
223   }
224   return 0;
225 }
226
227 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
228 {
229   return 1;
230 }
231
232 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
233 {
234   if (!theParent.isValid())
235     return createIndex(theRow, 0, (quintptr) MyRoot);
236
237   int aId = (int)theParent.internalId();
238   switch (aId) {
239   case MyRoot:
240     switch (theRow) {
241     case 0:
242       return createIndex(0, 0, (quintptr) ParamsFolder);
243     case 1:
244       return createIndex(1, 0, (quintptr) ConstructFolder);
245     }
246   case ParamsFolder:
247     return createIndex(theRow, 0, (quintptr) ParamObject);
248   case ConstructFolder:
249     return createIndex(theRow, 0, (quintptr) ConstructObject);
250   }
251   return QModelIndex();
252 }
253
254 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
255 {
256   switch (theIndex.internalId()) {
257   case MyRoot:
258     return QModelIndex();
259   case ParamsFolder:
260   case ConstructFolder:
261     return createIndex(0, 0, (quintptr) MyRoot);
262   case ParamObject:
263     return createIndex(0, 0, (quintptr) ParamsFolder);
264   case ConstructObject:
265     return createIndex(1, 0, (quintptr) ConstructFolder);
266   }
267   return QModelIndex();
268 }
269
270 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
271 {
272   return rowCount(theParent) > 0;
273 }
274
275
276 std::shared_ptr<ModelAPI_Document> XGUI_PartDataModel::featureDocument() const
277 {
278   std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
279   return aFeature->data()->docRef("PartDocument")->value();
280 }
281
282 FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const
283 {
284   switch (theIndex.internalId()) {
285   case MyRoot:
286     return myDocument->feature(PARTS_GROUP, myId);
287   case ParamsFolder:
288   case ConstructFolder:
289     return 0;
290   case ParamObject:
291     return featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
292   case ConstructObject:
293     return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
294   }
295   return 0;
296 }