1 #include "XGUI_PartDataModel.h"
2 #include "XGUI_Workshop.h"
4 #include <ModelAPI_PluginManager.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>
20 //ObjectPtr featureObj(const ObjectPtr& theFeature)
22 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
24 // return aObject->featureRef();
29 XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent)
30 : XGUI_FeaturesModel(theParent)
34 XGUI_TopDataModel::~XGUI_TopDataModel()
39 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
44 switch (theIndex.internalId()) {
46 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
49 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
50 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameters::group(), theIndex.row());
52 return aObject->data()->name().c_str();
56 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
59 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
60 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
62 return aObject->data()->name().c_str();
66 return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
69 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
70 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultBody::group(), theIndex.row());
72 return aObject->data()->name().c_str();
77 case Qt::DecorationRole:
79 switch (theIndex.internalId()) {
81 return QIcon(":pictures/params_folder.png");
84 return QIcon(":pictures/constr_folder.png");
87 return QIcon(":pictures/constr_object.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())
111 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
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() == BodiesFolder)
119 return aRootDoc->size(ModelAPI_ResultBody::group());
123 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
128 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
130 if (!theParent.isValid()) {
133 return createIndex(theRow, theColumn, (qint32) ParamsFolder);
135 return createIndex(theRow, theColumn, (qint32) ConstructFolder);
137 return createIndex(theRow, theColumn, (qint32) BodiesFolder);
140 if (theParent.internalId() == ParamsFolder)
141 return createIndex(theRow, theColumn, (qint32) ParamObject);
143 if (theParent.internalId() == ConstructFolder)
144 return createIndex(theRow, theColumn, (qint32) ConstructObject);
146 if (theParent.internalId() == BodiesFolder)
147 return createIndex(theRow, theColumn, (qint32) BodiesObject);
149 return QModelIndex();
152 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
154 int aId = (int)theIndex.internalId();
157 case ConstructFolder:
159 return QModelIndex();
161 return createIndex(0, 0, (qint32) ParamsFolder);
162 case ConstructObject:
163 return createIndex(1, 0, (qint32) ConstructFolder);
165 return createIndex(2, 0, (qint32) BodiesFolder);
167 return QModelIndex();
170 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
172 return rowCount(theParent) > 0;
175 ObjectPtr XGUI_TopDataModel::object(const QModelIndex& theIndex) const
177 switch (theIndex.internalId()) {
179 case ConstructFolder:
184 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
185 return aRootDoc->object(ModelAPI_ResultParameters::group(), theIndex.row());
187 case ConstructObject:
189 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
190 return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
194 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
195 return aRootDoc->object(ModelAPI_ResultBody::group(), theIndex.row());
202 QModelIndex XGUI_TopDataModel::findParent(const ObjectPtr& theObject) const
204 return findGroup(theObject->groupName().c_str());
207 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
209 if (theGroup == ModelAPI_ResultParameters::group())
210 return createIndex(0, 0, (qint32) ParamsFolder);
211 if (theGroup == ModelAPI_ResultConstruction::group())
212 return createIndex(1, 0, (qint32) ConstructFolder);
213 if (theGroup == ModelAPI_ResultBody::group())
214 return createIndex(2, 0, (qint32) BodiesFolder);
215 return QModelIndex();
218 QModelIndex XGUI_TopDataModel::objectIndex(const ObjectPtr& theObject) const
222 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
223 std::string aGroup = theObject->groupName();
224 int aNb = aRootDoc->size(aGroup);
226 for (int i = 0; i < aNb; i++) {
227 if (aRootDoc->object(aGroup, i) == theObject) {
233 if (aGroup == ModelAPI_ResultParameters::group())
234 return createIndex(aRow, 0, (qint32) ParamObject);
235 if (aGroup == ModelAPI_ResultConstruction::group())
236 return createIndex(aRow, 0, (qint32) ConstructObject);
237 if (aGroup == ModelAPI_ResultBody::group())
238 return createIndex(aRow, 0, (qint32) BodiesObject);
246 //******************************************************************
247 //******************************************************************
248 //******************************************************************
249 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
250 : XGUI_PartModel(theParent)
255 XGUI_PartDataModel::~XGUI_PartDataModel()
259 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
262 case Qt::DisplayRole:
264 switch (theIndex.internalId()) {
267 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
268 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
270 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
273 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
274 case ConstructFolder:
275 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
277 return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
280 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultParameters::group(), theIndex.row());
282 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
284 case ConstructObject:
286 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultConstruction::group(), theIndex.row());
288 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
292 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 3);
294 return aObject->data()->name().c_str();
298 case Qt::DecorationRole:
300 switch (theIndex.internalId()) {
302 return QIcon(":pictures/part_ico.png");
304 return QIcon(":pictures/params_folder.png");
305 case ConstructFolder:
307 return QIcon(":pictures/constr_folder.png");
308 case ConstructObject:
309 return QIcon(":pictures/constr_object.png");
311 // ObjectPtr aObject = partDocument()->object(ModelAPI_ResultConstruction::group(), theIndex.row());
313 // return QIcon(XGUI_Workshop::featureIcon(aObject->getKind()));
317 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 3);
318 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
320 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
324 case Qt::ToolTipRole:
327 case Qt::ForegroundRole:
328 return QBrush(myItemsColor);
334 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
339 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
341 if (!parent.isValid()) {
342 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
343 if (aRootDoc->object(ModelAPI_ResultPart::group(), myId))
348 switch (parent.internalId()) {
350 return 3 + partDocument()->size(ModelAPI_Feature::group());
352 return partDocument()->size(ModelAPI_ResultParameters::group());
353 case ConstructFolder:
354 return partDocument()->size(ModelAPI_ResultConstruction::group());
361 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
366 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
368 if (!theParent.isValid())
369 return createIndex(theRow, 0, (qint32) MyRoot);
371 int aId = (int)theParent.internalId();
376 return createIndex(0, 0, (qint32) ParamsFolder);
378 return createIndex(1, 0, (qint32) ConstructFolder);
380 return createIndex(2, 0, (qint32) BodiesFolder);
382 return createIndex(theRow, theColumn, (qint32) HistoryObject);
385 return createIndex(theRow, 0, (qint32) ParamObject);
386 case ConstructFolder:
387 return createIndex(theRow, 0, (qint32) ConstructObject);
389 return createIndex(theRow, 0, (qint32) BodiesObject);
391 return QModelIndex();
394 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
396 switch (theIndex.internalId()) {
398 return QModelIndex();
400 case ConstructFolder:
403 return createIndex(0, 0, (qint32) MyRoot);
405 return createIndex(0, 0, (qint32) ParamsFolder);
406 case ConstructObject:
407 return createIndex(1, 0, (qint32) ConstructFolder);
409 return QModelIndex();
412 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
414 return rowCount(theParent) > 0;
418 DocumentPtr XGUI_PartDataModel::partDocument() const
420 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
421 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
422 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
423 return aPart->partDoc();
426 ObjectPtr XGUI_PartDataModel::object(const QModelIndex& theIndex) const
428 switch (theIndex.internalId()) {
431 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
432 return aRootDoc->object(ModelAPI_ResultPart::group(), myId);
435 case ConstructFolder:
439 return partDocument()->object(ModelAPI_ResultParameters::group(), theIndex.row());
440 case ConstructObject:
441 return partDocument()->object(ModelAPI_ResultConstruction::group(), theIndex.row());
443 // return partDocument()->feature(ModelAPI_ResultConstruction::group(), theIndex.row());
445 return partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 3);
450 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
452 return (partDocument() == theDoc);
456 QModelIndex XGUI_PartDataModel::findParent(const ObjectPtr& theObject) const
458 return findGroup(theObject->groupName().c_str());
461 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
463 if (theGroup.compare(ModelAPI_ResultParameters::group()) == 0)
464 return createIndex(0, 0, (qint32) ParamsFolder);
465 if (theGroup.compare(ModelAPI_ResultConstruction::group()) == 0)
466 return createIndex(1, 0, (qint32) ConstructFolder);
467 return QModelIndex();
470 ResultPartPtr XGUI_PartDataModel::part() const
472 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
473 ObjectPtr aObj = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
474 return boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
477 QModelIndex XGUI_PartDataModel::objectIndex(const ObjectPtr& theObject) const
481 if (part() == theObject)
484 //std::string aGroup = theFeature->getGroup();
485 DocumentPtr aDoc = theObject->document();
486 int aNb = aDoc->size(ModelAPI_Feature::group());
488 for (int i = 0; i < aNb; i++) {
489 if (aDoc->object(ModelAPI_Feature::group(), i) == theObject) {
495 return createIndex(aRow + 3, 0, (qint32) HistoryObject);
496 /* if (aGroup.compare(ModelAPI_ResultParameters::group()) == 0)
497 return createIndex(aRow, 0, (qint32) ParamObject);
498 if (aGroup.compare(ModelAPI_ResultConstruction::group()) == 0)
499 return createIndex(aRow, 0, (qint32) ConstructObject);*/