2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2011-2017 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 16:20:43 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/qvalidatedlineedit.h>
|
2011-07-16 19:01:05 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/bitcoinaddressvalidator.h>
|
|
|
|
#include <qt/guiconstants.h>
|
2011-07-25 18:39:52 +02:00
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
QValidatedLineEdit::QValidatedLineEdit(QWidget *parent) :
|
2013-11-20 15:56:51 +01:00
|
|
|
QLineEdit(parent),
|
|
|
|
valid(true),
|
|
|
|
checkValidator(0)
|
2011-07-16 19:01:05 +02:00
|
|
|
{
|
|
|
|
connect(this, SIGNAL(textChanged(QString)), this, SLOT(markValid()));
|
|
|
|
}
|
|
|
|
|
2016-09-09 13:43:29 +02:00
|
|
|
void QValidatedLineEdit::setValid(bool _valid)
|
2011-07-16 19:01:05 +02:00
|
|
|
{
|
2016-09-09 13:43:29 +02:00
|
|
|
if(_valid == this->valid)
|
2011-07-16 19:01:05 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 13:43:29 +02:00
|
|
|
if(_valid)
|
2011-07-16 19:01:05 +02:00
|
|
|
{
|
|
|
|
setStyleSheet("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-25 18:39:52 +02:00
|
|
|
setStyleSheet(STYLE_INVALID);
|
2011-07-16 19:01:05 +02:00
|
|
|
}
|
2016-09-09 13:43:29 +02:00
|
|
|
this->valid = _valid;
|
2011-07-16 19:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void QValidatedLineEdit::focusInEvent(QFocusEvent *evt)
|
|
|
|
{
|
|
|
|
// Clear invalid flag on focus
|
|
|
|
setValid(true);
|
2013-11-20 15:56:51 +01:00
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
QLineEdit::focusInEvent(evt);
|
|
|
|
}
|
|
|
|
|
2013-11-20 15:56:51 +01:00
|
|
|
void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
|
|
|
|
{
|
|
|
|
checkValidity();
|
|
|
|
|
|
|
|
QLineEdit::focusOutEvent(evt);
|
|
|
|
}
|
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
void QValidatedLineEdit::markValid()
|
|
|
|
{
|
2013-11-20 15:56:51 +01:00
|
|
|
// As long as a user is typing ensure we display state as valid
|
2011-07-16 19:01:05 +02:00
|
|
|
setValid(true);
|
|
|
|
}
|
2011-07-22 17:06:37 +02:00
|
|
|
|
|
|
|
void QValidatedLineEdit::clear()
|
|
|
|
{
|
|
|
|
setValid(true);
|
|
|
|
QLineEdit::clear();
|
|
|
|
}
|
2013-11-20 15:56:51 +01:00
|
|
|
|
|
|
|
void QValidatedLineEdit::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (!enabled)
|
|
|
|
{
|
|
|
|
// A disabled QValidatedLineEdit should be marked valid
|
|
|
|
setValid(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Recheck validity when QValidatedLineEdit gets enabled
|
|
|
|
checkValidity();
|
|
|
|
}
|
|
|
|
|
|
|
|
QLineEdit::setEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QValidatedLineEdit::checkValidity()
|
|
|
|
{
|
|
|
|
if (text().isEmpty())
|
|
|
|
{
|
|
|
|
setValid(true);
|
|
|
|
}
|
|
|
|
else if (hasAcceptableInput())
|
|
|
|
{
|
|
|
|
setValid(true);
|
|
|
|
|
|
|
|
// Check contents on focus out
|
|
|
|
if (checkValidator)
|
|
|
|
{
|
|
|
|
QString address = text();
|
|
|
|
int pos = 0;
|
|
|
|
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
|
|
|
|
setValid(true);
|
|
|
|
else
|
|
|
|
setValid(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
setValid(false);
|
2015-11-16 10:43:36 +01:00
|
|
|
|
|
|
|
Q_EMIT validationDidChange(this);
|
2013-11-20 15:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QValidatedLineEdit::setCheckValidator(const QValidator *v)
|
|
|
|
{
|
|
|
|
checkValidator = v;
|
|
|
|
}
|
2015-11-16 10:43:36 +01:00
|
|
|
|
|
|
|
bool QValidatedLineEdit::isValid()
|
|
|
|
{
|
|
|
|
// use checkValidator in case the QValidatedLineEdit is disabled
|
|
|
|
if (checkValidator)
|
|
|
|
{
|
|
|
|
QString address = text();
|
|
|
|
int pos = 0;
|
|
|
|
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|