#include "genresetup.h"
//
genreSetup::genreSetup(QWidget * parent, Qt::WFlags f) : QDialog(parent,f)
{
int lineCount=0;
QStringList header("Genre");
	setupUi(this);
homePath=(QString)getenv("HOME")+(QString)"/.MediaBib/";
QFile genreSetupFile(homePath+(QString)"genre.txt");
QString line;
QStringList genre;

  genre << "Action" << "Science Fiction" << "Phantasie" << "Drama" << "Comedy" << "Thriller" << "Music" << "Documentation" << "Education" << "misc";

  genreTable->setRowCount(0);
  genreTable->setColumnCount(1);
  genreTable->setHorizontalHeaderLabels(header);
  if (genreSetupFile.open(QIODevice::ReadOnly|QIODevice::Text)){
    QTextStream stream(&genreSetupFile);
    while (!stream.atEnd()){
      line=stream.readLine();
      genreTable->insertRow(lineCount);
      genreTable->setItem(lineCount,0,new QTableWidgetItem(line));
      lineCount++;     
    }
    genreSetupFile.close();
  } else {
    QStringListIterator it(genre);
    while (it.hasNext()){
      genreTable->insertRow(lineCount);
      genreTable->setItem(lineCount,0,new QTableWidgetItem(it.next()));
      lineCount++;     
    }
  }
  genreTable->setColumnWidth(0,this->width()-70);
}

void genreSetup::slot_closeButtonClicked(){
homePath=(QString)getenv("HOME")+(QString)"/.MediaBib/";
QFile genreSetupFile(homePath+(QString)"genre.txt");
  if (genreSetupFile.open(QIODevice::WriteOnly|QIODevice::Text)){
    QTextStream stream(&genreSetupFile);
    for (int i=0;i<genreTable->rowCount();i++){
      stream << genreTable->item(i,0)->text() << endl;
    }
    genreSetupFile.close();
  }
  this->done(true);
}

void genreSetup::slot_addButtonClicked(){
int numRows = genreTable->rowCount();
  if (!genreLineEdit->text().isNull()){
    genreTable->insertRow(numRows);
    genreTable->setItem(numRows,0,new QTableWidgetItem(genreLineEdit->text()));
  }
}

void genreSetup::slot_removeButtonClicked(){
int index = genreTable->currentRow();
  genreTable->removeRow(index);
}


void genreSetup::slot_genreTableClicked(){
int index = genreTable->currentRow();
QString genre = genreTable->item(index,0)->text();
  genreLineEdit->setText(genre);
}

void genreSetup::slot_upButtonClicked(){
int pos = genreTable->currentRow();
QString content1, content2;
QTableWidgetItem * item1;
QTableWidgetItem * item2;

	if (pos > 0){
		item1 = genreTable->item(pos,0);
		item2 = genreTable->item(pos-1,0);
		content1 = genreTable->item(pos,0)->text();
		content2 = genreTable->item(pos-1,0)->text();
		item1->setText(content2);
		item2->setText(content1);
		genreTable->setCurrentCell(pos-1,0);
	}
}

void genreSetup::slot_downButtonClicked(){
int pos = genreTable->currentRow();
QString content1, content2;
QTableWidgetItem * item1;
QTableWidgetItem * item2;

	if (pos < genreTable->rowCount()-1){
		item1 = genreTable->item(pos,0);
		item2 = genreTable->item(pos+1,0);
		content1 = genreTable->item(pos,0)->text();
		content2 = genreTable->item(pos+1,0)->text();
		item1->setText(content2);
		item2->setText(content1);
		genreTable->setCurrentCell(pos+1,0);
	}

}
//

