//
// form1.ui.h
//

#include "interface.h"

#ifdef graphicOut

#ifndef FORM1_UI_H
#define FORM1_UI_H

#include <qfiledialog.h>
#include <qmessagebox.h>

#include "parser.h"
#include "CGeneration.h"
#include "CString.h"

#include <time.h>

//
// reakce na kliknuti na tlacitko START
//
void Form1::clickedStart()
{
    CGeneration *cgen=NULL;
//    popSizeEdit->setText(QString("33"));
//    eliteEdit->setText(QString("7"));
//    gencountEdit->setText(QString("222"));
//    mutationEdit->setText(QString("0"));
//    crossoverEdit->setText(QString("1"));

    time_t _time_t1, _time_t2;
    time(&_time_t1);

    paintPane->resetVector();

    messageEdit->setText(QString("satGen v1.0 starts with following parameters:\n"));
    messageEdit->setText(messageEdit->text() + QString("Selected selection method: %1\n").arg(comboBox1->currentItem()));
    messageEdit->setText(messageEdit->text() + QString("Selected fitness method: %1\n").arg(comboBox2->currentItem()));
    messageEdit->setText(messageEdit->text() + QString("Selected crossover operator: %1\n").arg(comboBox3->currentItem()));

    messageEdit->setText(messageEdit->text() + QString("Population size: " + popSizeEdit->text() + "\n"));
    messageEdit->setText(messageEdit->text() + QString("Generations count: " + gencountEdit->text() + "\n"));
    messageEdit->setText(messageEdit->text() + QString("Crossover probability: " + crossoverEdit->text() + "\n"));
    messageEdit->setText(messageEdit->text() + QString("Mutation probability: " + mutationEdit->text() + "\n"));
    messageEdit->setText(messageEdit->text() + QString("Elite count: " + eliteEdit->text() + "\n"));
    messageEdit->setText(messageEdit->text() + QString("Best unchanged: " + bestUnchanged->text() + "\n"));
    messageEdit->setText(messageEdit->text() + QString("Tournament size: " + tournamentSize->text() + "\n"));
    messageEdit->setText(messageEdit->text() + QString("File to process: " + pathEdit->text() + "\n"));
//    int xx=(popSizeEdit->text()).toInt();
//    paintPane->paintPoint(10,10,2);

    InnerForm *inner=new InnerForm();
    try{
      parseCNF(pathEdit->text().ascii(), inner);
      cgen=new CGeneration(inner, (popSizeEdit->text()).toInt(), (eliteEdit->text()).toInt(), (gencountEdit->text()).toInt(), comboBox2->currentItem(), (bestUnchanged->text()).toInt());
      cgen->setQMessage(messageEdit, lineEdit9);
      cgen->setPaintPane(paintPane);
      cgen->evolution((crossoverEdit->text()).toDouble(), (mutationEdit->text()).toDouble(), comboBox1->currentItem(), comboBox3->currentItem(), (tournamentSize->text()).toInt());
    }catch(const char *msg){
      messageEdit->setText(messageEdit->text() + QString(msg));
    }

// reproduction method
// 0=roulette
// 1=tournament
// 2=random

// crossover method
// 0=simple
// 1=two-point
// 2=uniform
// 3=improvement

// fitness
// 0=number of sat clauses
// 1=weighted sat

    time(&_time_t2);
    messageEdit->setText(messageEdit->text() + QString("Elapsed time: %1s\n").arg(difftime(_time_t2, _time_t1)));
    delete inner;
    if (cgen!=NULL){delete cgen;}
}


//
// reakce na kliknuti na tlacitko STOP
//
void Form1::clickedStop()
{
    QMessageBox* about = new QMessageBox( "satGen",
     "<h3>Simple SAT solver</h3>"
     "<ul>"
  "<li> based on genetic algorithms"
  "<li> made in QT and CBuilderX"
  "<li> programmed by Jaromir Horejsi"
  "<li> FEE CTU Prague"
                           "<li> (c) 2k6"
     "</ul>", QMessageBox::Information, 1, 0, 0, this, 0, FALSE );
    about->setButtonText( 1, "I understand" );
    about->show();
}


//
// reakce na kliknuti na tlacitko EXIT
//
void Form1::clickedExit()
{
    //ukonci program
    close();
}


//
// reakce na kliknuti na tlacitko ... (tri tecky)
//
void Form1::openFile()
{
    QString fn = QFileDialog::getOpenFileName( QString::null, tr( "DIMACS CNF files (*.cnf *.txt);;All Files (*)" ), this );
    pathEdit->setText(fn);
}
#endif


#endif
