gridview.hpp 2.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
\file gridview.hpp
\date 28/04/2021
\author Yann Boucher
\version 1
\brief GridView

Cette classe représente le widget utilisé pour l'affichage et l'interaction avec la grille actuelle.
**/

#ifndef GRIDVIEW_HPP
#define GRIDVIEW_HPP

14
15
16
17
// TODO : documenter

#include <map>

18
19
20
21
22
23
24
#include <QFrame>
#include <QGraphicsView>

#include <QGraphicsRectItem>

#include "coord.hpp"
#include "structure.hpp"
25
#include "alphabet.hpp"
26
27
28

#include "grid.h"

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

namespace detail
{

// based on https://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel
class Graphics_view_zoom : public QObject {
    Q_OBJECT
public:
    Graphics_view_zoom(QGraphicsView* view);
    void gentle_zoom(double factor);
    void set_zoom_factor_base(double value);

private:
    QGraphicsView* _view;
    double _zoom_factor_base;
    QPointF target_scene_pos, target_viewport_pos;
    bool eventFilter(QObject* object, QEvent* event);

signals:
    void zoomed();
};
}


53
class GridItem;
54
55
56
57
58
59
class GridView;
class GridGraphicsView : public QGraphicsView
{
public:
    GridGraphicsView(GridView& gridview, QWidget* parent)
        : QGraphicsView(parent), m_gridview(gridview)
60
61
62
    {
        setAcceptDrops(true);
    }
63
64
65

protected:
    void mousePressEvent(QMouseEvent* event);
66
    void mouseMoveEvent(QMouseEvent* event);
67
68
69
70
71

private:
    GridView& m_gridview;
};

72
73
74
75
class GridView : public QFrame
{
    Q_OBJECT
public:
76
    GridView(QWidget *parent = nullptr);
77
public:
78
    void set_alphabet(const Alphabet& alph);
79
80
81
82
    const Alphabet& alphabet() const;

    void set_current_pen(unsigned state);
    unsigned current_pen() const;
83

84
85
    void paste_structure_at(Coord origin, const Structure& s);

86
87
    void set_clipboard(const Structure& s);

88
89
90
    //! Retourne la taille du côté en pixels d'une cellule au niveau de zoom actuel
    unsigned cell_pixel_size() const;

91
92
93
94
    void copy_grid(const Grid& grid);
    Grid get_grid() const;

    void clear_selection();
95
    void fill_selection(unsigned state);
96
97
    Structure selected_cells() const;

98
99
100
101
102
signals:
    //! Emis quand le zoom change
    //! \param cell_size la nouvelle taille à l'écran en pixels d'une cellule
    void zoom_changed(unsigned cell_size);

103
104
private:
    GridItem* item_at(Coord pos);
105
    const GridItem* item_at(Coord pos) const;
106
107
108
109
110
111

    void copy_selection();
    void paste_clipboard();
    void delete_selection();

    Coord top_left_of_selection() const;
112
113
114
115
116
protected:
    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);

private:
117
    GridGraphicsView* m_view;
118
    QGraphicsScene* m_scene;
119
    detail::Graphics_view_zoom* m_zoom;
120
121
    unsigned m_width;
    unsigned m_height;
122
    unsigned m_pen;
123
    Structure m_copy_paste_buffer;
124
    Alphabet m_alph;
125
126
127
};

#endif // GRIDVIEW_HPP