bitcoin/src/qt/qvalidatedlineedit.cpp

124 lines
2.5 KiB
C++
Raw Normal View History

2018-07-26 18:36:45 -04:00
// Copyright (c) 2011-2018 The Bitcoin Core developers
2014-12-13 01:09:33 -03:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qt/qvalidatedlineedit.h>
2011-07-16 13:01:05 -04:00
#include <qt/bitcoinaddressvalidator.h>
#include <qt/guiconstants.h>
2011-07-25 12:39:52 -04:00
2011-07-16 13:01:05 -04:00
QValidatedLineEdit::QValidatedLineEdit(QWidget *parent) :
QLineEdit(parent),
valid(true),
checkValidator(nullptr)
2011-07-16 13:01:05 -04:00
{
2018-06-24 11:18:22 -04:00
connect(this, &QValidatedLineEdit::textChanged, this, &QValidatedLineEdit::markValid);
2011-07-16 13:01:05 -04:00
}
2016-09-09 08:43:29 -03:00
void QValidatedLineEdit::setValid(bool _valid)
2011-07-16 13:01:05 -04:00
{
2016-09-09 08:43:29 -03:00
if(_valid == this->valid)
2011-07-16 13:01:05 -04:00
{
return;
}
2016-09-09 08:43:29 -03:00
if(_valid)
2011-07-16 13:01:05 -04:00
{
setStyleSheet("");
}
else
{
2011-07-25 12:39:52 -04:00
setStyleSheet(STYLE_INVALID);
2011-07-16 13:01:05 -04:00
}
2016-09-09 08:43:29 -03:00
this->valid = _valid;
2011-07-16 13:01:05 -04:00
}
void QValidatedLineEdit::focusInEvent(QFocusEvent *evt)
{
// Clear invalid flag on focus
setValid(true);
2011-07-16 13:01:05 -04:00
QLineEdit::focusInEvent(evt);
}
void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
{
checkValidity();
QLineEdit::focusOutEvent(evt);
}
2011-07-16 13:01:05 -04:00
void QValidatedLineEdit::markValid()
{
// As long as a user is typing ensure we display state as valid
2011-07-16 13:01:05 -04:00
setValid(true);
}
2011-07-22 11:06:37 -04:00
void QValidatedLineEdit::clear()
{
setValid(true);
QLineEdit::clear();
}
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);
Q_EMIT validationDidChange(this);
}
void QValidatedLineEdit::setCheckValidator(const QValidator *v)
{
checkValidator = v;
}
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;
}