// Author: Vitaly Smetannikov
#include "ModuleBase_Tools.h"
+#include <ModuleBase_ParamSpinBox.h>
#include <ModelAPI_Result.h>
#include <ModelAPI_Data.h>
return QPixmap::fromImage(aResult);
}
+void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
+{
+ bool isBlocked = theSpin->blockSignals(true);
+ theSpin->setText(theText);
+ theSpin->blockSignals(isBlocked);
+}
+
void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
{
bool isBlocked = theSpin->blockSignals(true);
class QWidget;
class QLayout;
class QDoubleSpinBox;
+class ModuleBase_ParamSpinBox;
namespace ModuleBase_Tools {
/// \param theValue a new value
MODULEBASE_EXPORT void setSpinValue(QDoubleSpinBox* theSpin, double theValue);
+/// Sets programmatically the value to the spin box without emitting any signals(e.g. valueChanged)
+/// \param theSpin an ModuleBase_ParamSpinBox that accepts text
+/// \param theText a new value
+MODULEBASE_EXPORT void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText);
+
/// Converts the object to the feature or a result and generate information string
/// \param theObj an object
/// \param isUseAttributesInfo a flag whether the attribute values information is used
// reset the value just if there is a default value definition in the XML definition
// if the double value can not be found by the default value, do nothing
if (isOk) {
- ModuleBase_Tools::setSpinValue(mySpinBox, isOk ? aDefValue : 0.0);
+ ModuleBase_Tools::setSpinValue(mySpinBox, aDefValue);
storeValueCustom();
}
}
AttributeDoublePtr aRef = aData->real(attributeID());
std::string aTextRepr = aRef->text();
if (!aTextRepr.empty()) {
- bool isBlocked = mySpinBox->blockSignals(true);
- mySpinBox->setText(QString::fromStdString(aTextRepr));
- mySpinBox->blockSignals(isBlocked);
+ ModuleBase_Tools::setSpinText(mySpinBox, QString::fromStdString(aTextRepr));
} else {
ModuleBase_Tools::setSpinValue(mySpinBox, aRef->value());
}
#include <GeomDataAPI_Point2D.h>
-#include <QWidget>
-#include <QLineEdit>
-//#include <QTimer>
-#include <QDialog>
-#include <QLayout>
#include <QApplication>
+#include <QLineEdit>
+#include <QMenu>
+#include <QWidget>
+#include <QWidgetAction>
+#include <QRegExp>
+#include <QRegExpValidator>
ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent,
const Config_WidgetAPI* theData,
{
}
-double editedValue(double theValue, bool& isDone)
+void editedValue(double& outValue, QString& outText)
{
- QDialog aDlg;
- aDlg.setWindowFlags(Qt::FramelessWindowHint);
- QHBoxLayout* aLay = new QHBoxLayout(&aDlg);
- ModuleBase_Tools::zeroMargins(aLay);
+ QMenu* aPopup = new QMenu();
+
+ QLineEdit* aEditor = new QLineEdit(QString::number(outValue), aPopup);
+ QWidgetAction* aLineEditAction = new QWidgetAction(aPopup);
+ aLineEditAction->setDefaultWidget(aEditor);
+ aPopup->addAction(aLineEditAction);
- QLineEdit* aEditor = new QLineEdit(QString::number(theValue), &aDlg);
+ aEditor->setFocus();
aEditor->selectAll();
- aEditor->setValidator(new QDoubleValidator(aEditor));
- QObject::connect(aEditor, SIGNAL(returnPressed()), &aDlg, SLOT(accept()));
- aLay->addWidget(aEditor);
-
- QPoint aPoint = QCursor::pos();
- aDlg.move(aPoint);
-
- isDone = aDlg.exec() == QDialog::Accepted;
- double aValue = theValue;
- if (isDone)
- aValue = aEditor->text().toDouble();
- return aValue;
+ QString anExpression("([0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)|([_a-zA-Z][a-zA-Z0-9_]*)");
+ aEditor->setValidator(new QRegExpValidator(QRegExp(anExpression), aEditor));
+ QObject::connect(aEditor, SIGNAL(returnPressed()), aLineEditAction, SIGNAL(triggered()));
+ QObject::connect(aLineEditAction, SIGNAL(triggered()), aPopup, SLOT(hide()));
+
+ QAction* aResult = aPopup->exec(QCursor::pos());
+ 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
+ }
+ aPopup->deleteLater();
}
bool ModuleBase_WidgetEditor::focusTo()
// White while all events will be processed
//QApplication::processEvents();
double aValue = mySpinBox->value();
- bool isDone;
- aValue = editedValue(aValue, isDone);
-
- if (isDone) {
+ QString aText;
+ editedValue(aValue, aText);
+ if (aText.isEmpty()) {
ModuleBase_Tools::setSpinValue(mySpinBox, aValue);
+ } else {
+ ModuleBase_Tools::setSpinText(mySpinBox, aText);
}
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);
-}