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& thePlaceHolder )
79 : ModuleBase_ModelWidget(theParent, theData)
81 QFormLayout* aMainLay = new QFormLayout(this);
82 ModuleBase_Tools::adjustMargins(aMainLay);
83 QString aLabelText = QString::fromStdString(theData->widgetLabel());
84 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
85 QLabel* aLabel = new QLabel(aLabelText, this);
86 if (!aLabelIcon.isEmpty())
87 aLabel->setPixmap(QPixmap(aLabelIcon));
89 myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
90 // Here we do not use the Qt's standard method setPlaceHolderText() since it
91 // draws the place holder only if there is no focus on widget;
92 // we would like to see the place holder in the case of empty text
93 // even if the widget is focused.
94 // The corresponding patch appears in Qt only since version 5.x
96 myLineEdit->setMinimumHeight(20);
98 aMainLay->addRow(aLabel, myLineEdit);
99 this->setLayout(aMainLay);
101 connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
104 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
108 bool ModuleBase_WidgetLineEdit::storeValueCustom() const
110 // A rare case when plugin was not loaded.
113 DataPtr aData = myFeature->data();
114 AttributeStringPtr aStringAttr = aData->string(attributeID());
115 QString aWidgetValue = myLineEdit->text();
116 aStringAttr->setValue(aWidgetValue.toStdString());
117 updateObject(myFeature);
121 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
123 // A rare case when plugin was not loaded.
126 DataPtr aData = myFeature->data();
127 AttributeStringPtr aStringAttr = aData->string(attributeID());
129 bool isBlocked = myLineEdit->blockSignals(true);
130 myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
131 myLineEdit->blockSignals(isBlocked);
136 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
138 QList<QWidget*> result;
139 result << myLineEdit;
143 bool ModuleBase_WidgetLineEdit::processEnter()
145 bool isModified = getValueState() == ModifiedInPP;
147 emit valuesChanged();
148 myLineEdit->selectAll();