]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetEditor.cpp
Salome HOME
Issue #2536: Set minimal width of pop-up dialog box
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_WidgetEditor.h>
22 #include <ModuleBase_ParamSpinBox.h>
23 #include <ModuleBase_Tools.h>
24
25 #include <Config_Keywords.h>
26 #include <Config_WidgetAPI.h>
27
28 #include <Events_Loop.h>
29 #include <ModelAPI_Events.h>
30
31 #include <ModelAPI_Feature.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Object.h>
34 #include <ModelAPI_AttributeDouble.h>
35
36 #include <GeomDataAPI_Point2D.h>
37
38 #include <QApplication>
39 #include <QKeyEvent>
40 #include <QLineEdit>
41 #include <QMenu>
42 #include <QWidget>
43 #include <QWidgetAction>
44 #include <QRegExp>
45 #include <QRegExpValidator>
46 #include <QDesktopWidget>
47 #include <QDialog>
48 #include <QLayout>
49
50 // Dialog is redefined to avoid Escape key processing
51 class ModuleBase_EditorDialog : public QDialog
52 {
53 public:
54   ModuleBase_EditorDialog(QWidget* theParent, Qt::WindowFlags theFlags)
55     : QDialog(theParent, theFlags)
56   {
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   if (outText.isEmpty())
95     anEditor->setValue(outValue);
96   else
97     anEditor->setText(outText);
98
99   aLay->addWidget(anEditor);
100
101   ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
102   anEditor->selectAll();
103
104   QPoint aPoint = QCursor::pos();
105   if (myXPosition >= 0 && myYPosition >= 0)
106     aPoint = QPoint(myXPosition, myYPosition);
107
108   myEditorDialog->move(aPoint);
109   isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
110   if (isValueAccepted) {
111     outText = anEditor->text();
112     bool isDouble;
113     double aValue = outText.toDouble(&isDouble);
114     if (isDouble) {
115       outValue = aValue;
116       outText = ""; // return empty string, if it's can be converted to a double
117     }
118   }
119   delete myEditorDialog;
120   myEditorDialog = 0;
121   return isValueAccepted;
122 }
123
124 bool ModuleBase_WidgetEditor::focusTo()
125 {
126   showPopupEditor();
127   return false;
128 }
129
130 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
131 {
132   bool isValueAccepted = false;
133   // we need to emit the focus in event manually in order to save the widget as an active
134   // in the property panel before the mouse leave event happens in the viewer. The module
135   // ask an active widget and change the feature visualization if the widget is not the current
136   // one.  Also we need this widget as active to provide call of processEnter() applyed
137   // by operation manager to the current widget. If not, the myEditorDialog will stay opened
138   emitFocusInWidget();
139
140   // nds: it seems, that the envents processing is not necessary anymore
141   // White while all events will be processed
142   //QApplication::processEvents();
143   double aValue = mySpinBox->value();
144   QString aText;
145   if (mySpinBox->hasVariable())
146     aText = mySpinBox->text();
147
148   isValueAccepted = editedValue(mySpinBox->minimum(), mySpinBox->maximum(), aValue, aText);
149   if (isValueAccepted) {
150     if (aText.isEmpty()) {
151       if (mySpinBox->hasVariable()) {
152         // variable text should be cleared before setting value as the value
153         // will not be set if there is a varable in control
154         ModuleBase_Tools::setSpinText(mySpinBox, "");
155       }
156       ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
157     } else {
158       ModuleBase_Tools::setSpinText(mySpinBox, aText);
159     }
160     if (theSendSignals) {
161       emit valuesChanged();
162       // the focus leaves the control automatically by the Enter/Esc event
163       // it is processed in operation manager
164       //emit focusOutWidget(this);
165     }
166     else
167       storeValue();
168   }
169   ModuleBase_Tools::setFocus(mySpinBox, "ModuleBase_WidgetEditor::editedValue");
170   mySpinBox->selectAll();
171   // enter is processed, so we need not anymore emit clicked signal
172   //if (theSendSignals && !myIsEditing && isValueAccepted)
173   //  emit enterClicked(this);
174
175   return isValueAccepted;
176 }
177
178 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
179 {
180   myXPosition = theX;
181   myYPosition = theY;
182 }
183
184 bool ModuleBase_WidgetEditor::processEnter()
185 {
186   if (myEditorDialog) {
187     myEditorDialog->accept();
188     return true;
189   }
190
191   return ModuleBase_WidgetDoubleValue::processEnter();
192 }
193
194 bool ModuleBase_WidgetEditor::processEscape()
195 {
196   if (myEditorDialog) {
197     myEditorDialog->reject();
198     return true;
199   }
200
201   return ModuleBase_WidgetDoubleValue::processEscape();
202 }