]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetEditor.cpp
Salome HOME
Sources formated according to the codeing standards
[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,
35                                                  const std::string& theAttribute)
36     : ModuleBase_WidgetDoubleValue(theParent, 0, "")
37 {
38   setAttributeID(theAttribute);
39 }
40
41 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
42 {
43 }
44
45 double editedValue(double theValue, bool& isDone)
46 {
47   QDialog aDlg;
48   aDlg.setWindowFlags(Qt::FramelessWindowHint);
49   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
50   aLay->setContentsMargins(0, 0, 0, 0);
51
52   QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
53   aEditor->setValidator(new QDoubleValidator(aEditor));
54   QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
55   aLay->addWidget(aEditor);
56
57   QPoint aPoint = QCursor::pos();
58   aDlg.move(aPoint);
59
60   isDone = aDlg.exec() == QDialog::Accepted;
61   double aValue = theValue;
62   if (isDone)
63     aValue = aEditor->text().toDouble();
64   return aValue;
65 }
66
67 bool ModuleBase_WidgetEditor::focusTo()
68 {
69   double aValue = mySpinBox->value();
70   bool isDone;
71   aValue = editedValue(aValue, isDone);
72
73   if (isDone) {
74     bool isBlocked = mySpinBox->blockSignals(true);
75     mySpinBox->setValue(aValue);
76     mySpinBox->blockSignals(isBlocked);
77   }
78   emit valuesChanged();
79   emit focusOutWidget(this);
80
81   return false;
82 }
83
84 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
85                                                const std::string theAttribute)
86 {
87   DataPtr aData = theFeature->data();
88   AttributeDoublePtr aRef = aData->real(theAttribute);
89   double aValue = aRef->value();
90
91   bool isDone;
92   aValue = editedValue(aValue, isDone);
93   if (isDone)
94     aRef->setValue(aValue);
95 }