Skip to content

Instantly share code, notes, and snippets.

@gazza126
Created February 25, 2015 20:31
Show Gist options
  • Save gazza126/f43d5b0377649782a35d to your computer and use it in GitHub Desktop.
Save gazza126/f43d5b0377649782a35d to your computer and use it in GitHub Desktop.
//Simulation.h
#ifndef SIMULATION_H
#define SIMULATION_H
#include <QWidget>
#include <QGridLayout>
#include <QMainWindow>
#include <QFrame>
#include <QPushButton>
#include <QThread>
#include <QGraphicsView>
#include <QTimer>
#include "simboard.h"
#include "simsquare.h"
namespace Ui {
class Simulation;
}
class Simulation : public QMainWindow
{
Q_OBJECT
public:
Simulation(QWidget *parent = 0);
~Simulation();
void startTimer();
private:
void simLoop();
void setupUI();
void update();
void draw();
void createGraphicsViews();
Ui::Simulation *simui;
QVector<SimSquare*> squaresVec;
QGridLayout* simLayout;
SimSquare square;
QFrame* simFrame;
QPushButton* button;
QGraphicsView *graphicsView[64];
QGraphicsScene *graphicsScene[64];
public slots:
void updateSim();
};
#endif // SIMULATION_H
//Simulation.cpp
#include "simulation.h"
#include "ui_simulation.h"
#include "simsquare.cpp"
#include "simboard.cpp"
#include <QVector>
#include <QDebug>
#include <QApplication>
#include <QtGlobal>
QTimer *timer;
Simulation::Simulation(QWidget *parent) : QMainWindow(parent),
simui(new Ui::Simulation)
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateSim()));
setupUI();
//simui->setupUi(this);
//this->show();
}
void Simulation::setupUI(){
qDebug() << "Setting layout";
simui->setupUi(this);
qDebug() << "setu[ UI";
createGraphicsViews();
this->show();
}
void Simulation::createGraphicsViews(){
for(int i = 0; i < 64; i++){
for(int j = 0; j < 8; j++){
graphicsScene[i] = new QGraphicsScene();
graphicsView[i] = new QGraphicsView(graphicsScene[i]);
simui->gridLayout->addWidget(graphicsView[i], i/8, j);
}
}
}
void Simulation::startTimer(){
timer->start(16);
}
void Simulation::simLoop(){
qApp->processEvents();
update();
//draw();
}
void Simulation::update(){
int x1,y1,x2,y2;
//y1 = x2 = y2 = 3;
//qrands(qrand);
for(int x = 0; x < 64; x++){
x1 = qrand()%(50+1) - 1;
y1 = qrand()%(50+1)-1;
x2 = qrand()%(50+1)-1;
y2 = qrand()%(50+1)-1;
graphicsScene[x]->addLine(x1,y1,x2,y2);
qDebug() << "adding line to" << x << "at" << x1 <<","<<y1<<","<<x2<<","<<y2;
}
}
void Simulation::updateSim(){
simLoop();
}
void Simulation::draw(){
qDebug()<<"Drawing";
for(int x = 0; x < 64; x++){
graphicsView[x]->show();
qDebug()<<"showing" << x;
}
//this->repaint();
}
Simulation::~Simulation()
{
delete simui;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment