Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetEditor.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include <ModuleBase_WidgetEditor.h>
8 #include <ModuleBase_DoubleSpinBox.h>
9 #include <ModuleBase_Tools.h>
10
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <Events_Loop.h>
15 #include <ModelAPI_Events.h>
16
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_AttributeDouble.h>
21
22 #include <GeomDataAPI_Point2D.h>
23
24 #include <QWidget>
25 #include <QLineEdit>
26 #include <QTimer>
27 #include <QDialog>
28 #include <QLayout>
29 #include <QApplication>
30
31 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
32                                                  const Config_WidgetAPI* theData,
33                                                  const std::string& theParentId)
34     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
35 {
36 }
37
38 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
39 {
40 }
41
42 double editedValue(double theValue, bool& isDone)
43 {
44   QDialog aDlg;
45   aDlg.setWindowFlags(Qt::FramelessWindowHint);
46   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
47   ModuleBase_Tools::zeroMargins(aLay);
48
49   QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
50   aEditor->setValidator(new QDoubleValidator(aEditor));
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 bool ModuleBase_WidgetEditor::focusTo()
65 {
66   // We can not launch here modal process for value editing because 
67   // it can be called on other focusOutWidget event and will block it
68   QTimer::singleShot(1, this, SLOT(showPopupEditor()));
69   return true;
70 }
71
72 void ModuleBase_WidgetEditor::showPopupEditor()
73 {
74   // White while all events will be processed
75   QApplication::processEvents();
76   double aValue = mySpinBox->value();
77   bool isDone;
78   aValue = editedValue(aValue, isDone);
79
80   if (isDone) {
81     bool isBlocked = mySpinBox->blockSignals(true);
82     mySpinBox->setValue(aValue);
83     mySpinBox->blockSignals(isBlocked);
84   }
85   emit valuesChanged();
86   emit focusOutWidget(this);
87 }
88
89 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
90                                                const std::string theAttribute)
91 {
92   DataPtr aData = theFeature->data();
93   AttributeDoublePtr aRef = aData->real(theAttribute);
94   double aValue = aRef->value();
95
96   bool isDone;
97   aValue = editedValue(aValue, isDone);
98   if (isDone)
99     aRef->setValue(aValue);
100 }