Salome HOME
Issues #2173, #2169: processing focus in widget by SHAPER loop (instead of Qt loop...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetEditor.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include <ModuleBase_WidgetEditor.h>
8 #include <ModuleBase_ParamSpinBox.h>
9 #include <ModuleBase_Tools.h>
10
11 #include <Config_Keywords.h>
12 #include <Config_WidgetAPI.h>
13
14 #include <Events_Loop.h>
15 #include <ModelAPI_Events.h>
16
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_AttributeDouble.h>
21
22 #include <GeomDataAPI_Point2D.h>
23
24 #include <QApplication>
25 #include <QLineEdit>
26 #include <QMenu>
27 #include <QWidget>
28 #include <QWidgetAction>
29 #include <QRegExp>
30 #include <QRegExpValidator>
31 #include <QDesktopWidget>
32 #include <QDialog>
33 #include <QLayout>
34
35 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
36                                                  const Config_WidgetAPI* theData)
37 : ModuleBase_WidgetDoubleValue(theParent, theData),
38   myXPosition(-1), myYPosition(-1), myEditorDialog(0)
39 {
40 }
41
42 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
43 {
44 }
45
46 bool ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
47 {
48   bool isValueAccepted = false;
49
50   myEditorDialog = new QDialog(QApplication::desktop(), Qt::FramelessWindowHint);
51
52   QHBoxLayout* aLay = new QHBoxLayout(myEditorDialog);
53   aLay->setContentsMargins(2, 2, 2, 2);
54
55   ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(myEditorDialog);
56   anEditor->setMinimum(0);
57   anEditor->setMaximum(DBL_MAX);
58   if (outText.isEmpty())
59     anEditor->setValue(outValue);
60   else
61     anEditor->setText(outText);
62
63   aLay->addWidget(anEditor);
64
65   ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
66   anEditor->selectAll();
67
68   QPoint aPoint = QCursor::pos();
69   if (myXPosition >= 0 && myYPosition >= 0)
70     aPoint = QPoint(myXPosition, myYPosition);
71
72   myEditorDialog->move(aPoint);
73   isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
74   if (isValueAccepted) {
75     outText = anEditor->text();
76     bool isDouble;
77     double aValue = outText.toDouble(&isDouble);
78     if (isDouble) {
79       outValue = aValue;
80       outText = ""; // return empty string, if it's can be converted to a double
81     }
82   }
83   delete myEditorDialog;
84   myEditorDialog = 0;
85   return isValueAccepted;
86 }
87
88 bool ModuleBase_WidgetEditor::focusTo()
89 {
90   showPopupEditor();
91   return true;
92 }
93
94 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
95 {
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.
100   if (theSendSignals)
101     emitFocusInWidget();
102
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();
107   QString aText;
108   if (mySpinBox->hasVariable())
109     aText = mySpinBox->text();
110
111   isValueAccepted = editedValue(aValue, aText);
112   if (isValueAccepted) {
113     if (aText.isEmpty()) {
114       if (mySpinBox->hasVariable()) {
115         // variable text should be cleared before setting value as the value
116         // will not be set if there is a varable in control
117         ModuleBase_Tools::setSpinText(mySpinBox, "");
118       }
119       ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
120     } else {
121       ModuleBase_Tools::setSpinText(mySpinBox, aText);
122     }
123     if (theSendSignals) {
124       emit valuesChanged();
125       // the focus leaves the control automatically by the Enter/Esc event
126       // it is processed in operation manager
127       //emit focusOutWidget(this);
128     }
129     else
130       storeValue();
131   }
132
133   if (theSendSignals && !myIsEditing)
134     emit enterClicked(this);
135
136   return isValueAccepted;
137 }
138
139 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
140 {
141   myXPosition = theX;
142   myYPosition = theY;
143 }
144
145 bool ModuleBase_WidgetEditor::processEnter()
146 {
147   if (myEditorDialog) {
148     myEditorDialog->accept();
149     return true;
150   }
151
152   return ModuleBase_WidgetDoubleValue::processEnter();
153 }