X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_ParamSpinBox.cpp;h=9c9cc761329f1a34dc7c03becabc4e3864fabac0;hb=06e7f5859095193fc7f498bd89a7d28009794f53;hp=925b1cc94c8508545041d6b63677ae962331aa76;hpb=9e884586cb9e6221e2eff8f52c227332c289130c;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp index 925b1cc94..9c9cc7613 100644 --- a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp +++ b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp @@ -1,36 +1,97 @@ -#include "ModuleBase_ParamSpinBox.h" +// Copyright (C) 2014-2023 CEA, EDF +// +// 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 +#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) + : QAbstractSpinBox(theParent), + myIsEquation(false), + myAcceptVariables(true), + myMinimum(-DBL_MAX), + myMaximum(DBL_MAX), + mySingleStep(1) { - connectSignalsAndSlots(); + 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) +{ + theList.removeDuplicates(); + theList.sort(); + myCompleterModel->setStringList(theList); + + QAbstractItemView* aPopup = myCompleter->popup(); + QFontMetrics aMetric = aPopup->fontMetrics(); + int aWidth = 0; + QRect aRect; + foreach(QString aStr, theList) { + aRect = aMetric.boundingRect(aStr); + if (aRect.width() > aWidth) + aWidth = aRect.width(); + } + aPopup->setMinimumWidth(aWidth + 25); } /*! @@ -40,6 +101,7 @@ ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox() { } + /*! \brief Perform \a steps increment/decrement steps. @@ -54,66 +116,18 @@ void ModuleBase_ParamSpinBox::stepBy(int steps) 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&))); + myIsEquation = hasVariable(theText); + emit textChanged(theText); } -/*! - \brief This function is called when editing is finished. - */ -void ModuleBase_ParamSpinBox::onEditingFinished() -{ - if (myTextValue.isNull()) - myTextValue = text(); - - setText(myTextValue); -} - -/*! - \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; - } -} - -/*! - \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 -{ - if (!hasVariable(theText)) - return ModuleBase_DoubleSpinBox::valueFromText(theText); - - double aValue = 0; - findVariable(theText, aValue); - return aValue; -} /*! \brief This function is used to determine whether input is valid. @@ -124,39 +138,41 @@ double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const { // Trying to interpret the current input text as a numeric value - if (!hasVariable(str)) - return ModuleBase_DoubleSpinBox::validate(str, pos); - - 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-zA-Z][a-zA-Z0-9_]*"); - if (varNameMask.exactMatch(str)) - res = QValidator::Acceptable; - - if (res == QValidator::Invalid) { - varNameMask.setPattern("_"); - if (varNameMask.exactMatch(str)) - res = QValidator::Intermediate; - } + 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; + + 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(); } /*! @@ -165,7 +181,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); + } } /*! @@ -176,6 +202,9 @@ void ModuleBase_ParamSpinBox::setText(const QString& value) void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag) { myAcceptVariables = flag; + if ((!myAcceptVariables) && myIsEquation) { + setValue(0); + } } /*! @@ -188,75 +217,137 @@ bool ModuleBase_ParamSpinBox::isAcceptVariables() const bool ModuleBase_ParamSpinBox::hasVariable() const { - return hasVariable(text()); + return myIsEquation; } bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const { - QRegExp varNameMask("([a-z]|[A-Z]|_).*"); - return varNameMask.exactMatch(theText); + bool isDouble = false; + QLocale::c().toDouble(theText, &isDouble); + return !isDouble; } -/*! - \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 +void ModuleBase_ParamSpinBox::showCompletion(bool checkPrefix) { - if (hasVariable() && !findVariable(theText, theValue)) { - bool ok = false; - theValue = locale().toDouble(theText, &ok); - if (!ok) { - return NoVariable; + 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 is used to check that string value lies within predefined range. - \return check status - */ -bool ModuleBase_ParamSpinBox::checkRange(const double theValue) const +void ModuleBase_ParamSpinBox::keyReleaseEvent(QKeyEvent* e) { - return theValue >= minimum() && theValue <= maximum(); + QString aText; + + 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 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 +bool ModuleBase_ParamSpinBox::eventFilter(QObject* theObj, QEvent* theEvent) { - - return ModelAPI_Tools::findVariable(theName.toStdString(), outValue); + if (theEvent->type() == QEvent::KeyRelease) { + keyReleaseEvent((QKeyEvent*)theEvent); + } + return QAbstractSpinBox::eventFilter(theObj, theEvent); } -/*! - \brief This function is called when the spinbox recieves key press event. - */ -void ModuleBase_ParamSpinBox::keyPressEvent(QKeyEvent* e) + +QString ModuleBase_ParamSpinBox::getPrefix(int& theStart, int& theEnd) const { - if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { - QWidget::keyPressEvent(e); - } else { - ModuleBase_DoubleSpinBox::keyPressEvent(e); + 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 show event. - */ -void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent) + +void ModuleBase_ParamSpinBox::insertCompletion(const QString& theText) { - ModuleBase_DoubleSpinBox::showEvent(theEvent); - if (hasVariable(myTextValue)) { - setText(myTextValue); + 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; +} + + +void ModuleBase_ParamSpinBox::setValueEnabled(bool theEnable) +{ + setReadOnly(!theEnable); + + QPalette aPal = palette(); + aPal.setColor(QPalette::All, QPalette::Base, + theEnable ? myEnabledBaseColor : aPal.color(QPalette::Disabled, QPalette::Base)); + setPalette(aPal); }