Salome HOME
Accessibility of constraint operations under editing of sketch elements
[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 <Model_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 : ModuleBase_WidgetDoubleValue(theParent, theData)
30 {
31 }
32
33 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent, const std::string& theAttribute)
34 : ModuleBase_WidgetDoubleValue(theParent, 0)
35 {
36   setAttributeID(theAttribute);
37 }
38
39 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
40 {
41 }
42
43 double editedValue(double theValue, bool& isDone)
44 {
45   QDialog aDlg;
46   aDlg.setWindowFlags(Qt::FramelessWindowHint);
47   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
48   aLay->setContentsMargins(0,0,0,0);
49
50   QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
51   aEditor->setValidator(new QDoubleValidator(aEditor));
52   QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
53   aLay->addWidget(aEditor);
54
55   QPoint aPoint = QCursor::pos();
56   aDlg.move(aPoint);
57
58   isDone = aDlg.exec() == QDialog::Accepted;
59   double aValue = theValue;
60   if (isDone)
61     aValue = aEditor->text().toDouble();
62   return aValue;
63 }
64
65 bool ModuleBase_WidgetEditor::focusTo()
66 {
67   double aValue = mySpinBox->value();
68   bool isDone;
69   aValue = editedValue(aValue, isDone);
70
71   if (isDone) {
72     bool isBlocked = mySpinBox->blockSignals(true);
73     mySpinBox->setValue(aValue);
74     mySpinBox->blockSignals(isBlocked);
75   }
76   emit valuesChanged();
77   emit focusOutWidget(this);
78
79   return false;
80 }
81
82 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature, const std::string theAttribute)
83 {
84   DataPtr aData = theFeature->data();
85   AttributeDoublePtr aRef = aData->real(theAttribute);
86   double aValue = aRef->value();
87
88   bool isDone;
89   aValue = editedValue(aValue, isDone);
90   if (isDone)
91     aRef->setValue(aValue);
92 }