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>
32 * Customization of Line edit control
34 class CustomLineEdit : public QLineEdit
38 /// \param theParent a parent widget
39 /// \param thePlaceHolder a string which is shown when text is empty
40 CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
41 : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
45 virtual ~CustomLineEdit()
49 /// Redefiniotion of virtual method
50 /// \param theEvent a paint event
51 virtual void paintEvent( QPaintEvent* theEvent )
53 QLineEdit::paintEvent( theEvent );
54 if( text().isEmpty() && !myPlaceHolder.isEmpty() )
56 QPainter aPainter( this );
59 aRect.adjust( aHorMargin, 0, 0, 0 );
61 QColor aColor = palette().text().color();
62 aColor.setAlpha( 128 );
63 QPen anOldpen = aPainter.pen();
64 aPainter.setPen( aColor );
65 QFontMetrics aFontMetrics = fontMetrics();
66 QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
67 aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
68 aPainter.setPen( anOldpen );
73 QString myPlaceHolder;
76 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
77 const Config_WidgetAPI* theData,
78 const std::string& theParentId,
79 const std::string& thePlaceHolder )
80 : ModuleBase_ModelWidget(theParent, theData, theParentId)
82 QFormLayout* aMainLay = new QFormLayout(this);
83 ModuleBase_Tools::adjustMargins(aMainLay);
84 QString aLabelText = QString::fromStdString(theData->widgetLabel());
85 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
86 QLabel* aLabel = new QLabel(aLabelText, this);
87 if (!aLabelIcon.isEmpty())
88 aLabel->setPixmap(QPixmap(aLabelIcon));
90 myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
91 // Here we do not use the Qt's standard method setPlaceHolderText() since it
92 // draws the place holder only if there is no focus on widget;
93 // we would like to see the place holder in the case of empty text
94 // even if the widget is focused.
95 // The corresponding patch appears in Qt only since version 5.x
97 myLineEdit->setMinimumHeight(20);
99 aMainLay->addRow(aLabel, myLineEdit);
100 this->setLayout(aMainLay);
102 connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged()));
105 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
109 bool ModuleBase_WidgetLineEdit::storeValueCustom() const
111 // A rare case when plugin was not loaded.
114 DataPtr aData = myFeature->data();
115 AttributeStringPtr aStringAttr = aData->string(attributeID());
116 QString aWidgetValue = myLineEdit->text();
117 aStringAttr->setValue(aWidgetValue.toStdString());
118 updateObject(myFeature);
122 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
124 // A rare case when plugin was not loaded.
127 DataPtr aData = myFeature->data();
128 AttributeStringPtr aStringAttr = aData->string(attributeID());
130 bool isBlocked = myLineEdit->blockSignals(true);
131 myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
132 myLineEdit->blockSignals(isBlocked);
137 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
139 QList<QWidget*> result;
140 result << myLineEdit;
144 void ModuleBase_WidgetLineEdit::onTextChanged()