Salome HOME
Using variables in WidgetDoubleValue
[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 <QWidget>
25 #include <QLineEdit>
26 #include <QTimer>
27 #include <QDialog>
28 #include <QLayout>
29 #include <QApplication>
30
31 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
32                                                  const Config_WidgetAPI* theData,
33                                                  const std::string& theParentId)
34     : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
35 {
36 }
37
38 ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
39 {
40 }
41
42 double editedValue(double theValue, bool& isDone)
43 {
44   QDialog aDlg;
45   aDlg.setWindowFlags(Qt::FramelessWindowHint);
46   QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
47   ModuleBase_Tools::zeroMargins(aLay);
48
49   QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
50   aEditor->selectAll();
51   aEditor->setValidator(new QDoubleValidator(aEditor));
52   QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
53   aLay->addWidget(aEditor);
54
55   QPoint aPoint = QCursor::pos();
56   aDlg.move(aPoint);
57
58   isDone = aDlg.exec() == QDialog::Accepted;
59   double aValue = theValue;
60   if (isDone)
61     aValue = aEditor->text().toDouble();
62   return aValue;
63 }
64
65 bool ModuleBase_WidgetEditor::focusTo()
66 {
67   // We can not launch here modal process for value editing because 
68   // it can be called on other focusOutWidget event and will block it
69   QTimer::singleShot(1, this, SLOT(showPopupEditor()));
70   return true;
71 }
72
73 void ModuleBase_WidgetEditor::showPopupEditor()
74 {
75   // White while all events will be processed
76   QApplication::processEvents();
77   double aValue = mySpinBox->value();
78   bool isDone;
79   aValue = editedValue(aValue, isDone);
80
81   if (isDone) {
82     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
83   }
84   emit valuesChanged();
85   emit focusOutWidget(this);
86 }
87
88 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
89                                                const std::string theAttribute)
90 {
91   DataPtr aData = theFeature->data();
92   AttributeDoublePtr aRef = aData->real(theAttribute);
93   double aValue = aRef->value();
94
95   bool isDone;
96   aValue = editedValue(aValue, isDone);
97   if (isDone)
98     aRef->setValue(aValue);
99 }