Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into Dev_2.8.0
[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 <QLineEdit>
40 #include <QMenu>
41 #include <QWidget>
42 #include <QWidgetAction>
43 #include <QRegExp>
44 #include <QRegExpValidator>
45 #include <QDesktopWidget>
46 #include <QDialog>
47 #include <QLayout>
48
49 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
50                                                  const Config_WidgetAPI* theData)
51 : ModuleBase_WidgetDoubleValue(theParent, theData),
52   myXPosition(-1), myYPosition(-1), myEditorDialog(0)
53 {
54 }
55
56 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
57 {
58 }
59
60 bool ModuleBase_WidgetEditor::editedValue(double theSpinMinValue, double theSpinMaxValue,
61                                           double& outValue, QString& outText)
62 {
63   bool isValueAccepted = false;
64
65   myEditorDialog = new QDialog(QApplication::desktop(), Qt::FramelessWindowHint);
66
67   QHBoxLayout* aLay = new QHBoxLayout(myEditorDialog);
68   aLay->setContentsMargins(2, 2, 2, 2);
69
70   ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(myEditorDialog);
71   anEditor->setMinimum(theSpinMinValue);
72   anEditor->setMaximum(theSpinMaxValue);
73   if (outText.isEmpty())
74     anEditor->setValue(outValue);
75   else
76     anEditor->setText(outText);
77
78   aLay->addWidget(anEditor);
79
80   ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
81   anEditor->selectAll();
82
83   QPoint aPoint = QCursor::pos();
84   if (myXPosition >= 0 && myYPosition >= 0)
85     aPoint = QPoint(myXPosition, myYPosition);
86
87   myEditorDialog->move(aPoint);
88   isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
89   if (isValueAccepted) {
90     outText = anEditor->text();
91     bool isDouble;
92     double aValue = outText.toDouble(&isDouble);
93     if (isDouble) {
94       outValue = aValue;
95       outText = ""; // return empty string, if it's can be converted to a double
96     }
97   }
98   delete myEditorDialog;
99   myEditorDialog = 0;
100   return isValueAccepted;
101 }
102
103 bool ModuleBase_WidgetEditor::focusTo()
104 {
105   showPopupEditor();
106   return false;
107 }
108
109 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
110 {
111   bool isValueAccepted = false;
112   // we need to emit the focus in event manually in order to save the widget as an active
113   // in the property panel before the mouse leave event happens in the viewer. The module
114   // ask an active widget and change the feature visualization if the widget is not the current
115   // one.  Also we need this widget as active to provide call of processEnter() applyed
116   // by operation manager to the current widget. If not, the myEditorDialog will stay opened
117   emitFocusInWidget();
118
119   // nds: it seems, that the envents processing is not necessary anymore
120   // White while all events will be processed
121   //QApplication::processEvents();
122   double aValue = mySpinBox->value();
123   QString aText;
124   if (mySpinBox->hasVariable())
125     aText = mySpinBox->text();
126
127   isValueAccepted = editedValue(mySpinBox->minimum(), mySpinBox->maximum(), aValue, aText);
128   if (isValueAccepted) {
129     if (aText.isEmpty()) {
130       if (mySpinBox->hasVariable()) {
131         // variable text should be cleared before setting value as the value
132         // will not be set if there is a varable in control
133         ModuleBase_Tools::setSpinText(mySpinBox, "");
134       }
135       ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
136     } else {
137       ModuleBase_Tools::setSpinText(mySpinBox, aText);
138     }
139     if (theSendSignals) {
140       emit valuesChanged();
141       // the focus leaves the control automatically by the Enter/Esc event
142       // it is processed in operation manager
143       //emit focusOutWidget(this);
144     }
145     else
146       storeValue();
147   }
148   ModuleBase_Tools::setFocus(mySpinBox, "ModuleBase_WidgetEditor::editedValue");
149   mySpinBox->selectAll();
150
151   if (theSendSignals && !myIsEditing)
152     emit enterClicked(this);
153
154   return isValueAccepted;
155 }
156
157 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
158 {
159   myXPosition = theX;
160   myYPosition = theY;
161 }
162
163 bool ModuleBase_WidgetEditor::processEnter()
164 {
165   if (myEditorDialog) {
166     myEditorDialog->accept();
167     return true;
168   }
169
170   return ModuleBase_WidgetDoubleValue::processEnter();
171 }