#include "SliderWithTextMaxCurrent.h"	
#include "softProj\settings.h"

SliderWithTextMaxCurrent::SliderWithTextMaxCurrent(QWidget * parent) : SliderULongLong(parent) {
	showMaximum_ = true;
	showCurrent_ = true;

	setFontSize(8, QFont::Bold);
	setFontColor(QColor::fromRgb(220, 220, 220));
}

void SliderWithTextMaxCurrent::valueChangedEvent() {
	currentString_ = Settings::instance().formatNumber(getValue());
	SliderULongLong::valueChangedEvent();
}

void SliderWithTextMaxCurrent::paintEvent(QPaintEvent * paintEvent) {
	SliderULongLong::paintEvent(paintEvent);

	QPainter painter(this);
	QFont font("tahoma", fontSize_, fontWeight_);

	painter.setRenderHint(QPainter::Antialiasing, true);
	painter.setPen(fontColor_);
	painter.setFont(font);

	QFontMetrics fm = painter.fontMetrics();
 
	int maxStringWidth = fm.width(maxString_);
	int currentStringWidth = fm.width(currentString_);
	int textStringWidth = fm.width(text_);

	if (orientation() == Qt::Horizontal) {
		painter.drawText(QRect(4, 0, width() - 8, height()), text_, QTextOption(Qt::AlignLeft | Qt::AlignTop));
		
		if (showCurrent_) {
			painter.drawText(QRect(4, 0, width() - 8, height()), currentString_,  QTextOption(Qt::AlignHCenter | Qt::AlignBottom));
		}

		if (showMaximum_) {
			painter.drawText(QRect(6, 0, width() - 12, height()), maxString_, QTextOption(Qt::AlignRight | Qt::AlignTop));
		}		
	}
	else {		
		painter.drawText(QRect(2, 2, width() - 4, height() - 4), text_, QTextOption(Qt::AlignHCenter));

		if (showCurrent_) {
			painter.drawText(QRect(2, (height() - fm.height()) / 2, width() - 4, height() / 2), currentString_, QTextOption(Qt::AlignHCenter));
		}

		if (showMaximum_) {
			painter.drawText(QRect(2, 2, width() - 4, height() - 4), maxString_, QTextOption(Qt::AlignHCenter | Qt::AlignBottom));
		}
	}
}

void SliderWithTextMaxCurrent::setMaximumSuffix(const std::wstring & value) {
	maximumSuffix_ = QString::fromUtf16(value.c_str());
}

void SliderWithTextMaxCurrent::setFontSize(int size, int weight) {
	this->fontSize_ = size;
	this->fontWeight_ = weight;
}

void SliderWithTextMaxCurrent::setFontColor(const QColor & color) {
	this->fontColor_ = color;
}

void SliderWithTextMaxCurrent::showMaximum(bool show) {
	showMaximum_ = show;
}

void SliderWithTextMaxCurrent::showCurrent(bool show) {
	showCurrent_ = show;
}

void SliderWithTextMaxCurrent::setName(const std::wstring & name) {
	text_ = QString::fromUtf16(name.c_str());
}

void SliderWithTextMaxCurrent::setUnit(const std::wstring & unit) {
	unit_ = " " + QString::fromUtf16(unit.c_str());
}

void SliderWithTextMaxCurrent::setMaximum(unsigned long long max) {
	SliderULongLong::setMaximum(max);
	this->maxString_ = maximumPrefix_ + Settings::instance().formatNumber(getMaximum()) + unit_ + maximumSuffix_;
}

void SliderWithTextMaxCurrent::setLimits(unsigned long long min, unsigned long long max) {		
	setMaximum(max);
	setMinimum(min);
}

void SliderWithTextMaxCurrent::setValue(unsigned long long value) {
	SliderULongLong::setValue(value);
	currentString_ = currentPrefix_ + Settings::instance().formatNumber((getValue())) + unit_;
}

void SliderWithTextMaxCurrent::drawRotatedText(QPainter * painter, float degrees, int x, int y, const QString &text)
{
	painter->save();
	painter->translate(x, y);
	painter->rotate(degrees);
	painter->drawText(0, 0, text);
	painter->restore();
}

void SliderWithTextMaxCurrent::setMaximumPrefix(const std::wstring & value) {
	maximumPrefix_ = QString::fromUtf16(value.c_str());
}

void SliderWithTextMaxCurrent::setCurrentPrefix(const std::wstring & value) {
	currentPrefix_ = QString::fromUtf16(value.c_str());
}