X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FXGUI%2FXGUI_DataModel.cpp;h=d428a2c501986816457ec569efff57f10207b880;hb=00a3c1675ca0ead43040cafa3a99f984e1d8177b;hp=196957cc5f871da697a783a2a8074f3697645cf9;hpb=6fb5a58743556b877d64129002572de4116abd96;p=modules%2Fshaper.git diff --git a/src/XGUI/XGUI_DataModel.cpp b/src/XGUI/XGUI_DataModel.cpp index 196957cc5..d428a2c50 100644 --- a/src/XGUI/XGUI_DataModel.cpp +++ b/src/XGUI/XGUI_DataModel.cpp @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include @@ -73,7 +75,10 @@ ResultPartPtr getPartResult(ModelAPI_Object* theObj) /// Returns pointer on document if the given object is document object ModelAPI_Document* getSubDocument(void* theObj) { - ModelAPI_Document* aDoc = dynamic_cast((ModelAPI_Entity*)theObj); + ModelAPI_Document* aDoc = 0; + try { + aDoc = dynamic_cast((ModelAPI_Entity*)theObj); + } catch(...) {} return aDoc; } @@ -130,37 +135,45 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess // Check that new folders could appear QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(); foreach (QString aNotEmptyFolder, aNotEmptyFolders) { - if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1)) + if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) > 0)) { // Appears first object in folder which can not be shown empty - insertRow(myXMLReader->rootFolderId(aObjType)); + if (!hasShownFolder(aRootDoc, aNotEmptyFolder)) { + insertRow(myXMLReader->rootFolderId(aObjType)); + addShownFolder(aRootDoc, aNotEmptyFolder); + } + } } // Insert new object int aRow = aRootDoc->size(aObjType) - 1; if (aRow != -1) { - if (aObjType == aRootType) { + if ((aObjType == aRootType) || (aObjType == ModelAPI_Folder::group())) { insertRow(aRow + aNbFolders + 1); } else { int aFolderId = myXMLReader->rootFolderId(aObjType); if (aFolderId != -1) { - insertRow(aRow, createIndex(aFolderId, 1, (void*)Q_NULLPTR)); + insertRow(aRow, createIndex(aFolderId, 0, (void*)Q_NULLPTR)); } } } } else { // Object created in sub-document - QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get()); + QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0); if (aDocRoot.isValid()) { // Check that new folders could appear QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false); foreach (QString aNotEmptyFolder, aNotEmptyFolders) { - if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1)) + if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) > 0)) { // Appears first object in folder which can not be shown empty - insertRow(myXMLReader->subFolderId(aObjType), aDocRoot); - } - int aRow = aDoc->index(aObject); + if (!hasShownFolder(aDoc, aNotEmptyFolder)) { + insertRow(myXMLReader->subFolderId(aObjType), aDocRoot); + addShownFolder(aDoc, aNotEmptyFolder); + } + } + } + int aRow = aDoc->index(aObject, true); if (aRow != -1) { int aNbSubFolders = foldersCount(aDoc.get()); - if (aObjType == aSubType) { + if ((aObjType == aSubType) || (aObjType == ModelAPI_Folder::group())) { // List of objects under document root insertRow(aRow + aNbSubFolders, aDocRoot); } else { @@ -168,7 +181,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess if (aRow != -1) { int aFolderId = folderId(aObjType, aDoc.get()); if (aFolderId != -1) { - QModelIndex aParentFolder = createIndex(aFolderId, 1, aDoc.get()); + QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get()); insertRow(aRow, aParentFolder); emit dataChanged(aParentFolder, aParentFolder); } @@ -207,8 +220,8 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) { std::string aGroup = (*aIt); if (aDoc == aRootDoc) { // If root objects - int aRow = aRootDoc->size(aGroup); - if (aGroup == aRootType) { + int aRow = aRootDoc->size(aGroup, true); + if ((aGroup == aRootType) || (aGroup == ModelAPI_Folder::group())) { // Process root folder removeRow(aRow + aNbFolders); rebuildBranch(aNbFolders, aRow); @@ -216,7 +229,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess // Process root sub-folder int aFolderId = myXMLReader->rootFolderId(aGroup); if (aFolderId != -1) { - QModelIndex aFolderIndex = createIndex(aFolderId, 1, (void*)Q_NULLPTR); + QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR); removeRow(aRow, aFolderIndex); //rebuildBranch(0, aRow); } @@ -224,20 +237,21 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess // Check that some folders could erased QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(); foreach (QString aNotEmptyFolder, aNotEmptyFolders) { - if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) { + if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup, true) == 0)) { // Appears first object in folder which can not be shown empty removeRow(myXMLReader->rootFolderId(aGroup)); + removeShownFolder(aRootDoc, aNotEmptyFolder); //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType())); break; } } } else { // Remove row for sub-document - QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get()); + QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0); if (aDocRoot.isValid()) { - int aRow = aDoc->size(aGroup); + int aRow = aDoc->size(aGroup, true); int aNbSubFolders = foldersCount(aDoc.get()); - if (aGroup == aSubType) { + if ((aGroup == aSubType) || (aGroup == ModelAPI_Folder::group())) { // List of objects under document root removeRow(aRow + aNbSubFolders, aDocRoot); rebuildBranch(aNbSubFolders, aRow, aDocRoot); @@ -245,18 +259,19 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess // List of objects under a folder int aFolderId = folderId(aGroup, aDoc.get()); if (aFolderId != -1) { - QModelIndex aFolderRoot = createIndex(aFolderId, 1, aDoc.get()); + QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get()); removeRow(aRow, aFolderRoot); //rebuildBranch(0, aRow, aFolderRoot); } } // Check that some folders could disappear QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false); - int aSize = aDoc->size(aGroup); + int aSize = aDoc->size(aGroup, true); foreach (QString aNotEmptyFolder, aNotEmptyFolders) { if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) { // Appears first object in folder which can not be shown empty removeRow(myXMLReader->subFolderId(aGroup), aDocRoot); + removeShownFolder(aDoc, aNotEmptyFolder); //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot); break; } @@ -273,7 +288,6 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess std::set aObjects = aUpdMsg->objects(); std::set::const_iterator aIt; - std::string aObjType; for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) { ObjectPtr aObject = (*aIt); if (aObject->data()->isValid()) { @@ -282,12 +296,16 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) { ResultFieldPtr aResult = std::dynamic_pointer_cast(aFeature->firstResult()); - QModelIndex aIndex = objectIndex(aResult); + QModelIndex aIndex = objectIndex(aResult, 0); removeRows(0, aResult->stepsSize(), aIndex); } else { - QModelIndex aIndex = objectIndex(aObject); - if (aIndex.isValid()) { - emit dataChanged(aIndex, aIndex); + if (aObject->groupName() == ModelAPI_Folder::group()) { + rebuildDataTree(); + } else { + QModelIndex aIndex = objectIndex(aObject, 0); + if (aIndex.isValid()) { + emit dataChanged(aIndex, aIndex); + } } } } else { @@ -309,16 +327,16 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess if (aGroup == myXMLReader->rootType()) // Update objects under root aStartId = foldersCount(); else // Update objects in folder under root - aParent = createIndex(folderId(aGroup), 1, (void*)Q_NULLPTR); + aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR); } else { // Update a sub-document if (aGroup == myXMLReader->subType()) { // Update sub-document root - aParent = findDocumentRootIndex(aDoc.get()); + aParent = findDocumentRootIndex(aDoc.get(), 0); aStartId = foldersCount(aDoc.get()); } else // update folder in sub-document - aParent = createIndex(folderId(aGroup, aDoc.get()), 1, aDoc.get()); + aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get()); } int aChildNb = rowCount(aParent); rebuildBranch(aStartId, aChildNb - aStartId, aParent); @@ -328,7 +346,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr& theMess } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) { DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument(); if (aDoc != aRootDoc) { - QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get()); + QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0); if (aDocRoot.isValid()) emit dataChanged(aDocRoot, aDocRoot); else @@ -373,7 +391,7 @@ QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn { std::string aType = theObject->groupName(); DocumentPtr aDoc = theObject->document(); - int aRow = aDoc->index(theObject); + int aRow = aDoc->index(theObject, true); if (aRow == -1) { // it could be a part of complex object FeaturePtr aFeature = std::dynamic_pointer_cast(theObject); @@ -387,17 +405,16 @@ QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn } } } + int aFRow = -1; + FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aFRow); + if (aFolder.get()) + aRow = aFRow; } else { ResultPtr aResult = std::dynamic_pointer_cast(theObject); if (aResult.get()) { ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult); if (aCompRes.get()) { - for (int i = 0; i < aCompRes->numberOfSubs(true); i++) { - if (aCompRes->subResult(i, true) == theObject) { - aRow = i; - break; - } - } + aRow = ModelAPI_Tools::compSolidIndex(aResult); } } } @@ -408,10 +425,11 @@ QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn } SessionPtr aSession = ModelAPI_Session::get(); DocumentPtr aRootDoc = aSession->moduleDocument(); - if (aDoc == aRootDoc && myXMLReader->rootType() == aType) { + if (aDoc == aRootDoc && + ((myXMLReader->rootType() == aType) || (aType == ModelAPI_Folder::group()))) { // The object from root document aRow += foldersCount(); - } else if (myXMLReader->subType() == aType) { + } else if ((myXMLReader->subType() == aType) || (aType == ModelAPI_Folder::group())) { // The object from sub document aRow += foldersCount(aDoc.get()); } @@ -492,14 +510,13 @@ QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const } } } else { - ModelAPI_Object* aObj = - dynamic_cast((ModelAPI_Entity*)theIndex.internalPointer()); + ObjectPtr aObj = object(theIndex); if (aObj) { switch (theRole) { case Qt::DisplayRole: { if (aObj->groupName() == ModelAPI_ResultParameter::group()) { - ModelAPI_ResultParameter* aParam = dynamic_cast(aObj); + ResultParameterPtr aParam = std::dynamic_pointer_cast(aObj); AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); QString aVal = QString::number(aValueAttribute->value()); @@ -508,7 +525,7 @@ QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const } QString aSuffix; if (aObj->groupName() == myXMLReader->subType()) { - ResultPartPtr aPartRes = getPartResult(aObj); + ResultPartPtr aPartRes = getPartResult(aObj.get()); if (aPartRes.get()) { if (aPartRes->partDoc().get() == NULL) aSuffix = " (Not loaded)"; @@ -517,7 +534,12 @@ QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const return aObj->data()->name().c_str() + aSuffix; } case Qt::DecorationRole: - return ModuleBase_IconFactory::get()->getIcon(object(theIndex)); + { + if (aObj->groupName() == ModelAPI_Folder::group()) + return QIcon(":pictures/features_folder.png"); + else + return ModuleBase_IconFactory::get()->getIcon(aObj); + } } } else { switch (theRole) { @@ -559,7 +581,7 @@ int XGUI_DataModel::rowCount(const QModelIndex& theParent) const int aNbItems = 0; std::string aType = myXMLReader->rootType(); if (!aType.empty()) - aNbItems = aRootDoc->size(aType); + aNbItems = aRootDoc->size(aType, true); return aNbFolders + aNbItems; } @@ -596,7 +618,7 @@ int XGUI_DataModel::rowCount(const QModelIndex& theParent) const int aNbSubItems = 0; std::string aSubType = myXMLReader->subType(); if (!aSubType.empty()) - aNbSubItems = aSubDoc->size(aSubType); + aNbSubItems = aSubDoc->size(aSubType, true); return aNbSubItems + aNbSubFolders; } else { // Check for composite object @@ -609,6 +631,9 @@ int XGUI_DataModel::rowCount(const QModelIndex& theParent) const ModelAPI_ResultField* aFieldRes = dynamic_cast(aObj); if (aFieldRes) return aFieldRes->stepsSize(); + ModelAPI_Folder* aFolder = dynamic_cast(aObj); + if (aFolder) + return getNumberOfFolderItems(aFolder); } } } @@ -636,8 +661,8 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex & 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); + if (aObjId < aRootDoc->size(aType, true)) { + ObjectPtr aObj = aRootDoc->object(aType, aObjId, true); aIndex = objectIndex(aObj, theColumn); } } @@ -647,7 +672,7 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex & if (aId == 0) { // 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); + ObjectPtr aObj = aRootDoc->object(aType, theRow, true); aIndex = objectIndex(aObj, theColumn); } } else { @@ -680,7 +705,7 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex & } else { // this is an object under sub document root std::string aType = myXMLReader->subType(); - ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders); + ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders, true); aIndex = objectIndex(aObj, theColumn); } } else { @@ -699,6 +724,11 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex & dynamic_cast(aParentObj); if (aFieldRes) { aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow)); + } else { + ModelAPI_Folder* aFolder = dynamic_cast(aParentObj); + ObjectPtr aObj = getObjectInFolder(aFolder, theRow); + if (aObj.get()) + aIndex = objectIndex(aObj, theColumn); } } } @@ -729,7 +759,7 @@ QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const } ObjectPtr aObj = object(theIndex); if (!aObj.get()) { - // It can b e a step of a field + // It can be a step of a field ModelAPI_ResultField::ModelAPI_FieldStep* aStep = dynamic_cast ((ModelAPI_Entity*)theIndex.internalPointer()); @@ -755,6 +785,11 @@ QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const if (aCompFea.get()) { return objectIndex(aCompFea); } + DocumentPtr aDoc = aFeature->document(); + int aRow; + FolderPtr aFolder = aDoc->findContainingFolder(aFeature, aRow); + if (aFolder.get()) + return objectIndex(aFolder); } ResultPtr aResult = std::dynamic_pointer_cast(aObj); if (aResult.get()) { @@ -768,7 +803,7 @@ QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const DocumentPtr aRootDoc = aSession->moduleDocument(); DocumentPtr aSubDoc = aObj->document(); if (aSubDoc == aRootDoc) { - if (aType == myXMLReader->rootType()) + if ((aType == myXMLReader->rootType()) || (aType == ModelAPI_Folder::group())) return QModelIndex(); else { // return first level of folder index @@ -777,7 +812,7 @@ QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const return createIndex(aFolderId, 1, (void*)Q_NULLPTR); } } else { - if (aType == myXMLReader->subType()) + if ((aType == myXMLReader->subType()) || (aType == ModelAPI_Folder::group())) return findDocumentRootIndex(aSubDoc.get()); else { // return first level of folder index @@ -886,7 +921,8 @@ Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const } //****************************************************** -QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const +QModelIndex + XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const { SessionPtr aSession = ModelAPI_Session::get(); DocumentPtr aRootDoc = aSession->moduleDocument(); @@ -906,11 +942,11 @@ QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDo } } } else { // If document is attached to feature - int aNb = aRootDoc->size(ModelAPI_Feature::group()); + 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); + aObj = aRootDoc->object(ModelAPI_Feature::group(), i, true); aPartRes = getPartResult(aObj.get()); if (aPartRes.get() && (aPartRes->partDoc().get() == theDoc)) { int aRow = i; @@ -1077,14 +1113,21 @@ void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex& 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()) { + if (aObj->groupName() == ModelAPI_ResultParameter::group()) + return NoneState; ResultPtr aResObj = std::dynamic_pointer_cast(aObj); if (aResObj.get()) { XGUI_Displayer* aDisplayer = myWorkshop->displayer(); ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast(aResObj); if (aCompRes.get()) { - VisibilityState aState = NoneState; + 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; @@ -1106,3 +1149,45 @@ XGUI_DataModel::VisibilityState } return NoneState; } + + +int XGUI_DataModel::getNumberOfFolderItems(const ModelAPI_Folder* theFolder) const +{ + DocumentPtr aDoc = theFolder->document(); + + FeaturePtr aFirstFeatureInFolder; + AttributeReferencePtr aFirstFeatAttr = + theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID()); + if (aFirstFeatAttr) + aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value()); + if (!aFirstFeatureInFolder.get()) + return 0; + + FeaturePtr aLastFeatureInFolder; + AttributeReferencePtr aLastFeatAttr = + theFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID()); + if (aLastFeatAttr) + aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value()); + if (!aLastFeatureInFolder.get()) + return 0; + + int aFirst = aDoc->index(aFirstFeatureInFolder); + int aLast = aDoc->index(aLastFeatureInFolder); + return aLast - aFirst + 1; +} + +ObjectPtr XGUI_DataModel::getObjectInFolder(const ModelAPI_Folder* theFolder, int theId) const +{ + DocumentPtr aDoc = theFolder->document(); + + FeaturePtr aFirstFeatureInFolder; + AttributeReferencePtr aFirstFeatAttr = + theFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID()); + if (aFirstFeatAttr) + aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value()); + if (!aFirstFeatureInFolder.get()) + return ObjectPtr(); + + int aFirst = aDoc->index(aFirstFeatureInFolder); + return aDoc->object(ModelAPI_Feature::group(), aFirst + theId); +}