Skip to content

Commit abae86d

Browse files
committed
GUI: Point out position of invalid characters in Bech32 addresses
1 parent b910067 commit abae86d

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

src/qt/qvalidatedlineedit.cpp

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include <qt/bitcoinaddressvalidator.h>
88
#include <qt/guiconstants.h>
99

10+
#include <QColor>
11+
#include <QCoreApplication>
12+
#include <QFont>
13+
#include <QInputMethodEvent>
14+
#include <QList>
15+
#include <QTextCharFormat>
16+
1017
QValidatedLineEdit::QValidatedLineEdit(QWidget *parent) :
1118
QLineEdit(parent),
1219
valid(true),
@@ -26,15 +33,17 @@ void QValidatedLineEdit::setText(const QString& text)
2633
checkValidity();
2734
}
2835

29-
void QValidatedLineEdit::setValid(bool _valid, bool with_warning)
36+
void QValidatedLineEdit::setValid(bool _valid, bool with_warning, const std::vector<int>&error_locations)
3037
{
31-
if(_valid == this->valid)
38+
if(_valid && this->valid)
3239
{
33-
if (with_warning == m_has_warning || !valid) {
40+
if (with_warning == m_has_warning) {
3441
return;
3542
}
3643
}
3744

45+
QList<QInputMethodEvent::Attribute> attributes;
46+
3847
if(_valid)
3948
{
4049
m_has_warning = with_warning;
@@ -47,7 +56,22 @@ void QValidatedLineEdit::setValid(bool _valid, bool with_warning)
4756
else
4857
{
4958
setStyleSheet("QValidatedLineEdit { " STYLE_INVALID "}");
59+
if (!error_locations.empty()) {
60+
QTextCharFormat format;
61+
format.setFontUnderline(true);
62+
format.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
63+
format.setUnderlineColor(Qt::yellow);
64+
format.setForeground(Qt::yellow);
65+
format.setFontWeight(QFont::Bold);
66+
for (auto error_pos : error_locations) {
67+
attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, error_pos - cursorPosition(), /*length=*/ 1, format));
68+
}
69+
}
5070
}
71+
72+
QInputMethodEvent event(QString(), attributes);
73+
QCoreApplication::sendEvent(this, &event);
74+
5175
this->valid = _valid;
5276
}
5377

@@ -109,11 +133,20 @@ void QValidatedLineEdit::checkValidity()
109133
if (checkValidator)
110134
{
111135
QString address = text();
112-
int pos = 0;
113-
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
136+
const BitcoinAddressEntryValidator * const addressValidator = dynamic_cast<const BitcoinAddressEntryValidator*>(checkValidator);
137+
QValidator::State validation_result;
138+
std::vector<int> error_locations;
139+
if (addressValidator) {
140+
validation_result = addressValidator->validate(address, error_locations);
141+
} else {
142+
int pos = 0;
143+
validation_result = checkValidator->validate(address, pos);
144+
error_locations.push_back(pos);
145+
}
146+
if (validation_result == QValidator::Acceptable)
114147
setValid(true, has_warning);
115148
else
116-
setValid(false);
149+
setValid(false, false, error_locations);
117150
}
118151
}
119152
else

src/qt/qvalidatedlineedit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class QValidatedLineEdit : public QLineEdit
3535

3636
public Q_SLOTS:
3737
void setText(const QString&);
38-
void setValid(bool valid, bool with_warning=false);
38+
void setValid(bool valid, bool with_warning=false, const std::vector<int>&error_locations=std::vector<int>());
3939
void setEnabled(bool enabled);
4040

4141
Q_SIGNALS:

0 commit comments

Comments
 (0)