Salome HOME
#1042 Sometimes when setting distance constraints, the input field is not displayed...
[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   //myIsEnterPressedEmitted(false),
40   myXPosition(-1), myYPosition(-1)
41 {
42 }
43
44 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
45 {
46 }
47
48 void ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
49 {
50   QDialog aDlg(QApplication::desktop(), Qt::FramelessWindowHint);
51   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
52   aLay->setContentsMargins(2, 2, 2, 2);
53
54   ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(&aDlg);
55   anEditor->enableKeyPressEvent(true);
56   //if (!myIsEditing) {
57   //  connect(anEditor, SIGNAL(enterPressed()), this, SLOT(onEnterPressed()));
58   //}
59
60   anEditor->setMinimum(0);
61   anEditor->setMaximum(DBL_MAX);
62   if (outText.isEmpty())
63     anEditor->setValue(outValue);
64   else
65     anEditor->setText(outText);
66
67   aLay->addWidget(anEditor);
68
69   ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
70   anEditor->selectAll();
71   QObject::connect(anEditor, SIGNAL(enterReleased()), &aDlg, SLOT(accept()));
72
73   QPoint aPoint = QCursor::pos();
74   if (myXPosition >= 0 && myYPosition >= 0)
75     aPoint = QPoint(myXPosition, myYPosition);
76
77   aDlg.move(aPoint);
78   aDlg.exec();
79
80   //if (!myIsEditing) {
81   //  disconnect(anEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SLOT(onEnterPressed()));
82   //}
83
84   outText = anEditor->text();
85   bool isDouble;
86   double aValue = outText.toDouble(&isDouble);
87   if (isDouble) {
88     outValue = aValue;
89     outText = ""; // return empty string, if it's can be converted to a double
90   }
91 }
92
93 bool ModuleBase_WidgetEditor::focusTo()
94 {
95   showPopupEditor();
96   return true;
97 }
98
99 void ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
100 {
101   //myIsEnterPressedEmitted = false;
102
103   // we need to emit the focus in event manually in order to save the widget as an active
104   // in the property panel before the mouse leave event happens in the viewer. The module
105   // ask an active widget and change the feature visualization if the widget is not the current one.
106   if (theSendSignals)
107     emit focusInWidget(this);
108
109   // nds: it seems, that the envents processing is not necessary anymore
110   // White while all events will be processed
111   //QApplication::processEvents();
112   double aValue = mySpinBox->value();
113   QString aText;
114   if (mySpinBox->hasVariable())
115     aText = mySpinBox->text();
116
117   editedValue(aValue, aText);
118   if (aText.isEmpty()) {
119     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
120   } else {
121     ModuleBase_Tools::setSpinText(mySpinBox, aText);
122   }
123   if (theSendSignals) {
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 (myIsEnterPressedEmitted)
130     if (!myIsEditing)
131       emit enterClicked();
132   }
133   else
134     storeValue();
135 }
136
137 /*void ModuleBase_WidgetEditor::onEnterPressed()
138 {
139   myIsEnterPressedEmitted = true;
140 }*/
141
142 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
143 {
144   myXPosition = theX;
145   myYPosition = theY;
146 }