#include "hausdorfm_form.h"
#include "ui_hausdorfm_form.h"

hausdorfm_form::hausdorfm_form(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::hausdorfm_form)
{
    ui->setupUi(this);
    ChangeNumber(ui->Text_Edge_size->text().toInt());

    img=NULL;

    connect(ui->number,SIGNAL(valueChanged(int)),this,SLOT(ChangeNumber(int)));
    connect(ui->Check_Edge,SIGNAL(toggled(bool)),this,SLOT(Check_Change()));

    connect(ui->Check_Harris,SIGNAL(toggled(bool)),this,SLOT(Check_Change()));
    connect(ui->Check_Prah,SIGNAL(toggled(bool)),this,SLOT(Check_Change()));
    connect(ui->Check_Prah_Black,SIGNAL(toggled(bool)),this,SLOT(Check_Change()));
    connect(ui->Check_Gaus,SIGNAL(toggled(bool)),this,SLOT(Check_Change()));
    connect(ui->Check_Reg,SIGNAL(toggled(bool)),this,SLOT(Check_Change()));

    connect(ui->Btn_OK,SIGNAL(clicked()),this,SLOT(Btn_OK()));
    connect(ui->Btn_Show,SIGNAL(clicked()),this,SLOT(Show_image()));
    connect(ui->Btn_Show_2,SIGNAL(clicked()),this,SLOT(Show_image()));


    registruj=ui->Check_Reg->checkState();
}

hausdorfm_form::~hausdorfm_form()
{
    if(this->img!=NULL)
        delete img;
    delete ui;
}



my_image * hausdorfm_form::apply_filt(my_image *new_image){
    my_image *ret=new my_image(new_image);
    my_image *pom;

    if(ui->Check_Gaus->checkState()){
        int maska= ui->Text_Gaus_size->text().toInt();
        if(!(maska==1||maska==3||maska==5||maska==7||maska==9||maska==11)){
            maska=1;
            ui->Text_Gaus_size->setText(QString::number(maska));
        }
        pom= gauss_smooth(ret,maska);
        delete ret;
        ret=pom;
    }


    if(ui->Check_Edge->checkState()){
        double p1=ui->Text_Edge_prah1->text().toDouble();
        double p2=ui->Text_Edge_prah2->text().toDouble();
        int s=ui->Text_Edge_size->text().toInt();
        if(!(s==1||s==3||s==5||s==7||s==9||s==11)){
            s=1;
            ui->Text_Edge_size->setText(QString::number(s));
        }
        pom=edge(ret,p1,p2,s);
        delete ret;
        ret=pom;
    }


    if(ui->Check_Harris->checkState()){
        int okno=ui->Text_Harris_okno->text().toInt();
        if(okno<=0){
            okno=1;
            ui->Text_Edge_size->setText(QString::number(okno));
        }
        my_image_T<int> * pom2;
        if(ui->Check_Harris_CV)
            pom2=harris_cv(ret,okno);
        else
            pom2=harris(ret,okno);

        delete ret;
        ret=pom2->get_my_image();
        delete pom2;
        ret->invert_color();
    }

    if(ui->Check_Prah->checkState()){
        int prah=ui->Text_Prah->text().toInt();
        if(prah<=0){
            prah=1;
            ui->Text_Edge_size->setText(QString::number(prah));
        }
        ret->prahuj(prah,ui->Check_Prah_Black->checkState());
    }


    return ret;
}


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


void hausdorfm_form::load_image(my_image *image){
    if(img!=NULL)
        delete img;
    img=new my_image(image);
}




//sloty:

void hausdorfm_form::ChangeNumber(int num){
    this->Count_Number=num;
}



void hausdorfm_form::Btn_OK(){
    this->close();
    emit this->Form_Close();
}

void hausdorfm_form::Show_image(){
    if(img==NULL)
        return;

    my_image * newImage=this->apply_filt(img);
    show_image(newImage,-1);
    delete newImage;
}



void hausdorfm_form::Check_Change(){
        this->registruj=ui->Check_Reg->checkState();
        this->Show_image();
}
