Skip to content

Commit

Permalink
Relax input acceptance criteria in AddressInputWidget.
Browse files Browse the repository at this point in the history
The max length (previously `10`) has been lifted, and the input is now
self-corrected as the user enters text:

- Invalid characters are removed from the input.
- The optional `0x` prefix is removed.
- Any excess beyond the 8th character is removed.

For example, this now allows the user to paste an address that may
contain spaces, which is a fairly common case (e.g. `80 12 34 56`).
  • Loading branch information
cristian64 committed Jun 8, 2024
1 parent 9ab59e3 commit 5a3392d
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions Source/GUI/Widgets/AddressInputWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,36 @@ class AddressValidator : public QValidator
public:
QValidator::State validate(QString& input, int& pos) const override
{
if (input.startsWith("0x"))
// Strip invalid characters.
{
input.remove(0, 2);
pos -= 2;
const QString copy{input};
input.clear();
for (qsizetype i{0}; i < copy.size(); ++i)
{
const QChar c{copy[i].toLower()};
if (('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || c == 'x')
{
input.append(copy[i]);
continue;
}
if (i < pos)
{
pos -= 1;
}
}
}
if (input.size() > 8)

// Drop the optional prefix.
if (input.startsWith("0x"))
{
return QValidator::Invalid;
input.remove(0, 2);
pos = std::max(0, pos - 2);
}

// Drop excess.
input.truncate(8);
pos = std::min(8, pos);

return QValidator::Acceptable;
}
};
Expand All @@ -33,7 +54,6 @@ AddressInputWidget::AddressInputWidget(QWidget* const parent) : QLineEdit(parent
{
setPlaceholderText("00000000");
setValidator(new AddressValidator);
setMaxLength(10); // 8 + 2, to allow the user to paste a value that includes the "0x" prefix

const QFont fixedFont{QFontDatabase::systemFont(QFontDatabase::SystemFont::FixedFont)};
setFont(fixedFont);
Expand Down

0 comments on commit 5a3392d

Please sign in to comment.