Salome HOME
Issue #652: Qt can convert to double a string as with '.' as with ',', but method...
[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_ParamSpinBox.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 <QApplication>
25 #include <QLineEdit>
26 #include <QMenu>
27 #include <QWidget>
28 #include <QWidgetAction>
29 #include <QRegExp>
30 #include <QRegExpValidator>
31 #include <QDesktopWidget>
32 #include <QDialog>
33 #include <QLayout>
34
35 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
36                                                  const Config_WidgetAPI* theData,
37                                                  const std::string& theParentId)
38     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
39 {
40 }
41
42 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
43 {
44 }
45
46 void editedValue(double& outValue, QString& outText)
47 {
48   QDialog aDlg(QApplication::desktop(), Qt::Popup/* | Qt::FramelessWindowHint*/);
49   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
50   aLay->setContentsMargins(2, 2, 2, 2);
51
52   ModuleBase_ParamSpinBox* aEditor = new ModuleBase_ParamSpinBox(&aDlg);
53   aEditor->setMinimum(0);
54   aEditor->setMaximum(DBL_MAX);
55   aEditor->setValue(outValue);
56   aLay->addWidget(aEditor);
57
58   aEditor->setFocus();
59   aEditor->selectAll();
60   QObject::connect(aEditor, SIGNAL(editingFinished()), &aDlg, SLOT(accept()));
61
62   aDlg.move(QCursor::pos());
63   aDlg.exec();
64   outText = aEditor->text();
65   bool isDouble;
66   double aValue = outText.toDouble(&isDouble);
67   if (isDouble) {
68     outValue = aValue;
69     outText = ""; // return empty string, if it's can be converted to a double
70   }
71 }
72
73 bool ModuleBase_WidgetEditor::focusTo()
74 {
75   // nds: it seems, that the timer is not necessary anymore
76
77   // We can not launch here modal process for value editing because 
78   // it can be called on other focusOutWidget event and will block it
79   //QTimer::singleShot(1, this, SLOT(showPopupEditor()));
80
81   showPopupEditor();
82
83   return true;
84 }
85
86 void ModuleBase_WidgetEditor::showPopupEditor()
87 {
88   // we need to emit the focus in event manually in order to save the widget as an active
89   // in the property panel before the mouse leave event happens in the viewer. The module
90   // ask an active widget and change the feature visualization if the widget is not the current one.
91   emit focusInWidget(this);
92
93   // nds: it seems, that the envents processing is not necessary anymore
94   // White while all events will be processed
95   //QApplication::processEvents();
96   double aValue = mySpinBox->value();
97   QString aText;
98   editedValue(aValue, aText);
99   if (aText.isEmpty()) {
100     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
101   } else {
102     ModuleBase_Tools::setSpinText(mySpinBox, aText);
103   }
104   emit valuesChanged();
105   emit focusOutWidget(this);
106 }