#include "SliderULongLong.h"

void SliderULongLong::valueChangedEvent() {		
	// Reimplement in subclasses
}

void SliderULongLong::valueChangedSlot(int intValue) {
	if (isProgrammaticInput_) {
		valueChangedEvent();
		emit valueChangedSignal(current_);
		return;
	}
	else {
		if (isAdjusted_) {
			if (INT_MAX > 0) {
				current_ =  intValue * (max_ / (long double) INT_MAX);
			}
			else {
				current_ = 0;
			}
		}
		else {
			current_ = intValue;
		}

		if (current_ < min_) {
			setValue(min_);
		}

		valueChangedEvent();

		emit valueChangedSignal(current_);
	}
}

void SliderULongLong::setValue(unsigned long long value) {
	if (value < min_) {
		value = min_;
	}

	if (value > max_) {
		value = max_;
	}

	current_ = value;

	if (!isAdjusted_) {		
		QSlider::setValue(value);
	}
	else {
		if (max_ > 0) {
			int intValue = ((long double) value * (long double) INT_MAX) / (long double) max_;
			isProgrammaticInput_ = true;
			QSlider::setValue(intValue);
			isProgrammaticInput_ = false;
		}
		else {
			isProgrammaticInput_ = true;
			QSlider::setValue(0);
			isProgrammaticInput_ = false;
		}
	}
}

SliderULongLong::SliderULongLong(QWidget * parent) : QSlider(parent) {
	bool ok = connect(this, SIGNAL(valueChanged(int)), this, SLOT(valueChangedSlot(int)));
	assert(ok && "Signals not connected");
	
	min_ = 0;
	max_ = INT_MAX;

	QSlider::setMinimum(min_);
	QSlider::setMaximum(max_);
	
	current_ = min_;	
	isAdjusted_ = false;
	isProgrammaticInput_ = false;
}

void SliderULongLong::setMinimum(unsigned long long min) {
	this->min_ = min;

	if (!isAdjusted_) {		
		QSlider::setMinimum(min);
	}
	else {
		if (max_ > 0) {
			int intValue = ((long double) min * (long double) INT_MAX) / (long double) max_;
			QSlider::setMinimum(intValue);
		}
		else {
			QSlider::setValue(0);
		}
	}

	setValue(current_);
}

void SliderULongLong::setMaximum(unsigned long long max) {
	this->max_ = max;

	if (max > INT_MAX) {
		isAdjusted_ = true;
		QSlider::setMaximum(INT_MAX);
	}
	else {
		isAdjusted_ = false;
		QSlider::setMaximum(max);
	}

	setValue(current_);
}

unsigned long long SliderULongLong::getMaximum() {
	return max_;
}

unsigned long long SliderULongLong::getValue() {
	return current_;
}
