Salome HOME
Quick correction to do not build coincidence to central point of reentrant tangent arc
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_WidgetParamsMgr.cpp
index 679d42896eb06ba690bb2fde4bd5e6fbc8edc11c..291245b841c8693b509565d7beaa7a26c72e96f8 100644 (file)
@@ -8,6 +8,8 @@
 #include "ParametersPlugin_Parameter.h"
 #include "ParametersPlugin_Validators.h"
 
+#include <Events_InfoMessage.h>
+
 #include <ModelAPI_ResultParameter.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_AttributeRefList.h>
@@ -15,6 +17,8 @@
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
+#include <ModelAPI_Expression.h>
 
 #include <ModuleBase_Tools.h>
 
@@ -29,6 +33,7 @@
 #include <QTimer>
 #include <QEvent>
 #include <QKeyEvent>
+#include <QDialogButtonBox>
 
 enum ColumnType {
   Col_Name,
@@ -154,7 +159,7 @@ void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor, QAbstractItemD
 /////////////////////////////////////////////////////////////////////////////////////////////////
 
 ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent, const Config_WidgetAPI* theData)
-  : ModuleBase_ModelWidget(theParent, theData)
+  : ModuleBase_ModelDialogWidget(theParent, theData)
 {
   QVBoxLayout* aLayout = new QVBoxLayout(this);
 
@@ -250,11 +255,12 @@ bool ParametersPlugin_WidgetParamsMgr::storeValueCustom()
   ParametersPlugin_ExpressionValidator aValidator;
   std::list<std::string> aArgs;
   std::string aAttrId = ParametersPlugin_Parameter::VARIABLE_ID();
-  std::string aErr;
+  Events_InfoMessage aErr;
   int aId = 0;
   foreach(FeaturePtr aFeature, myParametersList) {
     if (!aValidator.isValid(aFeature->attribute(aAttrId), aArgs, aErr)) {
-      QMessageBox::warning(this, tr("Warning"), aErr.c_str());
+      // TODO(spo): translate
+      QMessageBox::warning(this, tr("Warning"), aErr.messageString().c_str());
       selectItemScroll(myParameters->child(aId));
       return false;
     }
@@ -296,23 +302,23 @@ void ParametersPlugin_WidgetParamsMgr::updateParametersFeatures()
 
 void ParametersPlugin_WidgetParamsMgr::updateFeaturesPart()
 {
-  updateItem(myFeatures, featuresItems(myParametersList));
+  QList<FeaturePtr> aFeatureList;
+  updateItem(myFeatures, featuresItems(myParametersList, aFeatureList));
 }
 
 void ParametersPlugin_WidgetParamsMgr::updateParametersPart()
 {
   updateItem(myParameters, parametersItems(myParametersList));
-  bool aIsValid = checkIsValid();
+  bool aIsValid = isValid();
   enableButtons(aIsValid);
 }
 
 
 QList<QStringList> ParametersPlugin_WidgetParamsMgr::
-  featuresItems(const QList<FeaturePtr>& theFeatures) const
+  featuresItems(const QList<FeaturePtr>& theFeatures, QList<FeaturePtr>& theFeatureList) const
 {
   QList<QStringList> aItemsList;
   ResultParameterPtr aParam;
-  QList<FeaturePtr> aFeatures;
   foreach(FeaturePtr aParameter, theFeatures) {
     aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParameter->firstResult());
     const std::set<std::shared_ptr<ModelAPI_Attribute>>& aRefs = aParam->data()->refsToMe();
@@ -325,10 +331,10 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
           // Find referenced feature Recursive
           QList<FeaturePtr> aList;
           aList.append(aReferenced);
-          QList<QStringList> aItems = featuresItems(aList);
+          QList<QStringList> aItems = featuresItems(aList, theFeatureList);
           aItemsList.append(aItems);
         } else {
-          if (!aFeatures.contains(aReferenced)) {
+          if (!theFeatureList.contains(aReferenced)) {
             QStringList aValNames;
             aValNames << aReferenced->data()->name().c_str();
 
@@ -344,7 +350,7 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
               }
             }
             aItemsList.append(aValNames);
-            aFeatures.append(aReferenced);
+            theFeatureList.append(aReferenced);
           }
         }
       }
@@ -416,7 +422,7 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
           aText.replace(" ", "");
         }
         if (hasName(aText)) {
-          myMessage = tr("Name %1 already exists.").arg(aText);
+          myMessage = tr("Name '%1' already exists.").arg(aText);
           QTimer::singleShot(50, this, SLOT(sendWarning()));
           return;
         }
@@ -453,9 +459,11 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
 
-  if (aColumn == Col_Equation)
+  if (aColumn != Col_Comment)
     updateParametersPart();
   updateFeaturesPart();
