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
26 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
27                                                  const Config_WidgetAPI* theData)
28 : ModuleBase_ModelWidget(theParent, theData), myValue(0)
29 {
30 }
31
32 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent, const std::string& theAttribute)
33 : ModuleBase_ModelWidget(theParent, 0), myValue(0)
34 {
35   this->setAttributeID(theAttribute);
36 }
37
38 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
39 {
40 }
41
42 bool ModuleBase_WidgetEditor::storeValue(FeaturePtr theFeature) const
43 {
44   DataPtr aData = theFeature->data();
45   AttributeDoublePtr aReal = aData->real(attributeID());
46   if (aReal->value() != myValue) {
47     aReal->setValue(myValue);
48     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
49   }
50   return true;
51 }
52
53 bool ModuleBase_WidgetEditor::restoreValue(FeaturePtr theFeature)
54 {
55   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
56   AttributeDoublePtr aRef = aData->real(attributeID());
57
58   myValue = aRef->value();
59   return true;
60 }
61
62 void ModuleBase_WidgetEditor::focusTo()
63 {
64   QPoint aPoint = QCursor::pos();
65
66   QDialog aDlg;
67   aDlg.setWindowFlags(Qt::FramelessWindowHint);
68   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
69   aLay->setContentsMargins(0,0,0,0);
70
71   QLineEdit* aEditor = new QLineEdit(QString::number(myValue), &aDlg);
72   connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
73   aLay->addWidget(aEditor);
74
75   aDlg.move(aPoint);
76   int aRes = aDlg.exec();
77
78   if (aRes == QDialog::Accepted)
79     myValue = aEditor->text().toDouble();
80
81   emit valuesChanged();
82   emit focusOutWidget(this);
83 }
84
85 QWidget* ModuleBase_WidgetEditor::getControl() const
86 {
87   return 0;
88 }
89
90 QList<QWidget*> ModuleBase_WidgetEditor::getControls() const
91 {
92   QList<QWidget*> aControls;
93   return aControls;
94 }
95
96 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature, const std::string theAttribute)
97 {
98   ModuleBase_WidgetEditor anEditor(0, theAttribute);
99
100   anEditor.restoreValue(theFeature);
101   anEditor.focusTo();
102   anEditor.storeValue(theFeature);
103 }