1 #include "XGUI_PartDataModel.h"
2 #include "XGUI_Workshop.h"
4 #include <ModelAPI_Session.h>
5 #include <ModelAPI_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Result.h>
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_AttributeDocRef.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_ResultPart.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_ResultParameters.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_ResultGroup.h>
20 //ObjectPtr featureObj(const ObjectPtr& theFeature)
22 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
24 // return aObject->featureRef();
28 XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent)
29 : XGUI_FeaturesModel(theParent)
33 XGUI_TopDataModel::~XGUI_TopDataModel()
37 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
42 switch (theIndex.internalId()) {
44 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
46 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
47 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameters::group(), theIndex.row());
49 return aObject->data()->name().c_str();
53 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
54 case ConstructObject: {
55 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
56 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(),
59 return aObject->data()->name().c_str();
63 // return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex));
65 // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
66 // ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultGroup::group(),
69 // return aObject->data()->name().c_str();
75 case Qt::DecorationRole:
78 switch (theIndex.internalId()) {
80 return QIcon(":pictures/params_folder.png");
82 return QIcon(":pictures/constr_folder.png");
84 return QIcon(":pictures/constr_object.png");
86 // return QIcon(":pictures/constr_folder.png");
94 case Qt::ForegroundRole:
95 return QBrush(myItemsColor);
101 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
106 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
108 if (!theParent.isValid())
109 return 2; // In case of groups using it has to be +1
111 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
112 if (theParent.internalId() == ParamsFolder)
113 return aRootDoc->size(ModelAPI_ResultParameters::group());
115 if (theParent.internalId() == ConstructFolder)
116 return aRootDoc->size(ModelAPI_ResultConstruction::group());
118 //if (theParent.internalId() == GroupsFolder)
119 // return aRootDoc->size(ModelAPI_ResultGroup::group());
124 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
129 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
131 if (!theParent.isValid()) {
134 return createIndex(theRow, theColumn, (qint32) ParamsFolder);
136 return createIndex(theRow, theColumn, (qint32) ConstructFolder);
138 // return createIndex(theRow, theColumn, (qint32) GroupsFolder);
141 if (theParent.internalId() == ParamsFolder)
142 return createIndex(theRow, theColumn, (qint32) ParamObject);
144 if (theParent.internalId() == ConstructFolder)
145 return createIndex(theRow, theColumn, (qint32) ConstructObject);
147 //if (theParent.internalId() == GroupsFolder)
148 // return createIndex(theRow, theColumn, (qint32) GroupObject);
150 return QModelIndex();
153 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
155 int aId = (int) theIndex.internalId();
158 case ConstructFolder:
160 return QModelIndex();
162 return createIndex(0, 0, (qint32) ParamsFolder);
163 case ConstructObject:
164 return createIndex(1, 0, (qint32) ConstructFolder);
166 // return createIndex(2, 0, (qint32) GroupsFolder);
168 return QModelIndex();
171 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
173 return rowCount(theParent) > 0;
176 ObjectPtr XGUI_TopDataModel::object(const QModelIndex& theIndex) const
178 switch (theIndex.internalId()) {
180 case ConstructFolder:
183 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
184 return aRootDoc->object(ModelAPI_ResultParameters::group(), theIndex.row());
186 case ConstructObject: {
187 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
188 return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
190 //case GroupObject: {
191 // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
192 // return aRootDoc->object(ModelAPI_ResultGroup::group(), theIndex.row());
198 QModelIndex XGUI_TopDataModel::findParent(const ObjectPtr& theObject) const
200 return findGroup(theObject->groupName().c_str());
203 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
205 if (theGroup == ModelAPI_ResultParameters::group())
206 return createIndex(0, 0, (qint32) ParamsFolder);
207 if (theGroup == ModelAPI_ResultConstruction::group())
208 return createIndex(1, 0, (qint32) ConstructFolder);
209 //if (theGroup == ModelAPI_ResultGroup::group())
210 // return createIndex(2, 0, (qint32) ConstructFolder);
211 return QModelIndex();
214 QModelIndex XGUI_TopDataModel::objectIndex(const ObjectPtr& theObject) const
218 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
219 std::string aGroup = theObject->groupName();
220 int aNb = aRootDoc->size(aGroup);
222 for (int i = 0; i < aNb; i++) {
223 if (aRootDoc->object(aGroup, i) == theObject) {
229 if (aGroup == ModelAPI_ResultParameters::group())
230 return createIndex(aRow, 0, (qint32) ParamObject);
231 if (aGroup == ModelAPI_ResultConstruction::group())
232 return createIndex(aRow, 0, (qint32) ConstructObject);
233 //if (aGroup == ModelAPI_ResultGroup::group())
234 // return createIndex(aRow, 0, (qint32) GroupObject);
240 //******************************************************************
241 //******************************************************************
242 //******************************************************************
243 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
244 : XGUI_PartModel(theParent)
248 XGUI_PartDataModel::~XGUI_PartDataModel()
252 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
255 case Qt::DisplayRole:
257 switch (theIndex.internalId()) {
259 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
260 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
262 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
265 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
266 case ConstructFolder:
267 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
269 return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
271 return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex));
273 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultParameters::group(),
276 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
279 case ConstructObject: {
280 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultConstruction::group(),
283 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
287 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultBody::group(), theIndex.row());
289 return aObject->data()->name().c_str();
293 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultGroup::group(), theIndex.row());
295 return aObject->data()->name().c_str();
297 case HistoryObject: {
298 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 4);
300 return aObject->data()->name().c_str();
304 case Qt::DecorationRole:
306 switch (theIndex.internalId()) {
308 return QIcon(":pictures/part_ico.png");
310 return QIcon(":pictures/params_folder.png");
311 case ConstructFolder:
313 return QIcon(":pictures/constr_folder.png");
315 return QIcon(":pictures/constr_folder.png");
316 case ConstructObject:
319 std::string aGroup = theIndex.internalId() == ConstructObject ?
320 ModelAPI_ResultConstruction::group() : ModelAPI_ResultBody::group();
321 ObjectPtr anObject = partDocument()->object(aGroup, theIndex.row());
322 if (anObject && anObject->data() && anObject->data()->mustBeUpdated()) {
323 return QIcon(":pictures/constr_object_modified.png");
325 return QIcon(":pictures/constr_object.png");
327 case HistoryObject: {
328 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 4);
329 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
331 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
335 case Qt::ToolTipRole:
338 case Qt::ForegroundRole:
339 return QBrush(myItemsColor);
345 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
350 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
352 if (!parent.isValid()) {
353 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
354 if (aRootDoc->object(ModelAPI_ResultPart::group(), myId))
359 switch (parent.internalId()) {
362 DocumentPtr aDoc = partDocument();
364 return 4 + aDoc->size(ModelAPI_Feature::group());
369 return partDocument()->size(ModelAPI_ResultParameters::group());
370 case ConstructFolder:
371 return partDocument()->size(ModelAPI_ResultConstruction::group());
373 return partDocument()->size(ModelAPI_ResultBody::group());
375 return partDocument()->size(ModelAPI_ResultGroup::group());
380 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
385 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
387 if (!theParent.isValid())
388 return createIndex(theRow, 0, (qint32) MyRoot);
390 int aId = (int) theParent.internalId();
395 return createIndex(theRow, 0, (qint32) ParamsFolder);
397 return createIndex(theRow, 0, (qint32) ConstructFolder);
399 return createIndex(theRow, 0, (qint32) BodiesFolder);
401 return createIndex(theRow, 0, (qint32) GroupsFolder);
403 return createIndex(theRow, theColumn, (qint32) HistoryObject);
406 return createIndex(theRow, 0, (qint32) ParamObject);
407 case ConstructFolder:
408 return createIndex(theRow, 0, (qint32) ConstructObject);
410 return createIndex(theRow, 0, (qint32) BodiesObject);
412 return createIndex(theRow, 0, (qint32) GroupObject);
414 return QModelIndex();
417 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
419 switch (theIndex.internalId()) {
421 return QModelIndex();
423 case ConstructFolder:
427 return createIndex(0, 0, (qint32) MyRoot);
430 return createIndex(0, 0, (qint32) ParamsFolder);
431 case ConstructObject:
432 return createIndex(1, 0, (qint32) ConstructFolder);
434 return createIndex(2, 0, (qint32) BodiesFolder);
436 return createIndex(3, 0, (qint32) GroupsFolder);
438 return QModelIndex();
441 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
443 return rowCount(theParent) > 0;
446 DocumentPtr XGUI_PartDataModel::partDocument() const
448 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
449 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
450 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
451 return aPart->partDoc();
454 ObjectPtr XGUI_PartDataModel::object(const QModelIndex& theIndex) const
456 switch (theIndex.internalId()) {
458 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
459 return aRootDoc->object(ModelAPI_ResultPart::group(), myId);
462 case ConstructFolder:
468 return partDocument()->object(ModelAPI_ResultParameters::group(), theIndex.row());
469 case ConstructObject:
470 return partDocument()->object(ModelAPI_ResultConstruction::group(), theIndex.row());
472 return partDocument()->object(ModelAPI_ResultBody::group(), theIndex.row());
474 return partDocument()->object(ModelAPI_ResultGroup::group(), theIndex.row());
476 return partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 4);
481 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
483 return (partDocument() == theDoc);
486 QModelIndex XGUI_PartDataModel::findParent(const ObjectPtr& theObject) const
488 return findGroup(theObject->groupName().c_str());
491 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
493 if (theGroup.compare(ModelAPI_ResultParameters::group()) == 0)
494 return createIndex(0, 0, (qint32) ParamsFolder);
495 if (theGroup.compare(ModelAPI_ResultConstruction::group()) == 0)
496 return createIndex(1, 0, (qint32) ConstructFolder);
497 if (theGroup.compare(ModelAPI_ResultBody::group()) == 0)
498 return createIndex(2, 0, (qint32) BodiesFolder);
499 if (theGroup.compare(ModelAPI_ResultGroup::group()) == 0)
500 return createIndex(3, 0, (qint32) GroupsFolder);
501 return QModelIndex();
504 ResultPartPtr XGUI_PartDataModel::part() const
506 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
507 ObjectPtr aObj = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
508 return boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
511 QModelIndex XGUI_PartDataModel::objectIndex(const ObjectPtr& theObject) const
515 if (part() == theObject)
518 std::string aGroup = theObject->groupName();
519 DocumentPtr aDoc = theObject->document();
520 int aNb = aDoc->size(aGroup);
522 for (int i = 0; i < aNb; i++) {
523 if (aDoc->object(aGroup, i) == theObject) {
530 if (aGroup == ModelAPI_ResultParameters::group())
531 return createIndex(aRow, 0, (qint32) ParamObject);
532 else if (aGroup == ModelAPI_ResultConstruction::group())
533 return createIndex(aRow, 0, (qint32) ConstructObject);
534 else if (aGroup == ModelAPI_ResultBody::group())
535 return createIndex(aRow, 0, (qint32) BodiesObject);
536 else if (aGroup == ModelAPI_ResultGroup::group())
537 return createIndex(aRow, 0, (qint32) GroupObject);
539 return createIndex(aRow + 4, 0, (qint32) HistoryObject);