#include "drawtools.h"
#include "ui_drawtools.h"
#include "alignedtextitem.h"
#include <QGraphicsLineItem>
#include <QLineEdit>
#include <QMouseEvent>
#include <QFileDialog>
#include <QImageReader>
#include <QDialogButtonBox>
#include <QDebug>


_DTSizeItem::_DTSizeItem(): QGraphicsObject() {
	cur_item = 0;
	grid = 10.;
	in_process = can_drag = false;
	setData(1008, true);
	for (int i = 0; i < 8; ++i) {
		//qDebug() << &(rects[i]);
		rects[i].setData(1008, true);
		rects[i].setFlag(QGraphicsItem::ItemIgnoresTransformations);
		rects[i].setZValue(10.);
		rects[i].setAcceptHoverEvents(true);
		rects[i].setRect(-5, -5, 10, 10);
		rects[i].setPen(QPen(Qt::darkBlue));
		rects[i].setBrush(QBrush(QColor(64, 64, 255, 128)));
		//rects[i].setData(1100, true);
	}
}


_DTSizeItem::~_DTSizeItem() {
	assignObject(0);
	//qDebug() << "!!!";
}


void _DTSizeItem::assignObject(QGraphicsItem * item) {
	if (cur_item)
		if (qgraphicsitem_cast<QGraphicsItem*>(cur_item))
			cur_item->removeSceneEventFilter(this);
	cur_item = item;
	if (!cur_item) {
		for (int i = 0; i < 8; ++i) {
			rects[i].hide();
			rects[i].setParentItem(0);
			rects[i].removeSceneEventFilter(this);
		}
		return;
	}
	if (item)
		if (item->scene())
			if (!item->scene()->views().isEmpty())
				grid = ((BlockView*)(item->scene()->views()[0]))->gridStep();
	QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
	QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
	QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
	if (irect || iell || iline) {
		is_line = qgraphicsitem_cast<QGraphicsLineItem*>(item);
		for (int i = 0; i < (is_line ? 2 : 8); ++i) {
			rects[i].setParentItem(item);
			rects[i].installSceneEventFilter(this);
			rects[i].show();
		}
	}
	item->installSceneEventFilter(this);
	moveRects();
}


void _DTSizeItem::moveRects() {
	if (!cur_item) return;
	QRectF rect = itemRect(cur_item);
	QPointF tl = rect.topLeft(), tr = rect.topRight(), bl = rect.bottomLeft(), br = rect.bottomRight();
	if (is_line) {
		rects[0].setPos(tl);	rects[0].setData(2001, int(Qt::SizeAllCursor));
		rects[1].setPos(br);	rects[1].setData(2001, int(Qt::SizeAllCursor));
	} else {
		rects[0].setPos(tl);				rects[0].setData(2001, int(Qt::SizeFDiagCursor));
		rects[1].setPos((tl + tr) / 2.);	rects[1].setData(2001, int(Qt::SizeVerCursor));
		rects[2].setPos(tr);				rects[2].setData(2001, int(Qt::SizeBDiagCursor));
		rects[3].setPos((tr + br) / 2.);	rects[3].setData(2001, int(Qt::SizeHorCursor));
		rects[4].setPos(br);				rects[4].setData(2001, int(Qt::SizeFDiagCursor));
		rects[5].setPos((br + bl) / 2.);	rects[5].setData(2001, int(Qt::SizeVerCursor));
		rects[6].setPos(bl);				rects[6].setData(2001, int(Qt::SizeBDiagCursor));
		rects[7].setPos((bl + tl) / 2.);	rects[7].setData(2001, int(Qt::SizeHorCursor));
	}
}


void _DTSizeItem::applyRect() {
	if (!cur_item) return;
	QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
	QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
	QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
	if (irect)
		irect->setRect(nrect);
	if (iell)
		iell->setRect(nrect);
	if (iline)
		iline->setLine(QLineF(nrect.topLeft(), nrect.bottomRight()));
}


void _DTSizeItem::doubleClick() {
	QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<QGraphicsSimpleTextItem*>(cur_item);
	AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem*>(cur_item);
	QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(cur_item);
	if (itext || iatext) {
		emit textEditRequest();
	}
	if (ipixmap) {
		emit pixmapEditRequest();
	}
}


