]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/ModuleBase/ModuleBase_WidgetExprEditor.cpp
Salome HOME
Issue #801: place holder for hint in input fields
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetExprEditor.cpp
index 01d7ba4d5ab8b0db8e9033d0f8da0ab896eab5da..6c23c4a9aed9653f3848565e26bb82c59e994000 100644 (file)
 #include <ModuleBase_WidgetExprEditor.h>
 #include <ModuleBase_Tools.h>
 
-#include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeDouble.h>
 
 #include <Config_WidgetAPI.h>
 
@@ -28,6 +29,8 @@
 #include <QSize>
 #include <QShortcut>
 #include <QScrollBar>
+#include <QFontMetrics>
+#include <QPainter>
 
 #include <memory>
 #include <string>
@@ -142,9 +145,51 @@ bool ExpressionEditor::handledCompletedAndSelected(QKeyEvent* theEvent)
   return true;
 }
 
-ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
-                                                     const Config_WidgetAPI* theData,
-                                                     const std::string& theParentId)
+void ExpressionEditor::setPlaceHolderText( const QString& thePlaceHolderText )
+{
+  myPlaceHolderText = thePlaceHolderText;
+}
+
+QString ExpressionEditor::placeHolderText() const
+{
+  return myPlaceHolderText;
+}
+
+void ExpressionEditor::paintEvent( QPaintEvent* theEvent )
+{
+  QPlainTextEdit::paintEvent( theEvent );
+
+  if( toPlainText().isEmpty() )
+  {
+    QPainter aPainter( viewport() );
+    QFontMetrics aFontMetrics = fontMetrics();
+
+    QPointF offset(contentOffset());
+    QRect r = rect();
+    int m = (int)document()->documentMargin();
+    QRect lineRect( r.x() + m + offset.x(), offset.y(),
+                    r.width() - 2*m, aFontMetrics.height() );
+
+    Qt::Alignment va = QStyle::visualAlignment( layoutDirection(), Qt::AlignLeft );
+    int minLB = qMax( 0, -aFontMetrics.minLeftBearing() );
+
+    QColor aColor = palette().text().color();
+    aColor.setAlpha( 128 );
+    QPen anOldpen = aPainter.pen();
+    aPainter.setPen( aColor );
+    lineRect.adjust(minLB, 0, 0, 0);
+    QString elidedText = aFontMetrics.elidedText( myPlaceHolderText, Qt::ElideRight, lineRect.width() );
+    aPainter.drawText( lineRect, va, elidedText );
+    aPainter.setPen( anOldpen );
+  }
+}
+
+
+
+ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor( QWidget* theParent,
+                                                          const Config_WidgetAPI* theData,
+                                                          const std::string& theParentId,
+                                                          const std::string& thePlaceHolder )
     : ModuleBase_ModelWidget(theParent, theData, theParentId)
 {
   QVBoxLayout* aMainLay = new QVBoxLayout(this);
@@ -152,9 +197,13 @@ ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
 
   myResultLabel = new QLabel(this);
   myResultLabel->setWordWrap(true);
+  QFontMetrics fm(myResultLabel->font());
+  myResultLabel->setMinimumHeight(fm.height() * 2); // set 2 line height as minimum
+  myResultLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
   aMainLay->addWidget(myResultLabel);
   myEditor = new ExpressionEditor(this);
   myEditor->setMinimumHeight(20);
+  myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
   aMainLay->addWidget(myEditor);
   this->setLayout(aMainLay);
 
@@ -176,17 +225,28 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const
   aStringAttr->setValue(aWidgetValue.toStdString());
   updateObject(myFeature);
 
-  if(!myFeature->firstResult().get())
-    return true;
-  
-  ResultParameterPtr aResult =
+  // Try to get the value
+  QString aStateMsg;
+  std::string anErrorMessage = myFeature->error();
+  if (anErrorMessage.empty()) {
+    ResultParameterPtr aParam =
       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(myFeature->firstResult());
-  AttributeStringPtr aErrorAttr = aResult->data()->string(ModelAPI_ResultParameter::STATE());
-  myResultLabel->setText(QString::fromStdString(aErrorAttr->value()));
+    if(aParam.get()) {
+      AttributeDoublePtr aValueAttr =
+        aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+      if (aValueAttr.get()) {
+        double aValue = aValueAttr->value();
+        aStateMsg = "Result: " + QString::number(aValue);
+      }
+    }
+  } else {
+    aStateMsg = QString::fromStdString(anErrorMessage);
+  }
+  myResultLabel->setText(aStateMsg);
   return true;
 }
 
-bool ModuleBase_WidgetExprEditor::restoreValue()
+bool ModuleBase_WidgetExprEditor::restoreValueCustom()
 {
   // A rare case when plugin was not loaded. 
   if(!myFeature)