#include "QtLturtle.hpp"

  Lturtle::Lturtle(QWidget *parent) : QWidget(parent)
  {

  }
  

//   void Lturtle::paintEvent(QPaintEvent *event)
//   {
// 
//     QPen pen(Qt::black, 2, Qt::SolidLine);
// 
//     QPainter painter(this);
// 
//     //lines
//     painter.setPen(pen);
//     painter.drawLine(20, 40, 250, 40);
// 
//     //color
//     painter.setPen(QColor("#d4d4d4"));
// 
//   painter.setBrush(QBrush("#c56c00"));
//   painter.drawRect(10, 15, 90, 60);
// 
//   
//   //shape
//   painter.setRenderHint(QPainter::Antialiasing);
//   painter.setPen(QPen(QBrush("#575555"), 1));
// 
//   QPainterPath path1;
// 
//   path1.moveTo(5, 5);
//   path1.cubicTo(40, 5,  50, 50,  99, 99);
//   path1.cubicTo(5, 99,  50, 50,  5, 5);
//   painter.drawPath(path1);  
// 
//   painter.drawPie(130, 20, 90, 60, 30*16, 120*16);
//   painter.drawChord(240, 30, 90, 60, 0, 16*180);
//   painter.drawRoundRect(20, 120, 80, 50);
// 
//   QPolygon polygon;
//   polygon << QPoint(130, 140) << QPoint(180, 170)
//           << QPoint(180, 140) << QPoint(220, 110)
//           << QPoint(140, 100);
//   painter.drawPolygon(polygon);
// 
//   painter.drawRect(250, 110, 60, 60);
// 
//   QPointF baseline(20, 250);
//   QFont font("Georgia", 55);
//   QPainterPath path2;
//   path2.addText(baseline, font, "Q");
//   painter.drawPath(path2);
// 
//   painter.drawEllipse(140, 200, 60, 60);
//   painter.drawEllipse(240, 200, 90, 60);
//   
//   }
  
  void Lturtle::paintEvent(QPaintEvent *event)
  {

    QPen pen(Qt::black, 2, Qt::SolidLine);

    QPainter painter(this);
    painter.setPen(pen);
    
    if (Lsystem != 0){
      int start = Lsystem.lastIndexOf("}");
      int end = Lsystem.length();
      QPointF position (200.0, 200.0);
      qreal r=0.0;
      QPointF destination;
      for (int i = start+1; i< end; i++)
      {
        QChar instruction = Lsystem.at(i);
        if (instruction.toAscii() == 'F')
        {
          destination.setX(position.x() + (qSin(r)*4.0));
          destination.setY(position.y() + (qCos(r)*4.0));
          painter.drawLine(position,destination);
          position.setX(destination.x());
          position.setY(destination.y());
        }
        else if (instruction.toAscii()=='+')
        {
            r += PI/3;
        }
        else if (instruction.toAscii()=='-')
        {
            r -= PI/3;
        }
      }
    }
    else
    {
      painter.drawLine(1,1,398,398);
      painter.drawLine(1,398,398,1);
    }
  }

