#include "hausdorff_setting.h"
#include "ui_hausdorff_setting.h"


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

    paletteERR = ui->BtnLoadRef->palette();
    paletteOK = ui->BtnLoadRef->palette();
    paletteERR.setColor(QPalette::Button,QColor("red"));
    //paletteOK.setColor(QPalette::Button,QColor("green"));
    ui->BtnLoadImg->setPalette(paletteERR);
    ui->BtnLoadRef->setPalette(paletteERR);


    Time=0;
    Time_ref=0;
    CanHRun=false;
    ResultIsValid=false;
    image=NULL;
    image_ref=NULL;
    haus=NULL;
    this->form=new hausdorf_form(this);

    connect(ui->vysledek,SIGNAL(clicked()),this,SLOT(ShowResult()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(ShowForm()));
    connect(ui->BtnLoadRef,SIGNAL(clicked()),this,SLOT(NeedLoadRefImage()));
    connect(ui->BtnLoadImg,SIGNAL(clicked()),this,SLOT(NeedLoadImage()));
}

hausdorff_setting::~hausdorff_setting()
{
    delete form;
    delete ui;
    if(image!=NULL)
        delete image;
    if(image_ref!=NULL)
        delete image_ref;
    if(haus!=NULL)
        delete haus;
}


void hausdorff_setting::load_image(my_image *img){
    if(this->image!=NULL)
        delete this->image;
    this->image=new my_image(img);
    ui->BtnLoadImg->setEnabled(false);
    ui->BtnLoadImg->setPalette(this->paletteOK);
    if(CanHRun==true){
        //QMessageBox::information(this,tr("Vypocet"),tr("Zacninam s vypoctem HD\nOperace muze trvat dlouho, vyckej\nasi tu taky dodelam kontrolu velikosti"));
        my_image *pom=form->apply_filt(image);

        QTime time;
        time.start();
        haus->find(pom);
        Time = time.elapsed();
        delete pom;
        ui->time_find->setText(QString::number(Time));

        this->position.erase(position.begin(),position.end());
        std::vector<BOD> ff(haus->body.begin(),haus->body.begin()+this->form->Count_Number+1);
        this->position=ff;
        ResultIsValid=true;
        //QMessageBox::information(NULL,tr("Vypocet"),tr("Vypocet byl dokoncen"));
    }
}


void hausdorff_setting::load_ref_image(my_image *img){
    if(this->image_ref!=NULL)
        delete this->image_ref;

    if(this->image!=NULL){
        delete this->image;
        this->image=NULL;
        ui->BtnLoadImg->setEnabled(true);
        ui->BtnLoadImg->setPalette(this->paletteERR);
    }

    this->image_ref=new my_image(img);
    ui->BtnLoadRef->setEnabled(false);
    ui->BtnLoadRef->setPalette(this->paletteOK);
    //QMessageBox::information(this,tr("Ref. img"),tr("budu pocitat ref. snimek\nOperace muze trvat dlouho, vyckej"));
    if(haus!=NULL)
        delete haus;
    my_image *pom=form->apply_filt(image_ref);
    QTime time;
    time.start();
    haus=new hausdorff(pom,this->form->registruj);
    Time_ref = time.elapsed();
    delete pom;
    ui->time->setText(QString::number(Time_ref));
    CanHRun=true;
    //QMessageBox::information(NULL,tr("Ref. img"),tr("Ref. obraz nacten"));
}




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

void hausdorff_setting::ShowForm(){
    if(this->image_ref==NULL)
        return;
    RefImageIsLoaded();
    form->load_image(image_ref);
    this->form->show();
}


void hausdorff_setting::RefImageIsLoaded(){
    ui->BtnLoadRef->setEnabled(true);
    ui->BtnLoadRef->setPalette(this->paletteERR);
    CanHRun=false;
    ResultIsValid=false;
}

void hausdorff_setting::ImageIsLoaded(){
    ui->BtnLoadImg->setEnabled(true);
    ui->BtnLoadImg->setPalette(this->paletteERR);
    ResultIsValid=false;
}

void hausdorff_setting::NeedLoadRefImage(){
    emit NeedLoadRef(1);
}

void hausdorff_setting::NeedLoadImage(){
    emit NeedLoadImg(1);
}



void hausdorff_setting::ShowResult(){
    if(ResultIsValid){
        show_point_on_image(this->image_ref,this->position,this->image->Width());
    }
}
