1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetLineEdit.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_IconFactory.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Validator.h>
30 #include <Config_WidgetAPI.h>
32 #include <QFormLayout>
38 #include <QResizeEvent>
44 * Customization of Line edit control
46 class CustomLineEdit : public QLineEdit
50 /// \param theParent a parent widget
51 /// \param thePlaceHolder a string which is shown when text is empty
52 CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
53 : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
57 virtual ~CustomLineEdit()
61 /// Redefiniotion of virtual method
62 /// \param theEvent a paint event
63 virtual void paintEvent( QPaintEvent* theEvent )
65 QLineEdit::paintEvent( theEvent );
66 if( text().isEmpty() && !myPlaceHolder.isEmpty() )
68 QPainter aPainter( this );
71 aRect.adjust( aHorMargin, 0, 0, 0 );
73 QColor aColor = palette().text().color();
74 aColor.setAlpha( 128 );
75 QPen anOldpen = aPainter.pen();
76 aPainter.setPen( aColor );
77 QFontMetrics aFontMetrics = fontMetrics();
78 QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
79 aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
80 aPainter.setPen( anOldpen );
85 QString myPlaceHolder;
88 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
89 const Config_WidgetAPI* theData,
90 const std::string& thePlaceHolder )
91 : ModuleBase_ModelWidget(theParent, theData)
93 QFormLayout* aMainLay = new QFormLayout(this);
94 ModuleBase_Tools::adjustMargins(aMainLay);
95 QString aLabelText = translate(theData->widgetLabel());
96 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
97 QLabel* aLabel = new QLabel(aLabelText, this);
98 if (!aLabelIcon.isEmpty())
99 aLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
101 myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
102 // Here we do not use the Qt's standard method setPlaceHolderText() since it
103 // draws the place holder only if there is no focus on widget;
104 // we would like to see the place holder in the case of empty text
105 // even if the widget is focused.
106 // The corresponding patch appears in Qt only since version 5.x
108 myLineEdit->setMinimumHeight(20);
110 aMainLay->addRow(aLabel, myLineEdit);
111 this->setLayout(aMainLay);
113 connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
116 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
120 bool ModuleBase_WidgetLineEdit::storeValueCustom()
122 // A rare case when plugin was not loaded.
125 DataPtr aData = myFeature->data();
126 AttributeStringPtr aStringAttr = aData->string(attributeID());
127 QString aWidgetValue = myLineEdit->text();
128 aStringAttr->setValue(aWidgetValue.toStdString());
129 updateObject(myFeature);
133 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
135 // A rare case when plugin was not loaded.
138 DataPtr aData = myFeature->data();
139 AttributeStringPtr aStringAttr = aData->string(attributeID());
141 bool isBlocked = myLineEdit->blockSignals(true);
142 myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
143 myLineEdit->blockSignals(isBlocked);
148 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
150 QList<QWidget*> result;
151 result << myLineEdit;
155 bool ModuleBase_WidgetLineEdit::processEnter()
157 bool isModified = getValueState() == ModifiedInPP;
159 emit valuesChanged();
160 myLineEdit->selectAll();