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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 * ModuleBase_WidgetLineEdit.cpp
23 * Created on: Aug 28, 2014
27 #include <ModuleBase_WidgetLineEdit.h>
28 #include <ModuleBase_Tools.h>
29 #include <ModuleBase_IconFactory.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Object.h>
34 #include <ModelAPI_Validator.h>
36 #include <Config_WidgetAPI.h>
38 #include <QFormLayout>
44 #include <QResizeEvent>
50 * Customization of Line edit control
52 class CustomLineEdit : public QLineEdit
56 /// \param theParent a parent widget
57 /// \param thePlaceHolder a string which is shown when text is empty
58 CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
59 : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
63 virtual ~CustomLineEdit()
67 /// Redefiniotion of virtual method
68 /// \param theEvent a paint event
69 virtual void paintEvent( QPaintEvent* theEvent )
71 QLineEdit::paintEvent( theEvent );
72 if( text().isEmpty() && !myPlaceHolder.isEmpty() )
74 QPainter aPainter( this );
77 aRect.adjust( aHorMargin, 0, 0, 0 );
79 QColor aColor = palette().text().color();
80 aColor.setAlpha( 128 );
81 QPen anOldpen = aPainter.pen();
82 aPainter.setPen( aColor );
83 QFontMetrics aFontMetrics = fontMetrics();
84 QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
85 aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
86 aPainter.setPen( anOldpen );
91 QString myPlaceHolder;
94 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
95 const Config_WidgetAPI* theData,
96 const std::string& thePlaceHolder )
97 : ModuleBase_ModelWidget(theParent, theData)
99 QFormLayout* aMainLay = new QFormLayout(this);
100 ModuleBase_Tools::adjustMargins(aMainLay);
101 QString aLabelText = translate(theData->widgetLabel());
102 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
103 QLabel* aLabel = new QLabel(aLabelText, this);
104 if (!aLabelIcon.isEmpty())
105 aLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
107 myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
108 // Here we do not use the Qt's standard method setPlaceHolderText() since it
109 // draws the place holder only if there is no focus on widget;
110 // we would like to see the place holder in the case of empty text
111 // even if the widget is focused.
112 // The corresponding patch appears in Qt only since version 5.x
114 myLineEdit->setMinimumHeight(20);
116 aMainLay->addRow(aLabel, myLineEdit);
117 this->setLayout(aMainLay);
119 connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
122 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
126 bool ModuleBase_WidgetLineEdit::storeValueCustom()
128 // A rare case when plugin was not loaded.
131 DataPtr aData = myFeature->data();
132 AttributeStringPtr aStringAttr = aData->string(attributeID());
133 QString aWidgetValue = myLineEdit->text();
134 aStringAttr->setValue(aWidgetValue.toStdString());
135 updateObject(myFeature);
139 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
141 // A rare case when plugin was not loaded.
144 DataPtr aData = myFeature->data();
145 AttributeStringPtr aStringAttr = aData->string(attributeID());
147 bool isBlocked = myLineEdit->blockSignals(true);
148 myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
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();