Salome HOME
updated copyright message
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_WidgetParamsMgr.cpp
index 9938df1e66b911170eae3108086274f1a72ea186..9a8061a06b58e23957a11edbc58fecc488ec6131 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -37,6 +37,8 @@
 #include <GeomDataAPI_Point2D.h>
 
 #include <ModuleBase_Tools.h>
+#include <ModuleBase_IWorkshop.h>
+#include <ModuleBase_IViewer.h>
 
 #include <Events_Loop.h>
 #include <Config_PropManager.h>
 #include <QKeyEvent>
 #include <QDialogButtonBox>
 #include <QShortcut>
+#include <QFileDialog>
+
+#ifdef WIN32
+#pragma warning(disable : 4456) // for nested foreach
+#endif
 
 enum ColumnType {
   Col_Name,
@@ -178,9 +185,9 @@ void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor,
 /////////////////////////////////////////////////////////////////////////////////////////////////
 
 ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent,
-  const Config_WidgetAPI* theData)
+  const Config_WidgetAPI* theData, ModuleBase_IWorkshop* theWorkshop)
   : ModuleBase_ModelDialogWidget(theParent, theData),
-  isUpplyBlocked(false)
+  myWorkshop(theWorkshop), isUpplyBlocked(false)
 {
   QVBoxLayout* aLayout = new QVBoxLayout(this);
 
@@ -251,6 +258,10 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP
   connect(myInsertBtn, SIGNAL(clicked(bool)), SLOT(onInsert()));
   aBtnLayout->addWidget(myInsertBtn);
 
+  myImportBtn = new QPushButton(translate("Import file"), this);
+  connect(myImportBtn, SIGNAL(clicked(bool)), SLOT(onImport()));
+  aBtnLayout->addWidget(myImportBtn);
+
   myRemoveBtn = new QPushButton(translate("Remove"), this);
   connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemove()));
   aBtnLayout->addWidget(myRemoveBtn);
@@ -383,27 +394,28 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
         } else {
           if (!theFeatureList.contains(aReferenced)) {
             QStringList aValNames;
-            aValNames << aReferenced->data()->name().c_str();
+            aValNames << QString::fromStdWString(aReferenced->data()->name());
 
             std::string aId = aAttr->attributeType();
             if (aId == ModelAPI_AttributeDouble::typeId()) {
               AttributeDoublePtr aDouble =
                 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aAttr);
-              aValNames << aDouble->text().c_str();
+              aValNames << QString::fromStdWString(aDouble->text());
               aValNames << QString::number(aDouble->value());
             }
             else if (aId == ModelAPI_AttributeInteger::typeId()) {
               AttributeIntegerPtr aInt =
                 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aAttr);
-              aValNames << aInt->text().c_str();
+              aValNames << QString::fromStdWString(aInt->text());
               aValNames << QString::number(aInt->value());
             }
             else if (aId == GeomDataAPI_Point::typeId()) {
               std::shared_ptr<GeomDataAPI_Point> aPnt =
                 std::dynamic_pointer_cast<GeomDataAPI_Point>(aAttr);
 
-              QString aExpr = QString("%1,%2,%3").arg(aPnt->textX().c_str()).
-                arg(aPnt->textY().c_str()).arg(aPnt->textZ().c_str());
+              QString aExpr = QString("%1,%2,%3").arg(QString::fromStdWString(aPnt->textX())).
+                                                  arg(QString::fromStdWString(aPnt->textY())).
+                                                  arg(QString::fromStdWString(aPnt->textZ()));
               aValNames << aExpr;
 
               QString aRes = QString("%1,%2,%3").arg(aPnt->x()).arg(aPnt->y()).arg(aPnt->z());
@@ -413,8 +425,8 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
               std::shared_ptr<GeomDataAPI_Point2D> aPnt =
                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
 
-              QString aExpr = QString("%1,%2").arg(aPnt->textX().c_str()).
-                arg(aPnt->textY().c_str());
+              QString aExpr = QString("%1,%2").arg(QString::fromStdWString(aPnt->textX())).
+                                               arg(QString::fromStdWString(aPnt->textY()));
               aValNames << aExpr;
 
               QString aRes = QString("%1,%2").arg(aPnt->x()).arg(aPnt->y());
@@ -453,8 +465,7 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
     } else
       aValues << aExpr.c_str();
 
