GUI: QValidatedLineEdit: Add support for a warning-but-valid state

This commit is contained in:
Luke Dashjr 2019-05-07 12:26:12 +00:00
parent 42e4f13246
commit 4b943c42fd
3 changed files with 52 additions and 6 deletions

View File

@ -26,6 +26,8 @@ static const bool DEFAULT_SPLASHSCREEN = true;
/* Invalid field background style */ /* Invalid field background style */
#define STYLE_INVALID "border: 3px solid #FF8080" #define STYLE_INVALID "border: 3px solid #FF8080"
/* "Warning" field background style */
#define STYLE_INCORRECT "border: 3px solid #FFFF80"
/* Transaction list -- unconfirmed transaction */ /* Transaction list -- unconfirmed transaction */
#define COLOR_UNCONFIRMED QColor(128, 128, 128) #define COLOR_UNCONFIRMED QColor(128, 128, 128)

View File

@ -13,22 +13,34 @@ QValidatedLineEdit::QValidatedLineEdit(QWidget* parent)
connect(this, &QValidatedLineEdit::textChanged, this, &QValidatedLineEdit::markValid); connect(this, &QValidatedLineEdit::textChanged, this, &QValidatedLineEdit::markValid);
} }
QValidatedLineEdit::~QValidatedLineEdit()
{
delete m_warning_validator;
}
void QValidatedLineEdit::setText(const QString& text) void QValidatedLineEdit::setText(const QString& text)
{ {
QLineEdit::setText(text); QLineEdit::setText(text);
checkValidity(); checkValidity();
} }
void QValidatedLineEdit::setValid(bool _valid) void QValidatedLineEdit::setValid(bool _valid, bool with_warning)
{ {
if(_valid == this->valid) if(_valid == this->valid)
{ {
return; if (with_warning == m_has_warning || !valid) {
return;
}
} }
if(_valid) if(_valid)
{ {
setStyleSheet(""); m_has_warning = with_warning;
if (with_warning) {
setStyleSheet("QValidatedLineEdit { " STYLE_INCORRECT "}");
} else {
setStyleSheet("");
}
} }
else else
{ {
@ -82,13 +94,14 @@ void QValidatedLineEdit::setEnabled(bool enabled)
void QValidatedLineEdit::checkValidity() void QValidatedLineEdit::checkValidity()
{ {
const bool has_warning = checkWarning();
if (text().isEmpty()) if (text().isEmpty())
{ {
setValid(true); setValid(true);
} }
else if (hasAcceptableInput()) else if (hasAcceptableInput())
{ {
setValid(true); setValid(true, has_warning);
// Check contents on focus out // Check contents on focus out
if (checkValidator) if (checkValidator)
@ -96,7 +109,7 @@ void QValidatedLineEdit::checkValidity()
QString address = text(); QString address = text();
int pos = 0; int pos = 0;
if (checkValidator->validate(address, pos) == QValidator::Acceptable) if (checkValidator->validate(address, pos) == QValidator::Acceptable)
setValid(true); setValid(true, has_warning);
else else
setValid(false); setValid(false);
} }
@ -126,3 +139,28 @@ bool QValidatedLineEdit::isValid()
return valid; return valid;
} }
void QValidatedLineEdit::setWarningValidator(const QValidator *v)
{
delete m_warning_validator;
m_warning_validator = v;
checkValidity();
}
bool QValidatedLineEdit::checkWarning() const
{
if (m_warning_validator && !text().isEmpty()) {
QString address = text();
int pos = 0;
if (m_warning_validator->validate(address, pos) != QValidator::Acceptable) {
return true;
}
}
return false;
}
bool QValidatedLineEdit::hasWarning() const
{
return m_has_warning;
}

View File

@ -16,9 +16,12 @@ class QValidatedLineEdit : public QLineEdit
public: public:
explicit QValidatedLineEdit(QWidget *parent); explicit QValidatedLineEdit(QWidget *parent);
~QValidatedLineEdit();
void clear(); void clear();
void setCheckValidator(const QValidator *v); void setCheckValidator(const QValidator *v);
bool isValid(); bool isValid();
void setWarningValidator(const QValidator *);
bool hasWarning() const;
protected: protected:
void focusInEvent(QFocusEvent *evt) override; void focusInEvent(QFocusEvent *evt) override;
@ -27,10 +30,12 @@ protected:
private: private:
bool valid{true}; bool valid{true};
const QValidator* checkValidator{nullptr}; const QValidator* checkValidator{nullptr};
bool m_has_warning{false};
const QValidator *m_warning_validator{nullptr};
public Q_SLOTS: public Q_SLOTS:
void setText(const QString&); void setText(const QString&);
void setValid(bool valid); void setValid(bool valid, bool with_warning=false);
void setEnabled(bool enabled); void setEnabled(bool enabled);
Q_SIGNALS: Q_SIGNALS:
@ -39,6 +44,7 @@ Q_SIGNALS:
private Q_SLOTS: private Q_SLOTS:
void markValid(); void markValid();
void checkValidity(); void checkValidity();
bool checkWarning() const;
}; };
#endif // BITCOIN_QT_QVALIDATEDLINEEDIT_H #endif // BITCOIN_QT_QVALIDATEDLINEEDIT_H