1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "PartSet_TopDataModel.h"
4 #include "PartSet_Module.h"
5 #include "PartSet_DocumentDataModel.h"
7 #include <ModelAPI_Session.h>
8 #include <ModelAPI_Document.h>
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_AttributeDocRef.h>
13 #include <ModelAPI_Object.h>
14 #include <ModelAPI_ResultPart.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_ResultParameter.h>
17 #include <ModelAPI_ResultBody.h>
18 #include <ModelAPI_ResultGroup.h>
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Events.h>
22 #include <Events_Loop.h>
28 PartSet_TopDataModel::PartSet_TopDataModel(QObject* theParent)
29 : PartSet_FeaturesModel(theParent)
33 PartSet_TopDataModel::~PartSet_TopDataModel()
37 QVariant PartSet_TopDataModel::data(const QModelIndex& theIndex, int theRole) const
39 if (theIndex.column() == 1)
45 switch (theIndex.internalId()) {
47 return tr("Parameters") + QString(" (%1)").arg(rowCount(theIndex));
49 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
50 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row());
52 ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObject);
53 AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
54 QString aVal = QString::number(aValueAttribute->value());
55 QString aTitle = QString(aObject->data()->name().c_str());
56 return aTitle + " = " + aVal;
61 return tr("Constructions") + QString(" (%1)").arg(rowCount(theIndex));
62 case ConstructObject: {
63 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
64 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultConstruction::group(),
67 return aObject->data()->name().c_str();
71 // return tr("Groups") + QString(" (%1)").arg(rowCount(theIndex));
73 // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
74 // ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultGroup::group(),
77 // return aObject->data()->name().c_str();
83 case Qt::DecorationRole:
86 switch (theIndex.internalId()) {
88 return QIcon(":pictures/params_folder.png");
90 return QIcon(":pictures/constr_folder.png");
92 return QIcon(":pictures/constr_object.png");
94 // return QIcon(":pictures/constr_folder.png");
102 case Qt::ForegroundRole:
103 return QBrush(myItemsColor);
109 QVariant PartSet_TopDataModel::headerData(int section, Qt::Orientation orientation, int role) const
114 int PartSet_TopDataModel::rowCount(const QModelIndex& theParent) const
116 if (!theParent.isValid())
117 return 2; // In case of groups using it has to be +1
119 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
120 if (theParent.internalId() == ParamsFolder)
121 return aRootDoc->size(ModelAPI_ResultParameter::group());
123 if (theParent.internalId() == ConstructFolder)
124 return aRootDoc->size(ModelAPI_ResultConstruction::group());
126 //if (theParent.internalId() == GroupsFolder)
127 // return aRootDoc->size(ModelAPI_ResultGroup::group());
132 int PartSet_TopDataModel::columnCount(const QModelIndex &parent) const
137 QModelIndex PartSet_TopDataModel::index(int theRow, int theColumn, const QModelIndex& theParent) const
139 if (!theParent.isValid()) {
142 return createIndex(theRow, theColumn, (qint32) ParamsFolder);
144 return createIndex(theRow, theColumn, (qint32) ConstructFolder);
146 // return createIndex(theRow, theColumn, (qint32) GroupsFolder);
149 if (theParent.internalId() == ParamsFolder)
150 return createIndex(theRow, theColumn, (qint32) ParamObject);
152 if (theParent.internalId() == ConstructFolder)
153 return createIndex(theRow, theColumn, (qint32) ConstructObject);
155 //if (theParent.internalId() == GroupsFolder)
156 // return createIndex(theRow, theColumn, (qint32) GroupObject);
158 return QModelIndex();
161 QModelIndex PartSet_TopDataModel::parent(const QModelIndex& theIndex) const
163 int aId = (int) theIndex.internalId();
166 case ConstructFolder:
168 return QModelIndex();
170 return createIndex(0, 0, (qint32) ParamsFolder);
171 case ConstructObject:
172 return createIndex(1, 0, (qint32) ConstructFolder);
174 // return createIndex(2, 0, (qint32) GroupsFolder);
176 return QModelIndex();
179 bool PartSet_TopDataModel::hasChildren(const QModelIndex& theParent) const
181 return rowCount(theParent) > 0;
184 ObjectPtr PartSet_TopDataModel::object(const QModelIndex& theIndex) const
186 switch (theIndex.internalId()) {
188 case ConstructFolder:
191 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
192 return aRootDoc->object(ModelAPI_ResultParameter::group(), theIndex.row());
194 case ConstructObject: {
195 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
196 return aRootDoc->object(ModelAPI_ResultConstruction::group(), theIndex.row());
198 //case GroupObject: {
199 // DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
200 // return aRootDoc->object(ModelAPI_ResultGroup::group(), theIndex.row());
206 QModelIndex PartSet_TopDataModel::findParent(const ObjectPtr& theObject) const
208 return findGroup(theObject->groupName().c_str());
211 QModelIndex PartSet_TopDataModel::findGroup(const std::string& theGroup) const
213 if (theGroup == ModelAPI_ResultParameter::group())
214 return createIndex(0, 0, (qint32) ParamsFolder);
215 if (theGroup == ModelAPI_ResultConstruction::group())
216 return createIndex(1, 0, (qint32) ConstructFolder);
217 //if (theGroup == ModelAPI_ResultGroup::group())
218 // return createIndex(2, 0, (qint32) ConstructFolder);
219 return QModelIndex();
222 QModelIndex PartSet_TopDataModel::objectIndex(const ObjectPtr& theObject) const
226 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
227 std::string aGroup = theObject->groupName();
228 int aNb = aRootDoc->size(aGroup);
230 for (int i = 0; i < aNb; i++) {
231 if (aRootDoc->object(aGroup, i) == theObject) {
237 if (aGroup == ModelAPI_ResultParameter::group())
238 return createIndex(aRow, 0, (qint32) ParamObject);
239 if (aGroup == ModelAPI_ResultConstruction::group())
240 return createIndex(aRow, 0, (qint32) ConstructObject);
241 //if (aGroup == ModelAPI_ResultGroup::group())
242 // return createIndex(aRow, 0, (qint32) GroupObject);