QRectF _DTSizeItem::itemRect(const QGraphicsItem * item) const {
	if (!cur_item) return QRectF();
	QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
	QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
	QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
	if (irect)
		return irect->rect();
	if (iell)
		return iell->rect();
	if (iline)
		return QRectF(iline->line().p1(), iline->line().p2());
	return QRectF();
}


QRectF _DTSizeItem::boundingRect() const {
	QRectF ret = rects[0].boundingRect().translated(rects[0].pos());
	for (int i = 1; i < 8; ++i)
		ret |= rects[i].boundingRect().translated(rects[i].pos());
	return ret;
}


bool _DTSizeItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) {
	QGraphicsSceneMouseEvent * me = (QGraphicsSceneMouseEvent * )event;
	if (watched == cur_item) {
		if (event->type() == QEvent::Close)
			assignObject(0);
		if (event->type() == QEvent::GraphicsSceneMouseDoubleClick) {
			doubleClick();
			return true;
		}
		return QGraphicsItem::sceneEventFilter(watched, event);
	}
	if (!cur_item)
		return QGraphicsItem::sceneEventFilter(watched, event);
	view_ = 0;
	switch (event->type()) {
	case QEvent::GraphicsSceneHoverEnter:
		if (watched->scene()) if (!watched->scene()->views().isEmpty()) view_ = watched->scene()->views()[0];
		if (view_) view_->setCursor(Qt::CursorShape(watched->data(2001).toInt()));
		break;
	case QEvent::GraphicsSceneHoverLeave:
		if (watched->scene()) if (!watched->scene()->views().isEmpty()) view_ = watched->scene()->views()[0];
		if (view_) view_->unsetCursor();
		break;
	case QEvent::GraphicsSceneMousePress:
		can_drag = (me->buttons() == Qt::LeftButton);
		if (in_process) {
			nrect = cur_item->data(2000).toRectF();
			applyRect();
			moveRects();
		}
		in_process = false;
		pp = quantize(me->scenePos(), grid);
		cur_item->setData(2000, itemRect(cur_item));
		return true;
	case QEvent::GraphicsSceneMouseMove:
		if (me->buttons().testFlag(Qt::LeftButton)) {
			sp = quantize(me->scenePos(), grid);
			if (pp != sp && can_drag) {
				in_process = true;
				nrect = itemRect(cur_item);
				if (is_line) {
					if (watched == &(rects[0])) nrect.setTopLeft(rects[0].pos() + (sp - pp));
					if (watched == &(rects[1])) nrect.setBottomRight(rects[1].pos() + (sp - pp));
				} else {
					if (watched == &(rects[0])) nrect.setTopLeft(rects[0].pos() + (sp - pp));
					if (watched == &(rects[1])) nrect.setTop(rects[1].pos().y() + (sp - pp).y());
					if (watched == &(rects[2])) nrect.setTopRight(rects[2].pos() + (sp - pp));
					if (watched == &(rects[3])) nrect.setRight(rects[3].pos().x() + (sp - pp).x());
					if (watched == &(rects[4])) nrect.setBottomRight(rects[4].pos() + (sp - pp));
					if (watched == &(rects[5])) nrect.setBottom(rects[5].pos().y() + (sp - pp).y());
					if (watched == &(rects[6])) nrect.setBottomLeft(rects[6].pos() + (sp - pp));
					if (watched == &(rects[7])) nrect.setLeft(rects[7].pos().x() + (sp - pp).x());
					nrect = nrect.normalized();
				}
				pp = sp;
				applyRect();
				moveRects();
			}
		}
		return true;
	case QEvent::GraphicsSceneMouseRelease:
		if (in_process)
			emit sizeChanged();
		in_process = false;
		can_drag = false;
		return true;
	default: break;
	}
	return QGraphicsItem::sceneEventFilter(watched, event);
}




