Salome HOME
Issue #1303 Re-ordering of Sketcher menus: Delete to be the last
[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& thePlaceHolder )
79 : ModuleBase_ModelWidget(theParent, theData)
80 {
81   QFormLayout* aMainLay = new QFormLayout(this);
82   ModuleBase_Tools::adjustMargins(aMainLay);
83   QString aLabelText = QString::fromStdString(theData->widgetLabel());
84   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
85   QLabel* aLabel = new QLabel(aLabelText, this);
86   if (!aLabelIcon.isEmpty())
87     aLabel->setPixmap(QPixmap(aLabelIcon));
88
89   myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
90   // Here we do not use the Qt's standard method setPlaceHolderText() since it
91   // draws the place holder only if there is no focus on widget;
92   // we would like to see the place holder in the case of empty text
93   // even if the widget is focused.
94   // The corresponding patch appears in Qt only since version 5.x
95
96   myLineEdit->setMinimumHeight(20);
97
98   aMainLay->addRow(aLabel, myLineEdit);
99   this->setLayout(aMainLay);
100
101   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
102 }
103
104 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
105 {
106 }
107
108 bool ModuleBase_WidgetLineEdit::storeValueCustom()
109 {
110   // A rare case when plugin was not loaded. 
111   if(!myFeature)
112     return false;
113   DataPtr aData = myFeature->data();
114   AttributeStringPtr aStringAttr = aData->string(attributeID());
115   QString aWidgetValue = myLineEdit->text();
116   aStringAttr->setValue(aWidgetValue.toStdString());
117   updateObject(myFeature);
118   return true;
119 }
120
121 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
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
129   bool isBlocked = myLineEdit->blockSignals(true);
130   myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
131   myLineEdit->blockSignals(isBlocked);
132
133   return true;
134 }
135
136 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
137 {
138   QList<QWidget*> result;
139   result << myLineEdit;
140   return result;
141 }
142
143 bool ModuleBase_WidgetLineEdit::processEnter()
144 {
145   bool isModified = getValueState() == ModifiedInPP;
146   if (isModified) {
147     emit valuesChanged();
148     myLineEdit->selectAll();
149   }
150   return isModified;
151 }