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