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>
18 //ObjectPtr featureObj(const ObjectPtr& theFeature)
20 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
22 // return aObject->featureRef();
27 XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent)
28 : XGUI_FeaturesModel(theParent)
32 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));
47 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
48 ObjectPtr aFeature = aRootDoc->object(PARAMETERS_GROUP, theIndex.row());
50 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->data()->name().c_str();
53 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
56 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
57 ObjectPtr aFeature = aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
59 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->data()->name().c_str();
64 case Qt::DecorationRole:
66 switch (theIndex.internalId()) {
68 return QIcon(":pictures/params_folder.png");
70 return QIcon(":pictures/constr_folder.png");
73 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
74 ObjectPtr aFeature = aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
76 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
84 case Qt::ForegroundRole:
85 return QBrush(myItemsColor);
91 QVariant XGUI_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
96 int XGUI_TopDataModel::rowCount(const QModelIndex& theParent) const
98 if (!theParent.isValid())
101 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
102 if (theParent.internalId() == ParamsFolder)
103 return aRootDoc->size(PARAMETERS_GROUP);
105 if (theParent.internalId() == ConstructFolder)
106 return aRootDoc->size(CONSTRUCTIONS_GROUP);
111 int XGUI_TopDataModel::columnCount(const QModelIndex &parent) const
116 QModelIndex XGUI_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
118 if (!theParent.isValid()) {
121 return createIndex(theRow, theColumn, (qint32) ParamsFolder);
123 return createIndex(theRow, theColumn, (qint32) ConstructFolder);
126 if (theParent.internalId() == ParamsFolder)
127 return createIndex(theRow, theColumn, (qint32) ParamObject);
129 if (theParent.internalId() == ConstructFolder)
130 return createIndex(theRow, theColumn, (qint32) ConstructObject);
132 return QModelIndex();
135 QModelIndex XGUI_TopDataModel::parent(const QModelIndex& theIndex) const
137 int aId = (int)theIndex.internalId();
140 case ConstructFolder:
141 return QModelIndex();
143 return createIndex(0, 0, (qint32) ParamsFolder);
144 case ConstructObject:
145 return createIndex(1, 0, (qint32) ConstructFolder);
147 return QModelIndex();
150 bool XGUI_TopDataModel::hasChildren(const QModelIndex& theParent) const
152 return rowCount(theParent) > 0;
155 ObjectPtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const
157 switch (theIndex.internalId()) {
159 case ConstructFolder:
163 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
164 return aRootDoc->feature(PARAMETERS_GROUP, theIndex.row());
166 case ConstructObject:
168 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
169 return aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row());
176 QModelIndex XGUI_TopDataModel::findParent(const ObjectPtr& theFeature) const
178 return findGroup(theFeature->getGroup().c_str());
181 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
183 if (theGroup.compare(PARAMETERS_GROUP) == 0)
184 return createIndex(0, 0, (qint32) ParamsFolder);
185 if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
186 return createIndex(1, 0, (qint32) ConstructFolder);
187 return QModelIndex();
190 QModelIndex XGUI_TopDataModel::featureIndex(const ObjectPtr& theFeature) const
194 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
195 std::string aGroup = theFeature->getGroup();
196 int aNb = aRootDoc->size(aGroup);
198 for (int i = 0; i < aNb; i++) {
199 if (aRootDoc->feature(aGroup, i) == theFeature) {
205 if (aGroup.compare(PARAMETERS_GROUP) == 0)
206 return createIndex(aRow, 0, (qint32) ParamObject);
207 if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0)
208 return createIndex(aRow, 0, (qint32) ConstructObject);
216 //******************************************************************
217 //******************************************************************
218 //******************************************************************
219 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
220 : XGUI_PartModel(theParent)
225 XGUI_PartDataModel::~XGUI_PartDataModel()
229 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
232 case Qt::DisplayRole:
234 switch (theIndex.internalId()) {
237 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
238 ObjectPtr aFeature = aRootDoc->feature(PARTS_GROUP, myId);
240 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
243 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
244 case ConstructFolder:
245 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
247 return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
250 ObjectPtr aFeature = featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
252 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
254 case ConstructObject:
256 ObjectPtr aFeature = featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
258 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
262 ObjectPtr aFeature = featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
264 return aFeature->data()->getName().c_str();
268 case Qt::DecorationRole:
270 switch (theIndex.internalId()) {
272 return QIcon(":pictures/part_ico.png");
274 return QIcon(":pictures/params_folder.png");
275 case ConstructFolder:
277 return QIcon(":pictures/constr_folder.png");
278 case ConstructObject:
280 ObjectPtr aFeature = featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
282 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
286 ObjectPtr aFeature = featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
288 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
292 case Qt::ToolTipRole:
295 case Qt::ForegroundRole:
296 return QBrush(myItemsColor);
302 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
307 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
309 if (!parent.isValid()) {
310 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
311 if (aRootDoc->feature(PARTS_GROUP, myId))
316 switch (parent.internalId()) {
318 return 3 + featureDocument()->size(FEATURES_GROUP);
320 return featureDocument()->size(PARAMETERS_GROUP);
321 case ConstructFolder:
322 return featureDocument()->size(CONSTRUCTIONS_GROUP);
329 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
334 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
336 if (!theParent.isValid())
337 return createIndex(theRow, 0, (qint32) MyRoot);
339 int aId = (int)theParent.internalId();
344 return createIndex(0, 0, (qint32) ParamsFolder);
346 return createIndex(1, 0, (qint32) ConstructFolder);
348 return createIndex(2, 0, (qint32) BodiesFolder);
350 return createIndex(theRow, theColumn, (qint32) HistoryObject);
353 return createIndex(theRow, 0, (qint32) ParamObject);
354 case ConstructFolder:
355 return createIndex(theRow, 0, (qint32) ConstructObject);
357 return createIndex(theRow, 0, (qint32) BodiesObject);
359 return QModelIndex();
362 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
364 switch (theIndex.internalId()) {
366 return QModelIndex();
368 case ConstructFolder:
371 return createIndex(0, 0, (qint32) MyRoot);
373 return createIndex(0, 0, (qint32) ParamsFolder);
374 case ConstructObject:
375 return createIndex(1, 0, (qint32) ConstructFolder);
377 return QModelIndex();
380 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
382 return rowCount(theParent) > 0;
386 DocumentPtr XGUI_PartDataModel::featureDocument() const
388 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
389 ObjectPtr aFeature = aRootDoc->feature(PARTS_GROUP, myId, true);
390 return aFeature->data()->docRef("PartDocument")->value();
393 ObjectPtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const
395 switch (theIndex.internalId()) {
398 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
399 return aRootDoc->feature(PARTS_GROUP, myId);
402 case ConstructFolder:
406 return featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
407 case ConstructObject:
408 return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
410 // return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
412 return featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
417 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
419 return (featureDocument() == theDoc);
423 QModelIndex XGUI_PartDataModel::findParent(const ObjectPtr& theFeature) const
425 return findGroup(theFeature->getGroup().c_str());
428 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
430 if (theGroup.compare(PARAMETERS_GROUP) == 0)
431 return createIndex(0, 0, (qint32) ParamsFolder);
432 if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
433 return createIndex(1, 0, (qint32) ConstructFolder);
434 return QModelIndex();
437 ObjectPtr XGUI_PartDataModel::part() const
439 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
440 return aRootDoc->object(ModelAPI_ResultPart::group(), myId);
443 QModelIndex XGUI_PartDataModel::featureIndex(const ObjectPtr& theObject) const
447 if (part() == theObject)
450 //std::string aGroup = theFeature->getGroup();
451 DocumentPtr aDoc = theObject->document();
452 int aNb = aDoc->size(ModelAPI_Feature::group());
454 for (int i = 0; i < aNb; i++) {
455 if (aDoc->object(ModelAPI_Feature::group(), i) == theObject) {
461 return createIndex(aRow + 3, 0, (qint32) HistoryObject);
462 /* if (aGroup.compare(PARAMETERS_GROUP) == 0)
463 return createIndex(aRow, 0, (qint32) ParamObject);
464 if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0)
465 return createIndex(aRow, 0, (qint32) ConstructObject);*/