#include <QDebug>
#include "grahicstextitem.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsSceneMouseEvent>

GrahicsTextItem::GrahicsTextItem(QGraphicsItem *parent):
    QGraphicsTextItem(parent)
{

}

GrahicsTextItem::GrahicsTextItem(const QString &text, QGraphicsItem *parent):
    QGraphicsTextItem(text, parent)
{
}

void GrahicsTextItem::advance(int phase)
{
    if(phase == 1){
        QRectF br = boundingRect();
        QPointF currentPos = QPointF(pos().x()+br.width(), pos().y()+br.height());
        QRectF rect = ( scene()->sceneRect());
        QPointF bottemRight = rect.bottomRight();
        if(currentPos.x() >= bottemRight.x() || currentPos.y() >= bottemRight.y())
            setPos(0, 0);
        moveBy(10, 10);
    }
}

void GrahicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsTextItem::mousePressEvent(event);
    QRect rect1 = scene()->views().first()->viewport()->rect();
    QRectF rect2 = scene()->sceneRect();
    QRectF br = boundingRect();
    QPointF currentPos = QPointF(pos().x()/*+br.width()*/, pos().y()/*+br.height()*/);

}

void GrahicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsTextItem::mouseReleaseEvent(event);
//    QRect rect1 = scene()->views().first()->viewport()->rect();
    QRect rect1 = scene()->views().first()->rect();
    QRectF rect2 = scene()->sceneRect();
    QRectF br = boundingRect();
    QPointF currentPos = QPointF(pos().x()/*+br.width()*/, pos().y()/*+br.height()*/);

}

void GrahicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsTextItem::mouseMoveEvent(event);
    QRect rect1 = scene()->views().first()->viewport()->rect();
    QRectF rect2 = scene()->sceneRect();
    QRectF br = boundingRect();
    QPointF currentPos = QPointF(pos().x(), pos().y());
}

void GrahicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QGraphicsTextItem::paint(painter, option, widget);
    painter->drawRect(boundingRect());
    QPen pen = painter->pen();
    pen.setColor(Qt::blue);
    painter->setPen(pen);
    painter->drawRect(QRectF(widget->rect().topLeft()-pos(), widget->rect().bottomRight()-pos()));

    pen.setColor(Qt::blue);
    painter->setPen(pen);
    painter->drawLine(widget->rect().topLeft()-pos(), widget->rect().bottomRight()-pos());

    pen.setColor(Qt::blue);
    painter->setPen(pen);
    painter->drawLine(widget->rect().topRight()-pos(), widget->rect().bottomLeft()-pos());

    pen.setColor(Qt::red);
    painter->setPen(pen);
    painter->drawRect(QRectF(scene()->sceneRect().topLeft()-pos(), scene()->sceneRect().bottomRight()-pos()));

    pen.setColor(Qt::red);
    painter->setPen(pen);
    painter->drawLine(scene()->sceneRect().topLeft()-pos(), scene()->sceneRect().bottomRight()-pos());

    pen.setColor(Qt::red);
    painter->setPen(pen);
    painter->drawLine(scene()->sceneRect().topRight()-pos(), scene()->sceneRect().bottomLeft()-pos());

}
