1 // Copyright (C) 2014-2017 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<mailto: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>
41 #include <QWidgetAction>
43 #include <QRegExpValidator>
44 #include <QDesktopWidget>
48 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
49 const Config_WidgetAPI* theData)
50 : ModuleBase_WidgetDoubleValue(theParent, theData),
51 myXPosition(-1), myYPosition(-1), myEditorDialog(0)
55 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
59 bool ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
61 bool isValueAccepted = false;
63 myEditorDialog = new QDialog(QApplication::desktop(), Qt::FramelessWindowHint);
65 QHBoxLayout* aLay = new QHBoxLayout(myEditorDialog);
66 aLay->setContentsMargins(2, 2, 2, 2);
68 ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(myEditorDialog);
69 anEditor->setMinimum(0);
70 anEditor->setMaximum(DBL_MAX);
71 if (outText.isEmpty())
72 anEditor->setValue(outValue);
74 anEditor->setText(outText);
76 aLay->addWidget(anEditor);
78 ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
79 anEditor->selectAll();
81 QPoint aPoint = QCursor::pos();
82 if (myXPosition >= 0 && myYPosition >= 0)
83 aPoint = QPoint(myXPosition, myYPosition);
85 myEditorDialog->move(aPoint);
86 isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
87 if (isValueAccepted) {
88 outText = anEditor->text();
90 double aValue = outText.toDouble(&isDouble);
93 outText = ""; // return empty string, if it's can be converted to a double
96 delete myEditorDialog;
98 return isValueAccepted;
101 bool ModuleBase_WidgetEditor::focusTo()
107 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
109 bool isValueAccepted = false;
110 // we need to emit the focus in event manually in order to save the widget as an active
111 // in the property panel before the mouse leave event happens in the viewer. The module
112 // ask an active widget and change the feature visualization if the widget is not the current one.
116 // nds: it seems, that the envents processing is not necessary anymore
117 // White while all events will be processed
118 //QApplication::processEvents();
119 double aValue = mySpinBox->value();
121 if (mySpinBox->hasVariable())
122 aText = mySpinBox->text();
124 isValueAccepted = editedValue(aValue, aText);
125 if (isValueAccepted) {
126 if (aText.isEmpty()) {
127 if (mySpinBox->hasVariable()) {
128 // variable text should be cleared before setting value as the value
129 // will not be set if there is a varable in control
130 ModuleBase_Tools::setSpinText(mySpinBox, "");
132 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
134 ModuleBase_Tools::setSpinText(mySpinBox, aText);
136 if (theSendSignals) {
137 emit valuesChanged();
138 // the focus leaves the control automatically by the Enter/Esc event
139 // it is processed in operation manager
140 //emit focusOutWidget(this);
145 ModuleBase_Tools::setFocus(mySpinBox, "ModuleBase_WidgetEditor::editedValue");
146 mySpinBox->selectAll();
148 if (theSendSignals && !myIsEditing)
149 emit enterClicked(this);
151 return isValueAccepted;
154 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
160 bool ModuleBase_WidgetEditor::processEnter()
162 if (myEditorDialog) {
163 myEditorDialog->accept();
167 return ModuleBase_WidgetDoubleValue::processEnter();