gridview.hpp 2.21 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
class GridItem;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class GridView;
class GridGraphicsView : public QGraphicsView
{
public:
    GridGraphicsView(GridView& gridview, QWidget* parent)
        : QGraphicsView(parent), m_gridview(gridview)
    {}

protected:
    void mousePressEvent(QMouseEvent* event);

private:
    GridView& m_gridview;
};

45
46
47
48
class GridView : public QFrame
{
    Q_OBJECT
public:
49
    GridView(QWidget *parent = nullptr);
50
public:
51
    void set_alphabet(const Alphabet& alph);
52
53
54
55
    const Alphabet& alphabet() const;

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

57
58
    void set_clipboard(const Structure& s);

59
60
61
62
63
64
    void copy_grid(const Grid& grid);
    Grid get_grid() const;

    void clear_selection();
    Structure selected_cells() const;

65
66
private:
    GridItem* item_at(Coord pos);
67
    const GridItem* item_at(Coord pos) const;
68
69
70
71
72
73

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

    Coord top_left_of_selection() const;
74
75
76
77
78
protected:
    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);

private:
79
    GridGraphicsView* m_view;
80
    QGraphicsScene* m_scene;
81
82
    unsigned m_width;
    unsigned m_height;
83
    unsigned m_pen;
84
    Structure m_copy_paste_buffer;
85
    Alphabet m_alph;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
};


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();
};
}

#endif // GRIDVIEW_HPP