+}
+
+//******************************************************
+Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
+{
+ quintptr 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 == 0) {
+ // Folders under root
+ DocumentPtr aRootDoc = aSession->moduleDocument();
+ if (aRootDoc != aActiveDoc)
+ return aDefaultFlag;
+ } else {
+ aDoc = getSubDocument(theIndex.internalPointer());
+ if (!aDoc)
+ aObj = dynamic_cast<ModelAPI_Object*>((ModelAPI_Entity*)theIndex.internalPointer());
+ }
+
+ if (aObj) {
+ // An object
+ if (aObj->isDisabled())
+ return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
+
+ if (aSession->moduleDocument() != aObj->document())
+ if (aActiveDoc != aObj->document())
+ return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
+
+ bool isCompositeSub = false;
+ // An object which is sub-object of a composite object can not be accessible in column 2
+ if (theIndex.column() == 2) {
+ 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, int aColumn) 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, aColumn, aObj.get());
+ }
+ }
+ } else { // If document is attached to feature
+ int aNb = aRootDoc->size(ModelAPI_Feature::group(), true);
+ ObjectPtr aObj;
+ ResultPartPtr aPartRes;
+ for (int i = 0; i < aNb; i++) {
+ aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true);
+ 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, aColumn, aObj.get());
+ }
+ }
+ }
+ return QModelIndex();
+}
+
+//******************************************************
+QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
+{
+ SessionPtr aSession = ModelAPI_Session::get();
+ DocumentPtr aRootDoc = aSession->moduleDocument();
+ if (theDoc == aRootDoc)
+ return QModelIndex();
+ else
+ return findDocumentRootIndex(theDoc.get(), theColumn);
+}
+
+//******************************************************
+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(), 2, aInd.internalPointer());
+ } else {
+ if (aCurDoc == aSession->moduleDocument())
+ return createIndex(foldersCount() - 1, 2, -1);
+ else
+ return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
+ }
+}
+
+//******************************************************
+int XGUI_DataModel::folderId(std::string theType, ModelAPI_Document* theDoc) const
+{
+ 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);
+ }
+}
+
+//******************************************************
+//bool XGUI_DataModel::blockEventsProcessing(const bool theState)
+//{
+// bool aPreviousState = myIsEventsProcessingBlocked;
+// myIsEventsProcessingBlocked = theState;
+// return aPreviousState;
+//}
+
+//******************************************************
+XGUI_DataModel::VisibilityState
+ XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
+{
+ Qt::ItemFlags aFlags = theIndex.flags();
+ if (aFlags == Qt::ItemFlags())
+ return NoneState;
+
+ ObjectPtr aObj = object(theIndex);
+ if (aObj.get()) {
+ ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+ if (aResObj.get()) {
+ XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+ ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
+ if (aCompRes.get()) {
+ VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
+ (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
+ for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
+ ResultPtr aSubRes = aCompRes->subResult(i, true);
+ VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;
+ if (aState == NoneState)
+ aState = aS;
+ else if (aState != aS) {
+ aState = SemiVisible;
+ break;
+ }
+ }
+ return aState;
+ } else {
+ if (aDisplayer->isVisible(aResObj))
+ return Visible;
+ else
+ return Hidden;
+ }
+ }
+ }
+ return NoneState;
+}