Salome HOME
Issue #1854 Recover feature control update
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetExprEditor.cpp
index b8e8ca60e4872d29582e0b222d4bb4d61bbb5c51..1fb6bf1aa8e735af11c357b3f5df4ff3abe6bac1 100644 (file)
@@ -52,6 +52,10 @@ ExpressionEditor::ExpressionEditor(QWidget* theParent)
           this,        SLOT(insertCompletion(const QString&)));
   (void) new QShortcut(QKeySequence(tr("Ctrl+Space", "Complete")),
                        this, SLOT(performCompletion()));
+
+  connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
+
+  setTabChangesFocus(true);
 }
 
 ExpressionEditor::~ExpressionEditor()
@@ -117,16 +121,28 @@ void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
     switch (theEvent->key()) {
       case Qt::Key_Up:
       case Qt::Key_Down:
+      case Qt::Key_Escape:
       case Qt::Key_Enter:
       case Qt::Key_Return:
-      case Qt::Key_Escape:
         theEvent->ignore();
-        return;
+      return;
       default:
         myCompleter->popup()->hide();
         break;
     }
   }
+  else {
+    switch (theEvent->key()) {
+      case Qt::Key_Enter:
+      case Qt::Key_Return:
+        emit keyReleased(this, theEvent);
+        // do not react to the Enter key, the property panel processes it
+        return;
+      break;
+      default:
+        break;
+    }
+  }
   QPlainTextEdit::keyPressEvent(theEvent);
 }
 
@@ -178,19 +194,23 @@ void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
     QPen anOldpen = aPainter.pen();
     aPainter.setPen( aColor );
     lineRect.adjust(minLB, 0, 0, 0);
-    QString elidedText = aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
+    QString elidedText =
+      aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
     aPainter.drawText( lineRect, va, elidedText );
     aPainter.setPen( anOldpen );
   }
 }
 
+void ExpressionEditor::onTextChanged()
+{
+  emit valueModified();
+}
 
 
 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
                                                           const Config_WidgetAPI* theData,
-                                                          const std::string& theParentId,
                                                           const std::string& thePlaceHolder )
-    : ModuleBase_ModelWidget(theParent, theData, theParentId)
+: ModuleBase_ModelWidget(theParent, theData)
 {
   QVBoxLayout* aMainLay = new QVBoxLayout(this);
   ModuleBase_Tools::adjustMargins(aMainLay);
@@ -207,27 +227,43 @@ ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
   aMainLay->addWidget(myEditor);
   this->setLayout(aMainLay);
 
-  connect(myEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
+  connect(myEditor, SIGNAL(valueModified()), this, SIGNAL(valuesModified()));
+  connect(myEditor, SIGNAL(keyReleased(QObject*, QKeyEvent*)),
+          this, SIGNAL(keyReleased(QObject*, QKeyEvent*)));
 }
 
 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
 {
 }
 
-bool ModuleBase_WidgetExprEditor::storeValueCustom() const
+void ModuleBase_WidgetExprEditor::activateCustom()
 {
-  // A rare case when plugin was not loaded. 
+  ModuleBase_ModelWidget::activateCustom();
+
+  QStringList aParameters;
+  ModuleBase_Tools::getParameters(aParameters);
+  myEditor->setCompletionList(aParameters);
+}
+
+void ModuleBase_WidgetExprEditor::initializeValueByActivate()
+{
+}
+
+bool ModuleBase_WidgetExprEditor::storeValueCustom()
+{
+  // A rare case when plugin was not loaded.
   if(!myFeature)
     return false;
   DataPtr aData = myFeature->data();
   AttributeStringPtr aStringAttr = aData->string(attributeID());
+
   QString aWidgetValue = myEditor->toPlainText();
   aStringAttr->setValue(aWidgetValue.toStdString());
   updateObject(myFeature);
 
   // Try to get the value
   QString aStateMsg;
-  std::string anErrorMessage = myFeature->error();
+  std::string anErrorMessage = myFeature->string("ExpressionError")->value();
   if (anErrorMessage.empty()) {
     ResultParameterPtr aParam =
       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
@@ -240,7 +276,7 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
       }
     }
   } else {
-    aStateMsg = QString::fromStdString(anErrorMessage);
+    aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
   }
   myResultLabel->setText(aStateMsg);
   return true;
@@ -248,7 +284,7 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
 
 bool ModuleBase_WidgetExprEditor::restoreValueCustom()
 {
-  // A rare case when plugin was not loaded. 
+  // A rare case when plugin was not loaded.
   if(!myFeature)
     return false;
   DataPtr aData = myFeature->data();
@@ -273,7 +309,17 @@ QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
   return result;
 }
 
+bool ModuleBase_WidgetExprEditor::processEnter()
+{
+  bool isModified = getValueState() == ModifiedInPP;
+  if (isModified) {
+    emit valuesChanged();
+    myEditor->selectAll();
+  }
+  return isModified;
+}
+
 void ModuleBase_WidgetExprEditor::onTextChanged()
 {
-  storeValue();
+  emit valuesChanged();
 }