//-----------------------------------------------------------------------------
///
/// \brief  Dummy Display, Qt Widget
///
///         Just use Qt for graphical drawing
///
/// \date   20151226
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


//---Includes------------------------------------------------------------------


//---General--------------------------

#include <string.h>     // strlen
//#include <unistd.h>     // sleep

//---Own------------------------------

#include <biwak/gdisplay.h>
#include <biwak/linux/gdisplay_qt.h>
#include <biwak/output.hpp>
#include <biwak/biwak.h>
#include <biwak/timer.hpp>      // delay
#include <biwak/spi.hpp>
#include <lepto/log.h>
#include <biwak/font.h>
#include <biwak/linux/boardgui.h>

//#include <biwak/linux/boardgui.h>

#include <QWidget>
#include <QPainter>
#include <QLabel>


//---Defines-------------------------------------------------------------------



//---Implementation------------------------------------------------------------


/// \brief  Le contructeur
///
CGraphicDisplayQt::CGraphicDisplayQt( QLabel *widget, int xs, int ys)
   :CGraphicDisplay(xs,ys)
   ,m_widget(widget) // pBoardGui->getGraphicDispay()
{
   m_fx1=0;
   m_fy1=0;
   m_fx2=xs-1;
   m_fy2=ys-1;
    
   if(!m_widget)
   {
      CBoardGui* gui=CBoardGui::instance();
      if(gui)
      {
         m_widget=gui->createDisplay();
      }
   }
    
   if(m_widget)
   {
      m_pixmap = new QPixmap(xs,ys);
      m_widget->setPixmap( *m_pixmap );
      m_pixmap->fill( Qt::black );
   }
   else
   {
      m_pixmap=nullptr;
   }
       
   //QPainter p( &m_pixmap );
   //p.clea
   //m_pixmap.drawLine( 0,0,xs-1,ys-1 );

   return;
}


/// \brief  Le destructeur
CGraphicDisplayQt::~CGraphicDisplayQt()
{
   return;
}


void CGraphicDisplayQt::drawPixels( const TRgb *pixels, int count) /* virtual abstract */
{
   if(!m_pixmap)
   {
       return;
   }
   
   QPainter p( m_pixmap );

   for(int i1=0; i1<count; i1++)
   {
      p.setPen( QColor(pixels[i1].red , pixels[i1].green , pixels[i1].blue ) );
      p.drawPoint(xPos++, yPos);
      checkFrame();
   }
   
   if( m_widget )
   {
      m_widget->setPixmap( *m_pixmap );
   }
   //widget->update();
}


void CGraphicDisplayQt::drawPixel( const TRgb color) /* virtual abstract */
{
   if(!m_pixmap)
   {
      return;
   }
   //QPixmap *pixmap=new QPixmap( *widget->pixmap() );
   //QPainter *p=new QPainter( &m_pixmap );
   //static int loop=0;
   //loop++;

   //QPainter p( widget );
   QPainter p( m_pixmap );

   //p.begin( &m_pixmap );
   p.setPen( QColor(color.red, color.green, color.blue ) );
   //p.setPen( ( xPos & 1 ) ? Qt::red : Qt::white  );
   p.drawPoint(xPos++, yPos);
   checkFrame();
   //p.end();
   if(m_widget)
   {
      m_widget->setPixmap( *m_pixmap );
   }

   //widget->update();
   //delete &p;
   //delete pixmap;
}


void CGraphicDisplayQt::gotoxyHw()
{
    return;
}
#if 1
void CGraphicDisplayQt::setFrame(int x1, int y1, int x2, int y2)
{
    m_fx1=x1;
    m_fx2=x2;
    m_fy1=y1;
    m_fy2=y2;
    xPos=x1;
    yPos=y1;
    return;
}
#endif
bool CGraphicDisplayQt::setRotation(bool rot)
{
    return(false);
}

void CGraphicDisplayQt::checkFrame()
{
    if(xPos>m_fx2)
    {
        xPos=m_fx1;
        yPos++;
    }

    if(yPos>m_fy2)
    {
        yPos=m_fy1;
    }

    return;
}


//---fin-----------------------------------------------------------------------
