/*
 * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
 *
 * 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 <tqpainter.h>
#include <tqpixmap.h>
#include <tqfontmetrics.h>
#include <tqfontdatabase.h>
#include <tqimage.h>

#include <ZLImage.h>

#include "ZLTQtPaintContext.h"
#include "../image/ZLTQtImageManager.h"

ZLTQtPaintContext::ZLTQtPaintContext() {
	myPainter = new TQPainter();
	myPixmap = 0;
	mySpaceWidth = -1;
	myDescent = 0;
	myFontIsStored = false;
}

ZLTQtPaintContext::~ZLTQtPaintContext() {
	if (myPixmap != 0) {
		myPainter->end();
		delete myPixmap;
	}
	delete myPainter;
}

void ZLTQtPaintContext::setSize(int w, int h) {
	if (myPixmap != 0) {
		if ((myPixmap->width() != w) || (myPixmap->height() != h)) {
			myPainter->end();
			delete myPixmap;
			myPixmap = 0;
		}
	}
	if ((myPixmap == 0) && (w > 0) && (h > 0)) {
		myPixmap = new TQPixmap(w, h);
		myPainter->begin(myPixmap);
		if (myFontIsStored) {
			myFontIsStored = false;
			setFont(myStoredFamily, myStoredSize, myStoredBold, myStoredItalic);
		}
	}
}

static const std::string HELVETICA = "Helvetica";

void ZLTQtPaintContext::fillFamiliesList(std::vector<std::string> &families) const {
	TQFontDatabase db;
	TQStringList qFamilies = db.families();
	bool helveticaFlag = false;
	for (TQStringList::Iterator it = qFamilies.begin(); it != qFamilies.end(); ++it) {
		std::string family = (const char*)(*it).utf8();
		if (family == HELVETICA) {
			helveticaFlag = true;
		}
		families.push_back(family);
	}
	if (!helveticaFlag) {
		families.push_back(HELVETICA);
	}
}

const std::string ZLTQtPaintContext::realFontFamilyName(std::string &fontFamily) const {
	TQString fullName = TQFontInfo(TQFont(fontFamily.c_str())).family();
	if (fullName.isNull() || fullName.isEmpty()) {
		return HELVETICA;
	}
	return fullName.left(fullName.find(" [")).ascii();
}

void ZLTQtPaintContext::setFont(const std::string &family, int size, bool bold, bool italic) {
	if (myPainter->device() == 0) {
		myFontIsStored = true;
		myStoredFamily = family;
		myStoredSize = size;
		myStoredBold = bold;
		myStoredItalic= italic;
	} else {
		TQFont font = myPainter->font();
		bool fontChanged = false;

		if (font.family() != family.c_str()) {
			font.setFamily(family.c_str());
			fontChanged = true;
		}

		if (font.pointSize() != size) {
			font.setPointSize(size);
			fontChanged = true;
		}

		if ((font.weight() != (bold ? TQFont::Bold : TQFont::Normal))) {
			font.setWeight(bold ? TQFont::Bold : TQFont::Normal);
			fontChanged = true;
		}

		if (font.italic() != italic) {
			font.setItalic(italic);
			fontChanged = true;
		}

		if (fontChanged) {
			myPainter->setFont(font);
			mySpaceWidth = -1;
			myDescent = myPainter->fontMetrics().descent();
		}
	}
}

void ZLTQtPaintContext::setColor(ZLColor color, LineStyle style) {
	myPainter->setPen(TQPen(
		TQColor(color.Red, color.Green, color.Blue),
		1,
		(style == SOLID_LINE) ? TQPainter::SolidLine : TQPainter::DashLine
	));
}

void ZLTQtPaintContext::setFillColor(ZLColor color, FillStyle style) {
	myPainter->setBrush(TQBrush(
		TQColor(color.Red, color.Green, color.Blue),
		(style == SOLID_FILL) ? TQPainter::SolidPattern : TQPainter::Dense4Pattern
	));
}

int ZLTQtPaintContext::stringWidth(const char *str, int len, bool) const {
	return myPainter->fontMetrics().width(TQString::fromUtf8(str, len));
}

int ZLTQtPaintContext::spaceWidth() const {
	if (mySpaceWidth == -1) {
		mySpaceWidth = myPainter->fontMetrics().width(' ');
	}
	return mySpaceWidth;
}

int ZLTQtPaintContext::descent() const {
	return myDescent;
}

int ZLTQtPaintContext::stringHeight() const {
	return myPainter->font().pointSize() + 2;
}

void ZLTQtPaintContext::drawString(int x, int y, const char *str, int len, bool rtl) {
	TQString qStr = TQString::fromUtf8(str, len);
	myPainter->drawText(x, y, qStr, 0, len, rtl ? TQPainter::RTL : TQPainter::LTR);
}

void ZLTQtPaintContext::drawImage(int x, int y, const ZLImageData &image) {
	const TQImage &qImage = (ZLTQtImageData&)image;
	myPainter->drawImage(x, y - qImage.height(), qImage);
}

void ZLTQtPaintContext::drawImage(int x, int y, const ZLImageData &image, int width, int height, ScalingType type) {
	const TQImage &qImage = (ZLTQtImageData&)image;
	const TQImage &scaled = qImage.smoothScale(imageWidth(image, width, height, type), imageHeight(image, width, height, type), TQImage::ScaleMin);
	if (qImage != 0) {
		myPainter->drawImage(x, y - scaled.height(), scaled);
	}
}

void ZLTQtPaintContext::drawLine(int x0, int y0, int x1, int y1) {
	myPainter->drawPoint(x0, y0);
	myPainter->drawLine(x0, y0, x1, y1);
	myPainter->drawPoint(x1, y1);
}

void ZLTQtPaintContext::fillRectangle(int x0, int y0, int x1, int y1) {
	if (x1 < x0) {
		int tmp = x1;
		x1 = x0;
		x0 = tmp;
	}
	if (y1 < y0) {
		int tmp = y1;
		y1 = y0;
		y0 = tmp;
	}
	myPainter->fillRect(x0, y0,
											x1 - x0 + 1, y1 - y0 + 1,
											myPainter->brush());
}

void ZLTQtPaintContext::drawFilledCircle(int x, int y, int r) {
	myPainter->drawEllipse(x - r, y - r, 2 * r + 1, 2 * r + 1);
}

void ZLTQtPaintContext::clear(ZLColor color) {
	if (myPixmap != 0) {
		myPixmap->fill(TQColor(color.Red, color.Green, color.Blue));
	}
}

int ZLTQtPaintContext::width() const {
	if (myPixmap == 0) {
		return 0;
	}
	return myPixmap->width();
}

int ZLTQtPaintContext::height() const {
	if (myPixmap == 0) {
		return 0;
	}
	return myPixmap->height();
}
