]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Issue #801: place holder for hint in input fields
authorasl <asl@opencascade.com>
Thu, 13 Aug 2015 04:46:26 +0000 (07:46 +0300)
committerasl <asl@opencascade.com>
Thu, 13 Aug 2015 05:18:43 +0000 (08:18 +0300)
src/Config/Config_Keywords.h
src/ModuleBase/ModuleBase_WidgetExprEditor.cpp
src/ModuleBase/ModuleBase_WidgetExprEditor.h
src/ModuleBase/ModuleBase_WidgetFactory.cpp
src/ModuleBase/ModuleBase_WidgetLineEdit.cpp
src/ModuleBase/ModuleBase_WidgetLineEdit.h
src/ParametersPlugin/plugin-Parameters.xml

index 4a39432139760c4e7fca8f5b8411cb537822669a..b9ee0b4a0045dcde4010581a905dc89a67f45d97 100644 (file)
@@ -33,6 +33,8 @@ const static char* WDG_CHOICE = "choice";
 const static char* WDG_DOUBLEVALUE_EDITOR = "doublevalue_editor";
 const static char* WDG_FILE_SELECTOR= "file_selector";
 const static char* WDG_EXPR_EDITOR = "expr_editor";
+const static char* WDG_PLACE_HOLDER = "placeholder";
+
 // Containers
 const static char* WDG_GROUP = "groupbox";
 const static char* WDG_CHECK_GROUP = "check_groupbox";
index 20d8c8298ac095dbc657b11a141f3184d4a16923..6c23c4a9aed9653f3848565e26bb82c59e994000 100644 (file)
@@ -30,6 +30,7 @@
 #include <QShortcut>
 #include <QScrollBar>
 #include <QFontMetrics>
+#include <QPainter>
 
 #include <memory>
 #include <string>
