Salome HOME
41ec508e2a08183e489992dd97107f6d718d4934
[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                                                  const std::string& theParentId)
38 : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId),
39   myXPosition(-1), myYPosition(-1)
40 {
41 }
42
43 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
44 {
45 }
46
47 void ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
48 {
49   QDialog aDlg(QApplication::desktop(), Qt::FramelessWindowHint);
50   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
51   aLay->setContentsMargins(2, 2, 2, 2);
52
53   ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(&aDlg);
54   anEditor->enableKeyPressEvent(true);
55
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   QObject::connect(anEditor, SIGNAL(enterReleased()), &aDlg, SLOT(accept()));
68
69   QPoint aPoint = QCursor::pos();
70   if (myXPosition >= 0 && myYPosition >= 0)
71     aPoint = QPoint(myXPosition, myYPosition);
72
73   aDlg.move(aPoint);
74   aDlg.exec();
75
76   outText = anEditor->text();
77   bool isDouble;
78   double aValue = outText.toDouble(&isDouble);
79   if (isDouble) {
80     outValue = aValue;
81     outText = ""; // return empty string, if it's can be converted to a double
82   }
83 }
84
85 bool ModuleBase_WidgetEditor::focusTo()
86 {
87   showPopupEditor();
88   return true;
89 }
90
91 void ModuleBase_WidgetEditor::showPopupEditor(const bool theSendSignals)
92 {
93   // we need to emit the focus in event manually in order to save the widget as an active
94   // in the property panel before the mouse leave event happens in the viewer. The module
95   // ask an active widget and change the feature visualization if the widget is not the current one.
96   if (theSendSignals)
97     emit focusInWidget(this);
98
99   // nds: it seems, that the envents processing is not necessary anymore
100   // White while all events will be processed
101   //QApplication::processEvents();
102   double aValue = mySpinBox->value();
103   QString aText;
104   if (mySpinBox->hasVariable())
105     aText = mySpinBox->text();
106
107   editedValue(aValue, aText);
108   if (aText.isEmpty()) {
109     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
110   } else {
111     ModuleBase_Tools::setSpinText(mySpinBox, aText);
112   }
113   if (theSendSignals) {
114     emit valuesChanged();
115     // the focus leaves the control automatically by the Enter/Esc event
116     // it is processed in operation manager
117     //emit focusOutWidget(this);
118
119     if (!myIsEditing)
120       emit enterClicked(this);
121   }
122   else
123     storeValue();
124 }
125
126 void ModuleBase_WidgetEditor::setCursorPosition(const int theX, const int theY)
127 {
128   myXPosition = theX;
129   myYPosition = theY;
130 }