Commit 6309e473 authored by Yann Boucher's avatar Yann Boucher
Browse files

On peut à présent dessiner à la souris sans 'trous'

parent b4096fb6
Pipeline #78776 passed with stages
in 17 seconds
......@@ -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);
......
......@@ -23,6 +23,7 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include <QDebug>
#include <vector>
#include <cstdlib>
#include <qmath.h>
......@@ -214,8 +215,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;
};
......@@ -224,6 +229,8 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
QGraphicsItem* gitem_ptr = itemAt(event->pos());
GridItem* item = dynamic_cast<GridItem*>(gitem_ptr);
m_last_mouse_pos = event->pos();
if (QGuiApplication::keyboardModifiers() == Qt::NoModifier && event->button() == Qt::LeftButton)
{
if (item)
......@@ -248,11 +255,7 @@ void GridGraphicsView::mouseMoveEvent(QMouseEvent *event)
if (QGuiApplication::keyboardModifiers() == Qt::NoModifier && event->buttons() == Qt::LeftButton)
{
if (item)
{
unsigned state = m_gridview.current_pen();
item->set_state(state, true);
}
draw_bresenham(mapToScene(m_last_mouse_pos), mapToScene(event->pos()));
}
else if (event->buttons() == Qt::RightButton && item)
{
......@@ -261,6 +264,34 @@ 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)
......
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