1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetLineEdit.h>
21 #include <ModuleBase_Tools.h>
22 #include <ModuleBase_IconFactory.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
29 #include <Config_WidgetAPI.h>
31 #include <QFormLayout>
37 #include <QResizeEvent>
43 * Customization of Line edit control
45 class CustomLineEdit : public QLineEdit
49 /// \param theParent a parent widget
50 /// \param thePlaceHolder a string which is shown when text is empty
51 CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
52 : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
56 virtual ~CustomLineEdit()
60 /// Redefiniotion of virtual method
61 /// \param theEvent a paint event
62 virtual void paintEvent( QPaintEvent* theEvent )
64 QLineEdit::paintEvent( theEvent );
65 if( text().isEmpty() && !myPlaceHolder.isEmpty() )
67 QPainter aPainter( this );
70 aRect.adjust( aHorMargin, 0, 0, 0 );
72 QColor aColor = palette().text().color();
73 aColor.setAlpha( 128 );
74 QPen anOldpen = aPainter.pen();
75 aPainter.setPen( aColor );
76 QFontMetrics aFontMetrics = fontMetrics();
77 QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
78 aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
79 aPainter.setPen( anOldpen );
84 QString myPlaceHolder;
87 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
88 const Config_WidgetAPI* theData,
89 const std::string& thePlaceHolder )
90 : ModuleBase_ModelWidget(theParent, theData)
92 QFormLayout* aMainLay = new QFormLayout(this);
93 ModuleBase_Tools::adjustMargins(aMainLay);
94 QString aLabelText = translate(theData->widgetLabel());
95 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
96 QLabel* aLabel = new QLabel(aLabelText, this);
97 if (!aLabelIcon.isEmpty())
98 aLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
100 myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
101 // Here we do not use the Qt's standard method setPlaceHolderText() since it
102 // draws the place holder only if there is no focus on widget;
103 // we would like to see the place holder in the case of empty text
104 // even if the widget is focused.
105 // The corresponding patch appears in Qt only since version 5.x
107 myLineEdit->setMinimumHeight(20);
109 aMainLay->addRow(aLabel, myLineEdit);
110 this->setLayout(aMainLay);
112 connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
115 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
119 bool ModuleBase_WidgetLineEdit::storeValueCustom()
121 // A rare case when plugin was not loaded.
124 DataPtr aData = myFeature->data();
125 AttributeStringPtr aStringAttr = aData->string(attributeID());
126 QString aWidgetValue = myLineEdit->text();
127 aStringAttr->setValue(aWidgetValue.toStdString());
128 updateObject(myFeature);
132 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
134 // A rare case when plugin was not loaded.
137 DataPtr aData = myFeature->data();
138 AttributeStringPtr aStringAttr = aData->string(attributeID());
140 bool isBlocked = myLineEdit->blockSignals(true);
141 myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
142 myLineEdit->blockSignals(isBlocked);
147 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
149 QList<QWidget*> result;
150 result << myLineEdit;
154 bool ModuleBase_WidgetLineEdit::processEnter()
156 bool isModified = getValueState() == ModifiedInPP;
158 emit valuesChanged();
159 myLineEdit->selectAll();