Salome HOME
Merge remote branch 'remotes/origin/vsr/libxml2_mdv' into Dev_2.1.0
[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   myIsKeyReleasedEmitted(false)
40 {
41 }
42
43 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
44 {
45 }
46
47 void ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
48 {
49   QDialog aDlg(QApplication::desktop(), Qt::FramelessWindowHint);
50   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
51   aLay->setContentsMargins(2, 2, 2, 2);
52
53   ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(&aDlg);
54   anEditor->enableKeyPressEvent(true);
55   if (!myIsEditing) {
56     connect(anEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SLOT(onKeyReleased(QKeyEvent*)));
57   }
58
59   anEditor->setMinimum(0);
60   anEditor->setMaximum(DBL_MAX);
61   if (outText.isEmpty())
62     anEditor->setValue(outValue);
63   else
64     anEditor->setText(outText);
65
66   aLay->addWidget(anEditor);
67
68   anEditor->setFocus();
69   anEditor->selectAll();
70   QObject::connect(anEditor, SIGNAL(editingFinished()), &aDlg, SLOT(accept()));
71
72   aDlg.move(QCursor::pos());
73   aDlg.exec();
74
75   if (!myIsEditing) {
76     disconnect(anEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SLOT(onKeyReleased(QKeyEvent*)));
77   }
78
79   outText = anEditor->text();
80   bool isDouble;
81   double aValue = outText.toDouble(&isDouble);
82   if (isDouble) {
83     outValue = aValue;
84     outText = ""; // return empty string, if it's can be converted to a double
85   }
86 }
87
88 bool ModuleBase_WidgetEditor::focusTo()
89 {
90   // nds: it seems, that the timer is not necessary anymore
91
92   // We can not launch here modal process for value editing because 
93   // it can be called on other focusOutWidget event and will block it
94   //QTimer::singleShot(1, this, SLOT(showPopupEditor()));
95
96   showPopupEditor();
97
98   return true;
99 }
100
101 void ModuleBase_WidgetEditor::showPopupEditor()
102 {
103   myIsKeyReleasedEmitted = false;
104
105   // we need to emit the focus in event manually in order to save the widget as an active
106   // in the property panel before the mouse leave event happens in the viewer. The module
107   // ask an active widget and change the feature visualization if the widget is not the current one.
108   emit focusInWidget(this);
109
110   // nds: it seems, that the envents processing is not necessary anymore
111   // White while all events will be processed
112   //QApplication::processEvents();
113   double aValue = mySpinBox->value();
114   QString aText;
115   if (mySpinBox->hasVariable())
116     aText = mySpinBox->text();
117
118   editedValue(aValue, aText);
119   if (aText.isEmpty()) {
120     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
121   } else {
122     ModuleBase_Tools::setSpinText(mySpinBox, aText);
123   }
124   emit valuesChanged();
125   // the focus leaves the control automatically by the Enter/Esc event
126   // it is processed in operation manager
127   //emit focusOutWidget(this);
128
129   if (myIsKeyReleasedEmitted)
130     emit enterClicked();
131 }
132
133 void ModuleBase_WidgetEditor::onKeyReleased(QKeyEvent* theEvent)
134 {
135   myIsKeyReleasedEmitted = true;
136 }