#include "LightShowWidget.h"
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <math.h>
#include <QScrollBar>


#define VIEW_CENTER viewport()->rect().center()
#define VIEW_WIDTH viewport()->rect().width()
#define VIEW_HEIGHT viewport()->rect().height()

CLightShowWidget::CLightShowWidget(QWidget *parent)
{
    mScale = 1.0;
    mDoMousePan = false;

    setParent(parent);
    mScene = new QGraphicsScene(this);
    mScene->setItemIndexMethod(QGraphicsScene::NoIndex);
    mScene->setSceneRect(-5000, -5000, 10000, 10000);
  //    scene->setSceneRect(-100,-100,10000,10000);
    setScene(mScene);
  //  setMinimumSize(500,500);
//    setMaximumSize(parent->size());

    setTransformationAnchor(AnchorUnderMouse);
//    setTransformationAnchor(AnchorViewCenter);
    setDragMode(QGraphicsView::NoDrag);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    mViewportRect = new QGraphicsRectItem(0,0,50,50);
    mViewportRect->setPen(QPen(Qt::darkBlue));
    mSceneRect = new QGraphicsRectItem(sceneRect());
    mSceneRect->setPen(QPen(Qt::red));
    mLedStringRect = new QGraphicsRectItem();
    mLedStringRect->setPen(QPen(Qt::darkGreen));
   // test->setBrush(QBrush(Qt::darkBlue));
//    mScene->addItem(mViewportRect);
//    mScene->addItem(mSceneRect);
//    mScene->addItem(mLedStringRect);



    mLedStringWidget = new CLedStringWidget(300);
    mScene->addItem(mLedStringWidget);
 //   mLedStringWidget->setTransform(QTransform::fromScale(10, 10), true);
    mLedStringWidget->setPos(0,100);

    setSceneRect(-100,-100,parentWidget()->geometry().width(),parentWidget()->geometry().height());
    setMinimumSize(parentWidget()->geometry().width(),parentWidget()->geometry().height());

    mScene->views().at(0)->setBackgroundBrush(QBrush(QColor(Qt::black)));




}

void CLightShowWidget::wheelEvent(QWheelEvent *event)
 {
     ScaleView(pow((double)2, event->delta() / 240.0));
 }

void CLightShowWidget::ScaleView(qreal scaleFactor)
 {
     qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
     if (/*factor < 0.07 || */factor > 1)
         return;

     scale(scaleFactor, scaleFactor);
     mScale *= scaleFactor;


    qreal deltaW = factor * sceneRect().width();
    qreal deltaH = factor * sceneRect().height();

 }

void CLightShowWidget::ZoomIn()
 {
     ScaleView(qreal(1.2));
 }

 void CLightShowWidget::ZoomOut()
 {
     ScaleView(1 / qreal(1.2));
 }

 void CLightShowWidget::showEvent(QShowEvent *event)
 {
     mNominalTransform = transform();
     mLedStringRect->setRect(mLedStringWidget->boundingRect());
 }

 void CLightShowWidget::mousePressEvent(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton){
         mLasMousePos = event->pos();
         mDoMousePan = true;
     }
     QGraphicsView::mousePressEvent(event);
 }

 void CLightShowWidget::mouseMoveEvent(QMouseEvent *event)
 {
     if (mDoMousePan){
         QPointF mouseDelta = mapToScene(event->pos()) - mapToScene(mLasMousePos);
         Pan(mouseDelta);
     }

     QGraphicsView::mouseMoveEvent(event);
     mLasMousePos = event->pos();
 }

 void CLightShowWidget::mouseReleaseEvent(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton){
         mDoMousePan = false;
     }
     QGraphicsView::mouseReleaseEvent(event);
 }

 void CLightShowWidget::Pan(QPointF delta)
 {
     // Scale the pan amount by the current zoom.
 //    delta *= mScale;
     delta *= PAN_SPEED;
//     qDebug("Moving by x:%f, y:%f",delta.x(),delta.y());

     delta.setX(-delta.rx());
     delta.setY(-delta.ry());

    QRectF NewViewRect = sceneRect();
     NewViewRect.translate(delta.x(),delta.y());


     mCurTransform = transform();
     setSceneRect(NewViewRect);


     mViewportRect->setRect(viewport()->geometry());
     mSceneRect->setRect(sceneRect());
 //    qDebug("Scene Rect: left: %f, right:%f, top:%f, bottom:%f",sceneRect().left(),sceneRect().right(),sceneRect().top(),sceneRect().bottom());
     // Have panning be anchored from the mouse.
     setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

 }
