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>
19 //ObjectPtr featureObj(const ObjectPtr& theFeature)
21 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
23 // return aObject->featureRef();
27 XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent)
28 : XGUI_FeaturesModel(theParent)
32 XGUI_TopDataModel::~XGUI_TopDataModel()
36 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
41 switch (theIndex.internalId()) {
43 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
45 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
46 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameters::group(), theIndex.row());
48 return aObject->data()->name().c_str();
52 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
53 case ConstructObject: {
54 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
55 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(),
58 return aObject->data()->name().c_str();
64 case Qt::DecorationRole:
67 switch (theIndex.internalId()) {
69 return QIcon(":pictures/params_folder.png");
71 return QIcon(":pictures/constr_folder.png");
73 return QIcon(":pictures/constr_object.png");
81 case Qt::ForegroundRole:
82 return QBrush(myItemsColor);
88 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
93 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
95 if (!theParent.isValid())
98 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
99 if (theParent.internalId() == ParamsFolder)
100 return aRootDoc->size(ModelAPI_ResultParameters::group());
102 if (theParent.internalId() == ConstructFolder)
103 return aRootDoc->size(ModelAPI_ResultConstruction::group());
108 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
113 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
115 if (!theParent.isValid()) {
118 return createIndex(theRow, theColumn, (qint32) ParamsFolder);
120 return createIndex(theRow, theColumn, (qint32) ConstructFolder);
123 if (theParent.internalId() == ParamsFolder)
124 return createIndex(theRow, theColumn, (qint32) ParamObject);
126 if (theParent.internalId() == ConstructFolder)
127 return createIndex(theRow, theColumn, (qint32) ConstructObject);
129 return QModelIndex();
132 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
134 int aId = (int) theIndex.internalId();
137 case ConstructFolder:
138 return QModelIndex();
140 return createIndex(0, 0, (qint32) ParamsFolder);
141 case ConstructObject:
142 return createIndex(1, 0, (qint32) ConstructFolder);
144 return QModelIndex();
147 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
149 return rowCount(theParent) > 0;
152 ObjectPtr XGUI_TopDataModel::object(const QModelIndex& theIndex) const
154 switch (theIndex.internalId()) {
156 case ConstructFolder:
159 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
160 return aRootDoc->object(ModelAPI_ResultParameters::group(), theIndex.row());
162 case ConstructObject: {
163 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
164 return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
170 QModelIndex XGUI_TopDataModel::findParent(const ObjectPtr& theObject) const
172 return findGroup(theObject->groupName().c_str());
175 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
177 if (theGroup == ModelAPI_ResultParameters::group())
178 return createIndex(0, 0, (qint32) ParamsFolder);
179 if (theGroup == ModelAPI_ResultConstruction::group())
180 return createIndex(1, 0, (qint32) ConstructFolder);
181 return QModelIndex();
184 QModelIndex XGUI_TopDataModel::objectIndex(const ObjectPtr& theObject) const
188 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
189 std::string aGroup = theObject->groupName();
190 int aNb = aRootDoc->size(aGroup);
192 for (int i = 0; i < aNb; i++) {
193 if (aRootDoc->object(aGroup, i) == theObject) {
199 if (aGroup == ModelAPI_ResultParameters::group())
200 return createIndex(aRow, 0, (qint32) ParamObject);
201 if (aGroup == ModelAPI_ResultConstruction::group())
202 return createIndex(aRow, 0, (qint32) ConstructObject);
208 //******************************************************************
209 //******************************************************************
210 //******************************************************************
211 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
212 : XGUI_PartModel(theParent)
216 XGUI_PartDataModel::~XGUI_PartDataModel()
220 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
223 case Qt::DisplayRole:
225 switch (theIndex.internalId()) {
227 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
228 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
230 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
233 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
234 case ConstructFolder:
235 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
237 return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
239 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultParameters::group(),
242 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
244 case ConstructObject: {
245 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultConstruction::group(),
248 return boost::dynamic_pointer_cast<ModelAPI_Object>(aObject)->data()->name().c_str();
251 ObjectPtr aObject = partDocument()->object(ModelAPI_ResultBody::group(), theIndex.row());
253 return aObject->data()->name().c_str();
255 case HistoryObject: {
256 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 3);
258 return aObject->data()->name().c_str();
262 case Qt::DecorationRole:
264 switch (theIndex.internalId()) {
266 return QIcon(":pictures/part_ico.png");
268 return QIcon(":pictures/params_folder.png");
269 case ConstructFolder:
271 return QIcon(":pictures/constr_folder.png");
272 case ConstructObject:
274 std::string aGroup = theIndex.internalId() == ConstructObject ?
275 ModelAPI_ResultConstruction::group() : ModelAPI_ResultBody::group();
276 ObjectPtr anObject = partDocument()->object(aGroup, theIndex.row());
277 if (anObject && anObject->data() && anObject->data()->mustBeUpdated()) {
278 return QIcon(":pictures/constr_object_modified.png");
280 return QIcon(":pictures/constr_object.png");
282 case HistoryObject: {
283 ObjectPtr aObject = partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 3);
284 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
286 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
290 case Qt::ToolTipRole:
293 case Qt::ForegroundRole:
294 return QBrush(myItemsColor);
300 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
305 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
307 if (!parent.isValid()) {
308 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
309 if (aRootDoc->object(ModelAPI_ResultPart::group(), myId))
314 switch (parent.internalId()) {
317 DocumentPtr aDoc = partDocument();
319 return 3 + aDoc->size(ModelAPI_Feature::group());
324 return partDocument()->size(ModelAPI_ResultParameters::group());
325 case ConstructFolder:
326 return partDocument()->size(ModelAPI_ResultConstruction::group());
328 return partDocument()->size(ModelAPI_ResultBody::group());
333 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
338 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
340 if (!theParent.isValid())
341 return createIndex(theRow, 0, (qint32) MyRoot);
343 int aId = (int) theParent.internalId();
348 return createIndex(0, 0, (qint32) ParamsFolder);
350 return createIndex(1, 0, (qint32) ConstructFolder);
352 return createIndex(2, 0, (qint32) BodiesFolder);
354 return createIndex(theRow, theColumn, (qint32) HistoryObject);
357 return createIndex(theRow, 0, (qint32) ParamObject);
358 case ConstructFolder:
359 return createIndex(theRow, 0, (qint32) ConstructObject);
361 return createIndex(theRow, 0, (qint32) BodiesObject);
363 return QModelIndex();
366 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
368 switch (theIndex.internalId()) {
370 return QModelIndex();
372 case ConstructFolder:
375 return createIndex(0, 0, (qint32) MyRoot);
377 return createIndex(0, 0, (qint32) ParamsFolder);
378 case ConstructObject:
379 return createIndex(1, 0, (qint32) ConstructFolder);
381 return createIndex(2, 0, (qint32) BodiesFolder);
383 return QModelIndex();
386 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
388 return rowCount(theParent) > 0;
391 DocumentPtr XGUI_PartDataModel::partDocument() const
393 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
394 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
395 ResultPartPtr aPart = boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
396 return aPart->partDoc();
399 ObjectPtr XGUI_PartDataModel::object(const QModelIndex& theIndex) const
401 switch (theIndex.internalId()) {
403 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
404 return aRootDoc->object(ModelAPI_ResultPart::group(), myId);
407 case ConstructFolder:
411 return partDocument()->object(ModelAPI_ResultParameters::group(), theIndex.row());
412 case ConstructObject:
413 return partDocument()->object(ModelAPI_ResultConstruction::group(), theIndex.row());
415 return partDocument()->object(ModelAPI_ResultBody::group(), theIndex.row());
417 return partDocument()->object(ModelAPI_Feature::group(), theIndex.row() - 3);
422 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
424 return (partDocument() == theDoc);
427 QModelIndex XGUI_PartDataModel::findParent(const ObjectPtr& theObject) const
429 return findGroup(theObject->groupName().c_str());
432 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
434 if (theGroup.compare(ModelAPI_ResultParameters::group()) == 0)
435 return createIndex(0, 0, (qint32) ParamsFolder);
436 if (theGroup.compare(ModelAPI_ResultConstruction::group()) == 0)
437 return createIndex(1, 0, (qint32) ConstructFolder);
438 if (theGroup.compare(ModelAPI_ResultBody::group()) == 0)
439 return createIndex(2, 0, (qint32) BodiesFolder);
440 return QModelIndex();
443 ResultPartPtr XGUI_PartDataModel::part() const
445 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
446 ObjectPtr aObj = aRootDoc->object(ModelAPI_ResultPart::group(), myId);
447 return boost::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
450 QModelIndex XGUI_PartDataModel::objectIndex(const ObjectPtr& theObject) const
454 if (part() == theObject)
457 std::string aGroup = theObject->groupName();
458 DocumentPtr aDoc = theObject->document();
459 int aNb = aDoc->size(aGroup);
461 for (int i = 0; i < aNb; i++) {
462 if (aDoc->object(aGroup, i) == theObject) {
469 if (aGroup == ModelAPI_ResultParameters::group())
470 return createIndex(aRow, 0, (qint32) ParamObject);
471 else if (aGroup == ModelAPI_ResultConstruction::group())
472 return createIndex(aRow, 0, (qint32) ConstructObject);
473 else if (aGroup == ModelAPI_ResultBody::group())
474 return createIndex(aRow, 0, (qint32) BodiesObject);
476 return createIndex(aRow + 3, 0, (qint32) HistoryObject);