Salome HOME
Property panel widgets redesign
[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
26 #include <memory>
27 #include <string>
28
29 ModuleBase_WidgetLineEdit::ModuleBase_WidgetLineEdit(QWidget* theParent,
30                                                      const Config_WidgetAPI* theData,
31                                                      const std::string& theParentId)
32     : ModuleBase_ModelWidget(theParent, theData, theParentId)
33 {
34   QFormLayout* aMainLay = new QFormLayout(this);
35   ModuleBase_Tools::adjustMargins(aMainLay);
36   QString aTitle = QString::fromStdString(theData->widgetLabel());
37   myLineEdit = new QLineEdit(this);
38   myLineEdit->setMinimumHeight(20);
39   aMainLay->addRow(aTitle, myLineEdit);
40   this->setLayout(aMainLay);
41
42   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged()));
43 }
44
45 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
46 {
47 }
48
49 bool ModuleBase_WidgetLineEdit::storeValue() const
50 {
51   // A rare case when plugin was not loaded. 
52   if(!myFeature)
53     return false;
54   DataPtr aData = myFeature->data();
55   AttributeStringPtr aStringAttr = aData->string(attributeID());
56   QString aWidgetValue = myLineEdit->text();
57   aStringAttr->setValue(aWidgetValue.toStdString());
58   updateObject(myFeature);
59   return true;
60 }
61
62 bool ModuleBase_WidgetLineEdit::restoreValue()
63 {
64   // A rare case when plugin was not loaded. 
65   if(!myFeature)
66     return false;
67   DataPtr aData = myFeature->data();
68   AttributeStringPtr aStringAttr = aData->string(attributeID());
69
70   bool isBlocked = myLineEdit->blockSignals(true);
71   myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
72   myLineEdit->blockSignals(isBlocked);
73
74   return true;
75 }
76
77 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
78 {
79   QList<QWidget*> result;
80   result << myLineEdit;
81   return result;
82 }
83
84 void ModuleBase_WidgetLineEdit::onTextChanged()
85 {
86   storeValue();
87 }