Salome HOME
Issue #720:Set cursor at the same position at editing of text in spin box
[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 aLabelText = QString::fromStdString(theData->widgetLabel());
37   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
38   QLabel* aLabel = new QLabel(aLabelText, this);
39   if (!aLabelIcon.isEmpty())
40     aLabel->setPixmap(QPixmap(aLabelIcon));
41
42   myLineEdit = new QLineEdit(this);
43   myLineEdit->setMinimumHeight(20);
44   aMainLay->addRow(aLabel, myLineEdit);
45   this->setLayout(aMainLay);
46
47   connect(myLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged()));
48 }
49
50 ModuleBase_WidgetLineEdit::~ModuleBase_WidgetLineEdit()
51 {
52 }
53
54 bool ModuleBase_WidgetLineEdit::storeValueCustom() const
55 {
56   // A rare case when plugin was not loaded. 
57   if(!myFeature)
58     return false;
59   DataPtr aData = myFeature->data();
60   AttributeStringPtr aStringAttr = aData->string(attributeID());
61   QString aWidgetValue = myLineEdit->text();
62   aStringAttr->setValue(aWidgetValue.toStdString());
63   updateObject(myFeature);
64   return true;
65 }
66
67 bool ModuleBase_WidgetLineEdit::restoreValue()
68 {
69   // A rare case when plugin was not loaded. 
70   if(!myFeature)
71     return false;
72   DataPtr aData = myFeature->data();
73   AttributeStringPtr aStringAttr = aData->string(attributeID());
74
75   bool isBlocked = myLineEdit->blockSignals(true);
76   myLineEdit->setText(QString::fromStdString(aStringAttr->value()));
77   myLineEdit->blockSignals(isBlocked);
78
79   return true;
80 }
81
82 QList<QWidget*> ModuleBase_WidgetLineEdit::getControls() const
83 {
84   QList<QWidget*> result;
85   result << myLineEdit;
86   return result;
87 }
88
89 void ModuleBase_WidgetLineEdit::onTextChanged()
90 {
91   storeValue();
92 }