forked from qt-creator/qt-creator
Wizards: Use a FancyLineEdit::ValidationFunction
... rather than a QValidator. QLineEdit's behavior of preventing invalid input is not suitable here, as its concept of intermediate states is too limited. Change-Id: If34793966e8b06762db86b90bb1dbb1526b74ea7 Fixes: QTCREATORBUG-22080 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -54,7 +54,6 @@
|
|||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QListView>
|
#include <QListView>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QRegularExpressionValidator>
|
|
||||||
#include <QStandardItem>
|
#include <QStandardItem>
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
@@ -107,37 +106,32 @@ namespace ProjectExplorer {
|
|||||||
// Helper:
|
// Helper:
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
class LineEditValidator : public QRegularExpressionValidator
|
class LineEdit : public FancyLineEdit
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LineEditValidator(MacroExpander *expander, const QRegularExpression &pattern, QObject *parent) :
|
LineEdit(MacroExpander *expander, const QRegularExpression &pattern)
|
||||||
QRegularExpressionValidator(pattern, parent)
|
|
||||||
{
|
{
|
||||||
|
if (pattern.pattern().isEmpty() || !pattern.isValid())
|
||||||
|
return;
|
||||||
m_expander.setDisplayName(JsonFieldPage::tr("Line Edit Validator Expander"));
|
m_expander.setDisplayName(JsonFieldPage::tr("Line Edit Validator Expander"));
|
||||||
m_expander.setAccumulating(true);
|
m_expander.setAccumulating(true);
|
||||||
m_expander.registerVariable("INPUT", JsonFieldPage::tr("The text edit input to fix up."),
|
m_expander.registerVariable("INPUT", JsonFieldPage::tr("The text edit input to fix up."),
|
||||||
[this]() { return m_currentInput; });
|
[this]() { return m_currentInput; });
|
||||||
m_expander.registerSubProvider([expander]() -> MacroExpander * { return expander; });
|
m_expander.registerSubProvider([expander]() -> MacroExpander * { return expander; });
|
||||||
|
setValidationFunction([this, pattern](FancyLineEdit *, QString *) {
|
||||||
|
return pattern.match(text()).hasMatch();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void setFixupExpando(const QString &expando)
|
void setFixupExpando(const QString &expando) { m_fixupExpando = expando; }
|
||||||
{
|
|
||||||
m_fixupExpando = expando;
|
|
||||||
}
|
|
||||||
|
|
||||||
QValidator::State validate(QString &input, int &pos) const override
|
private:
|
||||||
{
|
QString fixInputString(const QString &string) override
|
||||||
fixup(input);
|
|
||||||
return QRegularExpressionValidator::validate(input, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fixup(QString &fixup) const override
|
|
||||||
{
|
{
|
||||||
if (m_fixupExpando.isEmpty())
|
if (m_fixupExpando.isEmpty())
|
||||||
return;
|
return string;
|
||||||
|
m_currentInput = string;
|
||||||
m_currentInput = fixup;
|
return m_expander.expand(m_fixupExpando);
|
||||||
fixup = m_expander.expand(m_fixupExpando);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -508,7 +502,7 @@ bool LineEditField::parseData(const QVariant &data, QString *errorMessage)
|
|||||||
m_restoreLastHistoryItem = consumeValue(tmp, "restoreLastHistoryItem", false).toBool();
|
m_restoreLastHistoryItem = consumeValue(tmp, "restoreLastHistoryItem", false).toBool();
|
||||||
QString pattern = consumeValue(tmp, "validator").toString();
|
QString pattern = consumeValue(tmp, "validator").toString();
|
||||||
if (!pattern.isEmpty()) {
|
if (!pattern.isEmpty()) {
|
||||||
m_validatorRegExp = QRegularExpression(pattern);
|
m_validatorRegExp = QRegularExpression('^' + pattern + '$');
|
||||||
if (!m_validatorRegExp.isValid()) {
|
if (!m_validatorRegExp.isValid()) {
|
||||||
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
|
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
|
||||||
"LineEdit (\"%1\") has an invalid regular expression \"%2\" in \"validator\".")
|
"LineEdit (\"%1\") has an invalid regular expression \"%2\" in \"validator\".")
|
||||||
@@ -539,13 +533,8 @@ bool LineEditField::parseData(const QVariant &data, QString *errorMessage)
|
|||||||
QWidget *LineEditField::createWidget(const QString &displayName, JsonFieldPage *page)
|
QWidget *LineEditField::createWidget(const QString &displayName, JsonFieldPage *page)
|
||||||
{
|
{
|
||||||
Q_UNUSED(displayName)
|
Q_UNUSED(displayName)
|
||||||
auto w = new FancyLineEdit;
|
const auto w = new LineEdit(page->expander(), m_validatorRegExp);
|
||||||
|
w->setFixupExpando(m_fixupExpando);
|
||||||
if (m_validatorRegExp.isValid()) {
|
|
||||||
auto lv = new LineEditValidator(page->expander(), m_validatorRegExp, w);
|
|
||||||
lv->setFixupExpando(m_fixupExpando);
|
|
||||||
w->setValidator(lv);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_historyId.isEmpty())
|
if (!m_historyId.isEmpty())
|
||||||
w->setHistoryCompleter(m_historyId, m_restoreLastHistoryItem);
|
w->setHistoryCompleter(m_historyId, m_restoreLastHistoryItem);
|
||||||
@@ -592,7 +581,7 @@ bool LineEditField::validate(MacroExpander *expander, QString *message)
|
|||||||
|
|
||||||
const bool baseValid = JsonFieldPage::Field::validate(expander, message);
|
const bool baseValid = JsonFieldPage::Field::validate(expander, message);
|
||||||
m_isValidating = false;
|
m_isValidating = false;
|
||||||
return baseValid && !w->text().isEmpty();
|
return baseValid && !w->text().isEmpty() && w->isValid();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineEditField::initializeData(MacroExpander *expander)
|
void LineEditField::initializeData(MacroExpander *expander)
|
||||||
|
Reference in New Issue
Block a user