Salome HOME
Merge remote-tracking branch 'origin/Dev_0.6'
[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 #include <ModuleBase_DoubleSpinBox.h>
7 #include <ModuleBase_Tools.h>
8
9 #include <Config_Keywords.h>
10 #include <Config_WidgetAPI.h>
11
12 #include <Events_Loop.h>
13 #include <ModelAPI_Events.h>
14
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_AttributeDouble.h>
19
20 #include <GeomDataAPI_Point2D.h>
21
22 #include <QWidget>
23 #include <QLineEdit>
24 #include <QTimer>
25 #include <QDialog>
26 #include <QLayout>
27 #include <QApplication>
28
29 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
30                                                  const Config_WidgetAPI* theData,
31                                                  const std::string& theParentId)
32     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
33 {
34 }
35
36 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
37 {
38 }
39
40 double editedValue(double theValue, bool& isDone)
41 {
42   QDialog aDlg;
43   aDlg.setWindowFlags(Qt::FramelessWindowHint);
44   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
45   ModuleBase_Tools::zeroMargins(aLay);
46
47   QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
48   aEditor->setValidator(new QDoubleValidator(aEditor));
49   QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
50   aLay->addWidget(aEditor);
51
52   QPoint aPoint = QCursor::pos();
53   aDlg.move(aPoint);
54
55   isDone = aDlg.exec() == QDialog::Accepted;
56   double aValue = theValue;
57   if (isDone)
58     aValue = aEditor->text().toDouble();
59   return aValue;
60 }
61
62 bool ModuleBase_WidgetEditor::focusTo()
63 {
64   // We can not launch here modal process for value editing because 
65   // it can be called on other focusOutWidget event and will block it
66   QTimer::singleShot(1, this, SLOT(showPopupEditor()));
67   return true;
68 }
69
70 void ModuleBase_WidgetEditor::showPopupEditor()
71 {
72   // White while all events will be processed
73   QApplication::processEvents();
74   double aValue = mySpinBox->value();
75   bool isDone;
76   aValue = editedValue(aValue, isDone);
77
78   if (isDone) {
79     bool isBlocked = mySpinBox->blockSignals(true);
80     mySpinBox->setValue(aValue);
81     mySpinBox->blockSignals(isBlocked);
82   }
83   emit valuesChanged();
84   emit focusOutWidget(this);
85 }
86
87 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
88                                                const std::string theAttribute)
89 {
90   DataPtr aData = theFeature->data();
91   AttributeDoublePtr aRef = aData->real(theAttribute);
92   double aValue = aRef->value();
93
94   bool isDone;
95   aValue = editedValue(aValue, isDone);
96   if (isDone)
97     aRef->setValue(aValue);
98 }