Spreadsheet: Don't autocomplete when writing strings

After pull request https://github.com/FreeCAD/FreeCAD/pull/4215 it
doesn't make much sense helping the user to write expressions without
leading '=', as that content will be parsed as a string.

This change adjusts the behaviour in SpreadsheetGui's LineEdit to avoid
popping up the ExpressionCompleter when no leading equal sign is used.
This commit is contained in:
Benjamin Nauck
2021-02-13 02:19:17 +01:00
committed by wwmayer
parent 6eb89fe038
commit eb400fe249
3 changed files with 18 additions and 3 deletions

View File

@@ -372,6 +372,10 @@ void ExpressionCompleter::setNoProperty(bool enabled) {
static_cast<ExpressionCompleterModel*>(m)->setNoProperty(enabled);
}
void ExpressionCompleter::setRequireLeadingEqualSign(bool enabled) {
requireLeadingEqualSign = enabled;
}
QString ExpressionCompleter::pathFromIndex ( const QModelIndex & index ) const
{
auto m = model();
@@ -456,6 +460,12 @@ void ExpressionCompleter::slotUpdate(const QString & prefix, int pos)
// Compute start; if prefix starts with =, start parsing from offset 1.
int start = (prefix.size() > 0 && prefix.at(0) == QChar::fromLatin1('=')) ? 1 : 0;
if (requireLeadingEqualSign && start != 1) {
if (auto p = popup())
p->setVisible(false);
return;
}
std::string expression = Base::Tools::toStdString(prefix.mid(start));
// Tokenize prefix
@@ -555,12 +565,13 @@ void ExpressionCompleter::slotUpdate(const QString & prefix, int pos)
}
}
ExpressionLineEdit::ExpressionLineEdit(QWidget *parent, bool noProperty)
ExpressionLineEdit::ExpressionLineEdit(QWidget *parent, bool noProperty, bool requireLeadingEqualSign)
: QLineEdit(parent)
, completer(0)
, block(true)
, noProperty(noProperty)
, exactMatch(false)
, requireLeadingEqualSign(requireLeadingEqualSign)
{
connect(this, SIGNAL(textEdited(const QString&)), this, SLOT(slotTextChanged(const QString&)));
}
@@ -575,6 +586,7 @@ void ExpressionLineEdit::setDocumentObject(const App::DocumentObject * currentDo
completer = new ExpressionCompleter(currentDocObj, this, noProperty);
completer->setWidget(this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setRequireLeadingEqualSign(requireLeadingEqualSign);
#if QT_VERSION>=QT_VERSION_CHECK(5,2,0)
if (!exactMatch)
completer->setFilterMode(Qt::MatchContains);

View File

@@ -65,6 +65,7 @@ public:
void setDocumentObject(const App::DocumentObject*);
void setNoProperty(bool enabled=true);
void setRequireLeadingEqualSign(bool enabled);
public Q_SLOTS:
void slotUpdate(const QString &prefix, int pos);
@@ -76,6 +77,7 @@ private:
int prefixStart = 0;
int prefixEnd = 0;
bool requireLeadingEqualSign = false;
App::DocumentObjectT currentObj;
bool noProperty;
@@ -85,7 +87,7 @@ private:
class GuiExport ExpressionLineEdit : public QLineEdit {
Q_OBJECT
public:
ExpressionLineEdit(QWidget *parent = 0, bool noProperty=false);
ExpressionLineEdit(QWidget *parent = 0, bool noProperty = false, bool requireLeadingEqualSign = false);
void setDocumentObject(const App::DocumentObject *currentDocObj);
bool completerActive() const;
void hideCompleter();
@@ -104,6 +106,7 @@ private:
bool block;
bool noProperty;
bool exactMatch;
bool requireLeadingEqualSign;
};
class GuiExport ExpressionTextEdit : public QPlainTextEdit {

View File

@@ -31,7 +31,7 @@
using namespace SpreadsheetGui;
LineEdit::LineEdit(QWidget *parent)
: Gui::ExpressionLineEdit(parent)
: Gui::ExpressionLineEdit(parent, false, true)
, current()
, deltaCol(0)
, deltaRow(0)