2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2011-2015 The Bitcoin Core developers
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2013-11-04 16:20:43 +01:00
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
#include "qvalidatedlineedit.h"
|
|
|
|
|
|
2013-11-20 15:56:51 +01:00
|
|
|
#include "bitcoinaddressvalidator.h"
|
2011-07-25 18:39:52 +02:00
|
|
|
#include "guiconstants.h"
|
|
|
|
|
|
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()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QValidatedLineEdit::setValid(bool valid)
|
|
|
|
|
{
|
|
|
|
|
if(valid == this->valid)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(valid)
|
|
|
|
|
{
|
|
|
|
|
setStyleSheet("");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-07-25 18:39:52 +02:00
|
|
|
setStyleSheet(STYLE_INVALID);
|
2011-07-16 19:01:05 +02:00
|
|
|
}
|
|
|
|
|
this->valid = valid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|