Salome HOME
updated copyright message
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_WidgetParamsMgr.cpp
index 27140f32039077dac42646f8d0c6bdb37bd65e46..9a8061a06b58e23957a11edbc58fecc488ec6131 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  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
@@ -12,9 +12,9 @@
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "ParametersPlugin_WidgetParamsMgr.h"
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_Expression.h>
 
+#include <GeomDataAPI_Point.h>
+#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 <QLayout>
 #include <QPushButton>
 #include <QEvent>
 #include <QKeyEvent>
 #include <QDialogButtonBox>
+#include <QShortcut>
+#include <QFileDialog>
+
+#ifdef WIN32
+#pragma warning(disable : 4456) // for nested foreach
+#endif
 
 enum ColumnType {
   Col_Name,
@@ -57,7 +69,6 @@ enum ColumnType {
 
 const char* NoName = "<NoName>";
 const char* NoValue = "<NoValue>";
-const char* NotValid = "<NotValid>";
 
 /*!
  * \ingroup GUI
@@ -174,15 +185,17 @@ void ParametersPlugin_TreeWidget::closeEditor(QWidget* theEditor,
 /////////////////////////////////////////////////////////////////////////////////////////////////
 
 ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theParent,
-  const Config_WidgetAPI* theData)
-  : ModuleBase_ModelDialogWidget(theParent, theData)
+  const Config_WidgetAPI* theData, ModuleBase_IWorkshop* theWorkshop)
+  : ModuleBase_ModelDialogWidget(theParent, theData),
+  myWorkshop(theWorkshop), isUpplyBlocked(false)
 {
   QVBoxLayout* aLayout = new QVBoxLayout(this);
 
   myTable = new ParametersPlugin_TreeWidget(this);
   myTable->setColumnCount(4);
   QStringList aHeaders;
-  aHeaders << tr("Name") << tr("Expression") << tr("Result") << tr("Comment");
+  aHeaders << translate("Name") << translate("Expression")
+           << translate("Result") << translate("Comment");
   myTable->setHeaderLabels(aHeaders);
   myTable->setColumnWidth(Col_Name, 200);
   myTable->setColumnWidth(Col_Equation, 100);
@@ -205,13 +218,13 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP
 
   // Define root nodes
   QStringList aNames;
-  aNames<<tr("Parameters");
+  aNames<<translate("Parameters");
   myParameters = new QTreeWidgetItem(aNames);
   myParameters->setFlags(Qt::ItemIsEnabled);
   myTable->addTopLevelItem(myParameters);
 
   aNames.clear();
-  aNames<<tr("Features");
+  aNames<<translate("Features");
   myFeatures = new QTreeWidgetItem(aNames);
   myFeatures->setFlags(Qt::ItemIsEnabled);
   myTable->addTopLevelItem(myFeatures);
@@ -230,15 +243,26 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP
 
   aBtnLayout->addStretch();
 
-  myAddBtn = new QPushButton(tr("Add"), this);
+  myAddBtn = new QPushButton(translate("Add"), this);
   connect(myAddBtn, SIGNAL(clicked(bool)), SLOT(onAdd()));
   aBtnLayout->addWidget(myAddBtn);
 
-  myInsertBtn = new QPushButton(tr("Insert"), this);
+  QString aAddStr(Config_PropManager::string("Shortcuts", "add_parameter_shortcut").c_str());
+  if (aAddStr.isEmpty())
+    aAddStr = "Ctrl+A";
+
+  QShortcut* aAddShc = new QShortcut(QKeySequence(aAddStr), myAddBtn);
+  connect(aAddShc, SIGNAL(activated()), SLOT(onAdd()));
+
+  myInsertBtn = new QPushButton(translate("Insert"), this);
   connect(myInsertBtn, SIGNAL(clicked(bool)), SLOT(onInsert()));
   aBtnLayout->addWidget(myInsertBtn);
 
-  myRemoveBtn = new QPushButton(tr("Remove"), this);
+  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);
 
@@ -247,6 +271,20 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP
   onSelectionChanged();
 }
 
+void ParametersPlugin_WidgetParamsMgr::setDialogButtons(QDialogButtonBox* theButtons)
+{
+  ModuleBase_ModelDialogWidget::setDialogButtons(theButtons);
+
+  QWidget* aBtnParentWgt = myOkCancelBtn->parentWidget();
+  QHBoxLayout* aBtnParentLayout = dynamic_cast<QHBoxLayout*>(aBtnParentWgt->layout());
+
+  QPushButton* aPreviewBtn = new QPushButton(translate("See preview"), aBtnParentWgt);
+  aBtnParentLayout->insertWidget(0, aPreviewBtn);
+  aBtnParentLayout->insertStretch(1, 1);
+  connect(aPreviewBtn, SIGNAL(clicked(bool)), SLOT(onShowPreview()));
+}
+
+
 QList<QWidget*> ParametersPlugin_WidgetParamsMgr::getControls() const
 {
   QList<QWidget*> aList;
@@ -275,13 +313,17 @@ bool ParametersPlugin_WidgetParamsMgr::storeValueCustom()
   int aId = 0;
   foreach(FeaturePtr aFeature, myParametersList) {
     if (!aValidator.isValid(aFeature->attribute(aAttrId), aArgs, aErr)) {
-      // TODO(spo): translate
-      QMessageBox::warning(this, tr("Warning"), aErr.messageString().c_str());
+      QMessageBox::warning(this, translate("Warning"), aErr.messageString().c_str());
       selectItemScroll(myParameters->child(aId));
       return false;
     }
     aId++;
   }
+  // #2813 : make the current feature the latest in the document
+  std::list<FeaturePtr> allFeatures = myFeature->document()->allFeatures();
+  if (!allFeatures.empty()) {
+    myFeature->document()->setCurrentFeature(*(allFeatures.rbegin()), true);
+  }
   return true;
 }
 
@@ -310,7 +352,7 @@ void ParametersPlugin_WidgetParamsMgr::updateParametersFeatures()
   int aNbFeatures = aDoc->numInternalFeatures();
   for (int i = 0; i < aNbFeatures; i++) {
     aParamFeature = aDoc->internalFeature(i);
-    if (aParamFeature->getKind() == ParametersPlugin_Parameter::ID()) {
+    if (aParamFeature && aParamFeature->getKind() == ParametersPlugin_Parameter::ID()) {
       myParametersList.append(aParamFeature);
     }
   }
@@ -352,20 +394,43 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
         } else {
           if (!theFeatureList.contains(aReferenced)) {
             QStringList aValNames;
-            aValNames << aReferenced->data()->name().c_str();
+            aValNames << QString::fromStdWString(aReferenced->data()->name());
 
-            AttributeDoublePtr aDouble =
-              std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aAttr);
-            if (aDouble.get()) {
-              aValNames << aDouble->text().c_str();
+            std::string aId = aAttr->attributeType();
+            if (aId == ModelAPI_AttributeDouble::typeId()) {
+              AttributeDoublePtr aDouble =
+                std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aAttr);
+              aValNames << QString::fromStdWString(aDouble->text());
               aValNames << QString::number(aDouble->value());
-            } else {
+            }
+            else if (aId == ModelAPI_AttributeInteger::typeId()) {
               AttributeIntegerPtr aInt =
                 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aAttr);
-              if (aInt.get()) {
-                aValNames << aInt->text().c_str();
-                aValNames << QString::number(aInt->value());
-              }
+              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(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());
+              aValNames << aRes;
+            }
+            else if (aId == GeomDataAPI_Point2D::typeId()) {
+              std::shared_ptr<GeomDataAPI_Point2D> aPnt =
+                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
+
+              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());
+              aValNames << aRes;
             }
             aItemsList.append(aValNames);
             theFeatureList.append(aReferenced);
@@ -390,18 +455,17 @@ QList<QStringList> ParametersPlugin_WidgetParamsMgr::
 
     std::string aName = aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->value();
     if (aName.empty()) {
-      aValues << NoName;
+      aValues << translate(NoName);
     } else
       aValues << aName.c_str();
 
     std::string aExpr = aParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID())->value();
     if (aName.empty()) {
-      aValues << NoValue;
+      aValues << translate(NoValue);
     } 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());
@@ -429,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;
 
@@ -440,8 +505,8 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
         while (aText.indexOf(" ") != -1) {
           aText.replace(" ", "");
         }
-        if (hasName(aText)) {
-          myMessage = tr("Name '%1' already exists.").arg(aText);
+        if (hasName(aText, aRow)) {
+          myMessage = translate("Name '%1' already exists.").arg(aText);
           QTimer::singleShot(50, this, SLOT(sendWarning()));
           return;
         }
@@ -466,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;
@@ -529,8 +594,8 @@ FeaturePtr ParametersPlugin_WidgetParamsMgr::createParameter() const
 QTreeWidgetItem* ParametersPlugin_WidgetParamsMgr::createNewItem(QTreeWidgetItem* theParent) const
 {
   QStringList aValues;
-  aValues << NoName;
-  aValues << NoValue;
+  aValues << translate(NoName);
+  aValues << translate(NoValue);
 
   QTreeWidgetItem* aItem = new QTreeWidgetItem(aValues);
   if (theParent == myParameters) {
@@ -636,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();
@@ -709,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++;
   }
@@ -723,7 +808,7 @@ bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName) const
 
 void ParametersPlugin_WidgetParamsMgr::sendWarning()
 {
-  QMessageBox::warning(this, tr("Warning"), myMessage);
+  QMessageBox::warning(this, translate("Warning"), myMessage);
   QTreeWidgetItem* aItem = myTable->currentItem();
   if (aItem)
     myTable->editItem(aItem);
@@ -745,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());
 }
@@ -764,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);
 }
@@ -774,11 +862,36 @@ bool ParametersPlugin_WidgetParamsMgr::isValid()
   for(int i = 0; i < myParameters->childCount(); i++) {
     aItem = myParameters->child(i);
     if ((aItem->text(Col_Name) == NoName) ||
-        (aItem->text(Col_Equation) == NoValue) ||
-        (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ) {
+        (aItem->text(Col_Equation) == translate(NoValue)) ||
+        (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ||
+        (hasName(aItem->text(Col_Name), i)) ) {
       return false;
     }
   }
   return true;
 }
 
+void ParametersPlugin_WidgetParamsMgr::showEvent(QShowEvent* theEvent)
+{
+  ModuleBase_ModelDialogWidget::showEvent(theEvent);
+  SessionPtr aMgr = ModelAPI_Session::get();
+  isUpplyBlocked = aMgr->isAutoUpdateBlocked();
+  aMgr->blockAutoUpdate(true);
+  Events_Loop* aLoop = Events_Loop::loop();
+  aLoop->flush(aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE));
+}
+
+void ParametersPlugin_WidgetParamsMgr::hideEvent(QHideEvent* theEvent)
+{
+  ModuleBase_ModelDialogWidget::hideEvent(theEvent);
+  SessionPtr aMgr = ModelAPI_Session::get();
+  aMgr->blockAutoUpdate(isUpplyBlocked);
+}
+
+void ParametersPlugin_WidgetParamsMgr::onShowPreview()
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  aMgr->blockAutoUpdate(false);
+  aMgr->blockAutoUpdate(true);
+  myWorkshop->viewer()->update();
+}