lbrycrd/src/qt/qvalidatedlineedit.cpp

124 lines
2.5 KiB
C++
Raw Normal View History

2018-07-27 00:36:45 +02:00
// Copyright (c) 2011-2018 The Bitcoin Core developers
2014-12-13 05:09:33 +01: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 19:01:05 +02: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) :
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);
2011-07-16 19:01:05 +02:00
QLineEdit::focusInEvent(evt);
}
void QValidatedLineEdit::focusOutEvent(QFocusEvent *evt)
{
checkValidity();
QLineEdit::focusOutEvent(evt);
}
2011-07-16 19:01:05 +02:00
void QValidatedLineEdit::markValid()
{
// 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();
}
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;
}