-    std::string aErr =
-      aParameter->data()->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
+    aErr = aParameter->data()->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
     if (aErr.empty()) {
       AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
       aValues << QString::number(aValueAttribute->value());
@@ -482,6 +493,7 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
   FeaturePtr aFeature = myParametersList.at(myDelegate->editIndex().row());
   QTreeWidgetItem* aItem = myParameters->child(myDelegate->editIndex().row());
   int aColumn = myDelegate->editIndex().column();
+  int aRow = myDelegate->editIndex().row();
   QString aText = aItem->text(aColumn);
   bool isModified = false;
 
@@ -493,7 +505,7 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
         while (aText.indexOf(" ") != -1) {
           aText.replace(" ", "");
         }
-        if (hasName(aText)) {
+        if (hasName(aText, aRow)) {
           myMessage = translate("Name '%1' already exists.").arg(aText);
           QTimer::singleShot(50, this, SLOT(sendWarning()));
           return;
@@ -519,7 +531,7 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
   case Col_Comment:
     {
       AttributeStringPtr aStringAttr = aFeature->string(ParametersPlugin_Parameter::COMMENT_ID());
-      aStringAttr->setValue(aText.toStdString());
+      aStringAttr->setValue(aText.toStdWString());
       isModified = true;
     }
     break;
@@ -689,6 +701,27 @@ void ParametersPlugin_WidgetParamsMgr::onRemove()
   }
 }
 
+void ParametersPlugin_WidgetParamsMgr::onImport()
+{
+  std::string aWinText("Select txt file");
+  std::string aFileType("Text files (*.txt);;All files (*.*)");
+  QString aQPath = QFileDialog::getOpenFileName(nullptr,
+                                                aWinText.c_str(), "",
+                                                aFileType.c_str());
+  if (aQPath.size() == 0)
+    return;
+
+  std::string aPath(aQPath.toStdString());
+  std::shared_ptr<ModelAPI_ImportParametersMessage> aMessage =
+    std::shared_ptr<ModelAPI_ImportParametersMessage>(
+      new ModelAPI_ImportParametersMessage(ModelAPI_ImportParametersMessage::eventId()));
+  aMessage->setFilename(aPath);
+  Events_Loop::loop()->send(aMessage);
+
+  updateParametersFeatures();
+  updateParametersPart();
+}
+
 void ParametersPlugin_WidgetParamsMgr::onUp()
 {
   QTreeWidgetItem* aCurrentItem = selectedItem();
@@ -762,12 +795,11 @@ void ParametersPlugin_WidgetParamsMgr::onDown()
 }
 
 
-bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName) const
+bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName, int theIndex) const
 {
-  int aCurrent = myDelegate->editIndex().row();
   int i = 0;
   foreach(FeaturePtr aFeature, myParametersList) {
-    if ((i != aCurrent) && (aFeature->data()->name() == theName.toStdString()))
+    if ((i != theIndex) && (aFeature->data()->name() == theName.toStdWString()))
       return true;
     i++;
   }
@@ -798,11 +830,13 @@ void ParametersPlugin_WidgetParamsMgr::onSelectionChanged()
     //myRemoveBtn->setEnabled(isParameter);
     myUpBtn->setEnabled(isParameter);
     myDownBtn->setEnabled(isParameter);
+    myImportBtn->setEnabled(true);
   } else {
     myInsertBtn->setEnabled(false);
     //myRemoveBtn->setEnabled(false);
     myUpBtn->setEnabled(false);
     myDownBtn->setEnabled(false);
+    myImportBtn->setEnabled(false);
   }
   myRemoveBtn->setEnabled(!aItemsList.isEmpty());
 }
@@ -817,6 +851,7 @@ void ParametersPlugin_WidgetParamsMgr::enableButtons(bool theEnable)
     //myRemoveBtn->setEnabled(theEnable);
     myUpBtn->setEnabled(theEnable);
     myDownBtn->setEnabled(theEnable);
+    myImportBtn->setEnabled(theEnable);
   }
   myOkCancelBtn->button(QDialogButtonBox::Ok)->setEnabled(theEnable);
 }
@@ -828,7 +863,8 @@ bool ParametersPlugin_WidgetParamsMgr::isValid()
     aItem = myParameters->child(i);
     if ((aItem->text(Col_Name) == NoName) ||
         (aItem->text(Col_Equation) == translate(NoValue)) ||
-        (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ) {
+        (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ||
+        (hasName(aItem->text(Col_Name), i)) ) {
       return false;
     }
   }
@@ -857,4 +893,5 @@ void ParametersPlugin_WidgetParamsMgr::onShowPreview()
   SessionPtr aMgr = ModelAPI_Session::get();
   aMgr->blockAutoUpdate(false);
   aMgr->blockAutoUpdate(true);
-}
\ No newline at end of file
+  myWorkshop->viewer()->update();
+}