Commit b4096fb6 authored by Yann Boucher's avatar Yann Boucher
Browse files

Support de Ctrl+Z et Ctrl+A pour la GridView, et on peut à nouveau se déplacer...

Support de Ctrl+Z et Ctrl+A pour la GridView, et on peut à nouveau se déplacer dans la grille avec Shift
parent 6c1b8be8
Pipeline #78775 passed with stages
in 18 seconds
......@@ -23,7 +23,7 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include "alphabet.hpp"
#include "grid.h"
#include "history.h"
namespace detail
{
......@@ -109,6 +109,12 @@ public:
//! \returns Une structure représentant la sélection actuelle.
Structure selected_cells() const;
//! \brief Sauvegarde l'état de la grille avant une opération de modification, pour pouvoir Ctrl-Z.
void push_history();
//! \brief Annule la dernière opération (effectue un Ctrl-Z).
void undo();
signals:
//! \brief Signal émis quand le zoom change.
//! \param cell_size la nouvelle taille à l'écran en pixels d'une cellule
......@@ -118,6 +124,8 @@ private:
GridItem* item_at(Coord pos);
const GridItem* item_at(Coord pos) const;
void load_grid(const Grid& grid);
void copy_selection();
void paste_clipboard();
void delete_selection();
......@@ -131,6 +139,7 @@ private:
GridGraphicsView* m_view;
QGraphicsScene* m_scene;
detail::Graphics_view_zoom* m_zoom;
History m_undo_history;
unsigned m_width;
unsigned m_height;
unsigned m_pen;
......
......@@ -28,9 +28,9 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include "structurereader.hpp"
//! Permet de convertir un stateColor vers un QColor.
//! Détail d'implémentation.
static QColor stateColor_to_QColor(const stateColor& sc)
//! Permet de convertir un stateColor vers un QColor.
//! Détail d'implémentation.
static QColor stateColor_to_QColor(const stateColor& sc)
{
return QColor::fromRgb(sc.getRed(), sc.getGreen(), sc.getBlue());
}
......@@ -108,8 +108,11 @@ public:
unsigned state() const
{ return m_cell_state; }
void set_state(unsigned s)
void set_state(unsigned s, bool update_history = false)
{
if (update_history && m_cell_state != s)
m_grid_view.push_history();
const Alphabet& alph = m_grid_view.alphabet();
setBrush(QBrush(stateColor_to_QColor(alph.getState(s).getColor())));
setToolTip(QString::fromStdString(alph.getState(s).getStateLabel()));
......@@ -221,12 +224,12 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
QGraphicsItem* gitem_ptr = itemAt(event->pos());
GridItem* item = dynamic_cast<GridItem*>(gitem_ptr);
if (event->button() == Qt::LeftButton)
if (QGuiApplication::keyboardModifiers() == Qt::NoModifier && event->button() == Qt::LeftButton)
{
if (item)
{
unsigned state = m_gridview.current_pen();
item->set_state(state);
item->set_state(state, true);
}
}
else if (event->button() == Qt::RightButton && item)
......@@ -240,16 +243,15 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
void GridGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
QGraphicsItem* gitem_ptr = itemAt(event->pos());
GridItem* item = dynamic_cast<GridItem*>(gitem_ptr);
if (event->buttons() == Qt::LeftButton)
if (QGuiApplication::keyboardModifiers() == Qt::NoModifier && event->buttons() == Qt::LeftButton)
{
if (item)
{
unsigned state = m_gridview.current_pen();
item->set_state(state);
item->set_state(state, true);
}
}
else if (event->buttons() == Qt::RightButton && item)
......@@ -262,7 +264,7 @@ void GridGraphicsView::mouseMoveEvent(QMouseEvent *event)
}
GridView::GridView(QWidget *parent)
: QFrame(parent), m_width(10), m_height(10)
: QFrame(parent), m_undo_history(10), m_width(10), m_height(10)
{
setMouseTracking(true);
......@@ -300,7 +302,7 @@ GridView::GridView(QWidget *parent)
m_width = 10;
Grid default_grid(m_height, m_width);
copy_grid(default_grid);
load_grid(default_grid);
connect(m_zoom, &detail::Graphics_view_zoom::zoomed, this, [this]
{
......@@ -310,6 +312,9 @@ GridView::GridView(QWidget *parent)
void GridView::set_alphabet(const Alphabet &alph)
{
while (!m_undo_history.isEmpty())
m_undo_history.popGrid();
m_alph = alph;
}
......@@ -346,25 +351,8 @@ unsigned GridView::cell_pixel_size() const
void GridView::copy_grid(const Grid &grid)
{
m_scene->clear();
m_height = grid.get_rows();
m_width = grid.get_col();
// Populate scene;
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 = grid.get_state(pos);
GridItem *item = new GridItem(*this, state);
item->set_grid_pos(Coord{(int)i, (int)j});
m_scene->addItem(item);
}
}
m_width = grid.get_col();
m_height = grid.get_rows();
push_history();
load_grid(grid);
}
Grid GridView::get_grid() const
......@@ -408,6 +396,19 @@ Structure GridView::selected_cells() const
return Structure(cells.begin(), cells.end());
}
void GridView::push_history()
{
m_undo_history.pushGrid(get_grid());
}
void GridView::undo()
{
if (!m_undo_history.isEmpty())
{
load_grid(m_undo_history.popGrid());
}
}
GridItem *GridView::item_at(Coord pos)
{
QGraphicsItem* gitem_ptr = m_scene->itemAt(QPointF(pos.x*GridItem::width(), pos.y*GridItem::height()), QTransform());
......@@ -419,6 +420,29 @@ const GridItem *GridView::item_at(Coord pos) const
return dynamic_cast<GridItem*>(gitem_ptr);
}
void GridView::load_grid(const Grid &grid)
{
m_scene->clear();
m_height = grid.get_rows();
m_width = grid.get_col();
// Populate scene;
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 = grid.get_state(pos);
GridItem *item = new GridItem(*this, state);
item->set_grid_pos(Coord{(int)i, (int)j});
m_scene->addItem(item);
}
}
m_width = grid.get_col();
m_height = grid.get_rows();
}
void GridView::copy_selection()
{
m_copy_paste_buffer = selected_cells();
......@@ -430,6 +454,8 @@ void GridView::paste_clipboard()
if (m_scene->selectedItems().size() < 1)
return;
push_history();
Coord origin = top_left_of_selection();
paste_structure_at(origin, m_copy_paste_buffer);
......@@ -437,6 +463,8 @@ void GridView::paste_clipboard()
void GridView::fill_selection(unsigned state)
{
push_history();
for (auto& abstract_item : m_scene->selectedItems())
{
GridItem* item = static_cast<GridItem*>(abstract_item);
......@@ -452,6 +480,8 @@ void GridView::delete_selection()
void GridView::paste_structure_at(Coord origin, const Structure &s)
{
push_history();
for (const auto& cell : s)
{
Coord corrected = wrap_coords(cell.first + origin, m_width, m_height);
......@@ -506,6 +536,15 @@ void GridView::keyPressEvent(QKeyEvent *event)
{
paste_clipboard();
}
else if (event->key() == Qt::Key_Z)
{
undo();
}
else if (event->key() == Qt::Key_A)
{
for (auto& item : m_scene->items())
item->setSelected(true);
}
else if (event->key() == Qt::Key_Plus)
{
m_zoom->gentle_zoom(1.25);
......
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