PixelWars/src/mainwindow.cpp

113 lines
3.2 KiB
C++

#include "mainwindow.h"
#include "drawwidget.h"
#include "ui_mainwindow.h"
#include "simulationdialog.h"
#include "simulation.h"
#include <iostream>
#include <chrono>
#include <QTimer>
#include <QErrorMessage>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
p_simu(NULL),
m_simSpeed(0),
m_simSpeedChanged(false),
m_paused(false)
{
setWindowIcon(QIcon(":qss_icons/rc/PixelWars.png"));
ui->setupUi(this);
m_simuTimer = new QTimer(this);
connect(m_simuTimer,SIGNAL(timeout()),this, SLOT(updateSimu()));
m_simuTimer->start(m_simSpeed);
connect(ui->actionCreate_new_simulation, SIGNAL(triggered(bool)), this, SLOT(openSimuDialog()));
connect(ui->simSpeedSlider, SIGNAL(valueChanged(int)), this, SLOT(changeSimSpeed(int)));
connect(ui->flatSphereSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(setFlatSphere(int)));
connect(ui->surfaceRatioSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(setSurfaceRatio(int)));
connect(ui->flatSphereSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(repaint()));
connect(ui->surfaceRatioSlider, SIGNAL(valueChanged(int)), ui->drawWidget, SLOT(repaint()));
connect(ui->actionPlayPause, SIGNAL(toggled(bool)), this, SLOT(pauseSimu(bool)));
connect(ui->actionAction_step, SIGNAL(triggered(bool)), this, SLOT(stepSimu()));
connect(ui->actionStop, SIGNAL(triggered(bool)), this, SLOT(stopSimu()));
changeSimSpeed(ui->simSpeedSlider->value());
ui->advancedGroupBox->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openSimuDialog()
{
pauseSimu(true);
SimulationDialog *dialog = new SimulationDialog(this);
dialog->setWindowTitle("Starting a new Simulation");
QErrorMessage *err = new QErrorMessage(dialog);
connect(dialog, SIGNAL(sendError(QString)), err, SLOT(showMessage(QString)));
int ret = dialog->exec();
if(ret == QDialog::Accepted)
{
if(p_simu != NULL)
delete p_simu;
p_simu = dialog->getSimulation();
ui->drawWidget->startSimulation(p_simu->getMap());
m_date = 0;
}
pauseSimu(false);
delete dialog;
}
void MainWindow::changeSimSpeed(int newSpeed)
{
m_simSpeedChanged = true;
m_simSpeed = ui->simSpeedSlider->maximum()+20-newSpeed;
}
void MainWindow::updateSimu()
{
if(!m_paused)
{
if(p_simu != NULL)
{
p_simu->update();
ui->dateLabel->setText(QString::number(++m_date));
ui->populationLabel->setText(QString::number(p_simu->getPopulation()));
ui->drawWidget->updateDudesBehavior();
ui->drawWidget->repaint();
}
if(m_simSpeedChanged)
{
m_simuTimer->stop();
m_simuTimer->start(m_simSpeed);
m_simSpeedChanged = false;
}
}
}
void MainWindow::stepSimu()
{
m_paused = false;
updateSimu();
pauseSimu(true);
}
void MainWindow::stopSimu()
{
pauseSimu(true);
ui->drawWidget->stopSimulation();
delete p_simu;
}
void MainWindow::pauseSimu(bool pause)
{
m_paused = pause;
ui->actionPlayPause->setChecked(m_paused);
if(m_paused)
m_simuTimer->stop();
else
m_simuTimer->start(m_simSpeed);
m_simSpeedChanged = false;
}