X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_ParamSpinBox.cpp;h=e7836e656126d0378c8c3e5befcee38549d4de70;hb=be64ac512378281d54e4a73e8edb68ae43d0c5a7;hp=cfcd5e6e0c59d0a42ea27f36287999c779520ae3;hpb=3400f9977cb5bbc2c00d5d797b572acd083d1675;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp index cfcd5e6e0..e7836e656 100644 --- a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp +++ b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp @@ -1,37 +1,86 @@ -#include "ModuleBase_ParamSpinBox.h" +// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// -#include -#include -#include -#include +#include "ModuleBase_ParamSpinBox.h" #include -#include -#include +#include #include +#include +#include -#include +#include +#include +#include +#include -/*! - \class ModuleBase_ParamSpinBox - */ +#include +#include +#include -/*! - \brief Constructor. - Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value, - a step value of 1.0 and a precision of 2 decimal places. - The value is initially set to 0.00. +bool isVariableSymbol(const QChar& theChar) { + if (theChar.isLetterOrNumber()) + return true; + if (theChar == '_') + return true; + return false; +} - \param parent parent object - */ ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision) - : ModuleBase_DoubleSpinBox(theParent, thePrecision), - myAcceptVariables(true), - myHasVariables(false), - myDefaultValue(0.) + : QAbstractSpinBox(theParent), + myIsEquation(false), + myAcceptVariables(true), + mySingleStep(1), + myMinimum(-DBL_MAX), + myMaximum(DBL_MAX) +{ + myCompleter = new QCompleter(this); + myCompleter->setWidget(lineEdit()); + myCompleter->setCompletionMode(QCompleter::PopupCompletion); + + myCompleterModel = new QStringListModel(this); + myCompleter->setModel(myCompleterModel); + connect(myCompleter, SIGNAL(highlighted(const QString&)), + this, SLOT(insertCompletion(const QString&))); + + QAbstractItemView* aPopup = myCompleter->popup(); + aPopup->installEventFilter(this); + + // connectSignalsAndSlots(); + myEnabledBaseColor = palette().color(QPalette::Active, QPalette::Base); + connect(lineEdit(), SIGNAL(textChanged(const QString&)), + this, SLOT(onTextChanged(const QString&))); + + setLocale(QLocale::c()); + + myValidator = new QDoubleValidator(this); + myValidator->setLocale(locale()); + myValidator->setRange(myMinimum, myMaximum); + myValidator->setDecimals(thePrecision); +} + +void ModuleBase_ParamSpinBox::setCompletionList(QStringList& theList) { - connectSignalsAndSlots(); + theList.removeDuplicates(); + theList.sort(); + myCompleterModel->setStringList(theList); } /*! @@ -41,6 +90,7 @@ ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox() { } + /*! \brief Perform \a steps increment/decrement steps. @@ -52,94 +102,21 @@ ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox() */ void ModuleBase_ParamSpinBox::stepBy(int steps) { - QString str = text(); - QString pref = prefix(); - QString suff = suffix(); - - if (pref.length() && str.startsWith(pref)) - str = str.right(str.length() - pref.length()); - if (suff.length() && str.endsWith(suff)) - str = str.left(str.length() - suff.length()); - - QRegExp varNameMask("([a-z]|[A-Z]|_).*"); - if (varNameMask.exactMatch(str)) + if (hasVariable()) return; - ModuleBase_DoubleSpinBox::stepBy(steps); + double aVal = lineEdit()->text().toDouble(); + aVal += steps * mySingleStep; + setValue(aVal); + //QAbstractSpinBox::stepBy(steps); } -/*! - \brief Connect signals and slots. - */ -void ModuleBase_ParamSpinBox::connectSignalsAndSlots() +void ModuleBase_ParamSpinBox::onTextChanged(const QString& theText) { - connect(this, SIGNAL(editingFinished()), - this, SLOT(onEditingFinished())); - - connect(this, SIGNAL(valueChanged(const QString&)), - this, SLOT(onTextChanged(const QString&))); - - //connect(lineEdit(), SIGNAL(textChanged(const QString&)), - // this, SLOT(onTextChanged(const QString&))); - - //connect(lineEdit(), SIGNAL(textChanged(const QString&)), - // this, SIGNAL(textChanged(const QString&))); -} - -/*! - \brief This function is called when editing is finished. - */ -void ModuleBase_ParamSpinBox::onEditingFinished() -{ - if (myTextValue.isNull()) - myTextValue = text(); - - setText(myTextValue); + myIsEquation = hasVariable(theText); + emit textChanged(theText); } -/*! - \brief This function is called when value is changed. - */ -void ModuleBase_ParamSpinBox::onTextChanged(const QString& text) -{ - myTextValue = text; - - double value = 0; - if (isValid(text, value) == Acceptable) { - myCorrectValue = text; - myHasVariables = true; - } else { - myHasVariables = false; - } -} - -/*! - \brief Interpret text entered by the user as a value. - \param text text entered by the user - \return mapped value - \sa textFromValue() - */ -double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const -{ - double aValue = 0; - if (isValid(theText, aValue) == Acceptable) - return aValue; - - return defaultValue(); -} - -/*! - \brief This function is used by the spin box whenever it needs to display - the given value. - - \param val spin box value - \return text representation of the value - \sa valueFromText() - */ -QString ModuleBase_ParamSpinBox::textFromValue(double val) const -{ - return ModuleBase_DoubleSpinBox::textFromValue(val); -} /*! \brief This function is used to determine whether input is valid. @@ -149,51 +126,42 @@ QString ModuleBase_ParamSpinBox::textFromValue(double val) const */ QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const { - QValidator::State res = QValidator::Invalid; - - // Considering the input text as a variable name - // Applying Python identifier syntax: - // either a string starting with a letter, or a string starting with - // an underscore followed by at least one alphanumeric character - if (isAcceptVariables()) { - QRegExp varNameMask("(([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*)|" - "(_([a-z]|[A-Z]|[0-9])+([a-z]|[A-Z]|[0-9]|_)*)"); - if (varNameMask.exactMatch(str)) - res = QValidator::Acceptable; - - if (res == QValidator::Invalid) { - varNameMask.setPattern("_"); - if (varNameMask.exactMatch(str)) - res = QValidator::Intermediate; - } - } - // Trying to interpret the current input text as a numeric value - if (res == QValidator::Invalid) - res = ModuleBase_DoubleSpinBox::validate(str, pos); + if (!hasVariable(str)) { + /// If decimals = 0 do not accept '.' (interpret as int) + if ((myValidator->decimals() == 0) && str.endsWith('.')) + return QValidator::Invalid; + return myValidator->validate(str, pos); + } - return res; -} - -/*! - \brief This function is used to set a default value for this spinbox. - \param value default value - */ -void ModuleBase_ParamSpinBox::setDefaultValue(const double value) -{ - myDefaultValue = value; + return isAcceptVariables() ? QValidator::Acceptable : QValidator::Invalid; } /*! \brief This function is used to set a current value for this spinbox. \param value current value + + The new value is ignored if the spinbox has a variable. */ -void ModuleBase_ParamSpinBox::setValue(const double value) +void ModuleBase_ParamSpinBox::setValue(double value) { - ModuleBase_DoubleSpinBox::setValue(value); + myIsEquation = false; + double aVal = value; + if (aVal < myMinimum) + aVal = myMinimum; + else if (aVal > myMaximum) + aVal = myMaximum; + QString aText = (myValidator->decimals() == 0) ? QString::number((int)aVal) : + QString::number(aVal, 'g', decimals()); + lineEdit()->blockSignals(true); + lineEdit()->setText(aText); + lineEdit()->blockSignals(false); + emit textChanged(aText); +} - myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value); - myTextValue = myCorrectValue; +double ModuleBase_ParamSpinBox::value() const +{ + return lineEdit()->text().toDouble(); } /*! @@ -202,7 +170,17 @@ void ModuleBase_ParamSpinBox::setValue(const double value) */ void ModuleBase_ParamSpinBox::setText(const QString& value) { - lineEdit()->setText(value); + myIsEquation = hasVariable(value); + if (myAcceptVariables && myIsEquation) { + lineEdit()->setText(value); + emit textChanged(value); + } + else { + bool isConv = false; + double aVal = value.toDouble(&isConv); + if (isConv) + setValue(aVal); + } } /*! @@ -213,6 +191,9 @@ void ModuleBase_ParamSpinBox::setText(const QString& value) void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag) { myAcceptVariables = flag; + if ((!myAcceptVariables) && myIsEquation) { + setValue(0); + } } /*! @@ -223,89 +204,139 @@ bool ModuleBase_ParamSpinBox::isAcceptVariables() const return myAcceptVariables; } -bool ModuleBase_ParamSpinBox::hasVariables() const +bool ModuleBase_ParamSpinBox::hasVariable() const { - return myHasVariables; + return myIsEquation; } -/*! - \brief This function is used to determine whether input is valid. - \return validating operation result - */ -ModuleBase_ParamSpinBox::State ModuleBase_ParamSpinBox::isValid(const QString& theText, - double& theValue) const +bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const { - if (!findVariable(theText, theValue)) { - bool ok = false; - theValue = locale().toDouble(theText, &ok); - if (!ok) { - return NoVariable; + bool isDouble = false; + QLocale::c().toDouble(theText, &isDouble); + return !isDouble; +} + +void ModuleBase_ParamSpinBox::showCompletion(bool checkPrefix) +{ + myCompletePos = lineEdit()->cursorPosition(); + int aStart, aEnd; + QString aPrefix; + aPrefix = getPrefix(aStart, aEnd); + if (checkPrefix) { + if (aPrefix.length() > 0) { + myCompleter->setCompletionPrefix(aPrefix); + myCompleter->complete(); } + } else { + myCompleter->setCompletionPrefix(aPrefix); + myCompleter->complete(); } - if (!checkRange(theValue)) { - return Invalid; - } - - return Acceptable; } -/*! - \brief This function return a default acceptable value (commonly, 0.0). - \return default acceptable value - */ -double ModuleBase_ParamSpinBox::defaultValue() const +void ModuleBase_ParamSpinBox::keyReleaseEvent(QKeyEvent* e) { - if (minimum() > myDefaultValue || maximum() < myDefaultValue) - return minimum(); + QString aText; - return myDefaultValue; + switch (e->key()) { + case Qt::Key_Backspace: + if (myCompleter->popup()->isVisible()) { + myCompleter->popup()->hide(); + } + showCompletion(true); + break; + case Qt::Key_Return: + case Qt::Key_Enter: + if (myCompleter->popup()->isVisible()) { + myCompleter->popup()->hide(); + myIsEquation = true; + } + emit textChanged(lineEdit()->text()); + break; + case Qt::Key_Space: + if (e->modifiers() & Qt::ControlModifier) { + showCompletion(false); + } + break; default: + aText = e->text(); + if (aText.length() == 1) { + QChar aChar = aText.at(0); + if (isVariableSymbol(aChar)) { + showCompletion(true); + } + } + } + QAbstractSpinBox::keyReleaseEvent(e); } -/*! - \brief This function is used to check that string value lies within predefined range. - \return check status - */ -bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const +bool ModuleBase_ParamSpinBox::eventFilter(QObject* theObj, QEvent* theEvent) { - return theValue >= minimum() && theValue <= maximum(); + if (theEvent->type() == QEvent::KeyRelease) { + keyReleaseEvent((QKeyEvent*)theEvent); + } + return QAbstractSpinBox::eventFilter(theObj, theEvent); } -/*! - \brief This function is used to determine whether input is a variable name and to get its value. - \return status of search operation - */ -bool ModuleBase_ParamSpinBox::findVariable(const QString& theName, - double& outValue) const -{ - SessionPtr aSession = ModelAPI_Session::get(); - DocumentPtr aDocument = aSession->activeDocument(); - ObjectPtr aParamObj = aDocument->objectByName(ModelAPI_ResultParameter::group(), - theName.toStdString()); - ResultParameterPtr aParam = std::dynamic_pointer_cast(aParamObj); - if(!aParam.get()) - return false; - AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); - outValue = aValueAttribute->value(); - return true; +QString ModuleBase_ParamSpinBox::getPrefix(int& theStart, int& theEnd) const +{ + QString aPrefix; + QString aText = lineEdit()->text(); + theStart = theEnd = myCompletePos; + const int aLen = aText.length(); + if (aLen > 0) { + if (myCompletePos > 0) { + int aLastChar = myCompletePos - 1; + QChar aChar = aText.at(aLastChar); + while (isVariableSymbol(aChar)) { + aPrefix.prepend(aText.at(aLastChar)); + aLastChar--; + if (aLastChar < 0) + break; + aChar = aText.at(aLastChar); + } + theStart = aLastChar + 1; + } + if (myCompletePos < aLen) { + int aLastChar = myCompletePos; + QChar aChar = aText.at(aLastChar); + while (isVariableSymbol(aChar)) { + aPrefix.append(aText.at(aLastChar)); + aLastChar++; + if (aLastChar >= aLen) + break; + aChar = aText.at(aLastChar); + } + theEnd = aLastChar; + } + } + return aPrefix; } -/*! - \brief This function is called when the spinbox recieves key press event. - */ -void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e) + +void ModuleBase_ParamSpinBox::insertCompletion(const QString& theText) { - if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { - QWidget::keyPressEvent(e); - } else { - ModuleBase_DoubleSpinBox::keyPressEvent(e); + QString aText = lineEdit()->text(); + int aStart, aEnd; + QString aPrefix = getPrefix(aStart, aEnd); + + QString aResult; + int aPrefLen = aPrefix.length(); + if (aPrefLen == 0) + aResult = aText.insert(myCompletePos, theText); + else { + aResult = aText.left(aStart) + theText + aText.right(aText.length() - aEnd); } + lineEdit()->setText(aResult); + myIsEquation = true; } -/*! - \brief This function is called when the spinbox recieves show event. - */ -void ModuleBase_ParamSpinBox::showEvent(QShowEvent*) + +void ModuleBase_ParamSpinBox::setValueEnabled(bool theEnable) { - setText(myTextValue); + setReadOnly(!theEnable); + + QPalette aPal = palette(); + aPal.setColor(QPalette::All, QPalette::Base, + theEnable ? myEnabledBaseColor : aPal.color(QPalette::Disabled, QPalette::Base)); + setPalette(aPal); }