+// 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_ParamSpinBox.h>
#include <ModuleBase_Tools.h>
#include <Config_Keywords.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>
-#include <QApplication>
ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
const Config_WidgetAPI* theData,
const std::string& theParentId)
- : ModuleBase_WidgetDoubleValue(theParent, theData, theParentId)
+: ModuleBase_WidgetDoubleValue(theParent, theData, theParentId),
+ myIsKeyReleasedEmitted(false)
{
}
{
}
-double editedValue(double theValue, bool& isDone)
+void ModuleBase_WidgetEditor::editedValue(double& outValue, QString& outText)
{
- QDialog aDlg;
- aDlg.setWindowFlags(Qt::FramelessWindowHint);
+ QDialog aDlg(QApplication::desktop(), Qt::FramelessWindowHint);
QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
- ModuleBase_Tools::zeroMargins(aLay);
+ aLay->setContentsMargins(2, 2, 2, 2);
- QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
- aEditor->setValidator(new QDoubleValidator(aEditor));
- QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
- aLay->addWidget(aEditor);
+ ModuleBase_ParamSpinBox* anEditor = new ModuleBase_ParamSpinBox(&aDlg);
+ anEditor->enableKeyPressEvent(true);
+ if (!myIsEditing) {
+ connect(anEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SLOT(onKeyReleased(QKeyEvent*)));
+ }
+
+ anEditor->setMinimum(0);
+ anEditor->setMaximum(DBL_MAX);
+ if (outText.isEmpty())
+ anEditor->setValue(outValue);
+ else
+ anEditor->setText(outText);
+
+ aLay->addWidget(anEditor);
- QPoint aPoint = QCursor::pos();
- aDlg.move(aPoint);
+ ModuleBase_Tools::setFocus(anEditor, "ModuleBase_WidgetEditor::editedValue");
+ anEditor->selectAll();
+ QObject::connect(anEditor, SIGNAL(editingFinished()), &aDlg, SLOT(accept()));
- isDone = aDlg.exec() == QDialog::Accepted;
- double aValue = theValue;
- if (isDone)
- aValue = aEditor->text().toDouble();
- return aValue;
+ aDlg.move(QCursor::pos());
+ aDlg.exec();
+
+ if (!myIsEditing) {
+ disconnect(anEditor, SIGNAL(keyReleased(QKeyEvent*)), this, SLOT(onKeyReleased(QKeyEvent*)));
+ }
+
+ outText = anEditor->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
+ }
}
bool ModuleBase_WidgetEditor::focusTo()
{
+ // nds: it seems, that the timer is not necessary anymore
+
// 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()));
+ //QTimer::singleShot(1, this, SLOT(showPopupEditor()));
+
+ showPopupEditor();
+
return true;
}
void ModuleBase_WidgetEditor::showPopupEditor()
{
+ myIsKeyReleasedEmitted = false;
+
+ // 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();
+ //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);
+ 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);
+ // the focus leaves the control automatically by the Enter/Esc event
+ // it is processed in operation manager
+ //emit focusOutWidget(this);
+
+ if (myIsKeyReleasedEmitted)
+ emit enterClicked();
}
-void ModuleBase_WidgetEditor::editFeatureValue(FeaturePtr theFeature,
- const std::string theAttribute)
+void ModuleBase_WidgetEditor::onKeyReleased(QKeyEvent* theEvent)
{
- DataPtr aData = theFeature->data();
- AttributeDoublePtr aRef = aData->real(theAttribute);
- double aValue = aRef->value();
-
- bool isDone;
- aValue = editedValue(aValue, isDone);
- if (isDone)
- aRef->setValue(aValue);
+ myIsKeyReleasedEmitted = true;
}