#include "qpush1.h"

QPush1::QPush1(const QString msg , int  tabposition ,  QWidget *parent) 
: QWidget(parent),textBut(msg),nummer(tabposition),ActiveButton(false)
{
    initValue();
    //////SetModus(true);QString text
    setCursor(QCursor(Qt::PointingHandCursor));
    setMaximumHeight (44);
}
void QPush1::initValue()
{
    pushed = false;
    focus = false;
    colBut = Qt::blue;
    fontText = QFont("Verdana",30,QFont::StyleItalic);
    fontText.setStyleStrategy(QFont::PreferBitmap);
    textBut = "Bottone di Spinta .";
    colText = Qt::gray;
    click = 0;
    colActiveText = Qt::white;
    timeline = 0;
    cursortime = false;
    linearmove = 15;
    story.clear();
    ////////device = new DotPoint(this);
}


void QPush1::timerEvent(QTimerEvent *event)
{
	 cursortime = false;
    if (event->timerId() == cursorTimeLine.timerId()) {
            timeline++;
            if (timeline%2) {
							cursortime = true; 
            } else {
							cursortime = false;
            }
    }
}

void QPush1::paintEvent(QPaintEvent *)
{
    paintText();
}

void QPush1::setid( int tab )
{
    
    if ( nummer == tab ) {
       ActiveButton = true; 
    } else {
       ActiveButton = false;  
    }
    /////qDebug() << "### focus button   .." << focus;
    update(rect());
}







void QPush1::SetModus( bool e )
{
    
   focus = e;
   return;
    
   if (e) {
     cursorTimeLine.start( QApplication::cursorFlashTime() / 100,this);
   } else {
     cursorTimeLine.stop();
   }
}
void QPush1::painthistory()
{
    QPainter *p = new QPainter(this);
    p->setRenderHint(QPainter::Antialiasing);
    const QRectF reo = TXTrect;
    p->setWindow (0,0,  reo.width() , reo.height() );
    p->setPen(QPen(Qt::red,5, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
    
    
    if (story.size() > 0) {
        QMapIterator<int,QPointF> i(story);
         while (i.hasNext()) {
             i.next();
             /////path.lineTo(i.value());
             p->drawPoint(i.value());
         }
    }
    p->end();
    
}
void QPush1::paintText()
{
    QPainter *pg = new QPainter(this);
    pg->setRenderHint(QPainter::Antialiasing);
    const int borderI = 18;
    QPointF baseline(2,55 * 0.98);  /* font size */
     /* font size */
    /* button text */
    QPainterPath textPath;
    QPainterPath textPath2;
    textPath.addText(baseline,fontText,textBut);  ////  fontText is any font() //// textBut text one line
    /* fix view from text */
    const QRectF reo = textPath.boundingRect();
    TXTrect = reo;
    const QRectF paintArea( 0 , 0 ,  reo.width() + (borderI * 2), reo.height() + (borderI * 2) );
    //////QPixmap imgtext(reo.size().toSize()); 
    ////////QPainter *p = new QPainter(&imgtext);
    //////////p->setRenderHint(QPainter::Antialiasing);
    pg->setWindow ( 0 , 0 ,  reo.width() + (borderI * 2), reo.height() + (borderI * 2) );
    QRectF imagepos( borderI , borderI ,  reo.width() , reo.height() );
    QPointF textline(borderI / 2 ,reo.height() + borderI ); 
    textPath2.addText(textline,fontText,textBut);
    if (pushed || focus || ActiveButton ) {
    pg->setPen(QPen(Qt::red,1, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
    pg->setBrush(Qt::gray);
    pg->drawRect(paintArea);
    pg->setBrush(colActiveText);
    pg->drawPath(textPath2);     
    } else {
    pg->setPen(QPen(Qt::lightGray,2, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
    pg->setBrush(Qt::white);
    pg->drawRect(paintArea);
    pg->setBrush(colText);
    pg->drawPath(textPath2);  
    }
    pg->end();
    
    QMatrix matrix;
    matrix.scale(paintArea.width() / width(),paintArea.height() / height());
    emit CurrentMatrix(matrix,paintArea);
}

void QPush1::setText(QString text)
{
    textBut = text;
    update();
}

void QPush1::setFont(QFont font)
{
    fontText = font;
    update();
}

void QPush1::setColorText(QColor colorTx)
{
    colText = colorTx;
    update();
}

void QPush1::setColorButton(QColor colorBt)
{
    colBut = colorBt;
    update();
}

QSize QPush1::minimumSizeHint() const
{
    return QSize(20, 10);
}

QSize QPush1::sizeHint() const
{
    return QSize(200,45);
}


void QPush1::mousePressEvent(QMouseEvent *e)
{
    double x = e->x();
    double y = e->y();
    click++;
    qreal wrun = width();
    qreal hrun = height();
    QMatrix matrix;
    pushed = true;
    
    /////const qreal sx = TXTrect.width() / width();
    ///////////////qDebug() << "### press " << nummer;
    
    matrix.scale(TXTrect.width() / width(),TXTrect.height() / height());
    
    ///////matrix.scale(-6,-6);
    QPointF des = matrix.map(e->posF());
    //////qDebug() << "### des 0  ......" << des;
    /////qDebug() << "### des 1 " << e->posF();
    QDateTime tic = QDateTime::currentDateTime ();  /////  tic.toTime_t() only one per second 
    story.insert(click,des);
    update();
    emit indextab(nummer);
    //////qDebug() << "### size  " << story.size();
    
}

void QPush1::mouseReleaseEvent(QMouseEvent *e)
{
    double x = e->x();
    double y = e->y();

    double wrun = width();
    double hrun = height();

    double wstart = 200;
    double hstart = 100;

    double dx = wrun/wstart;
    double dy = hrun/hstart;

    if (y>=(5*dy) && y<=(95*dy) && x>=(5*dx) && x<=(195*dx))
    {
        pushed = false;
        update();
    }
}

void QPush1::enterEvent(QEvent *)
{
    focus = true;
    update();
}

void QPush1::leaveEvent(QEvent *)
{
    focus = false;
    update();
}
