Commit 26f2bfc4 authored by Yann Boucher's avatar Yann Boucher
Browse files

Fixed a bug when clicking outside the grid's area; fixed a bug where cells...

Fixed a bug when clicking outside the grid's area; fixed a bug where cells modified with the pen wouldn't register their state correctly
parent 1c7fd3f8
...@@ -46,7 +46,7 @@ class GridView : public QFrame ...@@ -46,7 +46,7 @@ class GridView : public QFrame
{ {
Q_OBJECT Q_OBJECT
public: public:
GridView(QWidget *parent = nullptr, const Grid *g = nullptr); GridView(QWidget *parent = nullptr);
public: public:
void set_alphabet(const Alphabet& alph); void set_alphabet(const Alphabet& alph);
const Alphabet& alphabet() const; const Alphabet& alphabet() const;
......
...@@ -25,7 +25,7 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av ...@@ -25,7 +25,7 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include <qmath.h> #include <qmath.h>
class GridItem : public QGraphicsRectItem class GridItem : public QGraphicsRectItem
{ {
public: public:
GridItem(QColor color, const QString& state_name, QGraphicsItem *parent = nullptr) GridItem(QColor color, const QString& state_name, QGraphicsItem *parent = nullptr)
...@@ -88,19 +88,15 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event) ...@@ -88,19 +88,15 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
QGraphicsItem* gitem_ptr = itemAt(event->pos()); QGraphicsItem* gitem_ptr = itemAt(event->pos());
GridItem* item = dynamic_cast<GridItem*>(gitem_ptr); GridItem* item = dynamic_cast<GridItem*>(gitem_ptr);
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton && item)
{ {
assert(item);
unsigned state = m_gridview.current_pen(); unsigned state = m_gridview.current_pen();
item->setBrush(QBrush(stateColor_to_QColor(m_gridview.alphabet().getState(state).getColor()))); item->setBrush(QBrush(stateColor_to_QColor(m_gridview.alphabet().getState(state).getColor())));
item->cell_state = 0; item->cell_state = state;
item->update(); item->update();
} }
else if (event->button() == Qt::RightButton) else if (event->button() == Qt::RightButton && item)
{ {
assert(item);
QGraphicsView::mousePressEvent(event); QGraphicsView::mousePressEvent(event);
item->setSelected(true); item->setSelected(true);
} }
...@@ -108,7 +104,7 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event) ...@@ -108,7 +104,7 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
QGraphicsView::mousePressEvent(event); QGraphicsView::mousePressEvent(event);
} }
GridView::GridView(QWidget *parent, const Grid *g) GridView::GridView(QWidget *parent)
: QFrame(parent), m_width(10), m_height(10) : QFrame(parent), m_width(10), m_height(10)
{ {
setMouseTracking(true); setMouseTracking(true);
...@@ -140,66 +136,12 @@ GridView::GridView(QWidget *parent, const Grid *g) ...@@ -140,66 +136,12 @@ GridView::GridView(QWidget *parent, const Grid *g)
alph.newEtat(state{stateColor{0, 0, 255}, "Alive"}); alph.newEtat(state{stateColor{0, 0, 255}, "Alive"});
set_alphabet(alph); set_alphabet(alph);
if( g != nullptr ) { // Initialisation la première fois
m_height = g->get_rows(); m_height = 10;
m_width = g->get_col(); m_width = 10;
for (unsigned i = 0; i < m_width; ++i)
{
for (unsigned j = 0; j < m_height; ++j)
{
Coord pos = {static_cast<int>(i), static_cast<int>(j)};
unsigned state = g->get_state(pos);
//std::cout << "state : " << state << endl;
GridItem *item = new GridItem(stateColor_to_QColor(m_alph.getState(state).getColor()), QString::fromStdString(m_alph.getState(state).getStateLabel()));
//GridItem *item = new GridItem((i ^ j) % 2 ? Qt::blue : Qt::white, QString::fromStdString(m_alph.getState(state).getStateLabel()));
item->setPos(QPointF(i*GridItem::width(), j*GridItem::height()));
item->cell_pos = Coord{(int)i, (int)j};
item->cell_state = state;
m_scene->addItem(item);
}
}
}
else // Initialisation la première fois
{
m_height = 10;
m_width = 10;
for (unsigned i = 0; i < m_width; ++i) Grid default_grid(m_height, m_width);
{ copy_grid(default_grid);
for (unsigned j = 0; j < m_height; ++j)
{
unsigned state = 0;
GridItem *item = new GridItem(stateColor_to_QColor(m_alph.getState(state).getColor()), QString::fromStdString(m_alph.getState(state).getStateLabel()));
//GridItem *item = new GridItem((i ^ j) % 2 ? Qt::blue : Qt::white, QString::fromStdString(m_alph.getState(state).getStateLabel()));
item->setPos(QPointF(i*GridItem::width(), j*GridItem::height()));
item->cell_pos = Coord{(int)i, (int)j};
item->cell_state = state;
m_scene->addItem(item);
}
}
}
// Populate scene;
/*
for (unsigned i = 0; i < m_width; ++i)
{
for (unsigned j = 0; j < m_height; ++j)
{
unsigned state = (i ^ j) % 2;
GridItem *item = new GridItem(stateColor_to_QColor(m_alph.getState(state).getColor()), QString::fromStdString(m_alph.getState(state).getStateLabel()));
//GridItem *item = new GridItem((i ^ j) % 2 ? Qt::blue : Qt::white, QString::fromStdString(m_alph.getState(state).getStateLabel()));
item->setPos(QPointF(i*GridItem::width(), j*GridItem::height()));
item->cell_pos = Coord{(int)i, (int)j};
item->cell_state = state;
m_scene->addItem(item);
}
}*/
} }
void GridView::set_alphabet(const Alphabet &alph) void GridView::set_alphabet(const Alphabet &alph)
...@@ -225,12 +167,15 @@ unsigned GridView::current_pen() const ...@@ -225,12 +167,15 @@ unsigned GridView::current_pen() const
void GridView::copy_grid(const Grid &grid) void GridView::copy_grid(const Grid &grid)
{ {
m_scene->clear(); m_scene->clear();
m_height = grid.get_rows();
m_width = grid.get_col();
// Populate scene; // Populate scene;
for (unsigned i = 0; i < m_width; ++i) for (unsigned i = 0; i < m_width; ++i)
{ {
for (unsigned j = 0; j < m_height; ++j) for (unsigned j = 0; j < m_height; ++j)
{ {
unsigned state = (i ^ j) % 2; Coord pos = {static_cast<int>(i), static_cast<int>(j)};
unsigned state = grid.get_state(pos);
GridItem *item = new GridItem(stateColor_to_QColor(m_alph.getState(state).getColor()), QString::fromStdString(m_alph.getState(state).getStateLabel())); GridItem *item = new GridItem(stateColor_to_QColor(m_alph.getState(state).getColor()), QString::fromStdString(m_alph.getState(state).getStateLabel()));
//GridItem *item = new GridItem((i ^ j) % 2 ? Qt::blue : Qt::white, (i ^ j) % 2 ? "Alive" : "Dead"); //GridItem *item = new GridItem((i ^ j) % 2 ? Qt::blue : Qt::white, (i ^ j) % 2 ? "Alive" : "Dead");
...@@ -247,7 +192,6 @@ void GridView::copy_grid(const Grid &grid) ...@@ -247,7 +192,6 @@ void GridView::copy_grid(const Grid &grid)
Grid GridView::get_grid() const Grid GridView::get_grid() const
{ {
Grid grid = Grid(m_height, m_width); Grid grid = Grid(m_height, m_width);
for (size_t i = 0; i < grid.get_col(); ++i) for (size_t i = 0; i < grid.get_col(); ++i)
{ {
for (size_t j = 0; j < grid.get_rows(); ++j) for (size_t j = 0; j < grid.get_rows(); ++j)
...@@ -312,7 +256,7 @@ void GridView::paste_clipboard() ...@@ -312,7 +256,7 @@ void GridView::paste_clipboard()
for (const auto& cell : m_copy_paste_buffer) for (const auto& cell : m_copy_paste_buffer)
{ {
Coord corrected = wrap_coords(cell.first + origin, 100, 100); Coord corrected = wrap_coords(cell.first + origin, m_width, m_height);
GridItem* item = item_at(corrected); GridItem* item = item_at(corrected);
assert(item != nullptr); assert(item != nullptr);
......
...@@ -120,7 +120,7 @@ void MainWindow::on_validateGridDim_clicked() ...@@ -120,7 +120,7 @@ void MainWindow::on_validateGridDim_clicked()
{ {
Grid oldGrid = ui->grid_view->get_grid(); Grid oldGrid = ui->grid_view->get_grid();
delete ui->grid_view;
unsigned int nbrRow = ui->heightSpinBox->value(); // nbr de lignes => axe y unsigned int nbrRow = ui->heightSpinBox->value(); // nbr de lignes => axe y
unsigned int nbrCol = ui->widthSpinBox->value(); // nbr de colonne => axe x unsigned int nbrCol = ui->widthSpinBox->value(); // nbr de colonne => axe x
Grid newGrid(nbrRow, nbrCol); Grid newGrid(nbrRow, nbrCol);
...@@ -152,13 +152,7 @@ void MainWindow::on_validateGridDim_clicked() ...@@ -152,13 +152,7 @@ void MainWindow::on_validateGridDim_clicked()
} }
} }
ui->grid_view->copy_grid(newGrid);
ui->grid_view = new GridView(ui->grid, &newGrid);
ui->grid_view->setObjectName(QString::fromUtf8("grid_view"));
ui->grid_view->setFrameShape(QFrame::StyledPanel);
ui->grid_view->setFrameShadow(QFrame::Raised);
ui->gridLayout_2->addWidget(ui->grid_view, 0, 0, 1, 1);
ui->validateGridDim->setEnabled(false); ui->validateGridDim->setEnabled(false);
} }
...@@ -188,16 +182,10 @@ void MainWindow::on_randomPatternButton_clicked() ...@@ -188,16 +182,10 @@ void MainWindow::on_randomPatternButton_clicked()
// TODO comprendre pourquoi il faut inverser x et y (???) // TODO comprendre pourquoi il faut inverser x et y (???)
unsigned state = rand() % 2; unsigned state = rand() % 2;
Coord pos = {static_cast<int>(x), static_cast<int>(y)}; Coord pos = {static_cast<int>(x), static_cast<int>(y)};
std::cout << "position x= "<< pos.x << " y= "<< pos.y << endl; //std::cout << "position x= "<< pos.x << " y= "<< pos.y << endl;
newGrid.set_cell(pos, state); newGrid.set_cell(pos, state);
} }
} }
delete ui->grid_view; ui->grid_view->copy_grid(newGrid);
ui->grid_view = new GridView(ui->grid, &newGrid);
ui->grid_view->setObjectName(QString::fromUtf8("grid_view"));
ui->grid_view->setFrameShape(QFrame::StyledPanel);
ui->grid_view->setFrameShadow(QFrame::Raised);
ui->gridLayout_2->addWidget(ui->grid_view, 0, 0, 1, 1);
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment