-#include "ModuleBase_ParamSpinBox.h"
+// Copyright (C) 2014-2017 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<mailto:webmaster.salome@opencascade.com>
+//
-#include <ModelAPI_Session.h>
-#include <ModelAPI_Document.h>
-#include <ModelAPI_ResultParameter.h>
-#include <ModelAPI_AttributeDouble.h>
+#include "ModuleBase_ParamSpinBox.h"
#include <QKeyEvent>
-#include <QLineEdit>
-#include <QToolTip>
+#include <QLocale>
#include <QRegExp>
+#include <QToolTip>
+#include <QApplication>
-#include <string>
+#include <QStringListModel>
+#include <QCompleter>
+#include <QAbstractItemView>
+#include <QShortcut>
-/*!
- \class ModuleBase_ParamSpinBox
- */
+#include <string>
+#include <iostream>
+#include <cfloat>
-/*!
- \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),
+ myPrecision(thePrecision),
+ myIsEquation(false),
+ myAcceptVariables(true),
+ mySingleStep(1),
+ myMinimum(DBL_MIN),
+ myMaximum(DBL_MAX)
{
- 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(myPrecision);
+}
+
+void ModuleBase_ParamSpinBox::setCompletionList(QStringList& theList)
+{
+ theList.removeDuplicates();
+ theList.sort();
+ myCompleterModel->setStringList(theList);
}
/*!
{
}
+
/*!
\brief Perform \a steps increment/decrement steps.
*/
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);
-}
-
-/*!
- \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;
- }
+ myIsEquation = hasVariable(theText);
+ emit textChanged(theText);
}
-/*!
- \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.
*/
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-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;
- }
- }
-
// Trying to interpret the current input text as a numeric value
- if (res == QValidator::Invalid)
- res = ModuleBase_DoubleSpinBox::validate(str, pos);
-
- return res;
-}
+ 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);
+ }
-/*!
- \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 = QString::number(aVal, 'g', decimals());
+ lineEdit()->setText(aText);
+ emit textChanged(aText);
+}
- myCorrectValue = ModuleBase_DoubleSpinBox::textFromValue(value);
- myTextValue = myCorrectValue;
+double ModuleBase_ParamSpinBox::value() const
+{
+ return lineEdit()->text().toDouble();
}
/*!
*/
void ModuleBase_ParamSpinBox::setText(const QString& value)
{
- lineEdit()->setText(value);
+ myIsEquation = hasVariable(value);
+ if (myAcceptVariables && myIsEquation) {
+ lineEdit()->setText(value);
+ emit textChanged(value);
+ }
}
/*!
void ModuleBase_ParamSpinBox::setAcceptVariables(const bool flag)
{
myAcceptVariables = flag;
+ if ((!myAcceptVariables) && myIsEquation) {
+ setValue(0);
+ }
}
/*!
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;
- }
- }
- if (!checkRange(theValue)) {
- return Invalid;
- }
-
- return Acceptable;
+ bool isDouble = false;
+ QLocale::c().toDouble(theText, &isDouble);
+ return !isDouble;
}
-/*!
- \brief This function return a default acceptable value (commonly, 0.0).
- \return default acceptable value
- */
-double ModuleBase_ParamSpinBox::defaultValue() const
+void ModuleBase_ParamSpinBox::showCompletion()
{
- if (minimum() > myDefaultValue || maximum() < myDefaultValue)
- return minimum();
-
- return myDefaultValue;
+ myCompletePos = lineEdit()->cursorPosition();
+ int aStart, aEnd;
+ QString aPrefix;
+ aPrefix = getPrefix(aStart, aEnd);
+ if (aPrefix.length() > 0) {
+ myCompleter->setCompletionPrefix(aPrefix);
+ myCompleter->complete();
+ }
}
-/*!
- \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:
+ showCompletion();
+ break;
+ case Qt::Key_Return:
+ case Qt::Key_Enter:
+ if (myCompleter->popup()->isVisible()) {
+ myCompleter->popup()->hide();
+ myIsEquation = true;
+ }
+ emit textChanged(lineEdit()->text());
+ break;
+ default:
+ aText = e->text();
+ if (aText.length() == 1) {
+ QChar aChar = aText.at(0);
+ if (isVariableSymbol(aChar)) {
+ showCompletion();
+ }
+ }
+ }
+ 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)
{
+ if (theEvent->type() == QEvent::KeyRelease) {
+ keyReleaseEvent((QKeyEvent*)theEvent);
+ }
+ return QAbstractSpinBox::eventFilter(theObj, theEvent);
+}
- SessionPtr aSession = ModelAPI_Session::get();
- DocumentPtr aDocument = aSession->activeDocument();
- ObjectPtr aParamObj = aDocument->objectByName(ModelAPI_ResultParameter::group(),
- theName.toStdString());
- ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(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);
}