#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Pixmap = new QPixmap(128, 128);

}

MainWindow::~MainWindow()
{
    delete Pixmap;
    delete ui;
}

void MainWindow::SetFreeTypeModel(TFreeTypeModel* freeTypeModel)
{
    FreeTypeModel = freeTypeModel;

    //Set signals
    QObject::connect(ui->LoadFontButton, SIGNAL(pressed()), FreeTypeModel, SLOT(LoadFont()));
    QObject::connect(ui->RenderImageButton, SIGNAL(pressed()), FreeTypeModel, SLOT(RenderFontSlot()));
    QObject::connect(ui->SaveButton, SIGNAL(pressed()), FreeTypeModel, SLOT(SaveFont()));

    QObject::connect(ui->selectBackgroundColor, SIGNAL(pressed()), FreeTypeModel, SLOT(SelectBackgroundColorPressed()));

    QObject::connect(ui->selectFontColor, SIGNAL(pressed()), FreeTypeModel, SLOT(SelectFontColorPressed()));


    RefreshColorMarkers();
}

void MainWindow::SetFontNameTextSlot(QString text)
{
    ui->FontFileNameLabel->setText("File name: "+text);
}

void MainWindow::SetLoadResultTextSlot(QString text)
{
    ui->LoadLibResultLabel->setText("Loading result: "+text);
}

void MainWindow::InvalidatePixmap()
{
    ui->Image->setPixmap(*Pixmap);
}

int MainWindow::GetFaceWidth()
{
    return ui->faceWidthEdit->text().toInt();
}

int MainWindow::GetFaceHeight()
{
    return ui->faceHeightEdit->text().toInt();
}

int MainWindow::GetAreaWidth()
{
    return ui->Image->width();
}

int MainWindow::GetAreaHeight()
{
    return ui->Image->height();
}

int MainWindow::GetIntervalX()
{
    return ui->IntervalXEdit->text().toInt();
}

int MainWindow::GetIntervalY()
{
    return ui->IntervalYEdit->text().toInt();
}

QString MainWindow::GetSymbolList()
{
    return ui->CharList->toPlainText();
}

void MainWindow::RefreshSheetHeightWidth()
{
    int width = (ui->SheetWidthEdit->text()).toInt();
    int height = (ui->SheetHeightEdit->text()).toInt();

    QRect r(ui->frame->geometry());
    r.setWidth(width);
    r.setHeight(height);
    ui->frame->setGeometry(r);

    r = QRect(ui->Image->geometry());
    r.setWidth(width);
    r.setHeight(height);
    ui->Image->setGeometry(r);

}

void MainWindow::SaveOutputEdit(std::vector<QString>& fontParamText, QString filename)
{

    QFile file(filename);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;

    QTextStream out(&file);
    for (std::vector<QString>::iterator i = fontParamText.begin(); i != fontParamText.end(); i++)
    {
        out << *i<<"\n";
    }

    file.close();

}

void MainWindow::UnlockDrawButton()
{
    ui->RenderImageButton->setEnabled(true);
}

void MainWindow::UnlockSaveButton()
{
    ui->SaveButton->setEnabled(true);
}

void MainWindow::RefreshColorMarkers()
{
    QPixmap* colorPixmap = new QPixmap(21,21);

    colorPixmap->fill(FreeTypeModel->GetBackgroundColor());
    ui->backgroundColorMarker->setPixmap(*colorPixmap);

    colorPixmap->fill(FreeTypeModel->GetFontColor());
    ui->fontColorMarker->setPixmap(*colorPixmap);

    delete colorPixmap;


    //Pixmap also:
    Pixmap->fill(FreeTypeModel->GetBackgroundColor());

}

