#include "blockbase.h"

#include "alignedtextitem.h"
#include "qvariantedit.h"


QDataStream & operator<<(QDataStream & s, const QGraphicsItem * item) {
	if (!item) {
		s << int(-1);
		return s;
	}
	const QGraphicsRectItem * irect       = qgraphicsitem_cast<const QGraphicsRectItem *>(item);
	const QGraphicsEllipseItem * iell     = qgraphicsitem_cast<const QGraphicsEllipseItem *>(item);
	const QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<const QGraphicsSimpleTextItem *>(item);
	const AlignedTextItem * iatext        = qgraphicsitem_cast<const AlignedTextItem *>(item);
	const QGraphicsLineItem * iline       = qgraphicsitem_cast<const QGraphicsLineItem *>(item);
	const QGraphicsPathItem * ipath       = qgraphicsitem_cast<const QGraphicsPathItem *>(item);
	const QGraphicsPixmapItem * ipixmap   = qgraphicsitem_cast<const QGraphicsPixmapItem *>(item);
	if (irect) {
		s << int(0) << (irect->pen()) << (irect->brush()) << (irect->rect());
	} else if (iell) {
		s << int(1) << (iell->pen()) << (iell->brush()) << (iell->rect());
	} else if (itext) {
		s << int(2) << (itext->pen()) << (itext->brush()) << (itext->font()) << (itext->text());
	} else if (iatext) {
		s << int(6) << (iatext->pen()) << (iatext->brush()) << (iatext->font()) << (iatext->text()) << int(iatext->alignment());
	} else if (iline) {
		s << int(3) << (iline->pen()) << (iline->line());
	} else if (ipath) {
		s << int(4) << (ipath->pen()) << (ipath->path());
	} else if (ipixmap) {
		s << int(7) << (ipixmap->pixmap()) << (ipixmap->transform());
	} else {
		s << int(-1);
		return s;
	}
	s << (item->pos()) << (item->rotation()) << int(item->flags());
	return s;
}


QDataStream & operator>>(QDataStream & s, QGraphicsItem *& item) {
	int type_;
	s >> type_;
	if (type_ < 0) {
		item = 0;
		return s;
	}
	QGraphicsRectItem * nrect       = 0;
	QGraphicsEllipseItem * nell     = 0;
	QGraphicsSimpleTextItem * ntext = 0;
	AlignedTextItem * natext        = 0;
	QGraphicsLineItem * nline       = 0;
	QGraphicsPathItem * npath       = 0;
	QGraphicsPixmapItem * npixmap   = 0;
	item                            = 0;
	switch (type_) {
	case 0:
		nrect = new QGraphicsRectItem();
		item  = nrect;
		{
			QPen _v;
			s >> _v;
			nrect->setPen(_v);
		}
		{
			QBrush _v;
			s >> _v;
			nrect->setBrush(_v);
		}
		{
			QRectF _v;
			s >> _v;
			nrect->setRect(_v);
		}
		break;
	case 1:
		nell = new QGraphicsEllipseItem();
		item = nell;
		{
			QPen _v;
			s >> _v;
			nell->setPen(_v);
		}
		{
			QBrush _v;
			s >> _v;
			nell->setBrush(_v);
		}
		{
			QRectF _v;
			s >> _v;
			nell->setRect(_v);
		}
		break;
	case 2:
		ntext = new QGraphicsSimpleTextItem();
		item  = ntext;
		{
			QPen _v;
			s >> _v;
			ntext->setPen(_v);
		}
		{
			QBrush _v;
			s >> _v;
			ntext->setBrush(_v);
		}
		{
			QFont _v;
			s >> _v;
			ntext->setFont(_v);
		}
		{
			QString _v;
			s >> _v;
			ntext->setText(_v);
		}
		break;
	case 6:
		natext = new AlignedTextItem();
		item   = natext;
		{
			QPen _v;
			s >> _v;
			natext->setPen(_v);
		}
		{
			QBrush _v;
			s >> _v;
			natext->setBrush(_v);
		}
		{
			QFont _v;
			s >> _v;
			natext->setFont(_v);
		}
		{
			QString _v;
			s >> _v;
			natext->setText(_v);
		}
		{
			int _v;
			s >> _v;
			natext->setAlignment((Qt::AlignmentFlag)_v);
		}
		break;
	case 3:
		nline = new QGraphicsLineItem();
		item  = nline;
		{
			QPen _v;
			s >> _v;
			nline->setPen(_v);
		}
		{
			QLineF _v;
			s >> _v;
			nline->setLine(_v);
		}
		break;
	case 4:
		npath = new QGraphicsPathItem();
		item  = npath;
		{
			QPen _v;
			s >> _v;
			npath->setPen(_v);
		}
		{
			QPainterPath _v;
			s >> _v;
			npath->setPath(_v);
		}
		break;
	case 5:
		npixmap = new QGraphicsPixmapItem();
		item    = npixmap;
		{
			QPixmap _v;
			s >> _v;
			npixmap->setPixmap(_v);
		}
		break;
	case 7:
		npixmap = new QGraphicsPixmapItem();
		item    = npixmap;
		{
			QPixmap _v;
			s >> _v;
			npixmap->setPixmap(_v);
		}
		{
			QTransform _t;
			s >> _t;
			npixmap->setTransform(_t);
		}
		break;
	}
	if (item) {
		{
			QPointF _v;
			s >> _v;
			item->setPos(_v);
		}
		{
			qreal _v;
			s >> _v;
			item->setRotation(_v);
		}
		{
			int _v;
			s >> _v;
			item->setFlags((QGraphicsItem::GraphicsItemFlags)_v);
		}
	}
	return s;
}
