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";
#include <QShortcut>
#include <QScrollBar>
#include <QFontMetrics>
+#include <QPainter>
#include <memory>
#include <string>
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);
aMainLay->addWidget(myResultLabel);
myEditor = new ExpressionEditor(this);
myEditor->setMinimumHeight(20);
+ myEditor->setPlaceHolderText( QString::fromStdString( thePlaceHolder ) );
aMainLay->addWidget(myEditor);
this->setLayout(aMainLay);
void setCompletionList(QStringList&);
+ void setPlaceHolderText( const QString& );
+ QString placeHolderText() const;
+
public slots:
void insertCompletion(const QString&, bool isSingleWord = false);
void performCompletion();
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;
};
/**
/// \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;
} 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) {
#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);
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);
/// \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;
/// Saves the internal parameters to the given feature
/// \return True in success
virtual bool storeValueCustom() const;
-
virtual bool restoreValueCustom();
private:
<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>