Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetLineEdit.cpp
1 // Copyright (C) 2014-2022  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 <Locale_Convert.h>
25
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Object.h>
29 #include <ModelAPI_Validator.h>
30
31 #include <Config_WidgetAPI.h>
32
33 #include <QFormLayout>
34 #include <QLabel>
35 #include <QLineEdit>
36 #include <QObject>
37 #include <QString>
38 #include <QPainter>
39 #include <QResizeEvent>
40
41 #include <memory>
42 #include <string>
43
44 /**
45 * Customization of Line edit control
46 */
47 class CustomLineEdit : public QLineEdit
48 {
49 public:
50   /// Constructor
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 )
55   {
56   }
57
58   virtual ~CustomLineEdit()
59   {
60   }
61
62   /// Redefiniotion of virtual method
63   /// \param theEvent a paint event
64   virtual void paintEvent( QPaintEvent* theEvent )
65   {
66     QLineEdit::paintEvent( theEvent );
67     if( text().isEmpty() && !myPlaceHolder.isEmpty() )
68     {
69       QPainter aPainter( this );
70       QRect aRect = rect();
71       int aHorMargin = 5;
72       aRect.adjust( aHorMargin, 0, 0, 0 );
73
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 );
82     }
83   }
84
85 private:
86   QString myPlaceHolder;
87 };
88
89 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
90                                                      const Config_WidgetAPI* theData,
91                                                      const std::string& thePlaceHolder )
92 : ModuleBase_ModelWidget(theParent, theData)
93 {
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));
101
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
108
109   myLineEdit->setMinimumHeight(20);
110
111   aMainLay->addRow(aLabel, myLineEdit);
112   this->setLayout(aMainLay);
113
114   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
115 }
116
117 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
118 {
119 }
120
121 bool ModuleBase_WidgetLineEdit::storeValueCustom()
122 {
123   // A rare case when plugin was not loaded.
124   if(!myFeature)
125     return false;
126   DataPtr aData = myFeature->data();
127   AttributeStringPtr aStringAttr = aData->string(attributeID());
128   QString aWidgetValue = myLineEdit->text();
129   aStringAttr->setValue(aWidgetValue.toStdWString());
130   updateObject(myFeature);
131   return true;
132 }
133
134 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
135 {
136   // A rare case when plugin was not loaded.
137   if(!myFeature)
138     return false;
139   DataPtr aData = myFeature->data();
140   AttributeStringPtr aStringAttr = aData->string(attributeID());
141
142   bool isBlocked = myLineEdit->blockSignals(true);
143   QString aText;
144   if (aStringAttr->isUValue())
145     aText = QString::fromStdWString(Locale::Convert::toWString(aStringAttr->valueU()));
146   else
147     aText = QString::fromStdString(aStringAttr->value());
148   myLineEdit->setText(aText);
149   myLineEdit->blockSignals(isBlocked);
150
151   return true;
152 }
153
154 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
155 {
156   QList<QWidget*> result;
157   result << myLineEdit;
158   return result;
159 }
160
161 bool ModuleBase_WidgetLineEdit::processEnter()
162 {
163   bool isModified = getValueState() == ModifiedInPP;
164   if (isModified) {
165     emit valuesChanged();
166     myLineEdit->selectAll();
167   }
168   return isModified;
169 }
170
171 bool ModuleBase_WidgetLineEdit::isModified() const
172 {
173   return !myLineEdit->text().isEmpty();
174 }