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 */
#define STYLE_INVALID "border: 3px solid #FF8080"
/* "Warning" field background style */
#define STYLE_INCORRECT "border: 3px solid #FFFF80"
/* Transaction list -- unconfirmed transaction */
#define COLOR_UNCONFIRMED QColor(128, 128, 128)

View File

@ -13,23 +13,35 @@ QValidatedLineEdit::QValidatedLineEdit(QWidget* parent)
connect(this, &QValidatedLineEdit::textChanged, this, &QValidatedLineEdit::markValid);
}
QValidatedLineEdit::~QValidatedLineEdit()
{
delete m_warning_validator;
}
void QValidatedLineEdit::setText(const QString& text)
{
QLineEdit::setText(text);
checkValidity();
}
void QValidatedLineEdit::setValid(bool _valid)
void QValidatedLineEdit::setValid(bool _valid, bool with_warning)
{
if(_valid == this->valid)
{
if (with_warning == m_has_warning || !valid) {
return;
}
}
if(_valid)
{
m_has_warning = with_warning;
if (with_warning) {
setStyleSheet("QValidatedLineEdit { " STYLE_INCORRECT "}");
} else {
setStyleSheet("");
}
}
else
{
setStyleSheet("QValidatedLineEdit { " STYLE_INVALID "}");
@ -82,13 +94,14 @@ void QValidatedLineEdit::setEnabled(bool enabled)
void QValidatedLineEdit::checkValidity()
{
const bool has_warning = checkWarning();
if (text().isEmpty())
{
setValid(true);
}
else if (hasAcceptableInput())
{
setValid(true);
setValid(true, has_warning);
// Check contents on focus out
if (checkValidator)
@ -96,7 +109,7 @@ void QValidatedLineEdit::checkValidity()
QString address = text();
int pos = 0;
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
setValid(true);
setValid(true, has_warning);
else
setValid(false);
}
@ -126,3 +139,28 @@ bool QValidatedLineEdit::isValid()
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:
explicit QValidatedLineEdit(QWidget *parent);
~QValidatedLineEdit();
void clear();
void setCheckValidator(const QValidator *v);
bool isValid();
void setWarningValidator(const QValidator *);
bool hasWarning() const;
protected:
void focusInEvent(QFocusEvent *evt) override;
@ -27,10 +30,12 @@ protected:
private:
bool valid{true};
const QValidator* checkValidator{nullptr};
bool m_has_warning{false};
const QValidator *m_warning_validator{nullptr};
public Q_SLOTS:
void setText(const QString&);
void setValid(bool valid);
void setValid(bool valid, bool with_warning=false);
void setEnabled(bool enabled);
Q_SIGNALS:
@ -39,6 +44,7 @@ Q_SIGNALS:
private Q_SLOTS:
void markValid();
void checkValidity();
bool checkWarning() const;
};
#endif // BITCOIN_QT_QVALIDATEDLINEEDIT_H