From 1b303fb931f07905222fe2a6b80d3bfa20e167d6 Mon Sep 17 00:00:00 2001 From: nds Date: Thu, 15 Jan 2015 11:28:39 +0300 Subject: [PATCH] Issue #231 Deletion of a feature Message box correction to show it only once with the list of refefenced features. --- src/Model/Model_Document.cpp | 32 +++++---- src/Model/Model_Document.h | 13 +++- src/ModelAPI/ModelAPI_Document.h | 13 +++- src/PartSetPlugin/PartSetPlugin_Remove.cpp | 6 +- src/SketchPlugin/SketchPlugin_Sketch.cpp | 4 +- src/XGUI/XGUI_Workshop.cpp | 75 +++++++++++++++------- 6 files changed, 101 insertions(+), 42 deletions(-) diff --git a/src/Model/Model_Document.cpp b/src/Model/Model_Document.cpp index ec10e7963..67339fdf3 100644 --- a/src/Model/Model_Document.cpp +++ b/src/Model/Model_Document.cpp @@ -574,22 +574,30 @@ static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced, co return aResult; } -void Model_Document::removeFeature(FeaturePtr theFeature, const bool theCheck) +void Model_Document::refsToFeature(FeaturePtr theFeature, + std::set >& theRefs, + const bool isSendError) { - if (theCheck) { - // check the feature: it must have no depended objects on it - std::list::const_iterator aResIter = theFeature->results().cbegin(); - for(; aResIter != theFeature->results().cend(); aResIter++) { - std::shared_ptr aData = - std::dynamic_pointer_cast((*aResIter)->data()); - if (aData && !aData->refsToMe().empty()) { - Events_Error::send( - "Feature '" + theFeature->data()->name() + "' is used and can not be deleted"); - return; - } + // check the feature: it must have no depended objects on it + std::list::const_iterator aResIter = theFeature->results().cbegin(); + for(; aResIter != theFeature->results().cend(); aResIter++) { + ResultPtr aResult = (*aResIter); + std::shared_ptr aData = + std::dynamic_pointer_cast(aResult->data()); + if (aData && !aData->refsToMe().empty()) { + FeaturePtr aFeature = ModelAPI_Feature::feature(aResult); + if (aFeature.get() != NULL) + theRefs.insert(aFeature); } } + if (!theRefs.empty() && isSendError) { + Events_Error::send( + "Feature '" + theFeature->data()->name() + "' is used and can not be deleted"); + } +} +void Model_Document::removeFeature(FeaturePtr theFeature/*, const bool theCheck*/) +{ std::shared_ptr aData = std::static_pointer_cast(theFeature->data()); if (aData) { TDF_Label aFeatureLabel = aData->label().Father(); diff --git a/src/Model/Model_Document.h b/src/Model/Model_Document.h index a766b981e..cb99181f9 100644 --- a/src/Model/Model_Document.h +++ b/src/Model/Model_Document.h @@ -78,10 +78,17 @@ class Model_Document : public ModelAPI_Document //! \param theID creates feature and puts it in the document MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID); + //! Return a list of features, which refers to the feature + //! \param theFeature a feature + //! \param theRefs a list of reference features + //! \param isSendError a flag whether the error message should be send + MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature, + std::set& theRefs, + const bool isSendError = true); + //! Removes the feature from the document (with result) - //! \param theFeature the feature to be removed - //! \param theCheck if it is false, do not check the references - MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature, const bool theCheck = true); + //! \param theFeature a removed feature + MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature); //! Returns the existing feature by the label //! \param theLabel base label of the feature diff --git a/src/ModelAPI/ModelAPI_Document.h b/src/ModelAPI/ModelAPI_Document.h index d6a7ce0bf..bf1fa3832 100644 --- a/src/ModelAPI/ModelAPI_Document.h +++ b/src/ModelAPI/ModelAPI_Document.h @@ -12,6 +12,7 @@ #include #include #include +#include class ModelAPI_Feature; class ModelAPI_Object; @@ -44,9 +45,17 @@ public: //! \param theID creates feature and puts it in the document (if it is not action) virtual std::shared_ptr addFeature(std::string theID) = 0; + //! Return a list of features, which refers to the feature + //! \param theFeature a feature + //! \param theRefs a list of features + //! \param isSendError a flag whether the error message should be send + virtual void refsToFeature(std::shared_ptr theFeature, + std::set >& theRefs, + const bool isSendError = true) = 0; + //! Removes the feature from the document - virtual void removeFeature(std::shared_ptr theFeature, - const bool theCheck = true) = 0; + //! \param theFeature a feature to be removed + virtual void removeFeature(std::shared_ptr theFeature) = 0; ///! Adds a new sub-document by the identifier, or returns existing one if it is already exist virtual std::shared_ptr subDocument(std::string theDocID) = 0; diff --git a/src/PartSetPlugin/PartSetPlugin_Remove.cpp b/src/PartSetPlugin/PartSetPlugin_Remove.cpp index d77712bb3..0c47368fc 100644 --- a/src/PartSetPlugin/PartSetPlugin_Remove.cpp +++ b/src/PartSetPlugin/PartSetPlugin_Remove.cpp @@ -11,6 +11,7 @@ #include #include #include +#include void PartSetPlugin_Remove::execute() { @@ -28,7 +29,10 @@ void PartSetPlugin_Remove::execute() if (aFeature) { // do remove aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value()->close(); - aRoot->removeFeature(aFeature); + std::set > aRefFeatures; + aRoot->refsToFeature(aFeature, aRefFeatures); + if (aRefFeatures.empty()) + aRoot->removeFeature(aFeature); } } } diff --git a/src/SketchPlugin/SketchPlugin_Sketch.cpp b/src/SketchPlugin/SketchPlugin_Sketch.cpp index 8e9644e45..024bda4b4 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.cpp +++ b/src/SketchPlugin/SketchPlugin_Sketch.cpp @@ -266,8 +266,8 @@ void SketchPlugin_Sketch::erase() for (; anIt != aFeatures.end(); anIt++) { FeaturePtr aFeature = std::dynamic_pointer_cast(*anIt); if (aFeature) { - // subs are referenced from sketch, but must be removed for sure, so not checkings - document()->removeFeature(aFeature, false); + // subs are referenced from sketch, but must be removed for sure, so not checkings + document()->removeFeature(aFeature); } } ModelAPI_CompositeFeature::erase(); diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 4022d7fc7..4eae59578 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -1259,31 +1259,62 @@ void XGUI_Workshop::activatePart(ResultPartPtr theFeature) void XGUI_Workshop::deleteObjects(const QObjectPtrList& theList) { QMainWindow* aDesktop = isSalomeMode() ? salomeConnector()->desktop() : myMainWindow; - QMessageBox::StandardButton aRes = QMessageBox::warning( - aDesktop, tr("Delete features"), tr("Seleted features will be deleted. Continue?"), - QMessageBox::No | QMessageBox::Yes, QMessageBox::No); - // ToDo: definbe deleting method - if (aRes == QMessageBox::Yes) { - SessionPtr aMgr = ModelAPI_Session::get(); - aMgr->startOperation(); - foreach (ObjectPtr aObj, theList) - { - ResultPartPtr aPart = std::dynamic_pointer_cast(aObj); - if (aPart) { - DocumentPtr aDoc = aPart->document(); - if (aDoc == aMgr->activeDocument()) { - aDoc->close(); - } - //aMgr->moduleDocument()->removeFeature(aPart->owner()); - } else { - FeaturePtr aFeature = std::dynamic_pointer_cast(aObj); - if (aFeature) - aObj->document()->removeFeature(aFeature); + + std::set aRefFeatures; + foreach (ObjectPtr aObj, theList) + { + ResultPartPtr aPart = std::dynamic_pointer_cast(aObj); + if (aPart) { + // TODO: check for what there is this condition. It is placed here historicaly because + // ther is this condition during remove features. + } else { + FeaturePtr aFeature = std::dynamic_pointer_cast(aObj); + if (aFeature) { + aObj->document()->refsToFeature(aFeature, aRefFeatures, false); } } - myDisplayer->updateViewer(); - aMgr->finishOperation(); } + + if (!aRefFeatures.empty()) { + QStringList aRefNames; + std::set::const_iterator anIt = aRefFeatures.begin(), + aLast = aRefFeatures.end(); + for (; anIt != aLast; anIt++) { + FeaturePtr aFeature = (*anIt); + std::string aFName = aFeature->data()->name().c_str(); + std::string aName = (*anIt)->name().c_str(); + aRefNames.append((*anIt)->name().c_str()); + } + QString aNames = aRefNames.join(", "); + + QMessageBox::StandardButton aRes = QMessageBox::warning( + aDesktop, tr("Delete features"), + QString(tr("Selected features are used in the following features: %1.\ +These features will be deleted also. Would you like to continue?")).arg(aNames), + QMessageBox::No | QMessageBox::Yes, QMessageBox::No); + if (aRes != QMessageBox::Yes) + return; + } + + SessionPtr aMgr = ModelAPI_Session::get(); + aMgr->startOperation(); + foreach (ObjectPtr aObj, theList) + { + DocumentPtr aDoc = aObj->document(); + ResultPartPtr aPart = std::dynamic_pointer_cast(aObj); + if (aPart) { + if (aDoc == aMgr->activeDocument()) { + aDoc->close(); + } + } else { + FeaturePtr aFeature = std::dynamic_pointer_cast(aObj); + if (aFeature) { + aDoc->removeFeature(aFeature); + } + } + } + myDisplayer->updateViewer(); + aMgr->finishOperation(); } //************************************************************** -- 2.39.2