Commit 09cff90f authored by Anthony Noir's avatar Anthony Noir
Browse files

Merge branch 'master' of...

Merge branch 'master' of https://gitlab.utc.fr/lo21_pin_noir_boucher_bouri_detree/cellulutlo21 into master
parents df808926 39b3ede4
Pipeline #78782 passed with stages
in 18 seconds
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
......@@ -618,12 +618,15 @@ pattern recorded :</string>
<property name="title">
<string>Éditer</string>
</property>
<addaction name="actionCharger_depuis_une_image"/>
<addaction name="action_save_struct"/>
</widget>
<widget class="QMenu" name="menuA_propos">
<property name="title">
<string>À propos</string>
</property>
<addaction name="action_propos_de_Cellulut"/>
<addaction name="action_propos_de_Qt"/>
</widget>
<addaction name="menuFichier"/>
<addaction name="menuEditer"/>
......@@ -638,6 +641,21 @@ pattern recorded :</string>
<string>Ctrl+Shift+S</string>
</property>
</action>
<action name="action_propos_de_Cellulut">
<property name="text">
<string>À propos de Cellulut</string>
</property>
</action>
<action name="action_propos_de_Qt">
<property name="text">
<string>À propos de Qt</string>
</property>
</action>
<action name="actionCharger_depuis_une_image">
<property name="text">
<string>Charger depuis une image...</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
......
......@@ -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
{
......@@ -62,6 +62,8 @@ class GridGraphicsView;
class GridView : public QFrame
{
Q_OBJECT
friend class GridGraphicsView;
public:
//! \brief Initialise le GridView comme n'importe quel autre QWidget avec son parent.
GridView(QWidget *parent = nullptr);
......@@ -109,6 +111,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 +126,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 +141,8 @@ private:
GridGraphicsView* m_view;
QGraphicsScene* m_scene;
detail::Graphics_view_zoom* m_zoom;
std::vector<GridItem*> m_item_cache; // cache pour pouvoir accéder efficacement à un Item selon une coordonnée
History m_undo_history;
unsigned m_width;
unsigned m_height;
unsigned m_pen;
......
......@@ -95,6 +95,9 @@ private:
//! \brief Déclenche la confirmation et la sauvegarde du modèle édité actuel
void save_model();
//! \brief Charge la grille depuis une image.
void load_from_image();
//! \brief Retourne le modèle par défaut à charger
QJsonObject default_model() const;
private:
......
......@@ -23,14 +23,15 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include <QDebug>
#include <vector>
#include <cstdlib>
#include <qmath.h>
#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 +109,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()));
......@@ -145,7 +149,8 @@ protected:
if (isSelected())
{
QBrush selection_brush(Qt::cyan);
QBrush selection_brush = brush();
selection_brush.setColor(brush().color().darker(175));
painter->setBrush(selection_brush);
}
else
......@@ -211,8 +216,12 @@ protected:
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
private:
void draw_bresenham(QPointF from, QPointF to);
private:
GridView& m_gridview;
QPoint m_last_mouse_pos;
};
......@@ -221,12 +230,14 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
QGraphicsItem* gitem_ptr = itemAt(event->pos());
GridItem* item = dynamic_cast<GridItem*>(gitem_ptr);
if (event->button() == Qt::LeftButton)
m_last_mouse_pos = event->pos();
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,17 +251,12 @@ 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);
}
draw_bresenham(mapToScene(m_last_mouse_pos), mapToScene(event->pos()));
}
else if (event->buttons() == Qt::RightButton && item)
{
......@@ -259,10 +265,38 @@ void GridGraphicsView::mouseMoveEvent(QMouseEvent *event)
}
else
QGraphicsView::mouseMoveEvent(event);
m_last_mouse_pos = event->pos();
}
// Algorithme de Bresenham pour tracer une ligne entre les deux points et dessiner sur ces items
void GridGraphicsView::draw_bresenham(QPointF from, QPointF to)
{
int x0 = from.x()/GridItem::width();
int y0 = from.y()/GridItem::height();
int x1 = to.x()/GridItem::width();
int y1 = to.y()/GridItem::height();
// cf. https://gist.github.com/bert/1085538
int dx = abs (x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs (y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2; /* error value e_xy */
for (;;){ /* loop */
//printf("Trying %d %d\n", x, y);
auto item = m_gridview.item_at(Coord{x0, y0});
if (item)
item->set_state(m_gridview.current_pen());
if (x0 == x1 && y0 == y1) break;
e2 = 2 * err;
if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
}
}
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 +334,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 +344,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 +383,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,15 +428,55 @@ 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());
return dynamic_cast<GridItem*>(gitem_ptr);
return m_item_cache[pos.y*m_width + pos.x];
//QGraphicsItem* gitem_ptr = m_scene->itemAt(QPointF(pos.x*GridItem::width(), pos.y*GridItem::height()), QTransform());
//return dynamic_cast<GridItem*>(gitem_ptr);
}
const GridItem *GridView::item_at(Coord pos) const
{
QGraphicsItem* gitem_ptr = m_scene->itemAt(QPointF(pos.x*GridItem::width(), pos.y*GridItem::height()), QTransform());
return dynamic_cast<GridItem*>(gitem_ptr);
return m_item_cache[pos.y*m_width + pos.x];
//QGraphicsItem* gitem_ptr = m_scene->itemAt(QPointF(pos.x*GridItem::width(), pos.y*GridItem::height()), QTransform());
//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();
m_item_cache.clear();
m_item_cache.resize(m_width*m_height);
// 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(pos);
m_item_cache[pos.y*m_width + pos.x] = item;
m_scene->addItem(item);
}
}
m_width = grid.get_col();
m_height = grid.get_rows();
}
void GridView::copy_selection()
......@@ -430,6 +490,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 +499,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 +516,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 +572,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);
......
......@@ -11,6 +11,8 @@
#include "neighborhoodWidget.hpp"
#include <QJsonArray>
#include <QDate>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
......@@ -31,6 +33,20 @@ MainWindow::MainWindow(QWidget *parent)
connect(ui->grid_view, &GridView::zoom_changed, ui->struct_library, &StructureLibraryView::update_cell_pixel_size);
connect(ui->transition_list, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int)
{ update_transition_settings(); });
connect(ui->action_propos_de_Cellulut, &QAction::triggered, this, [this](bool)
{
QMessageBox::about(this, "À propos de Cellulut",
"Projet de C++ réalisé dans le cadre de l'UV LO21 de l'UTC.\n\n"
"Contributeurs : Anthony Noir, Eugène Pin, Merwane Bouri, Arthur Detree, Yann Boucher");
});
connect(ui->action_propos_de_Qt, &QAction::triggered, this, [this](bool)
{
QMessageBox::aboutQt(this, "À propos de Qt");
});
connect(ui->actionCharger_depuis_une_image, &QAction::triggered, this, [this](bool)
{
load_from_image();
});
ui->struct_library->update_cell_pixel_size(ui->grid_view->cell_pixel_size());
......@@ -40,10 +56,7 @@ MainWindow::MainWindow(QWidget *parent)
MainWindow::~MainWindow()
{
delete ui;
if (m_transition_rule)
delete m_transition_rule;
if (m_neighborhood_rule)
delete m_neighborhood_rule;
// FIXME : il y a un risque de fuite de mémoire avec la gestion de pointeurs de m_transition_rule et m_neighborhood_rule, si l'application est quitée alors que ils pointent vers de la mémoire non transférée vers Simulation
}
/// Connections
......@@ -302,9 +315,6 @@ void MainWindow::enable_rule_customization()
ui->simulation_tab->setEnabled(false);
ui->customize_button->setEnabled(false);
ui->rule_settings->setEnabled(true);
update_neighborhood_settings();
update_transition_settings();
}
void MainWindow::disable_rule_customization()
......@@ -344,8 +354,9 @@ void MainWindow::load_model(const QJsonObject &obj)
}
// On transfère la propriété de ces pointeurs vers Simulation, qui en est désormais propriétaire pour l'exécution de l'automate
simulation.setNeighborhoodRule(m_neighborhood_rule); m_neighborhood_rule = nullptr;
simulation.setTransitionRule(m_transition_rule); m_transition_rule = nullptr;
simulation.setNeighborhoodRule(m_neighborhood_rule);
simulation.setTransitionRule(m_transition_rule);
simulation.setAlphabet(ui->grid_view->alphabet());
}
void MainWindow::save_model()
......@@ -393,6 +404,48 @@ void MainWindow::save_model()
load_model(root);
}
static unsigned closest_state(const Alphabet& alph, const QColor& color)
{
unsigned best_distance = std::numeric_limits<unsigned>::max();
unsigned best_match = 0;
for (unsigned i = 0; i < alph.taille(); ++i)
{
state s = alph.getState(i);
unsigned r_diff = qAbs(color.red() - s.getColor().getRed());
unsigned g_diff = qAbs(color.green() - s.getColor().getGreen());
unsigned b_diff = qAbs(color.blue() - s.getColor().getBlue());
unsigned distance = r_diff*r_diff + g_diff*g_diff + b_diff*b_diff;
if (distance < best_distance)
{
best_distance = distance;
best_match = i;
}
}
return best_match;
}
void MainWindow::load_from_image()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Image (*.png *.jpg *.jpeg *.bmp *.gif"));
QImage img(fileName);
if (img.isNull())
return;
Grid grid = ui->grid_view->get_grid();