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>
12 #include <ModuleBase_IconFactory.h>
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Validator.h>
19 #include <Config_WidgetAPI.h>
21 #include <QFormLayout>
27 #include <QResizeEvent>
33 * Customization of Line edit control
35 class CustomLineEdit : public QLineEdit
39 /// \param theParent a parent widget
40 /// \param thePlaceHolder a string which is shown when text is empty
41 CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
42 : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
46 virtual ~CustomLineEdit()
50 /// Redefiniotion of virtual method
51 /// \param theEvent a paint event
52 virtual void paintEvent( QPaintEvent* theEvent )
54 QLineEdit::paintEvent( theEvent );
55 if( text().isEmpty() && !myPlaceHolder.isEmpty() )
57 QPainter aPainter( this );
60 aRect.adjust( aHorMargin, 0, 0, 0 );
62 QColor aColor = palette().text().color();
63 aColor.setAlpha( 128 );
64 QPen anOldpen = aPainter.pen();
65 aPainter.setPen( aColor );
66 QFontMetrics aFontMetrics = fontMetrics();
67 QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
68 aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
69 aPainter.setPen( anOldpen );
74 QString myPlaceHolder;
77 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
78 const Config_WidgetAPI* theData,
79 const std::string& thePlaceHolder )
80 : ModuleBase_ModelWidget(theParent, theData)
82 QFormLayout* aMainLay = new QFormLayout(this);
83 ModuleBase_Tools::adjustMargins(aMainLay);
84 QString aLabelText = translate(theData->widgetLabel());
85 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
86 QLabel* aLabel = new QLabel(aLabelText, this);
87 if (!aLabelIcon.isEmpty())
88 aLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(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, SIGNAL(valuesModified()));
105 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
109 bool ModuleBase_WidgetLineEdit::storeValueCustom()
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 bool ModuleBase_WidgetLineEdit::processEnter()
146 bool isModified = getValueState() == ModifiedInPP;
148 emit valuesChanged();
149 myLineEdit->selectAll();