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