/* Backend item
*/

#include "objectitem.h"

#include <QPen>
#include <QRect>
#include <QBrush>
#include <QPoint>
#include <QString>
#include <QPainterPath>
#include <QColor>

ObjectItem::ObjectItem(QGraphicsItem *parent, const QDomElement &xmlStructure)
    : QGraphicsItem(parent),
      m_xmlStructure(xmlStructure)
{
}

QRectF ObjectItem::boundingRect() const
{
    return QRectF(0,0,200,100);
}

void ObjectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)

    QRect r = boundingRect().toRect();

    painter->save();
    painter->setRenderHint(QPainter::Antialiasing);

    int rad = 20;

    // Draw the title bar
    QPainterPath titlePath;
    titlePath.moveTo(r.width(), r.top() + rad);
    titlePath.arcTo(r.width() - rad, r.top(), rad, rad, 0.0, 90.0);
    titlePath.lineTo(r.left() + rad, r.top());
    titlePath.arcTo(r.left(), r.top(), rad, rad, 90.0, 90.0);
    titlePath.lineTo(r.left(), 20);
    titlePath.lineTo(r.width(), 20);
    titlePath.closeSubpath();

    QPen pen(QBrush(qRgb(180,180,180)), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    painter->setPen(pen);
    painter->setBrush(QBrush(qRgb(180,180,180)));
    painter->drawPath(titlePath);

    // Draw the border
    QPainterPath borderPath;
    borderPath.moveTo(r.width(), r.top() + rad);
    borderPath.arcTo(r.width() - rad, r.top(), rad, rad, 0.0, 90.0);
    borderPath.lineTo(r.left() + rad, r.top());
    borderPath.arcTo(r.left(), r.top(), rad, rad, 90.0, 90.0);
    borderPath.lineTo(r.left(), r.height() - 2 * rad);
    borderPath.arcTo(r.left(), r.height() - rad, rad, rad, 180.0, 90.0);
    borderPath.lineTo(r.width() - 2 * rad, r.height());
    borderPath.arcTo(r.width() - rad, r.height() - rad, rad, rad, 270.0, 90.0);
    borderPath.closeSubpath();

    QPen pen2(QBrush(qRgb(50,50,50)), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    painter->setPen(pen2);
    painter->setBrush(QBrush(QColor(250,250,250,30)));
    painter->drawPath(borderPath);

    // Draw the title of the backend

    /*QDomDocument doc;
    doc.setContent(m_xmlStructure);
    QDomElement backendElement = doc.documentElement();
    QDomNodeList nodelist = backendElement.elementsByTagName("Backend");
    QDomElement e = nodelist.at(0).toElement();

    QString pid = e.attribute("PID");*/
    QString name = m_xmlStructure.attribute("Name");
    /*QString prog = e.attribute("ParentProgram");*/
    
    QPen txtPen(Qt::black);
    QFont txtFont("Arial", 8);

    painter->setPen(txtPen);
    painter->setFont(txtFont);
    painter->drawText(QPoint(5,15), name);

    painter->restore();
}

