#include "showpoints.h"
#include "simulationgraphicview.h"

#include <QMimeData>
#include <QClipboard>
#include <QKeyEvent>

ShowPoints::ShowPoints(QWidget *parent, GraphInfo *graphInfo)
	: QDialog(parent)
{
	ui.setupUi(this);

	connect(ui.okBtn, SIGNAL(clicked(bool)), this, SLOT(accept()));

	int row = 0;
	int pointCount;
	QTableWidgetItem *item;
	QFont f;
	int lineCount = graphInfo->_lines.count();

	ui.pointTable->setColumnCount(2);

	if(!graphInfo->_title.isEmpty()) {
		ui.pointTable->setRowCount(row+1);
		item = new QTableWidgetItem(graphInfo->_title);
		item->setFlags(item->flags() & (~Qt::ItemIsEditable));
		f = item->font();
		f.setWeight(QFont::Black);
		item->setFont(f);
		ui.pointTable->setVerticalHeaderItem(row, new QTableWidgetItem(tr("")));
		ui.pointTable->setItem(row++, 0, item);
	}

	QStringList cols;

	cols.append(graphInfo->_xString);
	cols.append(graphInfo->_yString);
	ui.pointTable->setHorizontalHeaderLabels(cols);

	for(int i = 0; i < lineCount; i++) {
		LineInfo *line = graphInfo->_lines[i];

		if(i)
			row++;

		int titleRow = row;
		ui.pointTable->setRowCount(row+1);
		item = new QTableWidgetItem(line->_text);
		item->setFlags(item->flags() & (~Qt::ItemIsEditable));
		f = item->font();
		f.setWeight(QFont::Bold);
		item->setFont(f);
		ui.pointTable->setVerticalHeaderItem(row, new QTableWidgetItem(""));
		ui.pointTable->setItem(row++, 0, item);
		
		pointCount = line->_points.elementCount();
		ui.pointTable->setRowCount(row+pointCount);

		qreal average = 0;

		for(int j = 0; j < pointCount; j++) {
			QPointF p = line->_points.elementAt(j);
			item = new QTableWidgetItem(QString("%1").arg(j+1));
			item->setFlags(item->flags() & (~Qt::ItemIsEditable));
			item->setTextAlignment(Qt::AlignCenter);
			ui.pointTable->setVerticalHeaderItem(row, item);
			item = new QTableWidgetItem(QString("%1").arg(p.x()));
			item->setFlags(item->flags() & (~Qt::ItemIsEditable));
			item->setTextAlignment(Qt::AlignCenter);
			ui.pointTable->setItem(row, 0, item);
			item = new QTableWidgetItem(QString("%1").arg(p.y()));
			item->setFlags(item->flags() & (~Qt::ItemIsEditable));
			item->setTextAlignment(Qt::AlignCenter);
			ui.pointTable->setItem(row++, 1, item);
			average += p.y();
		}

		if(pointCount > 0) {
			item = new QTableWidgetItem(QString("%1").arg(average/pointCount));
			item->setFlags(item->flags() & (~Qt::ItemIsEditable));
			item->setTextAlignment(Qt::AlignCenter);
			ui.pointTable->setItem(titleRow, 1, item);
		}
	}
}

ShowPoints::~ShowPoints()
{

}

void ShowPoints::keyPressEvent(QKeyEvent *e)
{
	// implement copy to copy the selection
	if(e->matches(QKeySequence::Copy) || e->matches(QKeySequence::Cut)) {
		int i, j;
		QMimeData* mime = new QMimeData;
		QList<QTableWidgetSelectionRange> ranges;
		QList<QTableWidgetSelectionRange> result;
		QString html = "<table>", text;
		
		ranges = ui.pointTable->selectedRanges();

		while(ranges.count() > 0) {
			// these values are used by Qt in the constructor but I force them to prevent that a change in Qt will affect the
			// code.
			QTableWidgetSelectionRange smallerRow(-1, -1, -2, -2);
			QList<QTableWidgetSelectionRange>::iterator smaller;
			for(QList<QTableWidgetSelectionRange>::iterator it = ranges.begin(); it != ranges.end(); it++) {
				QTableWidgetSelectionRange r = *it;
				if(smallerRow.rowCount() <= 0 || r.topRow() < smallerRow.topRow()) {
					smallerRow = r;
					smaller = it;
				}
			}

			ranges.erase(smaller);

			QTableWidgetItem *item;

			html.append("<tr>");
			for(i = smallerRow.topRow(); i <= smallerRow.bottomRow() && i < ui.pointTable->rowCount(); i++) {
				for(j = smallerRow.leftColumn(); j <= smallerRow.rightColumn(); j++) {
					item = ui.pointTable->item(i, j);
					if(item) {
						html.append(QString("<td>%1</td>").arg(item->text()));

						if(j == smallerRow.leftColumn())
							text.append(item->text());
						else
							text.append(QString("\t%1").arg(item->text()));
					}
				}
				html.append("</tr>");
				text.append("\n");
			}
		}
		html.append("</table>");
		mime->setHtml(html);
		mime->setText(text);
		QApplication::clipboard()->setMimeData(mime);
	}
}
