Salome HOME
A regression correction for the following case:
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetLineEdit.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetLineEdit.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
10 #include <ModuleBase_WidgetLineEdit.h>
11 #include <ModuleBase_Tools.h>
12
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Validator.h>
17
18 #include <Config_WidgetAPI.h>
19
20 #include <QFormLayout>
21 #include <QLabel>
22 #include <QLineEdit>
23 #include <QObject>
24 #include <QString>
25 #include <QPainter>
26 #include <QResizeEvent>
27
28 #include <memory>
29 #include <string>
30
31 /**
32 * Customization of Line edit control
33 */
34 class CustomLineEdit : public QLineEdit
35 {
36 public:
37   /// Constructor
38   /// \param theParent a parent widget
39   /// \param thePlaceHolder a string which is shown when text is empty
40   CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
41     : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
42   {
43   }
44
45   virtual ~CustomLineEdit()
46   {
47   }
48
49   /// Redefiniotion of virtual method
50   /// \param theEvent a paint event
51   virtual void paintEvent( QPaintEvent* theEvent )
52   {
53     QLineEdit::paintEvent( theEvent );
54     if( text().isEmpty() && !myPlaceHolder.isEmpty() )
55     {
56       QPainter aPainter( this );
57       QRect aRect = rect();
58       int aHorMargin = 5;
59       aRect.adjust( aHorMargin, 0, 0, 0 );
60
61       QColor aColor = palette().text().color();
62       aColor.setAlpha( 128 );
63       QPen anOldpen = aPainter.pen();
64       aPainter.setPen( aColor );
65       QFontMetrics aFontMetrics = fontMetrics();
66       QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
67       aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
68       aPainter.setPen( anOldpen );
69     }
70   }
71
72 private:
73   QString myPlaceHolder;
74 };
75
76 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
77                                                      const Config_WidgetAPI* theData,
78                                                      const std::string& theParentId,
79                                                      const std::string& thePlaceHolder )
80     : ModuleBase_ModelWidget(theParent, theData, theParentId)
81 {
82   QFormLayout* aMainLay = new QFormLayout(this);
83   ModuleBase_Tools::adjustMargins(aMainLay);
84   QString aLabelText = QString::fromStdString(theData->widgetLabel());
85   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
86   QLabel* aLabel = new QLabel(aLabelText, this);
87   if (!aLabelIcon.isEmpty())
88     aLabel->setPixmap(QPixmap(aLabelIcon));
89
90   myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
91   // Here we do not use the Qt's standard method setPlaceHolderText() since it
92   // draws the place holder only if there is no focus on widget;
93   // we would like to see the place holder in the case of empty text
94   // even if the widget is focused.
95   // The corresponding patch appears in Qt only since version 5.x
96
97   myLineEdit->setMinimumHeight(20);
98
99   aMainLay->addRow(aLabel, myLineEdit);
100   this->setLayout(aMainLay);
101
102   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
103 }
104
105 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
106 {
107 }
108
109 bool ModuleBase_WidgetLineEdit::storeValueCustom() const
110 {
111   // A rare case when plugin was not loaded. 
112   if(!myFeature)
113     return false;
114   DataPtr aData = myFeature->data();
115   AttributeStringPtr aStringAttr = aData->string(attributeID());
116   QString aWidgetValue = myLineEdit->text();
117   aStringAttr->setValue(aWidgetValue.toStdString());
118   updateObject(myFeature);
119   return true;
120 }
121
122 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
123 {
124   // A rare case when plugin was not loaded. 
125   if(!myFeature)
126     return false;
127   DataPtr aData = myFeature->data();
128   AttributeStringPtr aStringAttr = aData->string(attributeID());
129
130   bool isBlocked = myLineEdit->blockSignals(true);
131   myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
132   myLineEdit->blockSignals(isBlocked);
133
134   return true;
135 }
136
137 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
138 {
139   QList<QWidget*> result;
140   result << myLineEdit;
141   return result;
142 }
143
144 bool ModuleBase_WidgetLineEdit::processEnter()
145 {
146   bool isModified = getValueState() == ModifiedInPP;
147   if (isModified) {
148     emit valuesChanged();
149     myLineEdit->selectAll();
150   }
151   return isModified;
152 }