1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetEditor.h>
21 #include <ModuleBase_ParamSpinBox.h>
22 #include <ModuleBase_Tools.h>
24 #include <Config_Keywords.h>
25 #include <Config_WidgetAPI.h>
27 #include <Events_Loop.h>
28 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_AttributeDouble.h>
35 #include <GeomDataAPI_Point2D.h>
37 #include <QApplication>
42 #include <QWidgetAction>
44 #include <QRegExpValidator>
45 #include <QDesktopWidget>
49 // Dialog is redefined to avoid Escape key processing
50 class ModuleBase_EditorDialog : public QDialog
53 ModuleBase_EditorDialog(QWidget* theParent, Qt::WindowFlags theFlags)
54 : QDialog(theParent, theFlags)
58 ~ModuleBase_EditorDialog() {}
61 // Do nothing if key pressed because it is processing on operation manager level
62 virtual void keyPressEvent(QKeyEvent* theEvent) {
63 if (theEvent->key() == Qt::Key_Escape)
65 QDialog::keyPressEvent(theEvent);
69 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
70 const Config_WidgetAPI* theData)
71 : ModuleBase_WidgetDoubleValue(theParent, theData),
72 myXPosition(-1), myYPosition(-1), myEditorDialog(0)
76 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
80 bool ModuleBase_WidgetEditor::editedValue(double theSpinMinValue, double theSpinMaxValue,
81 double& outValue, QString& outText)
83 bool isValueAccepted = false;
85 myEditorDialog = new ModuleBase_EditorDialog(QApplication::desktop(), Qt::FramelessWindowHint);
87 QHBoxLayout* aLay = new QHBoxLayout(myEditorDialog);
88 aLay->setContentsMargins(2, 2, 2, 2);
90 ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(myEditorDialog);
91 anEditor->setMinimum(theSpinMinValue);
92 anEditor->setMaximum(theSpinMaxValue);
94 QStringList aParameters;
95 ModuleBase_Tools::getParameters(aParameters);
96 anEditor->setCompletionList(aParameters);
98 if (outText.isEmpty())
99 anEditor->setValue(outValue);
101 anEditor->setText(outText);
103 aLay->addWidget(anEditor);
105 ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
106 anEditor->selectAll();
108 QPoint aPoint = QCursor::pos();
109 if (myXPosition >= 0 && myYPosition >= 0)
110 aPoint = QPoint(myXPosition, myYPosition);
112 myEditorDialog->move(aPoint);
113 isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
114 if (isValueAccepted) {
115 outText = anEditor->text();
117 double aValue = outText.toDouble(&isDouble);
120 outText = ""; // return empty string, if it's can be converted to a double
123 delete myEditorDialog;
125 return isValueAccepted;
128 bool ModuleBase_WidgetEditor::focusTo()
134 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
136 bool isValueAccepted = false;
137 // we need to emit the focus in event manually in order to save the widget as an active
138 // in the property panel before the mouse leave event happens in the viewer. The module
139 // ask an active widget and change the feature visualization if the widget is not the current
140 // one. Also we need this widget as active to provide call of processEnter() applyed
141 // by operation manager to the current widget. If not, the myEditorDialog will stay opened
144 // nds: it seems, that the envents processing is not necessary anymore
145 // White while all events will be processed
146 //QApplication::processEvents();
147 double aValue = mySpinBox->value();
149 if (mySpinBox->hasVariable())
150 aText = mySpinBox->text();
152 isValueAccepted = editedValue(mySpinBox->minimum(), mySpinBox->maximum(), aValue, aText);
153 if (isValueAccepted) {
154 if (aText.isEmpty()) {
155 if (mySpinBox->hasVariable()) {
156 // variable text should be cleared before setting value as the value
157 // will not be set if there is a varable in control
158 ModuleBase_Tools::setSpinText(mySpinBox, "");
160 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
162 ModuleBase_Tools::setSpinText(mySpinBox, aText);
164 if (theSendSignals) {
165 emit valuesChanged();
166 // the focus leaves the control automatically by the Enter/Esc event
167 // it is processed in operation manager
168 //emit focusOutWidget(this);
173 ModuleBase_Tools::setFocus(mySpinBox, "ModuleBase_WidgetEditor::editedValue");
174 mySpinBox->selectAll();
175 // enter is processed, so we need not anymore emit clicked signal
176 //if (theSendSignals && !myIsEditing && isValueAccepted)
177 // emit enterClicked(this);
179 return isValueAccepted;
182 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
188 bool ModuleBase_WidgetEditor::processEnter()
190 if (myEditorDialog) {
191 myEditorDialog->accept();
195 return ModuleBase_WidgetDoubleValue::processEnter();
198 bool ModuleBase_WidgetEditor::processEscape()
200 if (myEditorDialog) {
201 myEditorDialog->reject();
205 return ModuleBase_WidgetDoubleValue::processEscape();