Salome HOME
0234ee3b1bdfb5cffbd4898eb460934b6e24b749
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModuleBase_WidgetEditor.h>
21 #include <ModuleBase_ParamSpinBox.h>
22 #include <ModuleBase_Tools.h>
23
24 #include <Config_Keywords.h>
25 #include <Config_WidgetAPI.h>
26
27 #include <Events_Loop.h>
28 #include <ModelAPI_Events.h>
29
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_AttributeDouble.h>
34
35 #include <GeomDataAPI_Point2D.h>
36
37 #include <QApplication>
38 #include <QKeyEvent>
39 #include <QLineEdit>
40 #include <QMenu>
41 #include <QWidget>
42 #include <QWidgetAction>
43 #include <QRegExp>
44 #include <QRegExpValidator>
45 #include <QDesktopWidget>
46 #include <QDialog>
47 #include <QLayout>
48
49 // Dialog is redefined to avoid Escape key processing
50 class ModuleBase_EditorDialog : public QDialog
51 {
52 public:
53   ModuleBase_EditorDialog(QWidget* theParent, Qt::WindowFlags theFlags)
54     : QDialog(theParent, theFlags)
55   {
56     setObjectName("ModuleBase_EditorDialog");
57     setMinimumWidth(100);
58   }
59   ~ModuleBase_EditorDialog() {}
60
61 protected:
62   // Do nothing if key pressed because it is processing on operation manager level
63   virtual void keyPressEvent(QKeyEvent* theEvent) {
64     if (theEvent->key() == Qt::Key_Escape)
65         return;
66     QDialog::keyPressEvent(theEvent);
67   }
68 };
69
70 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
71                                                  const Config_WidgetAPI* theData)
72 : ModuleBase_WidgetDoubleValue(theParent, theData),
73   myXPosition(-1), myYPosition(-1), myEditorDialog(0)
74 {
75 }
76
77 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
78 {
79 }
80
81 bool ModuleBase_WidgetEditor::editedValue(double theSpinMinValue, double theSpinMaxValue,
82                                           double& outValue, QString& outText)
83 {
84   bool isValueAccepted = false;
85
86   myEditorDialog = new ModuleBase_EditorDialog(QApplication::desktop(), Qt::FramelessWindowHint);
87
88   QHBoxLayout* aLay = new QHBoxLayout(myEditorDialog);
89   aLay->setContentsMargins(2, 2, 2, 2);
90
91   ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(myEditorDialog);
92   anEditor->setMinimum(theSpinMinValue);
93   anEditor->setMaximum(theSpinMaxValue);
94
95   QStringList aParameters;
96   ModuleBase_Tools::getParameters(aParameters);
97   anEditor->setCompletionList(aParameters);
98
99   if (outText.isEmpty())
100     anEditor->setValue(outValue);
101   else
102     anEditor->setText(outText);
103
104   aLay->addWidget(anEditor);
105
106   ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
107   anEditor->selectAll();
108
109   QPoint aPoint = QCursor::pos();
110   if (myXPosition >= 0 && myYPosition >= 0)
111     aPoint = QPoint(myXPosition, myYPosition);
112
113   myEditorDialog->move(aPoint);
114   isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
115   if (isValueAccepted) {
116     outText = anEditor->text();
117     bool isDouble;
118     double aValue = outText.toDouble(&isDouble);
119     if (isDouble) {
120       outValue = aValue;
121       outText = ""; // return empty string, if it's can be converted to a double
122     }
123   }
124   delete myEditorDialog;
125   myEditorDialog = 0;
126   return isValueAccepted;
127 }
128
129 bool ModuleBase_WidgetEditor::focusTo()
130 {
131   showPopupEditor();
132   return false;
133 }
134
135 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
136 {
137   bool isValueAccepted = false;
138   // we need to emit the focus in event manually in order to save the widget as an active
139   // in the property panel before the mouse leave event happens in the viewer. The module
140   // ask an active widget and change the feature visualization if the widget is not the current
141   // one.  Also we need this widget as active to provide call of processEnter() applyed
142   // by operation manager to the current widget. If not, the myEditorDialog will stay opened
143   emitFocusInWidget();
144
145   // nds: it seems, that the envents processing is not necessary anymore
146   // White while all events will be processed
147   //QApplication::processEvents();
148   double aValue = mySpinBox->value();
149   QString aText;
150   if (mySpinBox->hasVariable())
151     aText = mySpinBox->text();
152
153   isValueAccepted = editedValue(mySpinBox->minimum(), mySpinBox->maximum(), aValue, aText);
154   if (isValueAccepted) {
155     if (aText.isEmpty()) {
156       if (mySpinBox->hasVariable()) {
157         // variable text should be cleared before setting value as the value
158         // will not be set if there is a varable in control
159         ModuleBase_Tools::setSpinText(mySpinBox, "");
160       }
161       ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
162     } else {
163       ModuleBase_Tools::setSpinText(mySpinBox, aText);
164     }
165     if (theSendSignals) {
166       emit valuesChanged();
167       // the focus leaves the control automatically by the Enter/Esc event
168       // it is processed in operation manager
169       //emit focusOutWidget(this);
170     }
171     else
172       storeValue();
173   }
174   ModuleBase_Tools::setFocus(mySpinBox, "ModuleBase_WidgetEditor::editedValue");
175   mySpinBox->selectAll();
176   // enter is processed, so we need not anymore emit clicked signal
177   //if (theSendSignals && !myIsEditing && isValueAccepted)
178   //  emit enterClicked(this);
179
180   return isValueAccepted;
181 }
182
183 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
184 {
185   myXPosition = theX;
186   myYPosition = theY;
187 }
188
189 bool ModuleBase_WidgetEditor::processEnter()
190 {
191   if (myEditorDialog) {
192     myEditorDialog->accept();
193     return true;
194   }
195
196   return ModuleBase_WidgetDoubleValue::processEnter();
197 }
198
199 bool ModuleBase_WidgetEditor::processEscape()
200 {
201   if (myEditorDialog) {
202     myEditorDialog->reject();
203     return true;
204   }
205
206   return ModuleBase_WidgetDoubleValue::processEscape();
207 }