Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include <ModuleBase_DoubleSpinBox.h>
7 #include <ModuleBase_Tools.h>
8
9 #include <Config_Keywords.h>
10 #include <Config_WidgetAPI.h>
11
12 #include <Events_Loop.h>
13 #include <ModelAPI_Events.h>
14
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_AttributeDouble.h>
19
20 #include <GeomDataAPI_Point2D.h>
21
22 #include <QWidget>
23 #include <QLineEdit>
24 #include <QTimer>
25 #include <QDialog>
26 #include <QLayout>
27
28 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
29                                                  const Config_WidgetAPI* theData,
30                                                  const std::string& theParentId)
31     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
32 {
33 }
34
35 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
36 {
37 }
38
39 double editedValue(double theValue, bool& isDone)
40 {
41   QDialog aDlg;
42   aDlg.setWindowFlags(Qt::FramelessWindowHint);
43   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
44   ModuleBase_Tools::zeroMargins(aLay);
45
46   QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
47   aEditor->setValidator(new QDoubleValidator(aEditor));
48   QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
49   aLay->addWidget(aEditor);
50
51   QPoint aPoint = QCursor::pos();
52   aDlg.move(aPoint);
53
54   isDone = aDlg.exec() == QDialog::Accepted;
55   double aValue = theValue;
56   if (isDone)
57     aValue = aEditor->text().toDouble();
58   return aValue;
59 }
60
61 bool ModuleBase_WidgetEditor::focusTo()
62 {
63   double aValue = mySpinBox->value();
64   bool isDone;
65   aValue = editedValue(aValue, isDone);
66
67   if (isDone) {
68     bool isBlocked = mySpinBox->blockSignals(true);
69     mySpinBox->setValue(aValue);
70     mySpinBox->blockSignals(isBlocked);
71   }
72   emit valuesChanged();
73   emit focusOutWidget(this);
74
75   return false;
76 }
77
78 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
79                                                const std::string theAttribute)
80 {
81   DataPtr aData = theFeature->data();
82   AttributeDoublePtr aRef = aData->real(theAttribute);
83   double aValue = aRef->value();
84
85   bool isDone;
86   aValue = editedValue(aValue, isDone);
87   if (isDone)
88     aRef->setValue(aValue);
89 }