Salome HOME
Issue #390 Selection restore problems during edit operation
[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 <QHBoxLayout>
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   myMainWidget = new QWidget(theParent);
35   QHBoxLayout* aMainLay = new QHBoxLayout(myMainWidget);
36   ModuleBase_Tools::adjustMargins(aMainLay);
37   QString aTitle = QString::fromStdString(theData->widgetLabel());
38   QLabel* aTitleLabel = new QLabel(aTitle, myMainWidget);
39   aMainLay->addWidget(aTitleLabel);
40   myLineEdit = new QLineEdit(myMainWidget);
41   aMainLay->addWidget(myLineEdit);
42   myLineEdit->setMinimumHeight(20);
43   myMainWidget->setLayout(aMainLay);
44
45   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged()));
46 }
47
48 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
49 {
50 }
51
52 bool ModuleBase_WidgetLineEdit::storeValueCustom() const
53 {
54   // A rare case when plugin was not loaded. 
55   if(!myFeature)
56     return false;
57   DataPtr aData = myFeature->data();
58   AttributeStringPtr aStringAttr = aData->string(attributeID());
59   QString aWidgetValue = myLineEdit->text();
60   aStringAttr->setValue(aWidgetValue.toStdString());
61   updateObject(myFeature);
62   return true;
63 }
64
65 bool ModuleBase_WidgetLineEdit::restoreValue()
66 {
67   // A rare case when plugin was not loaded. 
68   if(!myFeature)
69     return false;
70   DataPtr aData = myFeature->data();
71   AttributeStringPtr aStringAttr = aData->string(attributeID());
72
73   bool isBlocked = myLineEdit->blockSignals(true);
74   myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
75   myLineEdit->blockSignals(isBlocked);
76
77   return true;
78 }
79
80 QWidget* ModuleBase_WidgetLineEdit::getControl() const
81 {
82   return myMainWidget;
83 }
84
85 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
86 {
87   QList<QWidget*> result;
88   result << myLineEdit;
89   return result;
90 }
91
92 void ModuleBase_WidgetLineEdit::onTextChanged()
93 {
94   storeValue();
95 }