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

#include "qad_widgets_export.h"
#include "qpiconfig.h"
#include "qpiconfignewdialog.h"

#include <QAction>
#include <QComboBox>
#include <QMenu>
#include <QTreeWidget>


class ConfigValueWidget;


class QAD_WIDGETS_EXPORT UComboBox: public QComboBox {
	Q_OBJECT

public:
	UComboBox(QWidget * parent = nullptr);

private slots:
	void indexChange(int i);
signals:

	void currentIndexChanged(int, UComboBox *);
};


class QAD_WIDGETS_EXPORT QPIConfigWidget: public QTreeWidget {
	Q_OBJECT
	Q_PROPERTY(bool readOnlyName READ readOnlyName WRITE setReadOnlyName)
	Q_PROPERTY(bool readOnlyValue READ readOnlyValue WRITE setReadOnlyValue)
	Q_PROPERTY(bool readOnlyType READ readOnlyType WRITE setReadOnlyType)
	Q_PROPERTY(bool readOnlyComment READ readOnlyComment WRITE setReadOnlyComment)
	Q_PROPERTY(bool columnNameVisible READ columnNameVisible WRITE setColumnNameVisible)
	Q_PROPERTY(bool columnValueVisible READ columnValueVisible WRITE setColumnValueVisible)
	Q_PROPERTY(bool columnTypeVisible READ columnTypeVisible WRITE setColumnTypeVisible)
	Q_PROPERTY(bool columnCommentVisible READ columnCommentVisible WRITE setColumnCommentVisible)

public:
	QPIConfigWidget(QWidget * parent = nullptr, QPIConfig * c = nullptr, bool on = true);
	~QPIConfigWidget() override { clear(); }

	void setQPIConfig(QPIConfig * c);
	bool readOnlyName() { return read_only_name; }
	bool readOnlyValue() { return read_only_value; }
	bool readOnlyType() { return read_only_type; }
	bool readOnlyComment() { return read_only_comment; }
	bool columnNameVisible() { return !c_hidden[0]; }
	bool columnValueVisible() { return !c_hidden[1]; }
	bool columnTypeVisible() { return !c_hidden[2]; }
	bool columnCommentVisible() { return !c_hidden[3]; }
	QString writeToString();
	void readFromString(QString str);

public slots:
	void parse();
	void write();
	void clear();
	void buildTree();
	void filter(const QString & f);
	void setReadOnlyName(bool yes);
	void setReadOnlyValue(bool yes);
	void setReadOnlyType(bool yes);
	void setReadOnlyComment(bool yes);
	void setColumnNameVisible(bool yes);
	void setColumnValueVisible(bool yes);
	void setColumnTypeVisible(bool yes);
	void setColumnCommentVisible(bool yes);

private slots:
	void itemClicked(QTreeWidgetItem * item, int column);
	void itemChanged(QTreeWidgetItem * item, int column);
	void typeChange(int t, UComboBox * c);
	void valueChange(ConfigValueWidget * w, QString v);
	void on_actionAddItem_triggered();
	void on_actionAddNode_triggered();
	void on_actionRemove_triggered();

signals:
	void changed();

private:
	void changeEvent(QEvent * e) override;
	bool eventFilter(QObject * o, QEvent * e) override;
	void buildEntry(QTreeWidgetItem * i, QPIConfig::Entry * e);
	void buildFullNames(QTreeWidgetItem * i);
	QPIConfig::Entry * itemEntry(QTreeWidgetItem * i);
	ConfigValueWidget * itemCWidget(QTreeWidgetItem * i);
	UComboBox * itemTWidget(QTreeWidgetItem * i);
	QTreeWidgetItem * addEntry(QTreeWidgetItem * i, QPIConfig::Entry * e, bool node = false);
	void deleteEntry(QTreeWidgetItem * i);
	bool filter(const QString & f, QTreeWidgetItem * i);
	bool filterItem(const QString & f, QTreeWidgetItem * i);
	void translate();
	void addTrEntry(const QString & s, const QString & f);

	QPIConfig * conf;
	QPIConfigNewDialog new_dialog;
	QAction actionAddItem, actionAddNode, actionToItem, actionToNode, actionRemove, actionExpandAll, actionCollapseAll;
	QMenu popupMenu;
	QString c_name, c_comment;
	QTreeWidgetItem *pi, *ti, *c_pi;
	QHash<QString, QString> types;
	QStringList s_types;
	QVector<ConfigValueWidget *> w_values;
	QVector<UComboBox *> w_types;
	QVector<bool> c_hidden;
	bool active, read_only_name, read_only_value, read_only_type, read_only_comment;
};

#endif // QPICONFIGWIDGET_H
