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

#include <QIcon>
#include <QImage>
#include <QTextBlockFormat>
#include <QWidget>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#  include <QRegularExpression>
#else
#  include <QRegExp>
typedef QRegExp QRegularExpression;
#endif
#include "qad_application_export.h"

class QTextEdit;
class QTextBlock;
class QAction;

namespace Ui {
class LogView;
}

class QAD_APPLICATION_EXPORT LogView: public QWidget {
	Q_OBJECT
	Q_PROPERTY(bool filterVisible READ isFilterVisible WRITE setFilterVisible)
	Q_PROPERTY(int linesLimit READ linesLimit WRITE setLinesLimit)
	Q_PROPERTY(QFont logFont READ logFont WRITE setLogFont)

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

	const QTextEdit * textEdit() const;
	void setLogFont(QFont f);
	QFont logFont() const;

	bool isFilterVisible() const;
	int linesLimit() const;

	void registerCategory(const QString & label,
	                      QString keyword     = QString(),
	                      const QImage & icon = QImage(),
	                      QBrush text_brush   = Qt::NoBrush,
	                      QBrush background   = Qt::NoBrush,
	                      bool bold           = false);

	void registerCategory(const QString & label,
	                      QRegularExpression regexp,
	                      const QImage & icon = QImage(),
	                      QBrush text_brush   = Qt::NoBrush,
	                      QBrush background   = Qt::NoBrush,
	                      bool bold           = false);
	void removeCategory(QString keyword);
	void removeCategory(QRegularExpression regexp);
	void clearCategories();

	void addText(const QString & text, bool insert_newline = true);
	void addText(const QString & text, const QString & keyword, bool insert_newline = true);

private:
	struct QAD_APPLICATION_EXPORT Category {
		Category();
		void makeIcon(QSize size, QSize size_icon, qreal ratio = 1.);
		QString label;
		QRegularExpression regexp;
		QImage image, icon_image;
		QIcon icon;
		QBrush text_brush;
		QBrush background;
		bool bold;
		inline bool operator==(const Category & it) const { return (regexp.pattern() == it.regexp.pattern()); }
	};

	void changeEvent(QEvent * e);
	void newLine(const QString & keyword);
	QSize iconImageSize();
	void filterBlock(QTextBlock block, const QString & fs, const QRegularExpression & regexp);

	Ui::LogView * ui;
	QList<Category> categories;
	QTextCharFormat def_cf;
	QTextBlockFormat def_bf;
	QAction *actionLogSelectAll, *actionLogCopy, *actionLogClear;

public slots:
	void setFilterVisible(bool yes);
	void setLinesLimit(int l);
	void clear();

private slots:
	void scrollToBottom();
	void filter();

signals:
};


#endif // LOGVIEW_H
