Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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   // nds: it seems, that the timer is not necessary anymore
68
69   // We can not launch here modal process for value editing because 
70   // it can be called on other focusOutWidget event and will block it
71   //QTimer::singleShot(1, this, SLOT(showPopupEditor()));
72
73   showPopupEditor();
74
75   return true;
76 }
77
78 void ModuleBase_WidgetEditor::showPopupEditor()
79 {
80   // we need to emit the focus in event manually in order to save the widget as an active
81   // in the property panel before the mouse leave event happens in the viewer. The module
82   // ask an active widget and change the feature visualization if the widget is not the current one.
83   emit focusInWidget(this);
84
85   // nds: it seems, that the envents processing is not necessary anymore
86   // White while all events will be processed
87   //QApplication::processEvents();
88   double aValue = mySpinBox->value();
89   bool isDone;
90   aValue = editedValue(aValue, isDone);
91
92   if (isDone) {
93     ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
94   }
95   emit valuesChanged();
96   emit focusOutWidget(this);
97 }
98
99 void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
100                                                const std::string theAttribute)
101 {
102   DataPtr aData = theFeature->data();
103   AttributeDoublePtr aRef = aData->real(theAttribute);
104   double aValue = aRef->value();
105
106   bool isDone;
107   aValue = editedValue(aValue, isDone);
108   if (isDone)
109     aRef->setValue(aValue);
110 }