lbrycrd/src/qt/qvalidatedlineedit.cpp

108 lines
2.1 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2011-07-16 19:01:05 +02:00
#include "qvalidatedlineedit.h"
#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) :
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);
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);
}
void QValidatedLineEdit::setCheckValidator(const QValidator *v)
{
checkValidator = v;
}