]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetEditor.cpp
Salome HOME
Result attributes validators created
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
1 // File:        ModuleBase_WidgetEditor.cpp
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <ModuleBase_WidgetEditor.h>
6
7 #include <Config_Keywords.h>
8 #include <Config_WidgetAPI.h>
9
10 #include <Events_Loop.h>
11 #include <ModelAPI_Events.h>
12
13 #include <ModelAPI_Feature.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_AttributeDouble.h>
17
18 #include <GeomDataAPI_Point2D.h>
19
20 #include <QWidget>
21 #include <QLineEdit>
22 #include <QTimer>
23 #include <QDialog>
24 #include <QLayout>
25 #include <QDoubleSpinBox>
26
27 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
28                                                  const Config_WidgetAPI* theData, 
29                                                  const std::string& theParentId)
30 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
31 {
32 }
33
34 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent, const std::string& theAttribute)
35 : ModuleBase_WidgetDoubleValue(theParent, 0, "")
36 {
37   setAttributeID(theAttribute);
38 }
39
40 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
41 {
42 }
43
44 double editedValue(double theValue, bool& isDone)
45 {
46   QDialog aDlg;
47   aDlg.setWindowFlags(Qt::FramelessWindowHint);
48   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
49   aLay->setContentsMargins(0,0,0,0);
50
51   QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
52   aEditor->setValidator(new QDoubleValidator(aEditor));
53   QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
54   aLay->addWidget(aEditor);
55
56   QPoint aPoint = QCursor::pos();
57   aDlg.move(aPoint);
58
59   isDone = aDlg.exec() == QDialog::Accepted;
60   double aValue = theValue;
61   if (isDone)
62     aValue = aEditor->text().toDouble();
63   return aValue;
64 }
65
66 bool ModuleBase_WidgetEditor::focusTo()
67 {
68   double aValue = mySpinBox->value();
69   bool isDone;
70   aValue = editedValue(aValue, isDone);
71
72   if (isDone) {
73     bool isBlocked = mySpinBox->blockSignals(true);
74     mySpinBox->setValue(aValue);
75     mySpinBox->blockSignals(isBlocked);
76   }
77   emit valuesChanged();
78   emit focusOutWidget(this);
79
80   return false;
81 }
82
83 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature, const std::string theAttribute)
84 {
85   DataPtr aData = theFeature->data();
86   AttributeDoublePtr aRef = aData->real(theAttribute);
87   double aValue = aRef->value();
88
89   bool isDone;
90   aValue = editedValue(aValue, isDone);
91   if (isDone)
92     aRef->setValue(aValue);
93 }