+//**************************************************************
+bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
+{
+ bool isResult = false;
+
+ if (!theFeature->data()->isValid())
+ return isResult;
+
+ ResultPtr aFirstResult = theFeature->firstResult();
+ if (!aFirstResult.get())
+ return isResult;
+
+ return aFirstResult->groupName() == theGroupOfResult;
+}
+
+//**************************************************************
+bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
+{
+ bool aFoundModuleDocumentObject = false;
+ DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
+
+ std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
+ for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
+ if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
+ continue;
+ aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
+ }
+
+ return aFoundModuleDocumentObject;
+}
+
+//**************************************************************
+bool askToDelete(const std::set<FeaturePtr> theFeatures,
+ const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
+ QWidget* theParent,
+ std::set<FeaturePtr>& theReferencesToDelete,
+ const std::string& thePrefixInfo)
+{
+ QString aNotActivatedDocWrn;
+ std::string aNotActivatedNames;
+ if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
+ if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
+ aNotActivatedDocWrn =
+ QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n")
+ .arg(aNotActivatedNames.c_str());
+ }
+
+ std::set<FeaturePtr> aFeaturesRefsTo;
+ std::set<FeaturePtr> aFeaturesRefsToParameter;
+ std::set<FeaturePtr> aParameterFeatures;
+ QStringList aPartFeatureNames;
+ std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
+ aLast = theFeatures.end();
+ // separate features to references to parameter features and references to others
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ if (theReferences.find(aFeature) == theReferences.end())
+ continue;
+
+ if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
+ aPartFeatureNames.append(aFeature->name().c_str());
+
+ std::set<FeaturePtr> aRefFeatures;
+ std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
+ std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
+ for (; aRefIt != aRefLast; aRefIt++) {
+ FeaturePtr aRefFeature = *aRefIt;
+ if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
+ aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
+ aRefFeatures.insert(aRefFeature);
+ }
+
+ if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
+ aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
+ aParameterFeatures.insert(aFeature);
+ }
+ else {
+ theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
+ }
+ }
+
+ std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
+ anIt = aFeaturesRefsToParameter.begin();
+ aLast = aFeaturesRefsToParameter.end();
+ // separate features to references to parameter features and references to others
+ QStringList aParamFeatureNames;
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
+ aFeaturesRefsToParameterOnly.insert(aFeature);
+ aParamFeatureNames.append(aFeature->name().c_str());
+ }
+ }
+ aParamFeatureNames.sort();
+ QStringList anOtherFeatureNames;
+ anIt = theReferencesToDelete.begin();
+ aLast = theReferencesToDelete.end();
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
+ aPartFeatureNames.append(aFeature->name().c_str());
+ else
+ anOtherFeatureNames.append(aFeature->name().c_str());
+ }
+ aPartFeatureNames.sort();
+ anOtherFeatureNames.sort();
+
+ bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
+
+ QMessageBox aMessageBox(theParent);
+ aMessageBox.setWindowTitle(QObject::tr("Delete features"));
+ aMessageBox.setIcon(QMessageBox::Warning);
+ aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
+ aMessageBox.setDefaultButton(QMessageBox::No);
+
+ QString aText;
+ if (!thePrefixInfo.empty())
+ aText = thePrefixInfo.c_str();
+ QString aSep = ", ";
+ if (!aPartFeatureNames.empty()) {
+ aText += QString(QObject::tr("The following parts will be deleted: %1.\n"))
+ .arg(aPartFeatureNames.join(aSep));
+ }
+ if (!aNotActivatedDocWrn.isEmpty())
+ aText += aNotActivatedDocWrn;
+ if (!anOtherFeatureNames.empty()) {
+ const char* aMsg = "Features are used in the following features: %1.\nThese "
+ "features will be deleted.\n";
+ aText += QString(QObject::tr(aMsg))
+ .arg(anOtherFeatureNames.join(aSep));
+ }
+ if (!aParamFeatureNames.empty()) {
+ const char* aMsg = "Parameters are used in the following features: %1.\nThese features will "
+ "be deleted.\nOr parameters could be replaced by their values.\n";
+ aText += QString(QObject::tr(aMsg))
+ .arg(aParamFeatureNames.join(aSep));
+ QPushButton *aReplaceButton =
+ aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
+ }
+
+ if (!aText.isEmpty()) {
+ aText += "Would you like to continue?";
+ aMessageBox.setText(aText);
+ aMessageBox.exec();
+ QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
+
+ if (aButtonRole == QMessageBox::NoRole)
+ return false;
+
+ if (aButtonRole == QMessageBox::ActionRole) {
+ foreach (FeaturePtr aObj, aParameterFeatures)
+ ModelAPI_ReplaceParameterMessage::send(aObj, 0);
+ }
+ else
+ theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(),
+ aFeaturesRefsToParameterOnly.end());
+ }
+ return true;
+}
+
+//**************************************************************
+void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
+{
+ QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
+ for(; anIt != aLast; anIt++) {
+ ObjectPtr anObject = *anIt;
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
+ // for parameter result, use the corresponded reature to be removed
+ if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
+ aFeature = ModelAPI_Feature::feature(anObject);
+ }
+ if (aFeature.get())
+ theFeatures.insert(aFeature);
+ }
+}
+
+//**************************************************************
+void convertToFolders(const QObjectPtrList& theObjects,
+ std::set<FolderPtr>& theFolders)
+{
+ QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
+ for(; anIt != aLast; anIt++) {
+ ObjectPtr anObject = *anIt;
+ FolderPtr aFeature = std::dynamic_pointer_cast<ModelAPI_Folder>(anObject);
+ if (aFeature.get())
+ theFolders.insert(aFeature);
+ }
+}
+
+
+//**************************************************************
+QString translate(const Events_InfoMessage& theMessage)
+{
+ QString aMessage;
+
+ if (!theMessage.empty()) {
+ std::string aStr = Config_Translator::translate(theMessage);
+ if (!aStr.empty()) {
+ std::string aCodec = Config_Translator::codec(theMessage);
+ aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
+ }
+ }
+
+ return aMessage;
+}
+
+QString translate(const std::string& theContext, const std::string& theMessage)
+{
+ QString aMessage;
+
+ if (!theMessage.empty()) {
+ std::string aStr = Config_Translator::translate(theContext, theMessage);
+ if (!aStr.empty()) {
+ std::string aCodec = Config_Translator::codec(theContext);
+ aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
+ }
+ }
+
+ return aMessage;
+}
+
+void setPointBallHighlighting(AIS_Shape* theAIS)
+{
+ static Handle(Image_AlienPixMap) aPixMap;
+ if(aPixMap.IsNull()) {
+ // Load icon for the presentation
+ std::string aFile;
+ char* anEnv = getenv("SHAPER_ROOT_DIR");
+ if(anEnv) {
+ aFile = std::string(anEnv) +
+ FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
+ } else {
+ anEnv = getenv("OPENPARTS_ROOT_DIR");
+ if (anEnv)
+ aFile = std::string(anEnv) + FSEP + "resources";
+ }
+
+ aFile += FSEP;
+ static const std::string aMarkerName = "marker_dot.png";
+ aFile += aMarkerName;
+ aPixMap = new Image_AlienPixMap();
+ if(!aPixMap->Load(aFile.c_str())) {
+ // The icon for constraint is not found
+ static const std::string aMsg =
+ "Error: Point market not found by path: \"" + aFile + "\". Falling back.";
+ //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
+ }
+ }
+
+ Handle(Graphic3d_AspectMarker3d) anAspect;
+ Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes();
+#ifdef USE_OCCT_720
+ // to do: implement ball highlighting, in 7.2.0 this drawer is NULL
+#else
+ if(aDrawer->HasOwnPointAspect()) {
+ Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
+ if(aPixMap->IsEmpty()) {
+ anAspect = aPntAspect->Aspect();
+ anAspect->SetType(Aspect_TOM_BALL);
+ } else {
+ if(aPixMap->Format() == Image_PixMap::ImgGray) {
+ aPixMap->SetFormat (Image_PixMap::ImgAlpha);
+ } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
+ aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
+ }
+ anAspect = new Graphic3d_AspectMarker3d(aPixMap);
+ aPntAspect->SetAspect(anAspect);
+ }
+ aDrawer->SetPointAspect(aPntAspect);
+ theAIS->SetHilightAttributes(aDrawer);
+ }
+#endif
+}
+
+} // namespace ModuleBase_Tools
+