/***************************************************************************
 *   Copyright (C) 2004 by Kraschinski Arnold                              *
 *   arnold.kra67@gmx.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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.           *
 ***************************************************************************/
#include "qcounterview.h"

// Standard C++ header files

// Qt header files
#include <QSize>
#include <QPainter>
#include <QPaintEvent>
#include <QBrush>
#include <QColor>
#include <QFont>
#include <QFontMetrics>

// KDE-header files

// project header files

const int QCounterView::farbenR[21] =  {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                                        0xff, 0xe6, 0xc8, 0xa0, 0x7d, 0x64, 0x4b, 0x32, 0x19, 0x00, 
                                        0x00};
const int QCounterView::farbenG[21] =  {0x00, 0x19, 0x32, 0x4b, 0x64, 0x7d, 0xa0, 0xc8, 0xe6, 0xff,
                                        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                                        0xff};
const int QCounterView::farbenB[21] =  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00};

const int   QCounterView::COUNTER_WIDTH = 164;
const int   QCounterView::COUNTER_HEIGHT = 174;

QCounterView::QCounterView(QWidget *parent)
 : QWidget(parent), m_solved(0), m_correct(0)
{
    setMinimumSize( QSize(COUNTER_WIDTH, COUNTER_HEIGHT) );
}

QCounterView::~QCounterView()
{
}

int QCounterView::getExerciseSolved()
{
    return m_solved;
}

int QCounterView::getExerciseCorretSolved()
{
    return m_correct;
}

int QCounterView::getPercentageCorrect()
{
    if (m_solved == 0) {
        return 0;
    } else {
        return m_correct * 100 / m_solved;
    }
}

void QCounterView::slotAdd(int n, int correct) 
{
    if (correct < 0) {
        correct = 0;
    }
    if (correct > n) {
        correct = n;
    }
    m_solved += n;
    m_correct += correct;
    if ( correct > 0 ) {
        emit someCorrectAnswers( correct );
    }
    update();
}

void QCounterView::paintEvent(QPaintEvent* pe) 
{
    pe->accept();
    QPainter p(this);
    p.setBackgroundMode ( Qt::OpaqueMode );
    
    // Corresponding to the percentage of correct answers changes the color
    // of the background and the fonts.
    int percentage = getPercentageCorrect();
    p.setBackground( QBrush( QColor( farbenR[percentage / 5], farbenG[percentage / 5], farbenB[percentage / 5] ) ) );
    p.eraseRect(0, 0, width(), height());

    QFont fontSmall = p.font();
    fontSmall.setPixelSize(14);
    fontSmall.setBold(true);
    QFont fontBig = p.font();
    fontBig.setPixelSize(24);
    
    QString st1(tr("Solved exercises:"));
    QString st2(tr("Correct solutions:"));
    QString st3(tr("Percentage:"));
    QString value;
    int marginX(10);
    int marginY(10);
    int distance(5);
    
    p.setFont(fontBig);
    int bigHeight = p.fontMetrics().height();
    
    p.setFont(fontSmall);
    int smallHeight = p.fontMetrics().height();
    if (percentage / 5 < 6) {
        p.setPen( QColor(0xff, 0xff, 0xff) );
    } else {
        p.setPen( QColor(0x00, 0x00, 0x00) );
    }
    int posY(marginY + smallHeight - 1);
    p.drawText(marginX, posY, st1);
    posY += (smallHeight + bigHeight + distance);
    
    p.drawText(marginX, posY, st2);
    posY += (smallHeight + bigHeight + distance);
    
    p.drawText(marginX, posY, st3);
    
    p.setFont(fontBig);
    if (percentage / 5 < 6) {
        p.setPen( QColor(0xee, 0xee, 0xee) );
    } else {
        p.setPen( QColor(0x5c, 0x5c, 0x5c) );
    }
    posY = marginY + smallHeight - 1 + bigHeight;
    value.setNum(m_solved);    
    p.drawText(marginX * 2, posY, value);
    posY += (bigHeight + smallHeight + distance);
    
    value.setNum(m_correct);
    p.drawText(marginX * 2, posY, value);
    posY += (bigHeight + smallHeight + distance);    

    value.setNum(percentage);
    value += " %";
    p.drawText(marginX * 2, posY, value);
    
    p.end();
}


#include "qcounterview.moc"
