- SessionPtr aSession = ModelAPI_Session::get();
- DocumentPtr aRootDoc = aSession->moduleDocument();
- int aNbFolders = foldersCount();
-
- QModelIndex aIndex;
-
- if (!theParent.isValid()) {
- if (theRow < aNbFolders) // Return first level folder index
- return createIndex(theRow, theColumn, -1);
- else { // return object under root index
- std::string aType = myXMLReader->rootType();
- int aObjId = theRow - aNbFolders;
- if (aObjId < aRootDoc->size(aType)) {
- ObjectPtr aObj = aRootDoc->object(aType, aObjId);
- aIndex = objectIndex(aObj);
- }
- }
- } else {
- int aId = theParent.internalId();
- int aParentPos = theParent.row();
- if (aId == -1) { // return object index inside of first level of folders
- std::string aType = myXMLReader->rootFolderType(aParentPos);
- if (theRow < aRootDoc->size(aType)) {
- ObjectPtr aObj = aRootDoc->object(aType, theRow);
- aIndex = objectIndex(aObj);
- }
- } else {
- // It is an object which could have children
- ModelAPI_Document* aDoc = getSubDocument(theParent.internalPointer());
- if (aDoc) {
- // It is a folder of sub-document
- int aParentRow = aParentPos;
- QIntList aMissedIdx = missedFolderIndexes(aDoc);
- while (aMissedIdx.contains(aParentRow))
- aParentRow++;
- if (aParentRow < myXMLReader->subFoldersNumber()) {
- std::string aType = myXMLReader->subFolderType(aParentRow);
- if (theRow < aDoc->size(aType)) {
- ObjectPtr aObj = aDoc->object(aType, theRow);
- aIndex = objectIndex(aObj);
- }
- }
- } else {
- ModelAPI_Object* aParentObj = (ModelAPI_Object*)theParent.internalPointer();
-
- // Check for Part feature
- ResultPartPtr aPartRes = getPartResult(aParentObj);
- if (aPartRes.get()) {
- DocumentPtr aSubDoc = aPartRes->partDoc();
- int aNbSubFolders = foldersCount(aSubDoc.get());
- if (theRow < aNbSubFolders) { // Create a Folder of sub-document
- aIndex = createIndex(theRow, theColumn, aSubDoc.get());
- } else {
- // this is an object under sub document root
- std::string aType = myXMLReader->subType();
- ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
- aIndex = objectIndex(aObj);
- }
- } else {
- // Check for composite object
- ModelAPI_CompositeFeature* aCompFeature =
- dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
- if (aCompFeature) {
- aIndex = objectIndex(aCompFeature->subFeature(theRow));
- } else {
- ModelAPI_ResultCompSolid* aCompRes =
- dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
- if (aCompRes)
- aIndex = objectIndex(aCompRes->subResult(theRow));
- }
- }
- }
- }
- }
- if (theColumn != 0)
- return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
- return aIndex;