+          
+  onSelectionChanged();
 }
 
 void ParametersPlugin_WidgetParamsMgr::updateItem(QTreeWidgetItem* theItem, 
@@ -588,26 +596,16 @@ void ParametersPlugin_WidgetParamsMgr::onRemove()
   QObjectPtrList anObjects;
   anObjects.append(aCurFeature);
 
-  std::set<FeaturePtr> aDirectRefFeatures, aIndirectRefFeatures;
-  ModuleBase_Tools::findReferences(anObjects, aDirectRefFeatures, aIndirectRefFeatures);
+  std::map<FeaturePtr, std::set<FeaturePtr> > aReferences;
+  std::set<FeaturePtr> aFeatures;
+  ModuleBase_Tools::convertToFeatures(anObjects, aFeatures);
+  ModelAPI_Tools::findAllReferences(aFeatures, aReferences);
 
-  bool doDeleteReferences = true;
-  if (ModuleBase_Tools::isDeleteFeatureWithReferences(anObjects, aDirectRefFeatures, 
-      aIndirectRefFeatures, this, doDeleteReferences)) {
-
-    std::set<FeaturePtr> aFeaturesToDelete;
-    if (doDeleteReferences) {
-      aFeaturesToDelete = aDirectRefFeatures;
-      aFeaturesToDelete.insert(aIndirectRefFeatures.begin(), aIndirectRefFeatures.end());
-    }
-    aDoc->removeFeature(aCurFeature);
-    std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin(),
-                                         aLast = aFeaturesToDelete.end();
-    for (; anIt != aLast; anIt++) {
-      FeaturePtr aFeature = (*anIt);
-      DocumentPtr aDoc = aFeature->document();
-      aDoc->removeFeature(aFeature);
-    }
+  std::set<FeaturePtr> aFeatureRefsToDelete;
+  if (ModuleBase_Tools::askToDelete(aFeatures, aReferences, this, aFeatureRefsToDelete)) {
+    if (!aFeatureRefsToDelete.empty())
+      aFeatures.insert(aFeatureRefsToDelete.begin(), aFeatureRefsToDelete.end());
+    ModelAPI_Tools::removeFeatures(aFeatures, false);
 
     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED));
     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
@@ -705,13 +703,16 @@ bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName) const
 void ParametersPlugin_WidgetParamsMgr::sendWarning()
 {
   QMessageBox::warning(this, tr("Warning"), myMessage);
+  QTreeWidgetItem* aItem = myTable->currentItem();
+  if (aItem)
+    myTable->editItem(aItem);
 }
 
 void ParametersPlugin_WidgetParamsMgr::onSelectionChanged()
 {
-  bool isValid = checkIsValid();
-  if (isValid) {
-    QList<QTreeWidgetItem*> aItemsList = myTable->selectedItems();
+  QList<QTreeWidgetItem*> aItemsList = myTable->selectedItems();
+  bool aIsValid = isValid();
+  if (aIsValid) {
     bool isParameter = false;
     foreach(QTreeWidgetItem* aItem, aItemsList) {
       if (aItem->parent() == myParameters) {
@@ -720,15 +721,16 @@ void ParametersPlugin_WidgetParamsMgr::onSelectionChanged()
       }
     }
     myInsertBtn->setEnabled(isParameter);
-    myRemoveBtn->setEnabled(isParameter);
+    //myRemoveBtn->setEnabled(isParameter);
     myUpBtn->setEnabled(isParameter);
     myDownBtn->setEnabled(isParameter);
   } else {
     myInsertBtn->setEnabled(false);
-    myRemoveBtn->setEnabled(false);
+    //myRemoveBtn->setEnabled(false);
     myUpBtn->setEnabled(false);
     myDownBtn->setEnabled(false);
   }
+  myRemoveBtn->setEnabled(!aItemsList.isEmpty());
 }
 
 void ParametersPlugin_WidgetParamsMgr::enableButtons(bool theEnable)
@@ -742,15 +744,18 @@ void ParametersPlugin_WidgetParamsMgr::enableButtons(bool theEnable)
     myUpBtn->setEnabled(theEnable);
     myDownBtn->setEnabled(theEnable);
   }
+  myOkCancelBtn->button(QDialogButtonBox::Ok)->setEnabled(theEnable);
 }
 
-bool ParametersPlugin_WidgetParamsMgr::checkIsValid()
+bool ParametersPlugin_WidgetParamsMgr::isValid()
 {
   QTreeWidgetItem* aItem;
   bool aIsValid = true;
   for(int i = 0; i < myParameters->childCount(); i++) {
     aItem = myParameters->child(i);
-    if ((aItem->text(Col_Name) == NoName) || (aItem->text(Col_Equation) == NoValue)) {
+    if ((aItem->text(Col_Name) == NoName) || 
+        (aItem->text(Col_Equation) == NoValue) ||
+        (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ) {
       aIsValid = false;
       break;
     }