void LegendWidget::drawVector(PlotCurve *c, QPainter *p, int x, int y, int l)
{
	if (!c)
		return;

	VectorCurve *v = (VectorCurve*)c;
	p->save();

	if (d_plot->antialiasing())
		p->setRenderHints(QPainter::Antialiasing);

	QPen pen(v->color(), v->width(), Qt::SolidLine);
	p->setPen(pen);
	p->drawLine(x, y, x + l, y);

	p->translate(x + l, y);

	double pi=4*atan(-1.0);
	int headLength = v->headLength();
	int d=qRound(headLength*tan(pi*(double)v->headAngle()/180.0));

	QPolygon endArray(3);
	endArray[0] = QPoint(0, 0);
	endArray[1] = QPoint(-headLength, d);
	endArray[2] = QPoint(-headLength, -d);

	if (v->filledArrowHead())
		p->setBrush(QBrush(pen.color(), Qt::SolidPattern));

	p->drawPolygon(endArray);
	p->restore();
}
void LegendMarker::drawVector(QPainter *p, int x, int y, int l, int curveIndex) const
{
	Graph *g = (Graph *)d_plot->parent();
	if (!g)
		return;

	VectorCurve *v = (VectorCurve*)g->curve(curveIndex);
	if (!v)
		return;

	p->save();

	QPen pen(v->color(), v->width(), Qt::SolidLine);
	p->setPen(pen);
	QwtPainter::drawLine(p, x, y, x + l, y);

	p->translate(x+l, y);

	double pi=4*atan(-1.0);
	int headLength = v->headLength();
	int d=qRound(headLength*tan(pi*(double)v->headAngle()/180.0));

	QPolygon endArray(3);
	endArray[0] = QPoint(0, 0);
	endArray[1] = QPoint(-headLength, d);
	endArray[2] = QPoint(-headLength, -d);

	if (v->filledArrowHead())
		p->setBrush(QBrush(pen.color(), Qt::SolidPattern));

	QwtPainter::drawPolygon(p,endArray);
	p->restore();
}
void AssociationsDialog::changePlotAssociation(int curve, const QStringList& ass)
{
	DataCurve *c = (DataCurve *)graph->dataCurve(curvesIndicesList[curve]);
	if (!c)
		return;

	if (c->plotAssociation() == ass)
		return;

	QStringList lst = ass;
	if (lst.count() == 1){
		c->setTitle(lst[0]);
		if (c->type() == Graph::Box)
			((BoxCurve*)c)->loadData();
		else if (c->type() == Graph::Pie)
			((PieCurve*)c)->loadData();
		else if (c->type() == Graph::Histogram)
			((QwtHistogram*)c)->loadData();
	} else if (lst.count() == 2){
		c->setXColumnName(lst[0].remove("(X)"));
		c->setTitle(lst[1].remove("(Y)"));
		c->loadData();
	} else if (lst.count() == 3){//curve with error bars
		ErrorBarsCurve *er = (ErrorBarsCurve *)c;
		QString xColName = lst[0].remove("(X)");
		QString yColName = lst[1].remove("(Y)");
		QString erColName = lst[2].remove("(xErr)").remove("(yErr)");
		DataCurve *master_curve = graph->masterCurve(xColName, yColName);
		if (!master_curve)
			return;

		int type = ErrorBarsCurve::Vertical;
		if (ass.join(",").contains("(xErr)"))
			type = ErrorBarsCurve::Horizontal;
		er->setDirection(type);
		er->setTitle(erColName);
		if (master_curve != er->masterCurve())
			er->setMasterCurve(master_curve);
		else
			er->loadData();
	} else if (lst.count() == 4){
		VectorCurve *v = (VectorCurve *)c;
		v->setXColumnName(lst[0].remove("(X)"));
		v->setTitle(lst[1].remove("(Y)"));

		QString xEndCol = lst[2].remove("(X)").remove("(A)");
		QString yEndCol = lst[3].remove("(Y)").remove("(M)");
		if (v->vectorEndXAColName() != xEndCol || v->vectorEndYMColName() != yEndCol)
			v->setVectorEnd(xEndCol, yEndCol);
		else
			v->loadData();
	}
	graph->notifyChanges();
}
void AssociationsDialog::changePlotAssociation(int curve, const QString& text)
{
	DataCurve *c = dynamic_cast<DataCurve *>(graph->curve(curve)); //c_keys[curve]);
	if (!c)
        return;

    if (c->plotAssociation() == text)
        return;

	QStringList lst = text.split(",", QString::SkipEmptyParts);
	if (lst.count() == 1){
		c->setTitle(lst[0]);
		if (graph->curveType(curve) == Graph::Box)
			dynamic_cast<BoxCurve*>(c)->loadData();
		else if (graph->curveType(curve) == Graph::Pie)
			dynamic_cast<QwtPieCurve*>(c)->loadData();
	} else if (lst.count() == 2){
		c->setXColumnName(lst[0].remove("(X)"));
		c->setTitle(lst[1].remove("(Y)"));
		c->loadData();
	} else if (lst.count() == 3){//curve with error bars
		QwtErrorPlotCurve *er = dynamic_cast<QwtErrorPlotCurve *>(c);
		QString xColName = lst[0].remove("(X)");
		QString yColName = lst[1].remove("(Y)");
		QString erColName = lst[2].remove("(xErr)").remove("(yErr)");
		DataCurve *master_curve = graph->masterCurve(xColName, yColName);
		if (!master_curve)
			return;

		int type = QwtErrorPlotCurve::Vertical;
		if (text.contains("(xErr)"))
			type = QwtErrorPlotCurve::Horizontal;
		er->setDirection(type);
		er->setTitle(erColName);
		if (master_curve != er->masterCurve())
			er->setMasterCurve(master_curve);
		else
			er->loadData();
	} else if (lst.count() == 4) {
		VectorCurve *v = dynamic_cast<VectorCurve *>(c);
		v->setXColumnName(lst[0].remove("(X)"));
		v->setTitle(lst[1].remove("(Y)"));

		QString xEndCol = lst[2].remove("(X)").remove("(A)");
		QString yEndCol = lst[3].remove("(Y)").remove("(M)");
		if (v->vectorEndXAColName() != xEndCol || v->vectorEndYMColName() != yEndCol)
			v->setVectorEnd(xEndCol, yEndCol);
		else
			v->loadData();
	}
	graph->notifyChanges();
}