/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "QLongValidator.h"

QLongValidator::QLongValidator(QObject *parent) : QValidator(parent), m_Minimum(0), m_Maximum(0) {
}

QLongValidator::QLongValidator(QLongValidator::value_type minimum, QLongValidator::value_type maximum, QObject *parent) : QValidator(parent), m_Minimum(minimum), m_Maximum(maximum) {
}

QLongValidator::value_type QLongValidator::bottom() const {
	return m_Minimum;
}

void QLongValidator::setBottom(QLongValidator::value_type bottom) {
	m_Minimum = bottom;
}

void QLongValidator::setRange(QLongValidator::value_type bottom, QLongValidator::value_type top) {
	setBottom(bottom);
	setTop(top);
}

void QLongValidator::setTop(QLongValidator::value_type top) {
	m_Maximum = top;
}

QLongValidator::value_type QLongValidator::top() const {
	return m_Maximum;
}

QValidator::State QLongValidator::validate(QString &input, int &pos) const {
	Q_UNUSED(pos);

	if(input.isEmpty()) {
		return QValidator::Acceptable;
	}
	
	if(input.length() == 1 && input == "-") {
		return QValidator::Intermediate;
	}

	bool ok;
	const value_type temp = input.toLong(&ok);
	if(!ok) {
		return QValidator::Invalid;
	}

	return (temp >= bottom() && temp <= top()) ? QValidator::Acceptable : QValidator::Intermediate;
}
