/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    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 LICENSE and COPYING files for more details.
*/

#include "qgraphicsclusteritem.h"
#include <QPainter>
#include <boost/lexical_cast.hpp>

QGraphicsClusterItem::QGraphicsClusterItem(size_t x, size_t y, size_t hlength, size_t vlength, Node * node, QColor color, const std::string & label)
{
    this->x = x;
    this->y = y;
    this->hlength = hlength;
    this->vlength = vlength;
    this->node = node;
    this->setRect(x, y - vlength, hlength, 2 * vlength);
    this->setZValue(0);
    this->highlightedPen = QPen(QColor("blue"), 2);
    this->normalPen = QPen(color, 2);
    this->setPen(normalPen);
    this->label = label;
}

void QGraphicsClusterItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if (this->node->GetID() == -1) //don't draw artificial root
	return;

    painter->setPen(this->pen());

    painter->drawLine(x, y, x + hlength, y);

    if (node->HasChildren())
    {
	painter->drawLine(x + hlength, y - vlength, x + hlength, y + vlength);
	painter->setPen(QPen(Qt::black));
	painter->drawText(x + 10, y - 5, QString::fromStdString(boost::lexical_cast<std::string>(node->GetID())));
	painter->setPen(QPen(Qt::darkGreen));
	painter->drawText(x + 0.5 * hlength, y + 12, QString::fromStdString(boost::lexical_cast<std::string>(node->GetLevel())));
    }
    else
    {
	painter->setPen(QPen(Qt::blue));
	if (this->label == "")
	    painter->drawText(x + hlength + 5, y + 3, QString::fromStdString(boost::lexical_cast<std::string>(node->GetID())));
	else
	    painter->drawText(x + hlength + 5, y + 3, QString::fromUtf8(label.c_str(), label.size()).left(50));
    }
}

Node * QGraphicsClusterItem::GetNode()
{
    return node;
}

void QGraphicsClusterItem::SetHighlighted(bool highlighted)
{
    if (highlighted)
	this->setPen(highlightedPen);
    else
	this->setPen(normalPen);
}
