/*
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 "DialogInputValue.h"
#include <QRegExpValidator>
#include <QIntValidator>

class QUIntValidator : public QValidator {
public:
	typedef unsigned int uint_t;
	
public:
	QUIntValidator(QObject *parent = 0) : QValidator(parent), m_Minimum(0), m_Maximum(0) {
	}
	
	explicit QUIntValidator(uint_t minimum = 0, uint_t maximum = 0, QObject *parent = 0) : QValidator(parent), m_Minimum(minimum), m_Maximum(maximum) {
	}
	
	~QUIntValidator() {
	}
	
	uint_t bottom() const {
		return m_Minimum;
	}
	
	void setBottom(uint_t bottom) {
		m_Minimum = bottom;
	}
	
	virtual void setRange(uint_t bottom, uint_t top) {
		setBottom(bottom);
		setTop(top);
	}
		
	void setTop(uint_t top) {
		m_Maximum = top;
	}
	
	uint_t top() const {
		return m_Maximum;
	}
	
	virtual QValidator::State validate(QString &input, int &pos) const {
		Q_UNUSED(pos);
	
		if(input.isEmpty()) {
			return QValidator::Acceptable;
		}
		
		bool ok;
		const uint_t temp = input.toUInt(&ok);
		if(!ok) {
			return QValidator::Invalid;
		}
		
		return (temp >= bottom() && temp <= top()) ? QValidator::Acceptable : QValidator::Intermediate;
	}
	
private:
	uint_t m_Minimum;
	uint_t m_Maximum;
};

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
DialogInputValue::DialogInputValue(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
		
	ui.hexInput->setValidator(new QRegExpValidator(QRegExp("[A-Fa-f0-9]{0,8}"), this));
	ui.signedInput->setValidator(new QIntValidator(INT_MIN, INT_MAX, this));
	ui.unsignedInput->setValidator(new QUIntValidator(0, UINT_MAX, this));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
quint32 DialogInputValue::value() const {
	bool ok;
	return ui.hexInput->text().toUInt(&ok, 16);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::setValue(quint32 value) {
	ui.hexInput->setText(QString().sprintf("%08x", value));
	ui.signedInput->setText(QString().sprintf("%d", value));
	ui.unsignedInput->setText(QString().sprintf("%u", value));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::on_hexInput_textEdited(const QString &s) {
	bool ok;
	quint32 value = s.toUInt(&ok, 16);
	
	if(!ok) {
		value = 0;
	}
	
	ui.signedInput->setText(QString().sprintf("%d", value));
	ui.unsignedInput->setText(QString().sprintf("%u", value));
	
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::on_signedInput_textEdited(const QString &s) {
	bool ok;
	quint32 value = s.toInt(&ok, 10);
	
	if(!ok) {
		value = 0;
	}
	
	ui.hexInput->setText(QString().sprintf("%08x", value));
	ui.unsignedInput->setText(QString().sprintf("%u", value));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::on_unsignedInput_textEdited(const QString &s) {
	bool ok;
	quint32 value = s.toUInt(&ok, 10);
	
	if(!ok) {
		value = 0;
	}
	
	ui.hexInput->setText(QString().sprintf("%08x", value));
	ui.signedInput->setText(QString().sprintf("%d", value));
}
