/*
	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 SHORTCUTS_H
#define SHORTCUTS_H

#include <QTreeWidget>
#include <QMainWindow>
#include <QShortcut>
#include <QHeaderView>
#include <QAction>
#include <QLineEdit>
#include <QDebug>
#include <QKeyEvent>
#include <QMenu>
#include <QToolBar>
#include <QScrollBar>
#include <QWidgetAction>
#include "clineedit.h"
#include "qad_widgets_export.h"


class QAD_WIDGETS_EXPORT ShortcutEdit: public CLineEdit
{
	Q_OBJECT
	friend class Shortcuts;
public:
	explicit ShortcutEdit(QWidget * parent = 0): CLineEdit(parent) {ti = 0; ca = 0; connect(this, SIGNAL(textChanged(QString)), this, SLOT(textChanged_(QString)));}

	void assignAction(QAction * a) {clear(); ca = a; reset();}
	QAction * action() const {return ca;}
	bool isEmpty() const {return text().isEmpty();}
	void commit() {if (ca == 0) return; ca->setShortcut(QKeySequence(text()));}
	void reset() {if (ca == 0) return; setText(ca->shortcut().toString());}

private slots:
	void textChanged_(QString t) {if (ti != 0) ti->setText(1, t);}

private:
	void keyPressEvent(QKeyEvent * e);

	QAction * ca;
	QTreeWidgetItem * ti;

};


class QAD_WIDGETS_EXPORT Shortcuts: public QTreeWidget
{
	Q_OBJECT

public:
	explicit Shortcuts(QWidget * parent = 0, bool on = true);
	~Shortcuts();

	void assignWindow(QWidget * w);
	void setActive(bool on) {active = on;}
	QList<QPair<QString, QKeySequence> > shortcuts();
	QStringList actionTree(QAction * a);
	static bool checkAction(QAction * a);

public slots:
	void clear();
	void updateShortcuts();
	void commit();
	void reset();
	void filter(const QString & what);

private:
	virtual void updateEditorGeometries() {foreach (ShortcutEdit * i, edits) i->setGeometry(visualRect(indexFromItem(i->ti, 1)));}
	virtual void changeEvent(QEvent * );
	bool filterTree(QTreeWidgetItem * ti, QString f);

	QMainWindow * aw;
	QVector<ShortcutEdit * > edits;
	QIcon empty_icon;
	QFont bfont;
	bool active;

private slots:

signals:
	void shortcutChanged(QAction * , QShortcut & );

};

#endif // SPINSLIDER_H
