Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetEditor.cpp
index 84b63cf2835e12c3ccd399b6cd76a872a158983c..dbbe9fd5dbd09d4d8e575a3b8ce5aea6132db054 100644 (file)
 
 #include <QWidget>
 #include <QLineEdit>
+#include <QTimer>
+#include <QDialog>
+#include <QLayout>
+#include <QDoubleSpinBox>
 
 ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
                                                  const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData)
+: ModuleBase_WidgetDoubleValue(theParent, theData)
 {
-  myEditor = new QLineEdit(0);
-  myEditor->setWindowFlags(Qt::ToolTip);
-  myEditor->setFocusPolicy(Qt::StrongFocus);
-
-  connect(myEditor, SIGNAL(returnPressed()), this, SLOT(onStopEditing()));
-  connect(myEditor, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesChanged()));
 }
 
-ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
+ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent, const std::string& theAttribute)
+: ModuleBase_WidgetDoubleValue(theParent, 0)
 {
-  delete myEditor;
+  setAttributeID(theAttribute);
 }
 
-bool ModuleBase_WidgetEditor::storeValue(FeaturePtr theFeature) const
+ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
 {
-  DataPtr aData = theFeature->data();
-  AttributeDoublePtr aReal = aData->real(attributeID());
-  bool isOk;
-  double aValue = myEditor->text().toDouble(&isOk);
-  if (isOk && aReal->value() != aValue) {
-    //ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
-    //bool isBlocked = that->blockSignals(true);
-    aReal->setValue(aValue);
-    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
-    //that->blockSignals(isBlocked);
-  }
-  return true;
 }
 
-bool ModuleBase_WidgetEditor::restoreValue(FeaturePtr theFeature)
+double editedValue(double theValue, bool& isDone)
 {
-  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
-  AttributeDoublePtr aRef = aData->real(attributeID());
+  QDialog aDlg;
+  aDlg.setWindowFlags(Qt::FramelessWindowHint);
+  QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
+  aLay->setContentsMargins(0,0,0,0);
 
-  //bool isBlocked = this->blockSignals(true);
-  myEditor->setText(QString::number(aRef->value()));
-  //this->blockSignals(isBlocked);
-  return true;
-}
+  QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
+  QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
+  aLay->addWidget(aEditor);
 
-void ModuleBase_WidgetEditor::focusTo()
-{
   QPoint aPoint = QCursor::pos();
+  aDlg.move(aPoint);
 
-  myEditor->move(aPoint);
-  myEditor->show();
-
-  myEditor->selectAll();
-  myEditor->setFocus();
+  isDone = aDlg.exec() == QDialog::Accepted;
+  double aValue = theValue;
+  if (isDone)
+    aValue = aEditor->text().toDouble();
+  return aValue;
 }
 
-QWidget* ModuleBase_WidgetEditor::getControl() const
+bool ModuleBase_WidgetEditor::focusTo()
 {
-  return 0;
-}
+  double aValue = mySpinBox->value();
+  bool isDone;
+  aValue = editedValue(aValue, isDone);
+
+  if (isDone) {
+    bool isBlocked = mySpinBox->blockSignals(true);
+    mySpinBox->setValue(aValue);
+    mySpinBox->blockSignals(isBlocked);
+  }
+  emit valuesChanged();
+  emit focusOutWidget(this);
 
-QList<QWidget*> ModuleBase_WidgetEditor::getControls() const
-{
-  QList<QWidget*> aControls;
-  return aControls;
+  return false;
 }
 
-void ModuleBase_WidgetEditor::onStopEditing()
+void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature, const std::string theAttribute)
 {
-  myEditor->hide();
-  emit focusOutWidget(this);
+  DataPtr aData = theFeature->data();
+  AttributeDoublePtr aRef = aData->real(theAttribute);
+  double aValue = aRef->value();
+
+  bool isDone;
+  aValue = editedValue(aValue, isDone);
+  if (isDone)
+    aRef->setValue(aValue);
 }