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


MainWindow::MainWindow(QWidget *parent) :
		QMainWindow(parent),
		ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	adloader = new Loader(this);
	adloader->load("data2.xml");
	QAbstractItemModel * model = new AlienModel(adloader->aliens.values());
	ui->lvAliens->setModel(model);
	connect(ui->lvAliens->selectionModel(),SIGNAL(currentChanged(QModelIndex, QModelIndex)),this,SLOT(lvAliens_select(QModelIndex, QModelIndex)));
	ui->lvAliens->selectionModel()->setCurrentIndex(ui->lvAliens->model()->index(0,0),QItemSelectionModel::Select);
}


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


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


void MainWindow::on_cbAlArmor_toggled(bool checked)
{
	ui->sbAlArmor->setEnabled(checked);
}


void MainWindow::on_cbAlRegeneration_toggled(bool checked)
{
	ui->sbAlregeneration->setEnabled(checked);
}


void MainWindow::lvAliens_select(QModelIndex index, QModelIndex)
{
	tbAlien al = adloader->aliens.values().at(index.row());
	ui->leAlName->setText(al.name);
	ui->sbAlHealth->setValue(al.health);
	ui->sbAlArmor->setValue(al.armor);
	if (al.armor > 0) ui->cbAlArmor->setChecked(true);
	else ui->cbAlArmor->setChecked(false);
	ui->sbAlSpeed->setValue(al.speed);
	ui->sbAlPrise->setValue(al.prise);
	ui->sbAlregeneration->setValue(al.regeneration);
	if (al.regeneration > 0) ui->cbAlRegeneration->setChecked(true);
	else ui->cbAlRegeneration->setChecked(false);
	ui->sbAlScore->setValue(al.score);
	ui->cbAlIsFly->setChecked(al.isFlying);
}


void MainWindow::on_pbSave_clicked()
{
	adloader->save("data2.xml");
	adloader->load("data2.xml");
	QAbstractItemModel * model = new AlienModel(adloader->aliens.values());
	ui->lvAliens->setModel(model);
	ui->lvAliens->selectionModel()->setCurrentIndex(ui->lvAliens->model()->index(0,0),QItemSelectionModel::Select);
}