DrawTools::DrawTools(BlockView * parent): QObject(parent),
actions_Z_up(this), actions_Z_top(this), actions_Z_down(this), actions_Z_bottom(this) {
	widget_props = new QWidget();
	ui = new Ui::DrawTools();
	ui->setupUi(widget_props);
	widget_props->setEnabled(false);
	setAlignCompact(false);
	menu_hor.addActions(QList<QAction*>() << ui->actionLeft << ui->actionHCenter << ui->actionRight);
	menu_ver.addActions(QList<QAction*>() << ui->actionTop << ui->actionVCenter << ui->actionBottom);
	ui->buttonAlignHor->setMenu(&menu_hor);
	ui->buttonAlignVer->setMenu(&menu_ver);
	new_type = -1;
	new_item = cur_item = 0;
	view_ = 0;
	resize_enabled = true;
	text_dlg.setWindowTitle(trUtf8("Edit text"));
	text_dlg.setLayout(new QBoxLayout(QBoxLayout::TopToBottom));
	QDialogButtonBox * bbox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
	connect(bbox, SIGNAL(accepted()), &text_dlg, SLOT(accept()));
	connect(bbox, SIGNAL(rejected()), &text_dlg, SLOT(reject()));
	text_dlg.layout()->addWidget(&text_edit);
	text_dlg.layout()->addWidget(bbox);
	actions_Z_up.setText(trUtf8("Bring\nforward"));    actions_Z_up.setIcon(QIcon(":/icons/z-up.png"));         actions_Z_up.setEnabled(false);
	actions_Z_top.setText(trUtf8("Bring\nto front"));  actions_Z_top.setIcon(QIcon(":/icons/z-top.png"));       actions_Z_top.setEnabled(false);
	actions_Z_down.setText(trUtf8("Send\nbackward"));  actions_Z_down.setIcon(QIcon(":/icons/z-down.png"));     actions_Z_down.setEnabled(false);
	actions_Z_bottom.setText(trUtf8("Send\nto back")); actions_Z_bottom.setIcon(QIcon(":/icons/z-bottom.png")); actions_Z_bottom.setEnabled(false);
	actions_add << newAction(trUtf8("Draw\nRectangle"), QIcon(":/icons/draw-rectangle.png"), 1)
			<< newAction(trUtf8("Draw\nEllipse"), QIcon(":/icons/draw-ellipse.png"), 2)
			<< newAction(trUtf8("Draw\nLine"), QIcon(":/icons/draw-line.png"), 4)
			<< newAction(trUtf8("Draw\nText"), QIcon(":/icons/draw-text.png"), 0)
			<< newAction(trUtf8("Draw\nImage"), QIcon(":/icons/view-preview.png"), 3);
	buttons_align << ui->buttonAlignTL << ui->buttonAlignTC << ui->buttonAlignTR
				<< ui->buttonAlignCL << ui->buttonAlignCC << ui->buttonAlignCR
				<< ui->buttonAlignBL << ui->buttonAlignBC << ui->buttonAlignBR;
	foreach (QAction * a, actions_add)
		connect(a, SIGNAL(toggled(bool)), this, SLOT(toggleNewItem(bool)));
	foreach (QToolButton * b, buttons_align)
		connect(b, SIGNAL(clicked(bool)), this, SLOT(alignClicked()));
	connect(ui->buttonImage, SIGNAL(clicked(bool)), this, SLOT(buttonImage_clicked()));
	connect(ui->buttonFont, SIGNAL(clicked(bool)), this, SLOT(buttonFont_clicked()));
	connect(ui->buttonTextEdit, SIGNAL(clicked(bool)), this, SLOT(buttonTextEdit_clicked()));
	connect(ui->spinWidth, SIGNAL(valueChanged(double)), this, SLOT(propertyChanged()));
	connect(ui->spinWidth, SIGNAL(editingFinished()), this, SLOT(changeFinished()));
	connect(ui->spinHeight, SIGNAL(valueChanged(double)), this, SLOT(propertyChanged()));
	connect(ui->spinHeight, SIGNAL(editingFinished()), this, SLOT(changeFinished()));
	connect(ui->spinThick, SIGNAL(valueChanged(double)), this, SLOT(propertyChanged()));
	connect(ui->spinThick, SIGNAL(editingFinished()), this, SLOT(changeFinished()));
	connect(ui->comboText, SIGNAL(editTextChanged(QString)), this, SLOT(propertyChanged()));
	connect(ui->comboText->lineEdit(), SIGNAL(editingFinished()), this, SLOT(changeFinished()));
	connect(ui->colorButtonPen, SIGNAL(colorChanged(QColor)), this, SLOT(propertyChanged()));
	connect(ui->colorButtonPen, SIGNAL(colorChanged(QColor)), this, SLOT(changeFinished()));
	connect(ui->colorButtonBrush, SIGNAL(colorChanged(QColor)), this, SLOT(propertyChanged()));
	connect(ui->colorButtonBrush, SIGNAL(colorChanged(QColor)), this, SLOT(changeFinished()));
	connect(ui->actionTop, SIGNAL(triggered(bool)), this, SLOT(actionTop_triggered(bool)));
	connect(ui->actionVCenter, SIGNAL(triggered(bool)), this, SLOT(actionVCenter_triggered(bool)));
	connect(ui->actionBottom, SIGNAL(triggered(bool)), this, SLOT(actionBottom_triggered(bool)));
	connect(ui->actionLeft, SIGNAL(triggered(bool)), this, SLOT(actionLeft_triggered(bool)));
	connect(ui->actionHCenter, SIGNAL(triggered(bool)), this, SLOT(actionHCenter_triggered(bool)));
	connect(ui->actionRight, SIGNAL(triggered(bool)), this, SLOT(actionRight_triggered(bool)));
	connect(&font_dlg, SIGNAL(currentFontChanged(QFont)), this, SLOT(propertyChanged()));
	connect(&size_item, SIGNAL(sizeChanged()), this, SLOT(sizeChanged()));
	connect(&size_item, SIGNAL(textEditRequest()), this, SLOT(buttonTextEdit_clicked()));
	connect(&size_item, SIGNAL(pixmapEditRequest()), this, SLOT(buttonImage_clicked()));
	connect(&actions_Z_up, SIGNAL(triggered(bool)), this, SLOT(actionZ_triggered()));
	connect(&actions_Z_top, SIGNAL(triggered(bool)), this, SLOT(actionZ_triggered()));
	connect(&actions_Z_down, SIGNAL(triggered(bool)), this, SLOT(actionZ_triggered()));
	connect(&actions_Z_bottom, SIGNAL(triggered(bool)), this, SLOT(actionZ_triggered()));
	setBlockView(parent);
}


