1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetLineEdit.cpp
6 * Created on: Aug 28, 2014
10 #include <ModuleBase_WidgetLineEdit.h>
11 #include <ModuleBase_Tools.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Validator.h>
18 #include <Config_WidgetAPI.h>
20 #include <QFormLayout>
26 #include <QResizeEvent>
31 class CustomLineEdit : public QLineEdit
34 CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
35 : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
39 virtual ~CustomLineEdit()
43 virtual void paintEvent( QPaintEvent* theEvent )
45 QLineEdit::paintEvent( theEvent );
46 if( text().isEmpty() && !myPlaceHolder.isEmpty() )
48 QPainter aPainter( this );
51 aRect.adjust( aHorMargin, 0, 0, 0 );
53 QColor aColor = palette().text().color();
54 aColor.setAlpha( 128 );
55 QPen anOldpen = aPainter.pen();
56 aPainter.setPen( aColor );
57 QFontMetrics aFontMetrics = fontMetrics();
58 QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
59 aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
60 aPainter.setPen( anOldpen );
65 QString myPlaceHolder;
68 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
69 const Config_WidgetAPI* theData,
70 const std::string& theParentId,
71 const std::string& thePlaceHolder )
72 : ModuleBase_ModelWidget(theParent, theData, theParentId)
74 QFormLayout* aMainLay = new QFormLayout(this);
75 ModuleBase_Tools::adjustMargins(aMainLay);
76 QString aLabelText = QString::fromStdString(theData->widgetLabel());
77 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
78 QLabel* aLabel = new QLabel(aLabelText, this);
79 if (!aLabelIcon.isEmpty())
80 aLabel->setPixmap(QPixmap(aLabelIcon));
82 myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
83 // Here we do not use the Qt's standard method setPlaceHolderText() since it
84 // draws the place holder only if there is no focus on widget;
85 // we would like to see the place holder in the case of empty text
86 // even if the widget is focused.
87 // The corresponding patch appears in Qt only since version 5.x
89 myLineEdit->setMinimumHeight(20);
91 aMainLay->addRow(aLabel, myLineEdit);
92 this->setLayout(aMainLay);
94 connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged()));
97 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
101 bool ModuleBase_WidgetLineEdit::storeValueCustom() const
103 // A rare case when plugin was not loaded.
106 DataPtr aData = myFeature->data();
107 AttributeStringPtr aStringAttr = aData->string(attributeID());
108 QString aWidgetValue = myLineEdit->text();
109 aStringAttr->setValue(aWidgetValue.toStdString());
110 updateObject(myFeature);
114 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
116 // A rare case when plugin was not loaded.
119 DataPtr aData = myFeature->data();
120 AttributeStringPtr aStringAttr = aData->string(attributeID());
122 bool isBlocked = myLineEdit->blockSignals(true);
123 myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
124 myLineEdit->blockSignals(isBlocked);
129 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
131 QList<QWidget*> result;
132 result << myLineEdit;
136 void ModuleBase_WidgetLineEdit::onTextChanged()