Salome HOME
Simplification of the code
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetExprEditor.cpp
index 6c23c4a9aed9653f3848565e26bb82c59e994000..3e040c4d39e6a7a32c9df3ab58c697c141cb3300 100644 (file)
@@ -36,7 +36,7 @@
 #include <string>
 
 ExpressionEditor::ExpressionEditor(QWidget* theParent)
-: QPlainTextEdit(theParent)
+: QPlainTextEdit(theParent), myCompletedAndSelected(false), myIsModified(false)
 {
   myCompleter = new QCompleter(this);
   myCompleter->setWidget(this);
@@ -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()
@@ -110,6 +114,8 @@ void ExpressionEditor::performCompletion(const QString& theCompletionPrefix)
 
 void ExpressionEditor::keyPressEvent(QKeyEvent* theEvent)
 {
+  bool anIsModified = myIsModified;
+
   if (myCompletedAndSelected && handledCompletedAndSelected(theEvent))
     return;
   myCompletedAndSelected = false;
@@ -117,16 +123,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(theEvent);
+        // do not react to the Enter key, the property panel processes it
+        return;
+      break;
+      default:
+        break;
+    }
+  }
   QPlainTextEdit::keyPressEvent(theEvent);
 }
 
@@ -155,6 +173,16 @@ QString ExpressionEditor::placeHolderText() const
   return myPlaceHolderText;
 }
 
+bool ExpressionEditor::isModified() const
+{
+  return myIsModified;
+}
+
+void ExpressionEditor::clearModified()
+{
+  myIsModified = false;
+}
+
 void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
 {
   QPlainTextEdit::paintEvent( theEvent );
@@ -184,6 +212,18 @@ void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
   }
 }
 
+bool ExpressionEditor::focusNextPrevChild(bool theIsNext)
+{
+  emit valueStored();
+  emit focusNextPrev();
+  return QPlainTextEdit::focusNextPrevChild(theIsNext);
+}
+
+void ExpressionEditor::onTextChanged()
+{
+  myIsModified = true;
+  emit valueModified();
+}
 
 
 ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
@@ -207,13 +247,23 @@ 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(valueStored()), this, SLOT(onTextChanged()));
+  connect(myEditor, SIGNAL(focusNextPrev()), this, SIGNAL(focusNextPrev()));
+
+  connect(myEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SIGNAL(keyReleased(QKeyEvent*)));
+    /// The signal about key release on the control, that corresponds to the attribute
+  /// \param theEvent key release event
 }
 
 ModuleBase_WidgetExprEditor::~ModuleBase_WidgetExprEditor()
 {
 }
 
+void ModuleBase_WidgetExprEditor::initializeValueByActivate()
+{
+}
+
 bool ModuleBase_WidgetExprEditor::storeValueCustom() const
 {
   // A rare case when plugin was not loaded. 
@@ -221,13 +271,14 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
     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 +291,7 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
       }
     }
   } else {
-    aStateMsg = QString::fromStdString(anErrorMessage);
+    aStateMsg = "Error: " + QString::fromStdString(anErrorMessage);
   }
   myResultLabel->setText(aStateMsg);
   return true;
@@ -273,7 +324,18 @@ QList<QWidget*> ModuleBase_WidgetExprEditor::getControls() const
   return result;
 }
 
+bool ModuleBase_WidgetExprEditor::processEnter()
+{
+  bool isModified = myEditor->isModified();
+  if (isModified) {
+    emit valuesChanged();
+    myEditor->clearModified();
+    myEditor->selectAll();
+  }
+  return isModified;
+}
+
 void ModuleBase_WidgetExprEditor::onTextChanged()
 {
-  storeValue();
+  emit valuesChanged();
 }