Salome HOME
Align text and icons of features in the workbench to the left
[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     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
128 //******************************************************************
129 //******************************************************************
130 //******************************************************************
131 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
132   : QAbstractItemModel(theParent)
133 {
134 }
135
136
137 XGUI_PartDataModel::~XGUI_PartDataModel()
138 {
139 }
140
141 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
142 {
143   switch (theRole) {
144   case Qt::DisplayRole:
145     // return a name
146     switch (theIndex.internalId()) {
147     case MyRoot:
148       {
149         std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
150         return aFeature->data()->getName().c_str();
151       }
152     case ParamsFolder:
153       return tr("Parameters");
154     case ConstructFolder:
155       return tr("Constructions");
156     case ParamObject:
157       {
158         std::shared_ptr<ModelAPI_Feature> aFeature = 
159           featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
160         return aFeature->data()->getName().c_str();
161       }
162     case ConstructObject:
163       {
164         std::shared_ptr<ModelAPI_Feature> aFeature = 
165           featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
166         return aFeature->data()->getName().c_str();
167       }
168     }
169     break;
170   case Qt::DecorationRole:
171     // return an Icon
172     switch (theIndex.internalId()) {
173     case MyRoot:
174       return QIcon(":pictures/part_ico.png");
175     case ParamsFolder:
176       return QIcon(":pictures/params_folder.png");
177     case ConstructFolder:
178       return QIcon(":pictures/constr_folder.png");
179     case ConstructObject:
180         return QIcon(":pictures/point_ico.png");
181     }
182    break;
183   case Qt::ToolTipRole:
184     // return Tooltip
185     break;
186   }
187   return QVariant();
188 }
189
190 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
191 {
192   return QVariant();
193 }
194
195 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
196 {
197   if (!parent.isValid()) 
198     if (myDocument->feature(PARTS_GROUP, myId))
199       return 1;
200     else 
201       return 0;
202   switch (parent.internalId()) {
203   case MyRoot:
204     return 2;
205   case ParamsFolder:
206     return featureDocument()->featuresIterator(PARAMETERS_GROUP)->numIterationsLeft();
207   case ConstructFolder:
208     return featureDocument()->featuresIterator(CONSTRUCTIONS_GROUP)->numIterationsLeft();
209   }
210   return 0;
211 }
212
213 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
214 {
215   return 1;
216 }
217
218 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
219 {
220   if (!theParent.isValid())
221     return createIndex(theRow, 0, (quintptr) MyRoot);
222
223   int aId = (int)theParent.internalId();
224   switch (aId) {
225   case MyRoot:
226     switch (theRow) {
227     case 0:
228       return createIndex(0, 0, (quintptr) ParamsFolder);
229     case 1:
230       return createIndex(1, 0, (quintptr) ConstructFolder);
231     }
232   case ParamsFolder:
233     return createIndex(theRow, 0, (quintptr) ParamObject);
234   case ConstructFolder:
235     return createIndex(theRow, 0, (quintptr) ConstructObject);
236   }
237   return QModelIndex();
238 }
239
240 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
241 {
242   int aId = (int)theIndex.internalId();
243   switch (aId) {
244   case MyRoot:
245     return QModelIndex();
246   case ParamsFolder:
247   case ConstructFolder:
248     return createIndex(0, 0, (quintptr) MyRoot);
249   case ParamObject:
250     return createIndex(0, 0, (quintptr) ParamsFolder);
251   case ConstructObject:
252     return createIndex(1, 0, (quintptr) ConstructFolder);
253   }
254   return QModelIndex();
255 }
256
257 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
258 {
259   return rowCount(theParent) > 0;
260 }
261
262
263 std::shared_ptr<ModelAPI_Document> XGUI_PartDataModel::featureDocument() const
264 {
265   std::shared_ptr<ModelAPI_Feature> aFeature = myDocument->feature(PARTS_GROUP, myId);
266   return aFeature->data()->docRef("PartDocument")->value();
267 }