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