Salome HOME
Fix for #767: correct the internal history structure for nested features
[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 class CustomLineEdit : public QLineEdit
32 {
33 public:
34   CustomLineEdit( QWidget* theParent, const QString& thePlaceHolder )
35     : QLineEdit( theParent ), myPlaceHolder( thePlaceHolder )
36   {
37   }
38
39   virtual ~CustomLineEdit()
40   {
41   }
42
43   virtual void paintEvent( QPaintEvent* theEvent )
44   {
45     QLineEdit::paintEvent( theEvent );
46     if( text().isEmpty() && !myPlaceHolder.isEmpty() )
47     {
48       QPainter aPainter( this );
49       QRect aRect = rect();
50       int aHorMargin = 5;
51       aRect.adjust( aHorMargin, 0, 0, 0 );
52
53       QColor aColor = palette().text().color();
54       aColor.setAlpha( 128 );
55       QPen anOldpen = aPainter.pen();
56       aPainter.setPen( aColor );
57       QFontMetrics aFontMetrics = fontMetrics();
58       QString elidedText = aFontMetrics.elidedText( myPlaceHolder, Qt::ElideRight, aRect.width() );
59       aPainter.drawText( aRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText );
60       aPainter.setPen( anOldpen );
61     }
62   }
63
64 private:
65   QString myPlaceHolder;
66 };
67
68 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
69                                                      const Config_WidgetAPI* theData,
70                                                      const std::string& theParentId,
71                                                      const std::string& thePlaceHolder )
72     : ModuleBase_ModelWidget(theParent, theData, theParentId)
73 {
74   QFormLayout* aMainLay = new QFormLayout(this);
75   ModuleBase_Tools::adjustMargins(aMainLay);
76   QString aLabelText = QString::fromStdString(theData->widgetLabel());
77   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
78   QLabel* aLabel = new QLabel(aLabelText, this);
79   if (!aLabelIcon.isEmpty())
80     aLabel->setPixmap(QPixmap(aLabelIcon));
81
82   myLineEdit = new CustomLineEdit( this, QString::fromStdString( thePlaceHolder ) );
83   // Here we do not use the Qt's standard method setPlaceHolderText() since it
84   // draws the place holder only if there is no focus on widget;
85   // we would like to see the place holder in the case of empty text
86   // even if the widget is focused.
87   // The corresponding patch appears in Qt only since version 5.x
88
89   myLineEdit->setMinimumHeight(20);
90
91   aMainLay->addRow(aLabel, myLineEdit);
92   this->setLayout(aMainLay);
93
94   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged()));
95 }
96
97 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
98 {
99 }
100
101 bool ModuleBase_WidgetLineEdit::storeValueCustom() const
102 {
103   // A rare case when plugin was not loaded. 
104   if(!myFeature)
105     return false;
106   DataPtr aData = myFeature->data();
107   AttributeStringPtr aStringAttr = aData->string(attributeID());
108   QString aWidgetValue = myLineEdit->text();
109   aStringAttr->setValue(aWidgetValue.toStdString());
110   updateObject(myFeature);
111   return true;
112 }
113
114 bool ModuleBase_WidgetLineEdit::restoreValueCustom()
115 {
116   // A rare case when plugin was not loaded. 
117   if(!myFeature)
118     return false;
119   DataPtr aData = myFeature->data();
120   AttributeStringPtr aStringAttr = aData->string(attributeID());
121
122   bool isBlocked = myLineEdit->blockSignals(true);
123   myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
124   myLineEdit->blockSignals(isBlocked);
125
126   return true;
127 }
128
129 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
130 {
131   QList<QWidget*> result;
132   result << myLineEdit;
133   return result;
134 }
135
136 void ModuleBase_WidgetLineEdit::onTextChanged()
137 {
138   storeValue();
139 }