Salome HOME
EvalListener to evaluate expression from inputs
[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
32 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
33                                                  const Config_WidgetAPI* theData,
34                                                  const std::string& theParentId)
35     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
36 {
37 }
38
39 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
40 {
41 }
42
43 void editedValue(double& outValue, QString& outText)
44 {
45   QMenu* aPopup = new QMenu();
46
47   QLineEdit* aEditor = new QLineEdit(QString::number(outValue), aPopup);
48   QWidgetAction* aLineEditAction = new QWidgetAction(aPopup);
49   aLineEditAction->setDefaultWidget(aEditor);
50   aPopup->addAction(aLineEditAction);
51
52   aEditor->setFocus();
53   aEditor->selectAll();
54   QString anExpression("([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)|([_a-zA-Z][a-zA-Z0-9_]*)");
55   aEditor->setValidator(new QRegExpValidator(QRegExp(anExpression), aEditor));
56   QObject::connect(aEditor, SIGNAL(returnPressed()), aLineEditAction, SIGNAL(triggered()));
57   QObject::connect(aLineEditAction, SIGNAL(triggered()), aPopup, SLOT(hide()));
58
59   QAction* aResult = aPopup->exec(QCursor::pos());
60   outText = aEditor->text();
61   bool isDouble;
62   double aValue = outText.toDouble(&isDouble);
63   if (isDouble) {
64     outValue = aValue;
65   //  outText = ""; // return empty string, if it's can be converted to a double
66   }
67   aPopup->deleteLater();
68 }
69
70 bool ModuleBase_WidgetEditor::focusTo()
71 {
72   // nds: it seems, that the timer is not necessary anymore
73
74   // We can not launch here modal process for value editing because 
75   // it can be called on other focusOutWidget event and will block it
76   //QTimer::singleShot(1, this, SLOT(showPopupEditor()));
77
78   showPopupEditor();
79
80   return true;
81 }
82
83 void ModuleBase_WidgetEditor::showPopupEditor()
84 {
85   // we need to emit the focus in event manually in order to save the widget as an active
86   // in the property panel before the mouse leave event happens in the viewer. The module
87   // ask an active widget and change the feature visualization if the widget is not the current one.
88   emit focusInWidget(this);
89
90   // nds: it seems, that the envents processing is not necessary anymore
91   // White while all events will be processed
92   //QApplication::processEvents();
93   double aValue = mySpinBox->value();
94   QString aText;
95   editedValue(aValue, aText);
96   if (aText.isEmpty()) {
97     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
98   } else {
99     ModuleBase_Tools::setSpinText(mySpinBox, aText);
100   }
101   emit valuesChanged();
102   emit focusOutWidget(this);
103 }