1 // Copyright (C) 2014-2021 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 <Locale_Convert.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Object.h>
29 #include <ModelAPI_Validator.h>
31 #include <Config_WidgetAPI.h>
33 #include <QFormLayout>
39 #include <QResizeEvent>
45 * Customization of Line edit control
47 class CustomLineEdit : public QLineEdit
51 /// \param theParent a parent widget
52 /// \param thePlaceHolder a string which is shown when text is empty
53 CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
54 : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
58 virtual ~CustomLineEdit()
62 /// Redefiniotion of virtual method
63 /// \param theEvent a paint event
64 virtual void paintEvent( QPaintEvent* theEvent )
66 QLineEdit::paintEvent( theEvent );
67 if( text().isEmpty() && !myPlaceHolder.isEmpty() )
69 QPainter aPainter( this );
72 aRect.adjust( aHorMargin, 0, 0, 0 );
74 QColor aColor = palette().text().color();
75 aColor.setAlpha( 128 );
76 QPen anOldpen = aPainter.pen();
77 aPainter.setPen( aColor );
78 QFontMetrics aFontMetrics = fontMetrics();
79 QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
80 aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
81 aPainter.setPen( anOldpen );
86 QString myPlaceHolder;
89 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
90 const Config_WidgetAPI* theData,
91 const std::string& thePlaceHolder )
92 : ModuleBase_ModelWidget(theParent, theData)
94 QFormLayout* aMainLay = new QFormLayout(this);
95 ModuleBase_Tools::adjustMargins(aMainLay);
96 QString aLabelText = translate(theData->widgetLabel());
97 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
98 QLabel* aLabel = new QLabel(aLabelText, this);
99 if (!aLabelIcon.isEmpty())
100 aLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
102 myLineEdit = new CustomLineEdit( this, translate( thePlaceHolder ) );
103 // Here we do not use the Qt's standard method setPlaceHolderText() since it
104 // draws the place holder only if there is no focus on widget;
105 // we would like to see the place holder in the case of empty text
106 // even if the widget is focused.
107 // The corresponding patch appears in Qt only since version 5.x
109 myLineEdit->setMinimumHeight(20);
111 aMainLay->addRow(aLabel, myLineEdit);
112 this->setLayout(aMainLay);
114 connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
117 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
121 bool ModuleBase_WidgetLineEdit::storeValueCustom()
123 // A rare case when plugin was not loaded.
126 DataPtr aData = myFeature->data();
127 AttributeStringPtr aStringAttr = aData->string(attributeID());
128 QString aWidgetValue = myLineEdit->text();
129 aStringAttr->setValue(aWidgetValue.toStdWString());
130 updateObject(myFeature);
134 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
136 // A rare case when plugin was not loaded.
139 DataPtr aData = myFeature->data();
140 AttributeStringPtr aStringAttr = aData->string(attributeID());
142 bool isBlocked = myLineEdit->blockSignals(true);
144 if (aStringAttr->isUValue())
145 aText = QString::fromStdWString(Locale::Convert::toWString(aStringAttr->valueU()));
147 aText = QString::fromStdString(aStringAttr->value());
148 myLineEdit->setText(aText);
149 myLineEdit->blockSignals(isBlocked);
154 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
156 QList<QWidget*> result;
157 result << myLineEdit;
161 bool ModuleBase_WidgetLineEdit::processEnter()
163 bool isModified = getValueState() == ModifiedInPP;
165 emit valuesChanged();
166 myLineEdit->selectAll();
171 bool ModuleBase_WidgetLineEdit::isModified() const
173 return !myLineEdit->text().isEmpty();