+// 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_ParamSpinBox.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 <GeomDataAPI_Point2D.h>
-#include <QWidget>
+#include <QApplication>
#include <QLineEdit>
-#include <QTimer>
+#include <QMenu>
+#include <QWidget>
+#include <QWidgetAction>
+#include <QRegExp>
+#include <QRegExpValidator>
+#include <QDesktopWidget>
#include <QDialog>
#include <QLayout>
ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
- const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData), myValue(0)
-{
-}
-
-ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent, const std::string& theAttribute)
-: ModuleBase_ModelWidget(theParent, 0), myValue(0)
+ const Config_WidgetAPI* theData,
+ const std::string& theParentId)
+ : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
{
- this->setAttributeID(theAttribute);
}
ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor()
{
}
-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)
-{
- boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
- AttributeDoublePtr aRef = aData->real(attributeID());
-
- myValue = aRef->value();
- return true;
-}
-
-void ModuleBase_WidgetEditor::focusTo()
+void editedValue(double& outValue, QString& outText)
{
- QPoint aPoint = QCursor::pos();
-
- QDialog aDlg;
- aDlg.setWindowFlags(Qt::FramelessWindowHint);
+ QDialog aDlg(QApplication::desktop(), Qt::Popup/* | Qt::FramelessWindowHint*/);
QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
- aLay->setContentsMargins(0,0,0,0);
-
- QLineEdit* aEditor = new QLineEdit(QString::number(myValue), &aDlg);
- connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
- aLay->addWidget(aEditor);
+ aLay->setContentsMargins(2, 2, 2, 2);
- aDlg.move(aPoint);
- int aRes = aDlg.exec();
+ ModuleBase_ParamSpinBox* aEditor = new ModuleBase_ParamSpinBox(&aDlg);
+ aEditor->setMinimum(0);
+ aEditor->setMaximum(DBL_MAX);
+ if (outText.isEmpty())
+ aEditor->setValue(outValue);
+ else
+ aEditor->setText(outText);
- if (aRes == QDialog::Accepted)
- myValue = aEditor->text().toDouble();
+ aLay->addWidget(aEditor);
- emit valuesChanged();
- emit focusOutWidget(this);
+ aEditor->setFocus();
+ aEditor->selectAll();
+ QObject::connect(aEditor, SIGNAL(editingFinished()), &aDlg, SLOT(accept()));
+
+ aDlg.move(QCursor::pos());
+ aDlg.exec();
+ outText = aEditor->text();
+ bool isDouble;
+ double aValue = outText.toDouble(&isDouble);
+ if (isDouble) {
+ outValue = aValue;
+ outText = ""; // return empty string, if it's can be converted to a double
+ }
}
-QWidget* ModuleBase_WidgetEditor::getControl() const
+bool ModuleBase_WidgetEditor::focusTo()
{
- return 0;
-}
+ // nds: it seems, that the timer is not necessary anymore
-QList<QWidget*> ModuleBase_WidgetEditor::getControls() const
-{
- QList<QWidget*> aControls;
- return aControls;
+ // 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()));
+
+ showPopupEditor();
+
+ return true;
}
-void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature, const std::string theAttribute)
+void ModuleBase_WidgetEditor::showPopupEditor()
{
- ModuleBase_WidgetEditor anEditor(0, theAttribute);
-
- anEditor.restoreValue(theFeature);
- anEditor.focusTo();
- anEditor.storeValue(theFeature);
+ // we need to emit the focus in event manually in order to save the widget as an active
+ // in the property panel before the mouse leave event happens in the viewer. The module
+ // ask an active widget and change the feature visualization if the widget is not the current one.
+ emit focusInWidget(this);
+
+ // nds: it seems, that the envents processing is not necessary anymore
+ // White while all events will be processed
+ //QApplication::processEvents();
+ double aValue = mySpinBox->value();
+ QString aText;
+ if (mySpinBox->hasVariable())
+ aText = mySpinBox->text();
+
+ editedValue(aValue, aText);
+ if (aText.isEmpty()) {
+ ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
+ } else {
+ ModuleBase_Tools::setSpinText(mySpinBox, aText);
+ }
+ emit valuesChanged();
+ emit focusOutWidget(this);
}