/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "treeitem.h"
#include <QStringList>
#include <QDebug>

using namespace LeechCraft::Util;

TreeItem::TreeItem (const QList<QVariant>& data, TreeItem *parent)
: Parent_ (parent)
{
	Data_ [Qt::DisplayRole] = data.toVector ();
}

TreeItem::~TreeItem ()
{
	qDeleteAll (Children_);
}

void TreeItem::AppendChild (TreeItem *child)
{
	Children_.append (child);
}

void TreeItem::PrependChild (TreeItem *child)
{
	Children_.prepend (child);
}

void TreeItem::InsertChild (int index, TreeItem *child)
{
	Children_.insert (index, child);
}

int TreeItem::ChildPosition (const TreeItem *child) const
{
	return Children_.indexOf (const_cast<TreeItem*> (child));
}

void TreeItem::RemoveChild (int child)
{
	delete Children_.takeAt (child);
}

TreeItem* TreeItem::Child (int row) const
{
	return Children_.value (row);
}

int TreeItem::ChildCount () const
{
	return Children_.count ();
}

int TreeItem::ColumnCount (int role) const
{
	return Data_ [role].count ();
}

QVariant TreeItem::Data (int column, int role) const
{
	return Data_ [role].value (column);
}

void TreeItem::ModifyData (int column, const QVariant& data, int role)
{
	if (Data_ [role].size () <= column)
		Data_ [role].resize (column + 1);
	Data_ [role] [column] = data;
}

const TreeItem* TreeItem::Parent () const
{
	return Parent_;
}

TreeItem* TreeItem::Parent ()
{
	return Parent_;
}

int TreeItem::Row () const
{
	if (Parent_)
		return Parent_->Children_.indexOf (const_cast<TreeItem*> (this));
	return 0;
}

QDebug operator<< (QDebug dbg, const LeechCraft::Util::TreeItem& item)
{
	dbg.nospace () << "{ TreeItem| Parent:"
		<< item.Parent ();
	dbg.nospace () << "; Children:"
		<< item.ChildCount ();
	dbg.nospace () << "; Data[0]:"
		<< (item.ColumnCount () ? item.Data (0) : QString ("<no columns>"))
		<< "}";
	return dbg.space ();
}

QDebug operator<< (QDebug dbg, const LeechCraft::Util::TreeItem* const pitem)
{
	dbg.nospace () << "TreeItem @ " 
		<< static_cast<const void* const> (pitem)
		<< ";";
	if (pitem)
		dbg << *pitem;
	return dbg.space ();
}

