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_Data.h>
8 #include <ModelAPI_AttributeDocRef.h>
9 #include <ModelAPI_Object.h>
15 //FeaturePtr featureObj(const FeaturePtr& theFeature)
17 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
19 // return aObject->featureRef();
24 XGUI_TopDataModel::XGUI_TopDataModel(QObject* theParent)
25 : XGUI_FeaturesModel(theParent)
29 XGUI_TopDataModel::~XGUI_TopDataModel()
34 QVariant XGUI_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
39 switch (theIndex.internalId()) {
41 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
44 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
45 FeaturePtr aFeature = aRootDoc->feature(PARAMETERS_GROUP, theIndex.row());
47 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
50 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
53 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
54 FeaturePtr aFeature = aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row());
56 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
61 case Qt::DecorationRole:
63 switch (theIndex.internalId()) {
65 return QIcon(":pictures/params_folder.png");
67 return QIcon(":pictures/constr_folder.png");
70 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
71 FeaturePtr aFeature = aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row());
73 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
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_PluginManager::get()->rootDocument();
99 if (theParent.internalId() == ParamsFolder)
100 return aRootDoc->size(PARAMETERS_GROUP);
102 if (theParent.internalId() == ConstructFolder)
103 return aRootDoc->size(CONSTRUCTIONS_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 FeaturePtr XGUI_TopDataModel::feature(const QModelIndex& theIndex) const
154 switch (theIndex.internalId()) {
156 case ConstructFolder:
160 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
161 return aRootDoc->feature(PARAMETERS_GROUP, theIndex.row());
163 case ConstructObject:
165 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
166 return aRootDoc->feature(CONSTRUCTIONS_GROUP, theIndex.row());
173 QModelIndex XGUI_TopDataModel::findParent(const FeaturePtr& theFeature) const
175 return findGroup(theFeature->getGroup().c_str());
178 QModelIndex XGUI_TopDataModel::findGroup(const std::string& theGroup) const
180 if (theGroup.compare(PARAMETERS_GROUP) == 0)
181 return createIndex(0, 0, (qint32) ParamsFolder);
182 if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
183 return createIndex(1, 0, (qint32) ConstructFolder);
184 return QModelIndex();
187 QModelIndex XGUI_TopDataModel::featureIndex(const FeaturePtr& theFeature) const
191 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
192 std::string aGroup = theFeature->getGroup();
193 int aNb = aRootDoc->size(aGroup);
195 for (int i = 0; i < aNb; i++) {
196 if (aRootDoc->feature(aGroup, i) == theFeature) {
202 if (aGroup.compare(PARAMETERS_GROUP) == 0)
203 return createIndex(aRow, 0, (qint32) ParamObject);
204 if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0)
205 return createIndex(aRow, 0, (qint32) ConstructObject);
213 //******************************************************************
214 //******************************************************************
215 //******************************************************************
216 XGUI_PartDataModel::XGUI_PartDataModel(QObject* theParent)
217 : XGUI_PartModel(theParent)
222 XGUI_PartDataModel::~XGUI_PartDataModel()
226 QVariant XGUI_PartDataModel::data(const QModelIndex& theIndex, int theRole) const
229 case Qt::DisplayRole:
231 switch (theIndex.internalId()) {
234 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
235 FeaturePtr aFeature = aRootDoc->feature(PARTS_GROUP, myId);
237 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
240 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
241 case ConstructFolder:
242 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
244 return tr("Bodies") + QString(" (%1)").arg(rowCount(theIndex));
247 FeaturePtr aFeature = featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
249 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
251 case ConstructObject:
253 FeaturePtr aFeature = featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
255 return boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature)->getName().c_str();
259 FeaturePtr aFeature = featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
261 return aFeature->data()->getName().c_str();
265 case Qt::DecorationRole:
267 switch (theIndex.internalId()) {
269 return QIcon(":pictures/part_ico.png");
271 return QIcon(":pictures/params_folder.png");
272 case ConstructFolder:
274 return QIcon(":pictures/constr_folder.png");
275 case ConstructObject:
277 FeaturePtr aFeature = featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
279 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
283 FeaturePtr aFeature = featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
285 return QIcon(XGUI_Workshop::featureIcon(aFeature->getKind()));
289 case Qt::ToolTipRole:
292 case Qt::ForegroundRole:
293 return QBrush(myItemsColor);
299 QVariant XGUI_PartDataModel::headerData(int section, Qt::Orientation orientation, int role) const
304 int XGUI_PartDataModel::rowCount(const QModelIndex& parent) const
306 if (!parent.isValid()) {
307 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
308 if (aRootDoc->feature(PARTS_GROUP, myId))
313 switch (parent.internalId()) {
315 return 3 + featureDocument()->size(FEATURES_GROUP);
317 return featureDocument()->size(PARAMETERS_GROUP);
318 case ConstructFolder:
319 return featureDocument()->size(CONSTRUCTIONS_GROUP);
326 int XGUI_PartDataModel::columnCount(const QModelIndex &parent) const
331 QModelIndex XGUI_PartDataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
333 if (!theParent.isValid())
334 return createIndex(theRow, 0, (qint32) MyRoot);
336 int aId = (int)theParent.internalId();
341 return createIndex(0, 0, (qint32) ParamsFolder);
343 return createIndex(1, 0, (qint32) ConstructFolder);
345 return createIndex(2, 0, (qint32) BodiesFolder);
347 return createIndex(theRow, theColumn, (qint32) HistoryObject);
350 return createIndex(theRow, 0, (qint32) ParamObject);
351 case ConstructFolder:
352 return createIndex(theRow, 0, (qint32) ConstructObject);
354 return createIndex(theRow, 0, (qint32) BodiesObject);
356 return QModelIndex();
359 QModelIndex XGUI_PartDataModel::parent(const QModelIndex& theIndex) const
361 switch (theIndex.internalId()) {
363 return QModelIndex();
365 case ConstructFolder:
368 return createIndex(0, 0, (qint32) MyRoot);
370 return createIndex(0, 0, (qint32) ParamsFolder);
371 case ConstructObject:
372 return createIndex(1, 0, (qint32) ConstructFolder);
374 return QModelIndex();
377 bool XGUI_PartDataModel::hasChildren(const QModelIndex& theParent) const
379 return rowCount(theParent) > 0;
383 DocumentPtr XGUI_PartDataModel::featureDocument() const
385 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
386 FeaturePtr aFeature = aRootDoc->feature(PARTS_GROUP, myId, true);
387 return aFeature->data()->docRef("PartDocument")->value();
390 FeaturePtr XGUI_PartDataModel::feature(const QModelIndex& theIndex) const
392 switch (theIndex.internalId()) {
395 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
396 return aRootDoc->feature(PARTS_GROUP, myId);
399 case ConstructFolder:
403 return featureDocument()->feature(PARAMETERS_GROUP, theIndex.row());
404 case ConstructObject:
405 return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
407 // return featureDocument()->feature(CONSTRUCTIONS_GROUP, theIndex.row());
409 return featureDocument()->feature(FEATURES_GROUP, theIndex.row() - 3);
414 bool XGUI_PartDataModel::hasDocument(const DocumentPtr& theDoc) const
416 return (featureDocument() == theDoc);
420 QModelIndex XGUI_PartDataModel::findParent(const FeaturePtr& theFeature) const
422 return findGroup(theFeature->getGroup().c_str());
425 QModelIndex XGUI_PartDataModel::findGroup(const std::string& theGroup) const
427 if (theGroup.compare(PARAMETERS_GROUP) == 0)
428 return createIndex(0, 0, (qint32) ParamsFolder);
429 if (theGroup.compare(CONSTRUCTIONS_GROUP) == 0)
430 return createIndex(1, 0, (qint32) ConstructFolder);
431 return QModelIndex();
434 FeaturePtr XGUI_PartDataModel::part() const
436 DocumentPtr aRootDoc = ModelAPI_PluginManager::get()->rootDocument();
437 return aRootDoc->feature(PARTS_GROUP, myId, true);
440 QModelIndex XGUI_PartDataModel::featureIndex(const FeaturePtr& theFeature) const
444 if (part() == theFeature)
447 //std::string aGroup = theFeature->getGroup();
448 DocumentPtr aDoc = theFeature->document();
449 int aNb = aDoc->size(FEATURES_GROUP);
451 for (int i = 0; i < aNb; i++) {
452 if (aDoc->feature(FEATURES_GROUP, i) == theFeature) {
458 return createIndex(aRow + 3, 0, (qint32) HistoryObject);
459 /* if (aGroup.compare(PARAMETERS_GROUP) == 0)
460 return createIndex(aRow, 0, (qint32) ParamObject);
461 if (aGroup.compare(CONSTRUCTIONS_GROUP) == 0)
462 return createIndex(aRow, 0, (qint32) ConstructObject);*/