

#include <QtGui>

#include "diagramitem.h"
#include "diagramtextitem.h"
#include "arrow.h"
#include "arrow2.h"
#include "diagramscene.h"
#include "iteminfo.h"

//! [0]
DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
             QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsPolygonItem(0, scene)
{
    myDiagramType = diagramType;
    myContextMenu = contextMenu;
    weight=0;
    QPainterPath path;
    switch (myDiagramType) {
        case StartEnd:
            path.moveTo(200, 50);
            path.arcTo(150, 0, 50, 50, 0, 90);
            path.arcTo(50, 0, 50, 50, 90, 90);
            path.arcTo(50, 50, 50, 50, 180, 90);
            path.arcTo(150, 50, 50, 50, 270, 90);
            path.lineTo(200, 25);
            myPolygon = path.toFillPolygon();
            break;
        case Conditional:
     {
        QGraphicsPolygonItem *pol = new QGraphicsPolygonItem(parent);
        myPolygon << QPointF(-1, -152) << QPointF(1, -152)
                          << QPointF(1, -150) << QPointF(-1, -150)
                          << QPointF(-1, -152);
    //myPolygon << QPointF(-100, 0) << QPointF(0, 100)
    //         << QPointF(100, 0) << QPointF(0, -100)
    //         << QPointF(-100, 0);
        pol->setPolygon(myPolygon);
        pol->setVisible(false);
        break;
        }
        case Step:
    {
            myPolygon << QPointF(-100, -50) << QPointF(100, -50)
                      << QPointF(100, 50) << QPointF(-100, 50)
                      << QPointF(-100, -50);
             setPolygon(myPolygon);
            break;
    }
        default:
            myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
                      << QPointF(120, 80) << QPointF(70, -80)
                      << QPointF(-120, -80);
            break;
    }



    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
//! [0]

//! [1]
void DiagramItem::removeArrow(Arrow2 *arrow)
{
    int index = arrows.indexOf(arrow);

    if (index != -1)
        arrows.removeAt(index);
}
//! [1]

//! [2]
void DiagramItem::removeArrows()
{
    foreach (Arrow2 *arrow, arrows) {
        arrow->startItem()->removeArrow(arrow);
        arrow->endItem()->removeArrow(arrow);
        scene()->removeItem(arrow);
        delete arrow;
    }
}
//! [2]

//! [3]
void DiagramItem::addArrow(Arrow2 *arrow)
{
    arrows.append(arrow);
}
//! [3]

//! [4]
QPixmap DiagramItem::image() const
{
    QPixmap pixmap(250, 250);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(QPen(Qt::black, 8));
    painter.translate(125, 125);
    painter.drawPolyline(myPolygon);

    return pixmap;
}
//! [4]

//! [5]
void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    scene()->clearSelection();
    setSelected(true);
    myContextMenu->exec(event->screenPos());
}
//! [5]

//! [6]
QVariant DiagramItem::itemChange(GraphicsItemChange change,
                     const QVariant &value)
{
    if (change == QGraphicsItem::ItemPositionChange) {
        foreach (Arrow2 *arrow, arrows) {
            arrow->updatePosition();
        }
    }

    return value;
}
//! [6]


void DiagramItem::setID(int id)
{
	ID=id;
}
void DiagramItem::setNumberB(int num)
{
        NUMBERB=num;
}
void DiagramItem::setCom(QString text)
{
	Com = text;
}

void DiagramItem::dropEvent ( QGraphicsSceneDragDropEvent * event )
{
	QGraphicsItem *a=scene()->selectedItems()[0];
	DiagramItem *item;
	item=qgraphicsitem_cast<DiagramItem *>(a);

}

void DiagramItem::setText(QGraphicsItem *item, QString text)
{
    QString text2;
    QString text3;

    text2=text;
    text3=text;

    if (text.size()>65)
    {
        int tempcount=0;

        while (text.size()>30*(tempcount+1))
        {
            text3.insert(30*(tempcount+1)+tempcount, "\n");
            tempcount++;
        }

        text2.truncate(65);
        text2.push_back((QString)"...");
        item->setToolTip(text3);
    }


    DiagramTextItem *textItem = new DiagramTextItem(item);
    textItem->setPos(-90.0,-40.0);
    textItem->setTextWidth(180);
    textItem->setPlainText("");
    textItem->setPlainText(text2);
}

//отрисовка на блоке веса
void DiagramItem::setWeight(QGraphicsItem *item, int weight)
{
    QString temp="";

    temp.setNum(weight);

    DiagramTextItem *textItem = new DiagramTextItem(item);
    textItem->setPos(-100.0,30.0);
    textItem->setPlainText(temp);
}

//присваивает значение веса итему
void DiagramItem::setWeightNum(int WEIGHT)
{
        weight = WEIGHT;
}

//присваивает значение веса итему
void DiagramItem::setNumberNum(int num)
{
        NUMBERB = num;
}

//отрисовка на блоке номера
void DiagramItem::setNumber(QGraphicsItem *item, int num)
{
    QString temp="";

    temp.setNum(num);

    DiagramTextItem *textItem = new DiagramTextItem(item);
    textItem->setPos(70.0,30.0);
    textItem->setPlainText(temp);
}

DiagramItem::DiagramItem(ItemInfo info,QGraphicsScene *scene)
{
    DiagramItem *item = new DiagramItem(DiagramItem::Step,NULL,NULL, scene);
    item->setX(info.x);
    item->setY(info.y);
    item->setCom(info.comment);
    item->setID(info.id);
    item->setWeightNum(info.weight);
    //пока не уверен что передавать нмоер блока хорошая идея
    //item->setNumberNum(info.number);
}

ItemInfo DiagramItem::getItemInfo()
{
    ItemInfo info;
    info.x=this->x();
    info.y=this->y();
    info.comment=this->Com;
    info.id=this->ID;
    info.weight=this->weight;
    //так же не уверен стоит ли его возвращать
    //info.number=this->NUMBERB;
    return info;
}
