/*
    PIQt Utils - Qt utilites for PIP

    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 CONNECTION_VIEW_H
#define CONNECTION_VIEW_H

#include "blockview.h"
#include "piconnection.h"
#include "qad_piqt_utils_export.h"

const int __CV_Device = 1;
const int __CV_Filter = 2;
const int __CV_Sender = 3;


class QAD_PIQT_UTILS_EXPORT DeviceItem: public BlockItem {
public:
	DeviceItem();

	void setName(const QString & n) {
		addProperty(BlockItem::Property("name", "", n));
		rename();
	}
	void setPath(const QString & p) {
		addProperty(BlockItem::Property("device", "", p));
		rename();
	}
	void setMode(PIIODevice::DeviceMode m) {
		addProperty(BlockItem::Property("mode", "", int(m)));
		rename();
	}
	void setOptions(PIIODevice::DeviceOptions o) {
		addProperty(BlockItem::Property("options", "", int(o)));
		rename();
	}
	void setDisconnectTimeout(double v) {
		addProperty(BlockItem::Property("disconnectTimeout", "", v));
		rename();
	}
	void setBufferSize(int v) {
		addProperty(BlockItem::Property("bufferSize", "", v));
		rename();
	}

	QString name() const { return propertyByName("name").value.toString(); }
	QString path() const { return propertyByName("device").value.toString(); }
	PIIODevice::DeviceMode mode() const { return PIIODevice::DeviceMode(propertyByName("mode").value.toInt()); }
	PIIODevice::DeviceOptions options() const { return PIIODevice::DeviceOptions(propertyByName("options").value.toInt()); }
	double disconnectTimeout() const { return propertyByName("disconnectTimeout").value.toDouble(); }
	int bufferSize() const { return propertyByName("bufferSize").value.toInt(); }

	void rename();

protected:
	AlignedTextItem *text_name, *text_path;
};


class QAD_PIQT_UTILS_EXPORT FilterItem: public BlockItem {
public:
	FilterItem();

	void setName(const QString & n) {
		addProperty(BlockItem::Property("name", "", n));
		rename();
	}
	void setMode(PIPacketExtractor::SplitMode m) {
		addProperty(BlockItem::Property("mode", "", int(m)));
		rename();
	}
	void setHeader(const QString & v) {
		addProperty(BlockItem::Property("header", "", v));
		rename();
	}
	void setFooter(const QString & v) {
		addProperty(BlockItem::Property("footer", "", v));
		rename();
	}
	void setTimeout(double v) {
		addProperty(BlockItem::Property("timeout", "", v));
		rename();
	}
	void setPacketSize(int v) {
		addProperty(BlockItem::Property("size", "", v));
		rename();
	}
	void setDisconnectTimeout(double v) {
		addProperty(BlockItem::Property("disconnectTimeout", "", v));
		rename();
	}
	void setBufferSize(int v) {
		addProperty(BlockItem::Property("bufferSize", "", v));
		rename();
	}

	QString name() const { return propertyByName("name").value.toString(); }
	PIPacketExtractor::SplitMode mode() const { return PIPacketExtractor::SplitMode(propertyByName("mode").value.toInt()); }
	QString header() const { return propertyByName("header").value.toString(); }
	QString footer() const { return propertyByName("footer").value.toString(); }
	double timeout() const { return propertyByName("timeout").value.toDouble(); }
	int packetSize() const { return propertyByName("size").value.toInt(); }
	double disconnectTimeout() const { return propertyByName("disconnectTimeout").value.toDouble(); }
	int bufferSize() const { return propertyByName("bufferSize").value.toInt(); }

	void rename();

protected:
	AlignedTextItem *text_name, *text_mode;
};


class QAD_PIQT_UTILS_EXPORT SenderItem: public BlockItem {
public:
	SenderItem();

	void setName(const QString & n) {
		addProperty(BlockItem::Property("name", "", n));
		rename();
	}
	void setData(const QString & p) {
		addProperty(BlockItem::Property("data", "", p));
		rename();
	}
	void setFrequency(double m) {
		addProperty(BlockItem::Property("frequency", "", m));
		rename();
	}

	QString name() const { return propertyByName("name").value.toString(); }
	QString data() const { return propertyByName("data").value.toString(); }
	double frequency() const { return propertyByName("frequency").value.toDouble(); }

	void rename();

protected:
	AlignedTextItem *text_name, *text_frequency;
};


class QAD_PIQT_UTILS_EXPORT ConnectionView: public BlockView {
	Q_OBJECT

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

	DeviceItem * addDevice(const QString & name, const QString & path);
	FilterItem * addFilter(const QString & name);
	SenderItem * addSender(const QString & name);
	DeviceItem * findDevice(const QString & name) const;
	QList<BlockItem *> allDevices() const { return allByType(__CV_Device); }
	QList<BlockItem *> allFilters() const { return allByType(__CV_Filter); }
	QList<BlockItem *> allSenders() const { return allByType(__CV_Sender); }

private:
	QSize sizeHint() const { return QSize(800, 600); }
	void loadBus(BlockBusItem * bus);
	void placeBlock(BlockItem * b, QList<BlockItem *> coll);

	QList<BlockItem *> allByType(int type_) const;

private slots:
};

#endif // CONNECTION_VIEW_H
