+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
// File: ModuleBase_WidgetEditor.cpp
// Created: 25 Apr 2014
// Author: Natalia ERMOLAEVA
#include <ModuleBase_WidgetEditor.h>
+#include <ModuleBase_DoubleSpinBox.h>
+#include <ModuleBase_Tools.h>
#include <Config_Keywords.h>
#include <Config_WidgetAPI.h>
#include <Events_Loop.h>
-#include <Model_Events.h>
+#include <ModelAPI_Events.h>
#include <ModelAPI_Feature.h>
#include <ModelAPI_Data.h>
#include <QTimer>
#include <QDialog>
#include <QLayout>
+#include <QApplication>
ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
- const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData), myValue(0)
+ const Config_WidgetAPI* theData,
+ const std::string& theParentId)
+ : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
{
}
{
}
-bool ModuleBase_WidgetEditor::storeValue(FeaturePtr theFeature) const
-{
- DataPtr aData = theFeature->data();
- AttributeDoublePtr aReal = aData->real(attributeID());
- if (aReal->value() != myValue) {
- aReal->setValue(myValue);
- Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
- }
- 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());
-
- myValue = aRef->value();
- return true;
-}
-
-void ModuleBase_WidgetEditor::focusTo()
-{
- QPoint aPoint = QCursor::pos();
-
QDialog aDlg;
aDlg.setWindowFlags(Qt::FramelessWindowHint);
QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
- aLay->setContentsMargins(0,0,0,0);
+ ModuleBase_Tools::zeroMargins(aLay);
- QLineEdit* aEditor = new QLineEdit(QString::number(myValue), &aDlg);
- connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
+ QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
+ aEditor->setValidator(new QDoubleValidator(aEditor));
+ QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
aLay->addWidget(aEditor);
+ QPoint aPoint = QCursor::pos();
aDlg.move(aPoint);
- int aRes = aDlg.exec();
- if (aRes == QDialog::Accepted)
- myValue = aEditor->text().toDouble();
-
- emit valuesChanged();
- emit focusOutWidget(this);
+ 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;
+ // We can not launch here modal process for value editing because
+ // it can be called on other focusOutWidget event and will block it
+ QTimer::singleShot(1, this, SLOT(showPopupEditor()));
+ return true;
}
-QList<QWidget*> ModuleBase_WidgetEditor::getControls() const
+void ModuleBase_WidgetEditor::showPopupEditor()
{
- QList<QWidget*> aControls;
- return aControls;
+ // White while all events will be processed
+ QApplication::processEvents();
+ 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);
}
+void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
+ const std::string theAttribute)
+{
+ DataPtr aData = theFeature->data();
+ AttributeDoublePtr aRef = aData->real(theAttribute);
+ double aValue = aRef->value();
+
+ bool isDone;
+ aValue = editedValue(aValue, isDone);
+ if (isDone)
+ aRef->setValue(aValue);
+}