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

#include "qad_application_export.h"

#include <QDebug>
#include <QListWidget>


class QAD_APPLICATION_EXPORT HistoryView: public QListWidget {
	Q_OBJECT
	Q_PROPERTY(bool active READ isActive WRITE setActive)
	Q_PROPERTY(bool duplicatesEnabled READ isDuplicatesEnabled WRITE setDuplicatesEnabled)
	Q_PROPERTY(int limit READ limit WRITE setLimit)
	Q_PROPERTY(QColor historyColor READ historyColor WRITE setHistoryColor)

public:
	explicit HistoryView(QWidget * parent = 0);
	~HistoryView();

	bool isActive() const { return active_; }
	bool isDuplicatesEnabled() const { return duplicates_; }
	int limit() const { return limit_; }
	QColor historyColor() const { return color_; }
	QByteArray current() const;

	void addEntry(int action, int count = 0, const QString & suffix = QString());
	void registerAction(int action, const QString & text, const QImage & icon = QImage());

private:
	struct QAD_APPLICATION_EXPORT Action {
		Action(int i = -1, const QString & t = QString(), const QImage & c = QImage()): id(i), text(t) {
			QPixmap px = QPixmap::fromImage(c);
			icon.addPixmap(px, QIcon::Active);
			icon.addPixmap(px, QIcon::Disabled);
			icon.addPixmap(px, QIcon::Normal);
			icon.addPixmap(px, QIcon::Selected);
		}
		int id;
		QString text;
		QIcon icon;
	};
	struct QAD_APPLICATION_EXPORT Entry {
		Entry(int a = -1, const QByteArray & c = QByteArray()): action(a), command(c) {}
		int action;
		QByteArray command;
	};

	void checkLimit();
	void changeEvent(QEvent * e);
	QString actionText(int action, int count_);

	QMap<int, Action> actions_;
	QVector<Entry> history_;
	QColor color_;
	bool active_, duplicates_;
	int index, limit_;

public slots:
	void setActive(bool yes) { active_ = yes; }
	void setDuplicatesEnabled(bool yes) { duplicates_ = yes; }
	void setLimit(int l);
	void setHistoryColor(const QColor & c);
	void clear(bool silent);
	void clear() { clear(false); }
	void undo();
	void redo();

private slots:
	void itemClicked(QListWidgetItem * item);
	void itemSelectionChanged();

signals:
	void undoAvailable(bool);
	void redoAvailable(bool);
	void clearAvailable(bool);
	void commandRequest(QByteArray & s);
	void commandExecute(const QByteArray & s);
	void changed();
};

#endif // HISTORYVIEW_H
