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