]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetEditor.cpp
Salome HOME
854d451a8d59f3a95519a2807975420eaa702f32
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <ModuleBase_WidgetEditor.h>
21 #include <ModuleBase_ParamSpinBox.h>
22 #include <ModuleBase_Tools.h>
23
24 #include <Config_Keywords.h>
25 #include <Config_WidgetAPI.h>
26
27 #include <Events_Loop.h>
28 #include <ModelAPI_Events.h>
29
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_AttributeDouble.h>
34
35 #include <GeomDataAPI_Point2D.h>
36
37 #include <QApplication>
38 #include <QLineEdit>
39 #include <QMenu>
40 #include <QWidget>
41 #include <QWidgetAction>
42 #include <QRegExp>
43 #include <QRegExpValidator>
44 #include <QDesktopWidget>
45 #include <QDialog>
46 #include <QLayout>
47
48 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
49                                                  const Config_WidgetAPI* theData)
50 : ModuleBase_WidgetDoubleValue(theParent, theData),
51   myXPosition(-1), myYPosition(-1), myEditorDialog(0)
52 {
53 }
54
55 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
56 {
57 }
58
59 bool ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
60 {
61   bool isValueAccepted = false;
62
63   myEditorDialog = new QDialog(QApplication::desktop(), Qt::FramelessWindowHint);
64
65   QHBoxLayout* aLay = new QHBoxLayout(myEditorDialog);
66   aLay->setContentsMargins(2, 2, 2, 2);
67
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);
73   else
74     anEditor->setText(outText);
75
76   aLay->addWidget(anEditor);
77
78   ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
79   anEditor->selectAll();
80
81   QPoint aPoint = QCursor::pos();
82   if (myXPosition >= 0 && myYPosition >= 0)
83     aPoint = QPoint(myXPosition, myYPosition);
84
85   myEditorDialog->move(aPoint);
86   isValueAccepted = myEditorDialog->exec() == QDialog::Accepted;
87   if (isValueAccepted) {
88     outText = anEditor->text();
89     bool isDouble;
90     double aValue = outText.toDouble(&isDouble);
91     if (isDouble) {
92       outValue = aValue;
93       outText = ""; // return empty string, if it's can be converted to a double
94     }
95   }
96   delete myEditorDialog;
97   myEditorDialog = 0;
98   return isValueAccepted;
99 }
100
101 bool ModuleBase_WidgetEditor::focusTo()
102 {
103   showPopupEditor();
104   return true;
105 }
106
107 bool ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
108 {
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.
113   if (theSendSignals)
114     emitFocusInWidget();
115
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();
120   QString aText;
121   if (mySpinBox->hasVariable())
122     aText = mySpinBox->text();
123
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, "");
131       }
132       ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
133     } else {
134       ModuleBase_Tools::setSpinText(mySpinBox, aText);
135     }
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);
141     }
142     else
143       storeValue();
144   }
145   ModuleBase_Tools::setFocus(mySpinBox, "ModuleBase_WidgetEditor::editedValue");
146   mySpinBox->selectAll();
147
148   if (theSendSignals && !myIsEditing)
149     emit enterClicked(this);
150
151   return isValueAccepted;
152 }
153
154 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
155 {
156   myXPosition = theX;
157   myYPosition = theY;
158 }
159
160 bool ModuleBase_WidgetEditor::processEnter()
161 {
162   if (myEditorDialog) {
163     myEditorDialog->accept();
164     return true;
165   }
166
167   return ModuleBase_WidgetDoubleValue::processEnter();
168 }