@@ -144,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);
@@ -160,6 +203,7 @@ ModuleBase_WidgetExprEditor::ModuleBase_WidgetExprEditor(QWidget* theParent,
   aMainLay->addWidget(myResultLabel);
   myEditor = new ExpressionEditor(this);
   myEditor->setMinimumHeight(20);
+  myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
   aMainLay->addWidget(myEditor);
   this->setLayout(aMainLay);
 
index 54262288ec20017fcfa6e4d48619e17b37bc780f..c5ad7b26495390bc3a1a1cc445e77a3150faa924 100644 (file)
@@ -33,6 +33,9 @@ class ExpressionEditor: public QPlainTextEdit
 
   void setCompletionList(QStringList&);
 
+  void setPlaceHolderText( const QString& );
+  QString placeHolderText() const;
+
  public slots:
   void insertCompletion(const QString&, bool isSingleWord = false);
   void performCompletion();
@@ -41,11 +44,13 @@ class ExpressionEditor: public QPlainTextEdit
   void performCompletion(const QString& theCompletionPrefix);
   virtual void keyPressEvent(QKeyEvent* theEvent);
   bool handledCompletedAndSelected(QKeyEvent* theEvent);
+  virtual void paintEvent( QPaintEvent* );
 
  private:
   QStringListModel* myCompleterModel;
   QCompleter* myCompleter;
   bool myCompletedAndSelected;
+  QString myPlaceHolderText;
 };
 
 /**
@@ -60,9 +65,10 @@ class MODULEBASE_EXPORT ModuleBase_WidgetExprEditor : public ModuleBase_ModelWid
   /// \param theParent the parent object
   /// \param theData the widget configuration.
   /// \param theParentId is Id of a parent of the current attribute
-  ModuleBase_WidgetExprEditor(QWidget* theParent,
-                                const Config_WidgetAPI* theData,
-                                const std::string& theParentId);
+  ModuleBase_WidgetExprEditor( QWidget* theParent,
+                               const Config_WidgetAPI* theData,
+                               const std::string& theParentId,
+                               const std::string& thePlaceHolder );
   virtual ~ModuleBase_WidgetExprEditor();
 
   virtual QList<QWidget*> getControls() const;
index 85fb353a7398f29d64cfb36e40957c5859e2904a..b5d65a2cd05145ba9206506813fc602002f2ef39 100644 (file)
@@ -139,9 +139,11 @@ ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::
   } else if (theType == WDG_CHOICE) {
     result = new ModuleBase_WidgetChoice(theParent, myWidgetApi, myParentId);
   } else if (theType == WDG_STRINGVALUE) {
-    result = new ModuleBase_WidgetLineEdit(theParent, myWidgetApi, myParentId);
+    std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
+    result = new ModuleBase_WidgetLineEdit( theParent, myWidgetApi, myParentId, aPlaceHolder );
   } else if (theType == WDG_EXPR_EDITOR) {
-    result = new ModuleBase_WidgetExprEditor(theParent, myWidgetApi, myParentId);
+    std::string aPlaceHolder = myWidgetApi->getProperty( WDG_PLACE_HOLDER );
+    result = new ModuleBase_WidgetExprEditor( theParent, myWidgetApi, myParentId, aPlaceHolder );
   } else if (theType == WDG_MULTISELECTOR) {
     result = new ModuleBase_WidgetMultiSelector(theParent, myWorkshop, myWidgetApi, myParentId);
   } else if (theType == WDG_TOOLBOX) {
index f2f5034b8c380051a6055b77eddcb92b51353656..273cc902016246860653d4168e7fa0b56e0fc197 100644 (file)
 #include <QLineEdit>
 #include <QObject>
 #include <QString>
+#include <QPainter>
+#include <QResizeEvent>
 
 #include <memory>
 #include <string>
 
+class CustomLineEdit : public QLineEdit
+{
+public:
+  CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
+    : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
+  {
+  }
+
+  virtual ~CustomLineEdit()
+  {
+  }
+
+  virtual void paintEvent( QPaintEvent* theEvent )
+  {
+    QLineEdit::paintEvent( theEvent );
+    if( text().isEmpty() && !myPlaceHolder.isEmpty() )
+    {
+      QPainter aPainter( this );
+      QRect aRect = rect();
+      int aHorMargin = 5;
+      aRect.adjust( aHorMargin, 0, 0, 0 );
+
+      QColor aColor = palette().text().color();
+      aColor.setAlpha( 128 );
+      QPen anOldpen = aPainter.pen();
+      aPainter.setPen( aColor );
+      QFontMetrics aFontMetrics = fontMetrics();
+      QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
+      aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
+      aPainter.setPen( anOldpen );
+    }
+  }
+
+private:
+  QString myPlaceHolder;
+};
+
 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
                                                      const Config_WidgetAPI* theData,
-                                                     const std::string& theParentId)
+                                                     const std::string& theParentId,
+                                                     const std::string& thePlaceHolder )
     : ModuleBase_ModelWidget(theParent, theData, theParentId)
 {
   QFormLayout* aMainLay = new QFormLayout(this);
@@ -39,8 +79,15 @@ ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
   if (!aLabelIcon.isEmpty())
     aLabel->setPixmap(QPixmap(aLabelIcon));
 
-  myLineEdit = new QLineEdit(this);
+  myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
+  // Here we do not use the Qt's standard method setPlaceHolderText() since it
+  // draws the place holder only if there is no focus on widget;
+  // we would like to see the place holder in the case of empty text
+  // even if the widget is focused.
+  // The corresponding patch appears in Qt only since version 5.x
+
   myLineEdit->setMinimumHeight(20);
+
   aMainLay->addRow(aLabel, myLineEdit);
   this->setLayout(aMainLay);
 
index 624bdf9175f54c10b5af6feb2fc7b13005f979c6..d812bc158fe9bd893b16b54caf3aed8eebc72c86 100644 (file)
@@ -33,9 +33,10 @@ class MODULEBASE_EXPORT ModuleBase_WidgetLineEdit : public ModuleBase_ModelWidge
   /// \param theParent the parent object
   /// \param theData the widget configuration.
   /// \param theParentId is Id of a parent of the current attribute
-  ModuleBase_WidgetLineEdit(QWidget* theParent,
-                                const Config_WidgetAPI* theData,
-                                const std::string& theParentId);
+  ModuleBase_WidgetLineEdit( QWidget* theParent,
+                             const Config_WidgetAPI* theData,
+                             const std::string& theParentId,
+                             const std::string& thePlaceHolder );
   virtual ~ModuleBase_WidgetLineEdit();
 
   virtual QList<QWidget*> getControls() const;
@@ -48,7 +49,6 @@ protected:
   /// Saves the internal parameters to the given feature
   /// \return True in success
   virtual bool storeValueCustom() const;
-
   virtual bool restoreValueCustom();
 
 private:
index f0dbaf3d1a5d3c5cd7d5ab9f5143d167b679f0a0..3bab3831fd5f96cd253abc23cfb4aab19e84301f 100644 (file)
@@ -4,10 +4,10 @@
   <workbench id="Part">
     <group id="Parameters">
       <feature id="Parameter" title="Parameter" tooltip="Create a parameter" icon=":pictures/expression.png">
-        <stringvalue id="variable" label="Name" icon=":pictures/expression.png">
+        <stringvalue id="variable" label="Name" icon=":pictures/expression.png" placeholder="Please input the parameter name">
           <validator id="Parameters_VariableValidator"/>
         </stringvalue>
-        <expr_editor id="expression">
+        <expr_editor id="expression" placeholder="Please input the expression">
           <validator id="Parameters_ExpressionValidator"/>
         </expr_editor>
       </feature>