Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/qt/bitcoinaddressvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@ QValidator::State BitcoinAddressCheckValidator::validate(QString &input, int &po

return QValidator::Invalid;
}

void BitcoinAddressErrorLocator::locateErrors(const QString& str, std::string& error_message, std::vector<int>* error_locations) const
{
DecodeDestination(str.toStdString(), error_message, error_locations);
}
16 changes: 16 additions & 0 deletions src/qt/bitcoinaddressvalidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ class BitcoinAddressCheckValidator : public QValidator
State validate(QString &input, int &pos) const override;
};

class ErrorLocator
{
public:
virtual void locateErrors(const QString& str, std::string& error_message, std::vector<int>* error_locations) const = 0;
virtual ~ErrorLocator() {};
};

/** Bitcoin address error locator.
*/
class BitcoinAddressErrorLocator : public ErrorLocator
{
public:
void locateErrors(const QString& str, std::string& error_message, std::vector<int>* error_locations) const override;
~BitcoinAddressErrorLocator() override {};
};

#endif // BITCOIN_QT_BITCOINADDRESSVALIDATOR_H
1 change: 1 addition & 0 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
QString::fromStdString(DummyAddress(Params()))));
widget->setValidator(new BitcoinAddressEntryValidator(parent));
widget->setCheckValidator(new BitcoinAddressCheckValidator(parent));
widget->setErrorLocator(new BitcoinAddressErrorLocator());
}

void AddButtonShortcut(QAbstractButton* button, const QKeySequence& shortcut)
Expand Down
61 changes: 60 additions & 1 deletion src/qt/qvalidatedlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
#include <qt/bitcoinaddressvalidator.h>
#include <qt/guiconstants.h>

#include <QContextMenuEvent>
#include <QMenu>
#include <QMessageBox>

QValidatedLineEdit::QValidatedLineEdit(QWidget *parent) :
QLineEdit(parent),
valid(true),
checkValidator(nullptr)
checkValidator(nullptr),
errorLocator(nullptr)
{
connect(this, &QValidatedLineEdit::textChanged, this, &QValidatedLineEdit::markValid);
}
Expand Down Expand Up @@ -108,6 +113,11 @@ void QValidatedLineEdit::setCheckValidator(const QValidator *v)
checkValidator = v;
}

void QValidatedLineEdit::setErrorLocator(const ErrorLocator *e)
{
errorLocator = e;
}

bool QValidatedLineEdit::isValid()
{
// use checkValidator in case the QValidatedLineEdit is disabled
Expand All @@ -121,3 +131,52 @@ bool QValidatedLineEdit::isValid()

return valid;
}

void QValidatedLineEdit::locateErrors()
{
if (!errorLocator) return;

// Perform error location on the input
QString input = text();
std::vector<int> error_locations{};
std::string error_message;
errorLocator->locateErrors(input, error_message, &error_locations);

if (error_message.empty()) {
QMessageBox::information(this, "Error locator", "No errors detected.", QMessageBox::Ok);
} else {
QString error_text = "Error: " + QString::fromStdString(error_message) + "<br />";
if (!error_locations.empty()) {
error_text += "<pre>";
int prev_loc = 0;
for (int loc : error_locations) {
error_text += input.mid(prev_loc, loc - prev_loc);
error_text += QString("<FONT COLOR='#ff0000'>%1</FONT>").arg(input[loc]);
prev_loc = loc + 1;
}
if (prev_loc < input.size()) {
error_text += input.right(input.size() - prev_loc);
}
error_text += "</pre>";
}
QMessageBox msg = QMessageBox(QMessageBox::Information, "Error locator", error_text, QMessageBox::Ok, parentWidget());
msg.exec();
}
}

void QValidatedLineEdit::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *contextMenu = createStandardContextMenu();
if (errorLocator) {
contextMenu->addAction(tr("Attempt error location"), this, &QValidatedLineEdit::locateErrors);
}
contextMenu->exec(event->globalPos());
delete contextMenu;
}

QValidatedLineEdit::~QValidatedLineEdit()
{
if (errorLocator) {
delete errorLocator;
}
}
7 changes: 7 additions & 0 deletions src/qt/qvalidatedlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <QLineEdit>

class ErrorLocator;

/** Line edit that can be marked as "invalid" to show input validation feedback. When marked as invalid,
it will get a red background until it is focused.
*/
Expand All @@ -16,17 +18,21 @@ class QValidatedLineEdit : public QLineEdit

public:
explicit QValidatedLineEdit(QWidget *parent);
~QValidatedLineEdit();
void clear();
void setCheckValidator(const QValidator *v);
void setErrorLocator(const ErrorLocator *e);
bool isValid();

protected:
void focusInEvent(QFocusEvent *evt) override;
void focusOutEvent(QFocusEvent *evt) override;
void contextMenuEvent(QContextMenuEvent *event) override;

private:
bool valid;
const QValidator *checkValidator;
const ErrorLocator *errorLocator;

public Q_SLOTS:
void setValid(bool valid);
Expand All @@ -38,6 +44,7 @@ public Q_SLOTS:
private Q_SLOTS:
void markValid();
void checkValidity();
void locateErrors();
};

#endif // BITCOIN_QT_QVALIDATEDLINEEDIT_H