Salome HOME
8003a76f542cefc90bd670b3b78dbdb31fe96dde
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetLineEdit.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModuleBase_WidgetLineEdit.h>
21 #include <ModuleBase_Tools.h>
22 #include <ModuleBase_IconFactory.h>
23
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28
29 #include <Config_WidgetAPI.h>
30
31 #include <QFormLayout>
32 #include <QLabel>
33 #include <QLineEdit>
34 #include <QObject>
35 #include <QString>
36 #include <QPainter>
37 #include <QResizeEvent>
38
39 #include <memory>
40 #include <string>
41
42 /**
43 * Customization of Line edit control
44 */
45 class CustomLineEdit : public QLineEdit
46 {
47 public:
48   /// Constructor
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 )
53   {
54   }
55
56   virtual ~CustomLineEdit()
57   {
58   }
59
60   /// Redefiniotion of virtual method
61   /// \param theEvent a paint event
62   virtual void paintEvent( QPaintEvent* theEvent )
63   {
64     QLineEdit::paintEvent( theEvent );
65     if( text().isEmpty() && !myPlaceHolder.isEmpty() )
66     {
67       QPainter aPainter( this );
68       QRect aRect = rect();
69       int aHorMargin = 5;
70       aRect.adjust( aHorMargin, 0, 0, 0 );
71
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 );
80     }
81   }
82
83 private:
84   QString myPlaceHolder;
85 };
86
87 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
88                                                      const Config_WidgetAPI* theData,
89                                                      const std::string& thePlaceHolder )
90 : ModuleBase_ModelWidget(theParent, theData)
91 {
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));
99
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
106
107   myLineEdit->setMinimumHeight(20);
108
109   aMainLay->addRow(aLabel, myLineEdit);
110   this->setLayout(aMainLay);
111
112   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
113 }
114
115 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
116 {
117 }
118
119 bool ModuleBase_WidgetLineEdit::storeValueCustom()
120 {
121   // A rare case when plugin was not loaded.
122   if(!myFeature)
123     return false;
124   DataPtr aData = myFeature->data();
125   AttributeStringPtr aStringAttr = aData->string(attributeID());
126   QString aWidgetValue = myLineEdit->text();
127   aStringAttr->setValue(aWidgetValue.toStdString());
128   updateObject(myFeature);
129   return true;
130 }
131
132 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
133 {
134   // A rare case when plugin was not loaded.
135   if(!myFeature)
136     return false;
137   DataPtr aData = myFeature->data();
138   AttributeStringPtr aStringAttr = aData->string(attributeID());
139
140   bool isBlocked = myLineEdit->blockSignals(true);
141   myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
142   myLineEdit->blockSignals(isBlocked);
143
144   return true;
145 }
146
147 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
148 {
149   QList<QWidget*> result;
150   result << myLineEdit;
151   return result;
152 }
153
154 bool ModuleBase_WidgetLineEdit::processEnter()
155 {
156   bool isModified = getValueState() == ModifiedInPP;
157   if (isModified) {
158     emit valuesChanged();
159     myLineEdit->selectAll();
160   }
161   return isModified;
162 }