/***************************************************************************
 *   Copyright (C) 2007 by Andreas Krumnow   *
 *   andreas@krumnow.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// #include <math.h>
#include "zeigerfeld.h"

ZeigerFeld::ZeigerFeld(QWidget *parent)
 : QWidget(parent)
{
    paneH = 80;
    paneW = 40;
    bkCol = new QColor(0,0,0,255);
    pendelCol = new QColor(255,200,205,255);
    ausschlag = 0;
    grad = 45;
    swinging = false;
    from_left2right = true;
    setMaxWinkel(90);
    dbg_txt = "dbg:";
    
    clock = new QTimer(this);
    connect(clock, SIGNAL(timeout()), this, SLOT(move()));

    setPalette( QPalette(*bkCol));
    setBackgroundRole( QPalette::Window);
    setAutoFillBackground(true);
    resize(100, 150);
}

ZeigerFeld::~ZeigerFeld()
{
    delete bkCol;
    delete pendelCol;
    delete clock;
}

void ZeigerFeld::paintEvent(QPaintEvent *event)
{
    paneH = this->height();
    paneW = this->width();
    stift = new QPainter(this);
    drawPendel(stift);
    delete stift;
}

void ZeigerFeld::setMaxWinkel(int w)
{
    if (( w <= (MAX_WINKEL)) && (w >= 4)){
        maxWinkel = w;
    } else {
        maxWinkel = 4;
    }
    resetZeiger();
}

void ZeigerFeld::setClockTempo(int tpo)
{
    tempo = tpo;
    curStepwidth = (int)((maxWinkel * tpo)/1800);
    if (curStepwidth < 1) curStepwidth = 1;
    int ms = (int)((1000*60)/(maxWinkel * tpo )) * curStepwidth;
    clock->setInterval(ms);
    cur_ms = ms;
}

void ZeigerFeld::move()
{
    if (from_left2right){
        grad += (curStepwidth);
        if ( grad >= maxWinkel){ 
            from_left2right = false; 
        }
    } else {
        grad -= (curStepwidth);
        if ( grad <= 0){ 
            from_left2right = true; 
        }
    }
    ausschlag = grad - (maxWinkel/2);
    update();
}

void ZeigerFeld::startStop()
{
    if (swinging){
        swing(false);
    } else {
        swing(true);
    }
}

void ZeigerFeld::swing(bool b)
{
    if(b){
        clock->start((int)cur_ms);
        swinging = true;
    }else{
        clock->stop();
        swinging = false;
        resetZeiger();
    }
}

void ZeigerFeld::resetZeiger()
{
    grad = maxWinkel/2;
    ausschlag = 0;
    update();
}

void ZeigerFeld::drawPendel(QPainter *stift)
{
    int x, y, x1, y1, ww, wz, wy, pgbottom, pgtop;
    x = paneW /2;
    y = paneH/2;
    ww = (int)(paneW/16);
    wz = (int)(paneW/64);
    wy = (int)(paneH*0.75);
    x1 = x + (int)(ausschlag);
    y1 = (int) ausschlag / 10;
    pgtop = (int) ((double)(tempo-20)*(double)wy/280);
    pgbottom = pgtop + (wy/24)+2;
    QPointF points[4] = {
        QPointF(-wz-5, -(wy-pgtop)),
        QPointF( wz+5, -(wy-pgtop)),
        QPointF( wz, -(wy-pgbottom)),
        QPointF(-wz, -(wy-pgbottom))
    };
    stift->setPen(Qt::NoPen);
    stift->translate(x, paneH);
    stift->setBrush(Qt::lightGray);         // set Zeiger-color
    stift->rotate((float)ausschlag);
    stift->drawRect(QRect(-(wz/2), (wz/2), wz, -wy));
    stift->setBrush(Qt::darkGray);          // set Gewicht-color
    stift->drawPolygon(points,4);
    stift->setBrush(Qt::gray);              // set Achse-color
    stift->rotate((float)-ausschlag);
    stift->drawPie(QRect(-(ww/2)+1, (ww/2)+1, ww, -ww), 0, 180 * 16);
    
    /* / debug...
    stift->setPen(Qt::white);
    stift->drawText(10,40,dbg_txt);
    stift->setPen(Qt::white);
    stift->drawText(10,40,"tempo :");
    stift->drawText(100,40,QString::number(tempo, 'F', 3));
    stift->drawText(10,52,"wy :");
    stift->drawText(100,52,QString::number(wy, 'F', 3));
 //   stift->drawText(10,64,"pgt");
 //   stift->drawText(100,64,QString::number(pgt, 'F', 3));
    stift->drawText(10,76,"pgtop");
    stift->drawText(100,76,QString::number(pgtop, 'F', 3));
    stift->drawText(10,88,"pgbottom");
    stift->drawText(100,88,QString::number(pgbottom, 'F', 3));
    // */
//    stift->rotate((float) -ausschlag);
//    stift->translate(0,0);
}