/*
    QAD - Qt ADvanced

    Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef DRAWTOOLS_H
#define DRAWTOOLS_H

#include "blockview.h"
#include "qad_blockview_export.h"

#include <QAction>
#include <QFontDialog>
#include <QMenu>
#include <QObject>
#include <QPlainTextEdit>
#include <QToolButton>


class QComboBox;


class QAD_BLOCKVIEW_EXPORT _DTSizeItem: public QGraphicsObject {
	Q_OBJECT

public:
	_DTSizeItem();
	~_DTSizeItem();

	void assignObject(QGraphicsItem * item);

protected:
	void moveRects();
	void applyRect();
	void doubleClick();
	void resizeHandles();
	QRectF itemRect(const QGraphicsItem * item) const;
	QRectF boundingRect() const;
	void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0) {}
	bool sceneEventFilter(QGraphicsItem * watched, QEvent * event);

	QGraphicsItem * cur_item;
	QGraphicsView * view_;
	QGraphicsRectItem rects[8];
	QPointF pp, sp;
	QRectF nrect;
	qreal grid;
	bool in_process, can_drag, is_line;

signals:
	void sizeChanged();
	void textEditRequest();
	void pixmapEditRequest();
};


namespace Ui {
class DrawTools;
}


class QAD_BLOCKVIEW_EXPORT DrawTools: public QWidget {
	Q_OBJECT
	Q_PROPERTY(bool resizeHandlesEnabled READ isResizeHandlesEnabled WRITE setResizeHandlesEnabled)

public:
	explicit DrawTools(BlockView * parent = 0);
	~DrawTools();

	void setBlockView(BlockView * v);
	void resetSizeTool() { size_item.assignObject(0); }
	bool isResizeHandlesEnabled() const { return resize_enabled; }
	void setAlignCompact(bool yes);

	QComboBox * textEditCombo() const;
	QList<QAction *> actionsForAdd() const { return actions_add; }
	QList<QAction *> actionsForZ() const {
		return QList<QAction *>() << &actions_Z_bottom << &actions_Z_down << &actions_Z_up << &actions_Z_top;
	}
	QWidget * propertyWidget() const { return widget_props; }

protected:
	bool eventFilter(QObject * o, QEvent * e);
	void changeEvent(QEvent * e);

	QAction * newAction(const QIcon & icon, int type);
	void setToolButtonsEnabled(bool pen, bool brush, bool wid_hei);
	void blockPropSignals(bool block_);
	void actionAlignTrigger(bool vert, Qt::AlignmentFlag value);
	void emitZAvailabe(QGraphicsItem * item = 0);

	QWidget * widget_props;
	Ui::DrawTools * ui;
	BlockView * view_;
	QList<QAction *> actions_add;
	mutable QAction actions_Z_up, actions_Z_top, actions_Z_down, actions_Z_bottom;
	QList<QToolButton *> buttons_align;
	QGraphicsItem *new_item, *cur_item;
	QFontDialog font_dlg;
	QPointF pp;
	QDialog text_dlg;
	QPlainTextEdit text_edit;
	QMenu menu_hor, menu_ver;
	_DTSizeItem size_item;
	Qt::Alignment align;
	int new_type;
	bool resize_enabled;
	QString prev_dir;

private slots:
	void toggleNewItem(bool on);
	void alignClicked();
	void selectionChanged();
	void sizeChanged();
	void propertyChanged();
	void comboLineStyleChanged();
	void changeFinished() {
		if (cur_item) emit itemEdited(cur_item);
	}
	void moveZUpAvailable(bool yes) {
		actions_Z_up.setEnabled(yes);
		actions_Z_top.setEnabled(yes);
	}
	void moveZDownAvailable(bool yes) {
		actions_Z_down.setEnabled(yes);
		actions_Z_bottom.setEnabled(yes);
	}
	void decorVisibleLogicChanged();

	void buttonImage_clicked();
	void buttonImagePaste_clicked();
	void buttonFont_clicked();
	void buttonTextEdit_clicked();

	void actionTop_triggered(bool on) { actionAlignTrigger(true, Qt::AlignTop); }
	void actionVCenter_triggered(bool on) { actionAlignTrigger(true, Qt::AlignVCenter); }
	void actionBottom_triggered(bool on) { actionAlignTrigger(true, Qt::AlignBottom); }
	void actionLeft_triggered(bool on) { actionAlignTrigger(false, Qt::AlignLeft); }
	void actionHCenter_triggered(bool on) { actionAlignTrigger(false, Qt::AlignHCenter); }
	void actionRight_triggered(bool on) { actionAlignTrigger(false, Qt::AlignRight); }
	void actionZ_triggered();

public slots:
	void setResizeHandlesEnabled(bool on);

signals:
	void itemCreated(QGraphicsItem * item);
	void itemAddConfirm(QGraphicsItem * item);
	void itemEdited(QGraphicsItem * item);
	void itemZChanged(QGraphicsItem * item);

private:
	void retranslate();
};


#endif // DRAWTOOLS_H
