X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_ParamSpinBox.cpp;h=e7836e656126d0378c8c3e5befcee38549d4de70;hb=be64ac512378281d54e4a73e8edb68ae43d0c5a7;hp=992e9dd26d1636192c32c5d6e7ea6911beca5dbc;hpb=5e0fdd82190d9afb274cb2449edb76f6b6664700;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_ParamSpinBox.cpp b/src/ModuleBase/ModuleBase_ParamSpinBox.cpp index 992e9dd26..e7836e656 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-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 @@ -12,10 +12,9 @@ // // 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" @@ -35,13 +34,21 @@ #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), mySingleStep(1), - myMinimum(DBL_MIN), + myMinimum(-DBL_MAX), myMaximum(DBL_MAX) { myCompleter = new QCompleter(this); @@ -50,12 +57,12 @@ 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&)), @@ -66,13 +73,13 @@ ModuleBase_ParamSpinBox::ModuleBase_ParamSpinBox(QWidget* theParent, int thePrec myValidator = new QDoubleValidator(this); myValidator->setLocale(locale()); myValidator->setRange(myMinimum, myMaximum); - myValidator->setDecimals(3); + myValidator->setDecimals(thePrecision); } void ModuleBase_ParamSpinBox::setCompletionList(QStringList& theList) { - theList.sort(); theList.removeDuplicates(); + theList.sort(); myCompleterModel->setStringList(theList); } @@ -101,7 +108,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) @@ -144,8 +151,11 @@ void ModuleBase_ParamSpinBox::setValue(double value) aVal = myMinimum; else if (aVal > myMaximum) aVal = myMaximum; - QString aText = QString::number(aVal, 'g', decimals()); + 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); } @@ -165,6 +175,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); + } } /*! @@ -200,31 +216,64 @@ bool ModuleBase_ParamSpinBox::hasVariable(const QString& theText) const 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(); + } +} + 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()) { myCompleter->popup()->hide(); 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: - QAbstractSpinBox::keyReleaseEvent(e); + break; default: + aText = e->text(); + if (aText.length() == 1) { + QChar aChar = aText.at(0); + if (isVariableSymbol(aChar)) { + showCompletion(true); + } + } + } + QAbstractSpinBox::keyReleaseEvent(e); +} + +bool ModuleBase_ParamSpinBox::eventFilter(QObject* theObj, QEvent* theEvent) +{ + if (theEvent->type() == QEvent::KeyRelease) { + keyReleaseEvent((QKeyEvent*)theEvent); } + return QAbstractSpinBox::eventFilter(theObj, theEvent); } @@ -238,7 +287,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) @@ -250,7 +299,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)