1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetEditor.cpp
4 // Created: 25 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include <ModuleBase_WidgetEditor.h>
8 #include <ModuleBase_ParamSpinBox.h>
9 #include <ModuleBase_Tools.h>
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
14 #include <Events_Loop.h>
15 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_AttributeDouble.h>
22 #include <GeomDataAPI_Point2D.h>
24 #include <QApplication>
28 #include <QWidgetAction>
30 #include <QRegExpValidator>
31 #include <QDesktopWidget>
35 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
36 const Config_WidgetAPI* theData)
37 : ModuleBase_WidgetDoubleValue(theParent, theData),
38 myXPosition(-1), myYPosition(-1)
42 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
46 bool ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
48 bool isValueAccepted = false;
50 QDialog aDlg(QApplication::desktop(), Qt::FramelessWindowHint);
51 QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
52 aLay->setContentsMargins(2, 2, 2, 2);
54 ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(&aDlg);
55 anEditor->enableKeyPressEvent(true);
57 anEditor->setMinimum(0);
58 anEditor->setMaximum(DBL_MAX);
59 if (outText.isEmpty())
60 anEditor->setValue(outValue);
62 anEditor->setText(outText);
64 aLay->addWidget(anEditor);
66 ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
67 anEditor->selectAll();
68 QObject::connect(anEditor, SIGNAL(enterReleased()), &aDlg, SLOT(accept()));
70 QPoint aPoint = QCursor::pos();
71 if (myXPosition >= 0 && myYPosition >= 0)
72 aPoint = QPoint(myXPosition, myYPosition);
75 isValueAccepted = aDlg.exec() == QDialog::Accepted;
76 if (isValueAccepted) {
77 outText = anEditor->text();
79 double aValue = outText.toDouble(&isDouble);
82 outText = ""; // return empty string, if it's can be converted to a double
85 return isValueAccepted;
88 bool ModuleBase_WidgetEditor::focusTo()
94 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
96 bool isValueAccepted = false;
97 // we need to emit the focus in event manually in order to save the widget as an active
98 // in the property panel before the mouse leave event happens in the viewer. The module
99 // ask an active widget and change the feature visualization if the widget is not the current one.
101 emit focusInWidget(this);
103 // nds: it seems, that the envents processing is not necessary anymore
104 // White while all events will be processed
105 //QApplication::processEvents();
106 double aValue = mySpinBox->value();
108 if (mySpinBox->hasVariable())
109 aText = mySpinBox->text();
111 isValueAccepted = editedValue(aValue, aText);
112 if (isValueAccepted) {
113 if (aText.isEmpty()) {
114 ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
116 ModuleBase_Tools::setSpinText(mySpinBox, aText);
118 if (theSendSignals) {
119 emit valuesChanged();
120 // the focus leaves the control automatically by the Enter/Esc event
121 // it is processed in operation manager
122 //emit focusOutWidget(this);
128 if (theSendSignals && !myIsEditing)
129 emit enterClicked(this);
131 return isValueAccepted;
134 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)