DrawTools::~DrawTools() {
	size_item.assignObject(0);
	//delete ui;
	//delete widget_props;
}


void DrawTools::setBlockView(BlockView * v) {
	if (view_) view_->viewport()->removeEventFilter(this);
	disconnect(this, SLOT(selectionChanged()));
	view_ = v;
	if (!view_) return;
	view_->addItem(&size_item);
	view_->viewport()->installEventFilter(this);
	connect(view_->scene(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
}


void DrawTools::setAlignCompact(bool yes) {
	ui->widgetAlign2->setVisible(yes);
	ui->widgetAlign9->setVisible(!yes);
}


bool DrawTools::eventFilter(QObject * o, QEvent * e) {
	QMouseEvent * me = (QMouseEvent*)e;
	QPointF sp;
	if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseMove)
		sp = quantize(view_->mapToScene(me->pos()), view_->gridStep());
	QRectF mr;
	switch (e->type()) {
	case QEvent::MouseButtonPress:
		if (new_type < 0) break;
		if (new_item) {
			delete new_item;
			new_item = 0;
			if (!me->modifiers().testFlag(Qt::ControlModifier)) {
				foreach (QAction * a, actions_add)
					a->setChecked(false);
				new_type = -1;
				//view_->setCursor(Qt::ArrowCursor);
				return true;
			}
		}
		new_item = 0;
		pp = sp;
		switch (new_type) {
		case 0:
			new_item = new AlignedTextItem();
			((AlignedTextItem*)new_item)->setText("Text");
			((AlignedTextItem*)new_item)->setPos(sp);
			break;
		case 1:
			new_item = new QGraphicsRectItem();
			break;
		case 2:
			new_item = new QGraphicsEllipseItem();
			break;
		case 3:
			new_item = new QGraphicsPixmapItem(QPixmap(":/icons/view-preview.png"));
			((QGraphicsPixmapItem*)new_item)->setPos(sp - QPointF(new_item->boundingRect().width() / 2, new_item->boundingRect().height() / 2));
			break;
		case 4:
			new_item = new QGraphicsLineItem(QLineF(sp, sp));
			break;
		};
		if (new_item) {
			if (new_type == 1 || new_type == 2)
				((QAbstractGraphicsShapeItem*)new_item)->setBrush(Qt::white);
			new_item->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
			new_item->setData(1100, true);
			emit itemCreated(new_item);
			return true;
		}
		break;
	case QEvent::MouseMove:
		if (me->buttons().testFlag(Qt::LeftButton)) {
			if (new_item) {
				mr = new_item->mapRectFromScene(QRectF(pp, sp).normalized());
				//mr = QRectF(pp, sp).normalized();
				switch (new_type) {
				case 0:
					((AlignedTextItem*)new_item)->setPos(sp);
					break;
				case 1:
					((QGraphicsRectItem*)new_item)->setRect(mr);
					break;
				case 2:
					((QGraphicsEllipseItem*)new_item)->setRect(mr);
					break;
				case 3:
					((QGraphicsPixmapItem*)new_item)->setPos(sp - QPointF(new_item->boundingRect().width() / 2, new_item->boundingRect().height() / 2));
					break;
				case 4:
					((QGraphicsLineItem*)new_item)->setLine(QLineF(pp, sp));
					break;
				};
				return true;
			}
		}
		break;
	case QEvent::MouseButtonRelease:
		if (new_item) {
			if (new_item->boundingRect().isEmpty())
				delete new_item;
			else
				emit itemAddConfirm(new_item);
			new_item = 0;
			if (!me->modifiers().testFlag(Qt::ControlModifier)) {
				foreach (QAction * a, actions_add)
					a->setChecked(false);
				new_type = -1;
				//view_->setCursor(Qt::ArrowCursor);
			}
			return true;
		}
		break;
	default: break;
	}
	return QObject::eventFilter(o, e);
}


QComboBox * DrawTools::textEditCombo() const {
	return ui->comboText;
}


QAction * DrawTools::newAction(const QString & text, const QIcon & icon, int type) {
	QAction * ret = new QAction(icon, text, this);
	ret->setCheckable(true);
	ret->setData(type);
	return ret;
}


void DrawTools::toggleNewItem(bool on) {
	QAction * sa = (QAction * )sender();
	foreach (QAction * a, actions_add)
		if (a != sa)
			a->setChecked(false);
	if (!on) {
		new_type = -1;
		view_->unsetCursor();
		return;
	}
	new_type = sa->data().toInt();
	view_->setCursor(Qt::CrossCursor);
}


void DrawTools::alignClicked() {
	QToolButton * sb = (QToolButton * )sender();
	foreach (QToolButton * b, buttons_align)
		if (b != sb)
		b->setChecked(false);
	sb->setChecked(true);
	align = 0;
	QString als = sb->objectName().right(2).toLower();
	if (als[0] == 't') align |= Qt::AlignTop;
	if (als[0] == 'c') align |= Qt::AlignVCenter;
	if (als[0] == 'b') align |= Qt::AlignBottom;
	if (als[1] == 'l') align |= Qt::AlignLeft;
	if (als[1] == 'c') align |= Qt::AlignHCenter;
	if (als[1] == 'r') align |= Qt::AlignRight;
	propertyChanged();
}


void DrawTools::setPenBrushEnabled(bool pen, bool brush) {
	ui->labelPen->setEnabled(pen);
	ui->colorButtonPen->setEnabled(pen);
	ui->labelBrush->setEnabled(brush);
	ui->colorButtonBrush->setEnabled(brush);
}


void DrawTools::blockPropSignals(bool block_) {
	ui->spinWidth->blockSignals(block_);
	ui->spinHeight->blockSignals(block_);
	ui->spinThick->blockSignals(block_);
	ui->comboText->blockSignals(block_);
	ui->colorButtonPen->blockSignals(block_);
	ui->colorButtonBrush->blockSignals(block_);
	ui->actionTop->blockSignals(block_);
	ui->actionVCenter->blockSignals(block_);
	ui->actionBottom->blockSignals(block_);
	ui->actionHCenter->blockSignals(block_);
	ui->actionLeft->blockSignals(block_);
	ui->actionRight->blockSignals(block_);
	foreach (QToolButton * b, buttons_align)
		b->blockSignals(block_);
}


void DrawTools::actionAlignTrigger(bool vert, Qt::AlignmentFlag value) {
	blockPropSignals(true);
	if (vert) foreach (QAction * a, menu_ver.actions()) a->setChecked(false);
	else foreach (QAction * a, menu_hor.actions()) a->setChecked(false);
	align = align & (vert ? Qt::AlignHorizontal_Mask : Qt::AlignVertical_Mask);
	align |= value;
	((QAction*)sender())->setChecked(true);
	blockPropSignals(false);
	propertyChanged();
}


void DrawTools::emitZAvailabe(QGraphicsItem * item) {
	BlockView * view = 0;
	if (item) if (item->scene()) if (!item->scene()->views().isEmpty()) view = qobject_cast<BlockView * >(item->scene()->views()[0]);
	if (view == 0) {
		moveZUpAvailable(false);
		moveZDownAvailable(false);
		return;
	}
	QList<QGraphicsItem * > dl;
	if (item->parentItem() == 0) dl = view->decors();
	else if (item->parentItem()->data(1006) == "item") dl = ((BlockItem*)(item->parentItem()))->decors_;
	if (dl.size() <= 1) {
		moveZUpAvailable(false);
		moveZDownAvailable(false);
		return;
	}
	int ind = dl.indexOf(item);
	if (ind < 0) {
		moveZUpAvailable(false);
		moveZDownAvailable(false);
	} else {
		moveZUpAvailable(ind < dl.size() - 1);
		moveZDownAvailable(ind > 0);
	}
}


void DrawTools::selectionChanged() {
	cur_item = 0;
	size_item.assignObject(0);
	if (!view_) {
		emitZAvailabe();
		return;
	}
	QList<QGraphicsItem * > sil = view_->scene()->selectedItems();
	if (sil.size() != 1) {
		emitZAvailabe();
		widget_props->setEnabled(false);
		return;
	}
	widget_props->setEnabled(true);
	cur_item = sil[0];
	if (!cur_item) {
		emitZAvailabe();
		return;
	}
	QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<QGraphicsSimpleTextItem*>(cur_item);
	AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem*>(cur_item);
	QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(cur_item);
	QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
	QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
	QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
	blockPropSignals(true);
	if (itext) {
		ui->stackedProperties->setCurrentIndex(0);
		ui->comboText->setEditText(itext->text());
		ui->colorButtonPen->setColor(itext->brush().color());
		font_dlg.blockSignals(true);
		font_dlg.setCurrentFont(itext->font());
		font_dlg.blockSignals(false);
		setPenBrushEnabled(true, false);
		ui->widgetAlign2->setEnabled(false);
		ui->widgetAlign9->setEnabled(false);
		size_item.assignObject(itext);
	} else if (iatext) {
		ui->stackedProperties->setCurrentIndex(0);
		ui->comboText->setEditText(iatext->text());
		ui->colorButtonPen->setColor(iatext->brush().color());
		font_dlg.blockSignals(true);
		font_dlg.setCurrentFont(iatext->font());
		font_dlg.blockSignals(false);
		setPenBrushEnabled(true, false);
		foreach (QAction * a, menu_hor.actions()) a->setChecked(false);
		foreach (QAction * a, menu_ver.actions()) a->setChecked(false);
		align = iatext->alignment();
		QString als;
		if (align.testFlag(Qt::AlignTop)) {als += "T"; ui->actionTop->setChecked(true);}
		if (align.testFlag(Qt::AlignVCenter)) {als += "C"; ui->actionVCenter->setChecked(true);}
		if (align.testFlag(Qt::AlignBottom)) {als += "B"; ui->actionBottom->setChecked(true);}
		if (align.testFlag(Qt::AlignLeft)) {als += "L"; ui->actionLeft->setChecked(true);}
		if (align.testFlag(Qt::AlignHCenter)) {als += "C"; ui->actionHCenter->setChecked(true);}
		if (align.testFlag(Qt::AlignRight)) {als += "R"; ui->actionRight->setChecked(true);}
		foreach (QToolButton * b, buttons_align)
			b->setChecked(false);
		foreach (QToolButton * b, buttons_align)
			if (b->objectName().endsWith(als)) {
				b->setChecked(true);
				break;
			}
		ui->widgetAlign2->setEnabled(true);
		ui->widgetAlign9->setEnabled(true);
		size_item.assignObject(iatext);
	} else if (ipixmap) {
		ui->stackedProperties->setCurrentIndex(2);
		setPenBrushEnabled(false, false);
		size_item.assignObject(ipixmap);
	} else if (irect || iell) {
		ui->stackedProperties->setCurrentIndex(1);
		QAbstractGraphicsShapeItem * ishape(0);
		if (irect) {
			ishape = irect;
			ui->spinWidth->setValue(irect->rect().width());
			ui->spinHeight->setValue(irect->rect().height());
		}
		if (iell) {
			ishape = iell;
			ui->spinWidth->setValue(iell->rect().width());
			ui->spinHeight->setValue(iell->rect().height());
		}
		if (ishape) {
			ui->colorButtonPen->setColor(ishape->pen().color());
			ui->colorButtonBrush->setColor(ishape->brush().color());
			ui->spinThick->setValue(ishape->pen().widthF());
			setPenBrushEnabled(true, true);
			if (resize_enabled)
				size_item.assignObject(ishape);
		}
	} else if (iline) {
		ui->stackedProperties->setCurrentIndex(1);
		ui->colorButtonPen->setColor(iline->pen().color());
		ui->spinThick->setValue(iline->pen().widthF());
		setPenBrushEnabled(true, false);
			if (resize_enabled)
				size_item.assignObject(iline);
	} else {
		ui->stackedProperties->setCurrentIndex(3);
		widget_props->setEnabled(false);
	}
	emitZAvailabe(cur_item);
	blockPropSignals(false);
}


void DrawTools::sizeChanged() {
	blockPropSignals(true);
	QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
	QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
	if (irect || iell) {
		if (irect) {
			ui->spinWidth->setValue(irect->rect().width());
			ui->spinHeight->setValue(irect->rect().height());
		}
		if (iell) {
			ui->spinWidth->setValue(iell->rect().width());
			ui->spinHeight->setValue(iell->rect().height());
		}
	}
	blockPropSignals(false);
	changeFinished();
}


void DrawTools::propertyChanged() {
	if (!cur_item) return;
	QGraphicsSimpleTextItem * itext = qgraphicsitem_cast<QGraphicsSimpleTextItem*>(cur_item);
	AlignedTextItem * iatext = qgraphicsitem_cast<AlignedTextItem*>(cur_item);
	QGraphicsPixmapItem * ipixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(cur_item);
	QGraphicsRectItem * irect = qgraphicsitem_cast<QGraphicsRectItem*>(cur_item);
	QGraphicsEllipseItem * iell = qgraphicsitem_cast<QGraphicsEllipseItem*>(cur_item);
	QGraphicsLineItem * iline = qgraphicsitem_cast<QGraphicsLineItem*>(cur_item);
	if (itext) {
		QRectF obr = itext->boundingRect();
		itext->setFont(font_dlg.currentFont());
		itext->setText(ui->comboText->currentText());
		QRectF nbr = itext->boundingRect();
		QSizeF ds = (obr.size() - nbr.size()) / 2.;
		itext->setPos(itext->pos() + QPointF(ds.width(), ds.height()));
		itext->setBrush(ui->colorButtonPen->color());
	} else if (iatext) {
		iatext->setFont(font_dlg.currentFont());
		iatext->setText(ui->comboText->currentText());
		iatext->setBrush(ui->colorButtonPen->color());
		iatext->setAlignment(align);
	} else if (ipixmap) {
	} else if (irect || iell) {
		QAbstractGraphicsShapeItem * ishape(0);
		if (irect) {
			ishape = irect;
			irect->setRect(QRectF(irect->rect().topLeft(), QSizeF(ui->spinWidth->value(), ui->spinHeight->value())));
		}
		if (iell) {
			ishape = iell;
			iell->setRect(QRectF(iell->rect().topLeft(), QSizeF(ui->spinWidth->value(), ui->spinHeight->value())));
		}
		if (ishape) {
			ishape->setPen(QPen(ui->colorButtonPen->color(), ui->spinThick->value()));
			ishape->setBrush(ui->colorButtonBrush->color());
			if (resize_enabled)
				size_item.assignObject(ishape);
		}
	} else if (iline) {
		iline->setPen(QPen(ui->colorButtonPen->color(), ui->spinThick->value()));
		if (resize_enabled)
			size_item.assignObject(iline);
	}
}


void DrawTools::buttonImage_clicked() {
	QGraphicsPixmapItem * pi = qgraphicsitem_cast<QGraphicsPixmapItem * >(cur_item);
	if (!pi) return;
	QList<QByteArray> sif(QImageReader::supportedImageFormats());
	QString f;
	foreach (const QByteArray & i, sif) {
		if (!f.isEmpty()) f += " ";
		f += "*.";
		f += i;
	}
	QString ret = QFileDialog::getOpenFileName(0, trUtf8("Select image"), pi->data(1101).toString(), QString("Images(%1)").arg(f));
	if (ret.isEmpty()) return;
	QImage im(ret);
	if (im.isNull()) return;
	pi->setData(1101, ret);
	QRectF obr = pi->boundingRect();
	pi->setPixmap(QPixmap::fromImage(im));
	QRectF nbr = pi->boundingRect();
	QSizeF ds = (obr.size() - nbr.size()) / 2.;
	pi->setPos(pi->pos() + QPointF(ds.width(), ds.height()));
	changeFinished();
}


void DrawTools::buttonFont_clicked() {
	if (!cur_item) return;
	QGraphicsSimpleTextItem * ti = qgraphicsitem_cast<QGraphicsSimpleTextItem * >(cur_item);
	AlignedTextItem * ati = qgraphicsitem_cast<AlignedTextItem * >(cur_item);
	if (!ti && !ati) return;
	QFont font_prev;
	if (ti) font_prev = ti->font();
	if (ati) font_prev = ati->font();
	font_dlg.blockSignals(true);
	font_dlg.setCurrentFont(font_prev);
	font_dlg.blockSignals(false);
	if (font_dlg.exec() == QDialog::Rejected)
		font_dlg.setCurrentFont(font_prev);
	else
		changeFinished();
}


void DrawTools::buttonTextEdit_clicked() {
	text_edit.setPlainText(ui->comboText->lineEdit()->text());
	if (text_dlg.exec() == QDialog::Rejected)
		return;
	ui->comboText->lineEdit()->setText(text_edit.toPlainText());
	propertyChanged();
	changeFinished();
}


void DrawTools::actionZ_triggered() {
	if (!cur_item) return;
	if (cur_item->data(1009) == "decor") {
		BlockView * view = 0;
		if (cur_item) if (cur_item->scene()) if (!cur_item->scene()->views().isEmpty())
			view = qobject_cast<BlockView * >(cur_item->scene()->views()[0]);
		if (!view) return;
		QGraphicsScene * scene = view->scene();
		QList<QGraphicsItem*> dl = view->decors();
		scene->blockSignals(true);
		foreach (QGraphicsItem * d, dl) scene->removeItem(d);
		int ind = dl.indexOf(cur_item);
		dl.removeAt(ind);
		if (sender() == &actions_Z_up) dl.insert(ind + 1, cur_item);
		if (sender() == &actions_Z_top) dl.append(cur_item);
		if (sender() == &actions_Z_down) dl.insert(ind - 1, cur_item);
		if (sender() == &actions_Z_bottom) dl.prepend(cur_item);
		foreach (QGraphicsItem * d, dl) scene->addItem(d);
		scene->blockSignals(false);
	}
	if (cur_item->data(1011) == "decor") {
		BlockItem * bi = qgraphicsitem_cast<BlockItem*>(cur_item->parentItem());
		if (!bi) return;
		QList<QGraphicsItem*> dl = bi->decors_;
		foreach (QGraphicsItem * d, dl) d->setParentItem(0);
		int ind = dl.indexOf(cur_item);
		dl.removeAt(ind);
		if (sender() == &actions_Z_up) dl.insert(ind + 1, cur_item);
		if (sender() == &actions_Z_top) dl.append(cur_item);
		if (sender() == &actions_Z_down) dl.insert(ind - 1, cur_item);
		if (sender() == &actions_Z_bottom) dl.prepend(cur_item);
		bi->decors_ = dl;
		foreach (QGraphicsItem * d, dl) d->setParentItem(bi);
	}
	size_item.assignObject(cur_item);
	emitZAvailabe(cur_item);
	emit itemZChanged(cur_item);
}


void DrawTools::setResizeHandlesEnabled(bool on) {
	resize_enabled = on;
	if (!on) {
		size_item.assignObject(0);
		return;
	}
	if (cur_item && on)
		propertyChanged();
}
