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+
1017QValidatedLineEdit::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,21 @@ void QValidatedLineEdit::checkValidity()
109133 if (checkValidator)
110134 {
111135 QString address = text ();
112- int pos = 0 ;
113- if (checkValidator->validate (address, pos) == QValidator::Acceptable)
136+ QValidator::State validation_result;
137+ std::vector<int > error_locations;
138+ const BitcoinAddressEntryValidator * const address_validator = dynamic_cast <const BitcoinAddressEntryValidator*>(checkValidator);
139+ if (address_validator) {
140+ validation_result = address_validator->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 (/* valid= */ true , has_warning);
115- else
116- setValid (false );
148+ } else {
149+ setValid (/* valid= */ false , /* with_warning= */ false , error_locations);
150+ }
117151 }
118152 }
119153 else
0 commit comments