// LICENSE CC-BY-SA : http://creativecommons.org/licenses/by-sa/2.0/fr/

#include "Visualiseur.h"
#include "ui_Visualiseur.h"

Visualiseur::Visualiseur(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Visualiseur)
{
    this->show();
    ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(loadFile()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(loadFrac()));
    connect(ui->lignes, SIGNAL(valueChanged(int)), this, SLOT(refresh()));
    connect(ui->colonnes, SIGNAL(valueChanged(int)), this, SLOT(refresh()));

    filePath = new QString();
    draw = new QGraphicsScene();
    ui->graphicsView->setRenderHint(QPainter::HighQualityAntialiasing, true);
    line = new QString("");

    refresh();
}

Visualiseur::~Visualiseur()
{
    delete ui;
}

void Visualiseur::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Visualiseur::loadFile()
{
    *filePath = QFileDialog::getOpenFileName(this, "Choisir un fichier plein de décimales trop coules", "", "Fichier texte (*.txt)");
    if(*filePath == "")
    {
        ui->pushButton->setText("Load file");
    } else {
        ui->pushButton->setText(QString("Load file (").append(*filePath).append(")"));
        QFile *file = new QFile(*filePath);
        file->open(QIODevice::ReadOnly);
        QTextStream stream(file);
        *line = stream.readLine();
        file->close();
        refresh();
    }
}

void Visualiseur::loadFrac()
{
    *line = amoi(ui->spinBox->value(),ui->spinBox_2->value(),ui->lignes->value()*ui->colonnes->value());
    refresh();
}

void Visualiseur::refresh()
{    
    draw->clear();
    line->truncate(ui->colonnes->value()*ui->lignes->value());
    QString linebackup = *line;
    ui->total->setText(QString::number(ui->lignes->value()*ui->colonnes->value()));
    ui->preview->setText(*line);
    if(*line != "")
    {
        QPen *pen = new QPen();
        pen->setColor(Qt::red);
        pen->setWidth(2);
        QGraphicsRectItem *canevas = new QGraphicsRectItem(-1, -1, ui->colonnes->value(), ui->lignes->value());
        canevas->setPen(*pen);
        draw->addItem(canevas);
        ui->graphicsView->setScene(draw);
        ui->graphicsView->fitInView(canevas, Qt::KeepAspectRatio);

        // On dessine tout le bordel
        for(int j=0; j<ui->lignes->value(); j++)
        {
            for(int i=0; i<ui->colonnes->value(); i++)
            {
                QGraphicsRectItem *rect = new QGraphicsRectItem(canevas);
                rect->setRect(-1,-1,1,1);
                rect->setPen(QPen(Qt::NoPen));
                if(line->startsWith("."))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::red);
                }
                else if(line->startsWith("0"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::black);
                }
                else if(line->startsWith("1"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::lightGray);
                }
                else if(line->startsWith("2"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::gray);
                }
                else if(line->startsWith("3"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::green);
                }
                else if(line->startsWith("4"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::darkGray);
                }
                else if(line->startsWith("5"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::blue);
                }
                else if(line->startsWith("6"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::darkCyan);
                }
                else if(line->startsWith("7"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::yellow);
                }
                else if(line->startsWith("8"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::darkMagenta);
                }
                else if(line->startsWith("9"))
                {
                    rect->setPos(i, j);
                    rect->setBrush(Qt::darkRed);
                }
                line->remove(0,1);
            }
        }
    }
    *line = linebackup;
}

QString Visualiseur::amoi(int p, int q, int precision)
{
    QString result = "";
    double intpart;
    modf(p/q, &intpart);
    result.append(QString::number(intpart));
    result.append(".");
    p = (p-intpart*q)*10;

    for(int i=0; i<precision; i++)
    {
        modf(p/q, &intpart);
        result.append(QString::number(intpart));
        p = (p-intpart*q)*10;
    }

    return result;
}
