#ifndef TREE_ITEM_H
#define TREE_ITEM_H

#include <iostream>
#include <QObject>
#include "qgu_qt4.h"
#include "QObject.h"
#include "Var.h"
#include "Viostream.h"

class WLItem;

QDOS::Vostream& operator<<(QDOS::Vostream& v_os, WLItem& item);
QDOS::Vistream& operator>>(QDOS::Vistream& v_is, WLItem& item);

class WLItem : public QObject
{
	Q_OBJECT
	
	friend QDOS::Vostream& operator<<(QDOS::Vostream& v_os, WLItem& item);
	friend QDOS::Vistream& operator>>(QDOS::Vistream& v_is, WLItem& item);
	
	public:
		WLItem(const QString& name="", WLItem* parent=NULL, bool isFolder=false);
		
		~WLItem();
		
		// Accessors
		
		bool setName(const QString& newName);
		QString getName() const {return _name;}
		QString getPath() const {return _path;}
		
		WLItem* parent() const {return _parent;}
		
		bool setFolder(bool isFolder=true);
		bool isFolder() const {return _isFolder;}
		
		bool isRoot() const {return (_parent==NULL);}
		bool hasChildren() const {return !_children.isEmpty();}
		int numChildren() const {return _children.size();}
		int childNumber()
		{
			if(_parent!=NULL)
			{
				return _parent->_children.indexOf(this);
			}
			return -1;
		}
		
		bool isParentOf(WLItem* other);
		bool isDescendentOf(WLItem* other);
		
		WLItem* getChildByName(const QString& name) const;
		WLItem* getChildByPath(const QString& path) const;
		WLItem* getChildByNumber(int i) const;
		
		bool removeChild(WLItem* child, bool alsoDelete);
		bool removeChild(int i, bool alsoDelete);
		
		bool addChild(WLItem* newChild, int position=-1);
		
		bool reparent(WLItem* newParent, int position=-1);
		
		void refreshPath();
		void clear();
		
		void display(int offset=0) const;
		
	signals:
		void pathChanged(const QString& oldPath, const QString& newPath);
		void deleted(const QString& path);
		
	protected:
		
		WLItem* _parent;
		QString _name;
		QString _path;
		bool _isFolder;
		QList<WLItem*> _children;
// 		QDOS::QdObject_var obj;
};

#endif
