Skip to content

Commit

Permalink
GUI: Point out position of invalid characters in Bech32 addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Feb 9, 2022
1 parent e0a0b28 commit 539beea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
48 changes: 41 additions & 7 deletions src/qt/qvalidatedlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#include <qt/bitcoinaddressvalidator.h>
#include <qt/guiconstants.h>

#include <QColor>
#include <QCoreApplication>
#include <QFont>
#include <QInputMethodEvent>
#include <QList>
#include <QTextCharFormat>

QValidatedLineEdit::QValidatedLineEdit(QWidget *parent) :
QLineEdit(parent),
valid(true),
Expand All @@ -26,15 +33,17 @@ void QValidatedLineEdit::setText(const QString& text)
checkValidity();
}

void QValidatedLineEdit::setValid(bool _valid, bool with_warning)
void QValidatedLineEdit::setValid(bool _valid, bool with_warning, const std::vector<int>&error_locations)
{
if(_valid == this->valid)
if(_valid && this->valid)
{
if (with_warning == m_has_warning || !valid) {
if (with_warning == m_has_warning) {
return;
}
}

QList<QInputMethodEvent::Attribute> attributes;

if(_valid)
{
m_has_warning = with_warning;
Expand All @@ -47,7 +56,22 @@ void QValidatedLineEdit::setValid(bool _valid, bool with_warning)
else
{
setStyleSheet("QValidatedLineEdit { " STYLE_INVALID "}");
if (!error_locations.empty()) {
QTextCharFormat format;
format.setFontUnderline(true);
format.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
format.setUnderlineColor(Qt::yellow);
format.setForeground(Qt::yellow);
format.setFontWeight(QFont::Bold);
for (auto error_pos : error_locations) {
attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, error_pos - cursorPosition(), /*length=*/ 1, format));
}
}
}

QInputMethodEvent event(QString(), attributes);
QCoreApplication::sendEvent(this, &event);

this->valid = _valid;
}

Expand Down Expand Up @@ -109,11 +133,21 @@ void QValidatedLineEdit::checkValidity()
if (checkValidator)
{
QString address = text();
int pos = 0;
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
QValidator::State validation_result;
std::vector<int> error_locations;
const BitcoinAddressEntryValidator * const address_validator = dynamic_cast<const BitcoinAddressEntryValidator*>(checkValidator);
if (address_validator) {
validation_result = address_validator->validate(address, error_locations);
} else {
int pos = 0;
validation_result = checkValidator->validate(address, pos);
error_locations.push_back(pos);
}
if (validation_result == QValidator::Acceptable) {
setValid(/* valid= */ true, has_warning);
else
setValid(false);
} else {
setValid(/* valid= */ false, /* with_warning= */ false, error_locations);
}
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/qt/qvalidatedlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class QValidatedLineEdit : public QLineEdit

public Q_SLOTS:
void setText(const QString&);
void setValid(bool valid, bool with_warning=false);
void setValid(bool valid, bool with_warning=false, const std::vector<int>&error_locations=std::vector<int>());
void setEnabled(bool enabled);

Q_SIGNALS:
Expand Down

0 comments on commit 539beea

Please sign in to comment.