Salome HOME
e8da69f1745cd679ff589506e1a44e0d636030d5
[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 #include <ModuleBase_IconFactory.h>
13
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Validator.h>
18
19 #include <Config_WidgetAPI.h>
20
21 #include <QFormLayout>
22 #include <QLabel>
23 #include <QLineEdit>
24 #include <QObject>
25 #include <QString>
26 #include <QPainter>
27 #include <QResizeEvent>
28
29 #include <memory>
30 #include <string>
31
32 /**
33 * Customization of Line edit control
34 */
35 class CustomLineEdit : public QLineEdit
36 {
37 public:
38   /// Constructor
39   /// \param theParent a parent widget
40   /// \param thePlaceHolder a string which is shown when text is empty
41   CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
42     : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
43   {
44   }
45
46   virtual ~CustomLineEdit()
47   {
48   }
49
50   /// Redefiniotion of virtual method
51   /// \param theEvent a paint event
52   virtual void paintEvent( QPaintEvent* theEvent )
53   {
54     QLineEdit::paintEvent( theEvent );
55     if( text().isEmpty() && !myPlaceHolder.isEmpty() )
56     {
57       QPainter aPainter( this );
58       QRect aRect = rect();
59       int aHorMargin = 5;
60       aRect.adjust( aHorMargin, 0, 0, 0 );
61
62       QColor aColor = palette().text().color();
63       aColor.setAlpha( 128 );
64       QPen anOldpen = aPainter.pen();
65       aPainter.setPen( aColor );
66       QFontMetrics aFontMetrics = fontMetrics();
67       QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
68       aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
69       aPainter.setPen( anOldpen );
70     }
71   }
72
73 private:
74   QString myPlaceHolder;
75 };
76
77 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
78                                                      const Config_WidgetAPI* theData,
79                                                      const std::string& thePlaceHolder )
80 : ModuleBase_ModelWidget(theParent, theData)
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(ModuleBase_IconFactory::loadPixmap(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()
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 }