X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_ParamSpinBox.cpp;h=9c9cc761329f1a34dc7c03becabc4e3864fabac0;hb=06e7f5859095193fc7f498bd89a7d28009794f53;hp=4438684664dc273d3b71b17d9c12aaaed609cfd8;hpb=a74cad92cb1932ada8c74ac7f410b398415c7443;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp index 443868466..9c9cc7613 100644 --- a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp +++ b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 CEA/DEN, EDF R&D +// 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 @@ -12,21 +12,13 @@ // // 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 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // -// See http://www.salome-platform.org/ or -// email : webmaster.salome@opencascade.com +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // #include "ModuleBase_ParamSpinBox.h" -#include -#include -#include -#include -#include -#include - #include #include #include @@ -40,16 +32,24 @@ #include #include +#include + + +bool isVariableSymbol(const QChar& theChar) { + if (theChar.isLetterOrNumber()) + return true; + if (theChar == '_') + return true; + return false; +} ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrecision) : QAbstractSpinBox(theParent), - myPrecision(thePrecision), myIsEquation(false), myAcceptVariables(true), - myDecimals(3), - mySingleStep(1), - myMinimum(DBL_MIN), - myMaximum(DBL_MAX) + myMinimum(-DBL_MAX), + myMaximum(DBL_MAX), + mySingleStep(1) { myCompleter = new QCompleter(this); myCompleter->setWidget(lineEdit()); @@ -57,27 +57,41 @@ ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrec myCompleterModel = new QStringListModel(this); myCompleter->setModel(myCompleterModel); - // Use sorted model to accelerate completion (QCompleter will use binary search) - myCompleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel); - myCompleter->setCaseSensitivity(Qt::CaseInsensitive); 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.sort(); 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); } /*! @@ -87,6 +101,7 @@ ModuleBase_ParamSpinBox::~ModuleBase_ParamSpinBox() { } + /*! \brief Perform \a steps increment/decrement steps. @@ -104,7 +119,7 @@ void ModuleBase_ParamSpinBox::stepBy(int steps) double aVal = lineEdit()->text().toDouble(); aVal += steps * mySingleStep; setValue(aVal); - QAbstractSpinBox::stepBy(steps); + //QAbstractSpinBox::stepBy(steps); } void ModuleBase_ParamSpinBox::onTextChanged(const QString& theText) @@ -114,38 +129,6 @@ void ModuleBase_ParamSpinBox::onTextChanged(const QString& theText) } -///*! -// \brief Connect signals and slots. -// */ -//void ModuleBase_ParamSpinBox::connectSignalsAndSlots() -//{ -// connect(this, SIGNAL(valueChanged(const QString&)), -// this, SLOT(onTextChanged(const QString&))); -//} -// -//void ModuleBase_ParamSpinBox::onTextChanged(const QString& text) -//{ -// myTextValue = text; -// emit textChanged(text); -//} -// -//double ModuleBase_ParamSpinBox::valueFromText(const QString& theText) const -//{ -// if (!hasVariable(theText)) -// return ModuleBase_DoubleSpinBox::valueFromText(theText); -// -// // small hack: return hash of the string to initiate valuesChanged signal -// return qHash(theText); -//} -// -//QString ModuleBase_ParamSpinBox::textFromValue (double theValue) const -//{ -// if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)){ -// return myTextValue; -// } -// return ModuleBase_DoubleSpinBox::textFromValue(theValue); -//} - /*! \brief This function is used to determine whether input is valid. \param str currently entered value @@ -155,8 +138,12 @@ void ModuleBase_ParamSpinBox::onTextChanged(const QString& theText) QValidator::State ModuleBase_ParamSpinBox::validate(QString& str, int& pos) const { // Trying to interpret the current input text as a numeric value - if (!hasVariable(str)) + 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 isAcceptVariables() ? QValidator::Acceptable : QValidator::Invalid; } @@ -175,16 +162,16 @@ void ModuleBase_ParamSpinBox::setValue(double value) aVal = myMinimum; else if (aVal > myMaximum) aVal = myMaximum; - QString aText = QString::number(aVal, 'g', myDecimals); + 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); } double ModuleBase_ParamSpinBox::value() const { - //if (myIsEquation) { - - //} return lineEdit()->text().toDouble(); } @@ -199,6 +186,12 @@ void ModuleBase_ParamSpinBox::setText(const QString& value) lineEdit()->setText(value); emit textChanged(value); } + else { + bool isConv = false; + double aVal = value.toDouble(&isConv); + if (isConv) + setValue(aVal); + } } /*! @@ -234,53 +227,34 @@ bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const 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 -//{ -// if (hasVariable() && !findVariable(theText, theValue)) { -// bool ok = false; -// theValue = locale().toDouble(theText, &ok); -// if (!ok) { -// return NoVariable; -// } -// } -// 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 -//{ -// return theValue >= minimum() && theValue <= maximum(); -//} -// -///*! -// \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 -//{ -// ResultParameterPtr aParam; -// return ModelAPI_Tools::findVariable(FeaturePtr(), theName.toStdString(), outValue, aParam); -//} +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(); + } +} -/*! - \brief This function is called when the spinbox receives key press event. - */ void ModuleBase_ParamSpinBox::keyReleaseEvent(QKeyEvent* e) { + 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()) { @@ -288,21 +262,32 @@ void ModuleBase_ParamSpinBox::keyReleaseEvent(QKeyEvent* e) myIsEquation = true; } emit textChanged(lineEdit()->text()); - return; + break; case Qt::Key_Space: if (e->modifiers() & Qt::ControlModifier) { - myCompletePos = lineEdit()->cursorPosition(); - int aStart, aEnd; - QString aPrefix = getPrefix(aStart, aEnd); - myCompleter->setCompletionPrefix(aPrefix); - myCompleter->complete(); + showCompletion(false); + } + break; default: + aText = e->text(); + if (aText.length() == 1) { + QChar aChar = aText.at(0); + if (isVariableSymbol(aChar)) { + showCompletion(true); + } } - break; - default: - QAbstractSpinBox::keyReleaseEvent(e); } + QAbstractSpinBox::keyReleaseEvent(e); } +bool ModuleBase_ParamSpinBox::eventFilter(QObject* theObj, QEvent* theEvent) +{ + if (theEvent->type() == QEvent::KeyRelease) { + keyReleaseEvent((QKeyEvent*)theEvent); + } + return QAbstractSpinBox::eventFilter(theObj, theEvent); +} + + QString ModuleBase_ParamSpinBox::getPrefix(int& theStart, int& theEnd) const { QString aPrefix; @@ -313,7 +298,7 @@ QString ModuleBase_ParamSpinBox::getPrefix(int& theStart, int& theEnd) const if (myCompletePos > 0) { int aLastChar = myCompletePos - 1; QChar aChar = aText.at(aLastChar); - while (aChar.isLetter() || aChar.isDigit()) { + while (isVariableSymbol(aChar)) { aPrefix.prepend(aText.at(aLastChar)); aLastChar--; if (aLastChar < 0) @@ -325,7 +310,7 @@ QString ModuleBase_ParamSpinBox::getPrefix(int& theStart, int& theEnd) const if (myCompletePos < aLen) { int aLastChar = myCompletePos; QChar aChar = aText.at(aLastChar); - while (aChar.isLetter() || aChar.isDigit()) { + while (isVariableSymbol(aChar)) { aPrefix.append(aText.at(aLastChar)); aLastChar++; if (aLastChar >= aLen) @@ -354,25 +339,9 @@ void ModuleBase_ParamSpinBox::insertCompletion(const QString& theText) } lineEdit()->setText(aResult); myIsEquation = true; - - qDebug("### aPos=%i", myCompletePos); - qDebug("### text=%s", qPrintable(aText)); - qDebug("### prefix=%s", qPrintable(aPrefix)); - qDebug("### result=%s", qPrintable(aResult)); } -///*! -// \brief This function is called when the spinbox receives show event. -// */ -//void ModuleBase_ParamSpinBox::showEvent(QShowEvent* theEvent) -//{ -// ModuleBase_DoubleSpinBox::showEvent(theEvent); -// if ((!myTextValue.isEmpty()) && hasVariable(myTextValue)) { -// setText(myTextValue); -// } -//} - void ModuleBase_ParamSpinBox::setValueEnabled(bool theEnable) { setReadOnly(!theEnable);