Salome HOME
Copyright update 2021
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_WidgetParamsMgr.cpp
index d03062163d77941b0130eac26b983f5a6b7e2953..62a432a8b95cd06c72969d2843b5290e2d00c267 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2021  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #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>
@@ -50,6 +53,7 @@
 #include <QEvent>
 #include <QKeyEvent>
 #include <QDialogButtonBox>
+#include <QShortcut>
 
 enum ColumnType {
   Col_Name,
@@ -60,7 +64,6 @@ enum ColumnType {
 
 const char* NoName = "<NoName>";
 const char* NoValue = "<NoValue>";
-const char* NotValid = "<NotValid>";
 
 /*!
  * \ingroup GUI
@@ -177,16 +180,17 @@ 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);
 
   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);
@@ -209,13 +213,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);
@@ -234,15 +238,22 @@ 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);
+  myRemoveBtn = new QPushButton(translate("Remove"), this);
   connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemove()));
   aBtnLayout->addWidget(myRemoveBtn);
 
@@ -258,7 +269,7 @@ void ParametersPlugin_WidgetParamsMgr::setDialogButtons(QDialogButtonBox* theBut
   QWidget* aBtnParentWgt = myOkCancelBtn->parentWidget();
   QHBoxLayout* aBtnParentLayout = dynamic_cast<QHBoxLayout*>(aBtnParentWgt->layout());
 
-  QPushButton* aPreviewBtn = new QPushButton(tr("See preview"), aBtnParentWgt);
+  QPushButton* aPreviewBtn = new QPushButton(translate("See preview"), aBtnParentWgt);
   aBtnParentLayout->insertWidget(0, aPreviewBtn);
   aBtnParentLayout->insertStretch(1, 1);
   connect(aPreviewBtn, SIGNAL(clicked(bool)), SLOT(onShowPreview()));
@@ -293,8 +304,7 @@ 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;
     }
@@ -375,27 +385,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());
@@ -405,8 +416,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());
@@ -435,18 +446,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());
@@ -486,7 +496,7 @@ void ParametersPlugin_WidgetParamsMgr::onCloseEditor(QWidget* theEditor,
           aText.replace(" ", "");
         }
         if (hasName(aText)) {
-          myMessage = tr("Name '%1' already exists.").arg(aText);
+          myMessage = translate("Name '%1' already exists.").arg(aText);
           QTimer::singleShot(50, this, SLOT(sendWarning()));
           return;
         }
@@ -574,8 +584,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) {
@@ -759,7 +769,7 @@ bool ParametersPlugin_WidgetParamsMgr::hasName(const QString& theName) const
   int aCurrent = myDelegate->editIndex().row();
   int i = 0;
   foreach(FeaturePtr aFeature, myParametersList) {
-    if ((i != aCurrent) && (aFeature->data()->name() == theName.toStdString()))
+    if ((i != aCurrent) && (aFeature->data()->name() == theName.toStdWString()))
       return true;
     i++;
   }
@@ -768,7 +778,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);
@@ -819,7 +829,7 @@ 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) ||
+        (aItem->text(Col_Equation) == translate(NoValue)) ||
         (!ModelAPI_Expression::isVariable(aItem->text(Col_Name).toStdString())) ) {
       return false;
     }
@@ -849,4 +859,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();
+}