From: sbh Date: Fri, 24 Apr 2015 15:21:20 +0000 (+0300) Subject: Popup menu for constraints updated: no second window, accepts parameters X-Git-Tag: V_1.2.0~184 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=78ee886adcb26b842317a0d5fa1c452d84c6080c;p=modules%2Fshaper.git Popup menu for constraints updated: no second window, accepts parameters --- diff --git a/src/ModuleBase/ModuleBase_Tools.cpp b/src/ModuleBase/ModuleBase_Tools.cpp index 1d87b4ae2..b42db0d7f 100644 --- a/src/ModuleBase/ModuleBase_Tools.cpp +++ b/src/ModuleBase/ModuleBase_Tools.cpp @@ -5,6 +5,7 @@ // Author: Vitaly Smetannikov #include "ModuleBase_Tools.h" +#include #include #include @@ -107,6 +108,13 @@ QPixmap lighter(const QString& theIcon, const int theLighterValue) 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); diff --git a/src/ModuleBase/ModuleBase_Tools.h b/src/ModuleBase/ModuleBase_Tools.h index 75c6b95fe..7bb5ababf 100644 --- a/src/ModuleBase/ModuleBase_Tools.h +++ b/src/ModuleBase/ModuleBase_Tools.h @@ -17,6 +17,7 @@ class QWidget; class QLayout; class QDoubleSpinBox; +class ModuleBase_ParamSpinBox; namespace ModuleBase_Tools { @@ -60,6 +61,11 @@ MODULEBASE_EXPORT QPixmap lighter(const QString& theIcon, const int theLighterVa /// \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 diff --git a/src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp b/src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp index 9e222e986..b7faf46d6 100644 --- a/src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp +++ b/src/ModuleBase/ModuleBase_WidgetDoubleValue.cpp @@ -106,7 +106,7 @@ void ModuleBase_WidgetDoubleValue::reset() // 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(); } } @@ -134,9 +134,7 @@ bool ModuleBase_WidgetDoubleValue::restoreValue() 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()); } diff --git a/src/ModuleBase/ModuleBase_WidgetEditor.cpp b/src/ModuleBase/ModuleBase_WidgetEditor.cpp index aa612eb28..29e07a1f3 100644 --- a/src/ModuleBase/ModuleBase_WidgetEditor.cpp +++ b/src/ModuleBase/ModuleBase_WidgetEditor.cpp @@ -21,12 +21,13 @@ #include -#include -#include -//#include -#include -#include #include +#include +#include +#include +#include +#include +#include ModuleBase_WidgetEditor::ModuleBase_WidgetEditor(QWidget* theParent, const Config_WidgetAPI* theData, @@ -39,27 +40,31 @@ ModuleBase_WidgetEditor::~ModuleBase_WidgetEditor() { } -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() @@ -86,25 +91,13 @@ void ModuleBase_WidgetEditor::showPopupEditor() // 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); -} diff --git a/src/ModuleBase/ModuleBase_WidgetEditor.h b/src/ModuleBase/ModuleBase_WidgetEditor.h index c992e60f9..8bdf6f9e1 100644 --- a/src/ModuleBase/ModuleBase_WidgetEditor.h +++ b/src/ModuleBase/ModuleBase_WidgetEditor.h @@ -43,11 +43,6 @@ Q_OBJECT /// \return the state whether the widget can accept the focus virtual bool focusTo(); - /// Creates an editor for the real value and set the new value to the feature - /// \param theFeature the model feature - /// \param theAttribute the feature attribute - static void editFeatureValue(FeaturePtr theFeature, const std::string theAttribute); - private slots: /// Shous popup window under cursor for data editing void showPopupEditor();