Salome HOME
parameter problem. Scenario: dimension: parameter using, scenario: create a=100,...
[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   if (outText.isEmpty())
56     aEditor->setValue(outValue);
57   else
58     aEditor->setText(outText);
59
60   aLay->addWidget(aEditor);
61
62   aEditor->setFocus();
63   aEditor->selectAll();
64   QObject::connect(aEditor, SIGNAL(editingFinished()), &aDlg, SLOT(accept()));
65
66   aDlg.move(QCursor::pos());
67   aDlg.exec();
68   outText = aEditor->text();
69   bool isDouble;
70   double aValue = outText.toDouble(&isDouble);
71   if (isDouble) {
72     outValue = aValue;
73     outText = ""; // return empty string, if it's can be converted to a double
74   }
75 }
76
77 bool ModuleBase_WidgetEditor::focusTo()
78 {
79   // nds: it seems, that the timer is not necessary anymore
80
81   // We can not launch here modal process for value editing because 
82   // it can be called on other focusOutWidget event and will block it
83   //QTimer::singleShot(1, this, SLOT(showPopupEditor()));
84
85   showPopupEditor();
86
87   return true;
88 }
89
90 void ModuleBase_WidgetEditor::showPopupEditor()
91 {
92   // we need to emit the focus in event manually in order to save the widget as an active
93   // in the property panel before the mouse leave event happens in the viewer. The module
94   // ask an active widget and change the feature visualization if the widget is not the current one.
95   emit focusInWidget(this);
96
97   // nds: it seems, that the envents processing is not necessary anymore
98   // White while all events will be processed
99   //QApplication::processEvents();
100   double aValue = mySpinBox->value();
101   QString aText;
102   if (mySpinBox->hasVariable())
103     aText = mySpinBox->text();
104
105   editedValue(aValue, aText);
106   if (aText.isEmpty()) {
107     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
108   } else {
109     ModuleBase_Tools::setSpinText(mySpinBox, aText);
110   }
111   emit valuesChanged();
112   emit focusOutWidget(this);
113 }