+ qint64 aIt = theIndex.internalId();
+ ModelAPI_Object* aObj = 0;
+ ModelAPI_Document* aDoc = 0;
+ SessionPtr aSession = ModelAPI_Session::get();
+ DocumentPtr aActiveDoc = aSession->activeDocument();
+
+ Qt::ItemFlags aNullFlag;
+ Qt::ItemFlags aDefaultFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
+ Qt::ItemFlags aEditingFlag = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
+
+
+ if (aIt == -1) {
+ // Folders under root
+ DocumentPtr aRootDoc = aSession->moduleDocument();
+ if (aRootDoc != aActiveDoc)
+ return aDefaultFlag;
+ } else {
+ aDoc = getSubDocument(theIndex.internalPointer());
+ if (!aDoc)
+ aObj = (ModelAPI_Object*) theIndex.internalPointer();
+ }
+
+ if (aObj) {
+ // An object
+ if (aObj->isDisabled())
+ return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
+
+ bool isCompositeSub = false;
+ // An object which is sub-object of a composite object can not be accessible in column 1
+ if (theIndex.column() == 1) {
+ ObjectPtr aObjPtr = aObj->data()->owner();
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
+ if (aFeature.get()) {
+ CompositeFeaturePtr aCompFea = ModelAPI_Tools::compositeOwner(aFeature);
+ if (aCompFea.get())
+ isCompositeSub = true;
+ } else {
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObjPtr);
+ if (aResult.get()) {
+ ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
+ if (aCompRes.get())
+ isCompositeSub = true;
+ }
+ }
+ }
+ if (isCompositeSub)
+ return Qt::ItemIsSelectable;
+
+ if (aObj->document() != aActiveDoc) {
+ // The object could be a root of sub-tree
+ ResultPartPtr aPartRes = getPartResult(aObj);
+ if (aPartRes.get()) {
+ if (aPartRes->partDoc() == aActiveDoc)
+ return aEditingFlag;
+ }
+ return aDefaultFlag;
+ }
+ } else if (aDoc) {
+ // A folder under sub-document
+ if (aActiveDoc.get() != aDoc)
+ return aNullFlag;
+ }
+ return aEditingFlag;
+}
+
+//******************************************************
+QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
+{
+ SessionPtr aSession = ModelAPI_Session::get();
+ DocumentPtr aRootDoc = aSession->moduleDocument();
+ if (myXMLReader.isAttachToResult()) { // If document is attached to result
+ int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
+ ObjectPtr aObj;
+ ResultPartPtr aPartRes;
+ for (int i = 0; i < aNb; i++) {
+ aObj = aRootDoc->object(ModelAPI_ResultPart::group(), i);
+ aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
+ if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
+ int aRow = i;
+ if (myXMLReader.rootType() == ModelAPI_Feature::group()) {
+ aRow += foldersCount();
+ }
+ return createIndex(aRow, 0, aObj.get());
+ }
+ }
+ } else { // If document is attached to feature
+ int aNb = aRootDoc->size(ModelAPI_Feature::group());
+ ObjectPtr aObj;
+ ResultPartPtr aPartRes;
+ for (int i = 0; i < aNb; i++) {
+ aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
+ aPartRes = getPartResult(aObj.get());
+ if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) {
+ int aRow = i;
+ if (myXMLReader.rootType() == ModelAPI_Feature::group())
+ aRow += foldersCount();
+ return createIndex(aRow, 0, aObj.get());
+ }
+ }
+ }
+ return QModelIndex();
+}
+
+//******************************************************
+QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
+{
+ SessionPtr aSession = ModelAPI_Session::get();
+ DocumentPtr aRootDoc = aSession->moduleDocument();
+ if (theDoc == aRootDoc)
+ return QModelIndex();
+ else
+ return findDocumentRootIndex(theDoc.get());
+}
+
+//******************************************************
+int XGUI_DataModel::foldersCount(ModelAPI_Document* theDoc) const
+{
+ int aNb = 0;
+ SessionPtr aSession = ModelAPI_Session::get();
+ DocumentPtr aRootDoc = aSession->moduleDocument();
+ if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
+ for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
+ if (myXMLReader.rootShowEmpty(i))
+ aNb++;
+ else {
+ if (aRootDoc->size(myXMLReader.rootFolderType(i)) > 0)
+ aNb++;
+ }
+ }
+ } else {
+ for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
+ if (myXMLReader.subShowEmpty(i))
+ aNb++;
+ else {
+ if (theDoc->size(myXMLReader.subFolderType(i)) > 0)
+ aNb++;
+ }
+ }
+ }
+ return aNb;
+}
+
+
+//******************************************************
+QIntList XGUI_DataModel::missedFolderIndexes(ModelAPI_Document* theDoc) const
+{
+ QIntList aList;
+ SessionPtr aSession = ModelAPI_Session::get();
+ DocumentPtr aRootDoc = aSession->moduleDocument();
+ if ((theDoc == 0) || (theDoc == aRootDoc.get())) {
+ for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
+ if (!myXMLReader.rootShowEmpty(i)) {
+ if (aRootDoc->size(myXMLReader.rootFolderType(i)) == 0)
+ aList.append(i);
+ }
+ }
+ } else {
+ for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
+ if (!myXMLReader.subShowEmpty(i)) {
+ if (theDoc->size(myXMLReader.subFolderType(i)) == 0)
+ aList.append(i);
+ }
+ }
+ }
+ return aList;
+}
+
+
+//******************************************************
+QStringList XGUI_DataModel::listOfShowNotEmptyFolders(bool fromRoot) const
+{
+ QStringList aResult;
+ if (fromRoot) {
+ for (int i = 0; i < myXMLReader.rootFoldersNumber(); i++) {
+ if (!myXMLReader.rootShowEmpty(i))
+ aResult << myXMLReader.rootFolderType(i).c_str();
+ }
+ } else {
+ for (int i = 0; i < myXMLReader.subFoldersNumber(); i++) {
+ if (!myXMLReader.subShowEmpty(i))
+ aResult << myXMLReader.subFolderType(i).c_str();
+ }
+ }
+ return aResult;
+}
+
+//******************************************************
+QModelIndex XGUI_DataModel::lastHistoryIndex() const
+{
+ SessionPtr aSession = ModelAPI_Session::get();
+ DocumentPtr aCurDoc = aSession->activeDocument();
+ FeaturePtr aFeature = aCurDoc->currentFeature(true);
+ if (aFeature.get()) {
+ QModelIndex aInd = objectIndex(aFeature);
+ return createIndex(aInd.row(), 1, aInd.internalPointer());
+ } else {
+ if (aCurDoc == aSession->moduleDocument())
+ return createIndex(foldersCount() - 1, 1, -1);
+ else
+ return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
+ }
+}
+
+//******************************************************
+int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc)
+{
+ SessionPtr aSession = ModelAPI_Session::get();
+ ModelAPI_Document* aDoc = theDoc;
+ if (aDoc == 0)
+ aDoc = aSession->moduleDocument().get();
+
+ bool aUseSubDoc = (aDoc != aSession->moduleDocument().get());
+
+ int aRes = -1;
+ if (aUseSubDoc) {
+ int aId = myXMLReader.subFolderId(theType);
+ aRes = aId;
+ for (int i = 0; i < aId; i++) {
+ if (!myXMLReader.subShowEmpty(i)) {
+ if (aDoc->size(myXMLReader.subFolderType(i)) == 0)
+ aRes--;
+ }
+ }
+ } else {
+ int aId = myXMLReader.rootFolderId(theType);
+ aRes = aId;
+ for (int i = 0; i < aId; i++) {
+ if (!myXMLReader.rootShowEmpty(i)) {
+ if (aDoc->size(myXMLReader.rootFolderType(i)) == 0)
+ aRes--;
+ }
+ }
+ }
+ return aRes;
+}
+
+//******************************************************
+void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& theParent)
+{
+ if (theCount > 0) {
+ removeRows(theRow, theCount, theParent);
+ insertRows(theRow, theCount, theParent);