]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_PartDataModel.cpp
Salome HOME
61e56130c84ad986e7f0e8a1e02349d6b82e8dac
[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(QObject* theParent)
13   : QAbstractItemModel(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     if (theIndex.model() == this) {
28       if (theIndex.internalId() == ParamsFolder)
29         return tr("Parameters");
30
31       if (theIndex.internalId() == ParamObject) {
32         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARAMETERS_GROUP, theIndex.row());
33         return aFeature->data()->getName().c_str();
34       } 
35       if (theIndex.internalId() == ConstructFolder)
36         return tr("Constructions");
37
38       if (theIndex.internalId() == ConstructObject) {
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     if (theIndex.model() == this) {
48       if (theIndex.internalId() == ParamsFolder)
49         return QIcon(":pictures/params_folder.png");
50       else if (theIndex.internalId() == ConstructFolder)
51         return QIcon(":pictures/constr_folder.png");
52     }
53     break;
54
55   case Qt::ToolTipRole:
56     // return Tooltip
57     break;
58   }
59   return QVariant();
60 }
61
62 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
63 {
64   return QVariant();
65 }
66
67 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
68 {
69   if (!theParent.isValid()) 
70     return 2;
71
72   if (theParent.internalId() == ParamsFolder)
73     return myDocument->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
74
75   if (theParent.internalId() == ConstructFolder)
76     return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
77
78   return 0;
79 }
80
81 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
82 {
83   return 1;
84 }
85
86 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
87 {
88   if (!theParent.isValid()) {
89     switch (theRow) {
90     case 0:
91       return createIndex(theRow, theColumn, (quintptr) ParamsFolder);
92     case 1:
93       return createIndex(theRow, theColumn, (quintptr) ConstructFolder);
94     }
95   } else {
96     if (theParent.internalId() == ParamsFolder)
97       return createIndex(theRow, theColumn, (quintptr) ParamObject);
98
99     if (theParent.internalId() == ConstructFolder)
100       return createIndex(theRow, theColumn, (quintptr) ConstructObject);
101   }
102   return QModelIndex();
103 }
104
105 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
106 {
107   int aId = (int)theIndex.internalId();
108   switch (aId) {
109   case ParamsFolder:
110   case ConstructFolder:
111     return QModelIndex();
112   case ParamObject:
113     return createIndex(0, 0, (quintptr) ParamsFolder);
114   case ConstructObject:
115     return createIndex(1, 0, (quintptr) ConstructFolder);
116   }
117   return QModelIndex();
118 }
119
120 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
121 {
122   if (!theParent.isValid())
123     return true;
124
125   int aId = (int)theParent.internalId();
126   switch (aId) {
127   case ParamsFolder:
128     return myDocument->featuresIterator(PARAMETERS_GROUP)->more();
129   case ConstructFolder:
130     return myDocument->featuresIterator(CONSTRUCTIONS_GROUP)->more();
131   case ParamObject:
132   case ConstructObject:
133     return false;
134   } 
135   return false;
136 }
137
138
139 //******************************************************************
140 //******************************************************************
141 //******************************************************************
142 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
143   : QAbstractItemModel(theParent)
144 {
145 }
146
147
148 XGUI_PartDataModel::~XGUI_PartDataModel()
149 {
150 }
151
152 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
153 {
154   switch (theRole) {
155   case Qt::DisplayRole:
156     // return a name
157     if (theIndex.internalId() == MyRoot) {
158       std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
159       return aFeature->data()->getName().c_str();
160     }
161     if (theIndex.internalId() == ParamsFolder)
162       return tr("Parameters");
163     if (theIndex.internalId() == ConstructFolder)
164       return tr("Constructions");
165     break;
166   case Qt::DecorationRole:
167     // return an Icon
168     if (theIndex.internalId() == MyRoot) 
169       return QIcon(":pictures/part_ico.png");
170     if (theIndex.internalId() == ParamsFolder)
171       return QIcon(":pictures/params_folder.png");
172     if (theIndex.internalId() == ConstructFolder)
173       return QIcon(":pictures/constr_folder.png");
174    break;
175   case Qt::ToolTipRole:
176     // return Tooltip
177     break;
178   }
179   return QVariant();
180 }
181
182 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
183 {
184   return QVariant();
185 }
186
187 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
188 {
189   if (!parent.isValid()) 
190     if (myDocument->feature(PARTS_GROUP, myId))
191       return 1;
192     else 
193       return 0;
194   if (parent.internalId() == MyRoot)
195       return 2;
196   return 0;
197 }
198
199 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
200 {
201   return 1;
202 }
203
204 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
205 {
206   if (!theParent.isValid())
207     return createIndex(theRow, 0, (quintptr) MyRoot);
208
209   int aId = (int)theParent.internalId();
210   switch (aId) {
211   case MyRoot:
212     switch (theRow) {
213     case 0:
214       return createIndex(0, 0, (quintptr) ParamsFolder);
215     case 1:
216       return createIndex(1, 0, (quintptr) ConstructFolder);
217     }
218   case ParamsFolder:
219     return createIndex(theRow, 0, (quintptr) ParamObject);
220   case ConstructFolder:
221     return createIndex(theRow, 0, (quintptr) ConstructObject);
222   }
223   return QModelIndex();
224 }
225
226 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
227 {
228   int aId = (int)theIndex.internalId();
229   switch (aId) {
230   case MyRoot:
231     return QModelIndex();
232   case ParamsFolder:
233   case ConstructFolder:
234     return createIndex(0, 0, (quintptr) MyRoot);
235   case ParamObject:
236     return createIndex(0, 0, (quintptr) ParamsFolder);
237   case ConstructObject:
238     return createIndex(1, 0, (quintptr) ConstructFolder);
239   }
240   return QModelIndex();
241 }
242
243 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
244 {
245   if (!theParent.isValid())
246     return myDocument->feature(PARTS_GROUP, myId);
247
248   int aId = (int)theParent.internalId();
249   switch (aId) {
250   case MyRoot:
251     return true;
252   case ParamsFolder:
253     return false; // TODO
254   case ConstructFolder:
255     return false; // TODO
256   case ParamObject:
257   case ConstructObject:
258     return false;
259   }
260   return false;
261 }