]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetEditor.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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   QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
52   aLay->addWidget(aEditor);
53
54   QPoint aPoint = QCursor::pos();
55   aDlg.move(aPoint);
56
57   isDone = aDlg.exec() == QDialog::Accepted;
58   double aValue = theValue;
59   if (isDone)
60     aValue = aEditor->text().toDouble();
61   return aValue;
62 }
63
64 void ModuleBase_WidgetEditor::focusTo()
65 {
66   double aValue = mySpinBox->value();
67   bool isDone;
68   aValue = editedValue(aValue, isDone);
69
70   if (isDone) {
71     bool isBlocked = mySpinBox->blockSignals(true);
72     mySpinBox->setValue(aValue);
73     mySpinBox->blockSignals(isBlocked);
74   }
75   emit valuesChanged();
76   emit focusOutWidget(this);
77 }
78
79 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature, 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 }