Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LO21_Pin_Noir_Boucher_Bouri_Detree
CellulutLO21
Commits
2ac00be6
Commit
2ac00be6
authored
May 28, 2021
by
Yann Boucher
Browse files
Improved encapsulation of GridItem
parent
44268bdc
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/gridview.cpp
View file @
2ac00be6
...
...
@@ -28,22 +28,27 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include
"structurereader.hpp"
// FIXME : faire en sorte que le tooltip soit correctement mis à jour à chaque fois
static
QColor
stateColor_to_QColor
(
const
stateColor
&
sc
)
{
return
QColor
::
fromRgb
(
sc
.
getRed
(),
sc
.
getGreen
(),
sc
.
getBlue
());
}
class
GridItem
:
public
QGraphicsRectItem
class
GridItem
:
public
QGraphicsRectItem
{
public:
GridItem
(
GridView
&
in_gridview
,
QColor
color
,
const
QString
&
state_name
,
QGraphicsItem
*
parent
=
nullptr
)
:
QGraphicsRectItem
(
0
,
0
,
GridItem
::
width
(),
GridItem
::
height
(),
parent
),
grid_view
(
in_gridview
)
,
m_state_name
(
state_name
)
GridItem
(
GridView
&
in_gridview
,
unsigned
state
=
0
,
Coord
pos
=
Coord
{
0
,
0
}
,
QGraphicsItem
*
parent
=
nullptr
)
:
QGraphicsRectItem
(
0
,
0
,
GridItem
::
width
(),
GridItem
::
height
(),
parent
),
m_
grid_view
(
in_gridview
)
{
setAcceptHoverEvents
(
true
);
setPen
(
QPen
(
QBrush
(
color
),
0
));
setBrush
(
QBrush
(
color
));
setToolTip
(
m_state_name
);
setFlag
(
QGraphicsItem
::
ItemIsSelectable
);
setAcceptDrops
(
true
);
view
=
nullptr
;
set_state
(
state
);
set_grid_pos
(
pos
);
setPen
(
QPen
(
brush
(),
0
));
m_view
=
nullptr
;
}
static
int
width
()
...
...
@@ -51,16 +56,35 @@ public:
static
int
height
()
{
return
20
;
}
unsigned
state
()
const
{
return
m_cell_state
;
}
void
set_state
(
unsigned
s
)
{
const
Alphabet
&
alph
=
m_grid_view
.
alphabet
();
setBrush
(
QBrush
(
stateColor_to_QColor
(
alph
.
getState
(
s
).
getColor
())));
setToolTip
(
QString
::
fromStdString
(
alph
.
getState
(
s
).
getStateLabel
()));
m_cell_state
=
s
;
update
();
}
Coord
grid_pos
()
const
{
return
m_cell_pos
;
}
void
set_grid_pos
(
Coord
c
)
{
setPos
(
QPointF
(
c
.
x
*
GridItem
::
width
(),
c
.
y
*
GridItem
::
height
()));
m_cell_pos
=
c
;
}
protected:
void
paint
(
QPainter
*
painter
,
const
QStyleOptionGraphicsItem
*
,
QWidget
*
)
override
{
if
(
!
view
)
if
(
!
m_
view
)
{
assert
(
scene
()
->
views
().
size
()
!=
0
);
view
=
scene
()
->
views
().
first
();
m_
view
=
scene
()
->
views
().
first
();
}
QSizeF
screen_size
=
view
->
transform
().
mapRect
(
sceneBoundingRect
()).
size
();
QSizeF
screen_size
=
m_
view
->
transform
().
mapRect
(
sceneBoundingRect
()).
size
();
QRectF
rect
=
QRectF
(
0
,
0
,
width
(),
height
());
...
...
@@ -107,26 +131,17 @@ protected:
return
;
}
grid_view
.
paste_structure_at
(
cell_pos
,
s
);
m_
grid_view
.
paste_structure_at
(
m_
cell_pos
,
s
);
}
// infos associées à la cellule affichée
public:
unsigned
cell_state
;
Coord
cell_pos
;
QGraphicsView
*
view
;
GridView
&
grid_view
;
private:
Coord
m_cell_pos
;
unsigned
m_cell_state
;
QString
m_state_name
;
QGraphicsView
*
m_view
;
GridView
&
m_grid_view
;
};
static
QColor
stateColor_to_QColor
(
const
stateColor
&
sc
)
{
return
QColor
::
fromRgb
(
sc
.
getRed
(),
sc
.
getGreen
(),
sc
.
getBlue
());
}
void
GridGraphicsView
::
mousePressEvent
(
QMouseEvent
*
event
)
{
QGraphicsItem
*
gitem_ptr
=
itemAt
(
event
->
pos
());
...
...
@@ -137,9 +152,7 @@ void GridGraphicsView::mousePressEvent(QMouseEvent *event)
if
(
item
)
{
unsigned
state
=
m_gridview
.
current_pen
();
item
->
setBrush
(
QBrush
(
stateColor_to_QColor
(
m_gridview
.
alphabet
().
getState
(
state
).
getColor
())));
item
->
cell_state
=
state
;
item
->
update
();
item
->
set_state
(
state
);
}
}
else
if
(
event
->
button
()
==
Qt
::
RightButton
&&
item
)
...
...
@@ -162,9 +175,7 @@ void GridGraphicsView::mouseMoveEvent(QMouseEvent *event)
if
(
item
)
{
unsigned
state
=
m_gridview
.
current_pen
();
item
->
setBrush
(
QBrush
(
stateColor_to_QColor
(
m_gridview
.
alphabet
().
getState
(
state
).
getColor
())));
item
->
cell_state
=
state
;
item
->
update
();
item
->
set_state
(
state
);
}
}
else
if
(
event
->
buttons
()
==
Qt
::
RightButton
&&
item
)
...
...
@@ -272,11 +283,9 @@ void GridView::copy_grid(const Grid &grid)
Coord
pos
=
{
static_cast
<
int
>
(
i
),
static_cast
<
int
>
(
j
)};
unsigned
state
=
grid
.
get_state
(
pos
);
GridItem
*
item
=
new
GridItem
(
*
this
,
stateColor_to_QColor
(
m_alph
.
getState
(
state
).
getColor
()),
QString
::
fromStdString
(
m_alph
.
getState
(
state
).
getStateLabel
()));
//GridItem *item = new GridItem((i ^ j) % 2 ? Qt::blue : Qt::white, (i ^ j) % 2 ? "Alive" : "Dead");
item
->
setPos
(
QPointF
(
i
*
GridItem
::
width
(),
j
*
GridItem
::
height
()));
item
->
cell_pos
=
Coord
{(
int
)
i
,
(
int
)
j
};
item
->
cell_state
=
state
;
GridItem
*
item
=
new
GridItem
(
*
this
,
state
);
item
->
set_grid_pos
(
Coord
{(
int
)
i
,
(
int
)
j
});
m_scene
->
addItem
(
item
);
}
}
...
...
@@ -296,7 +305,7 @@ Grid GridView::get_grid() const
Coord
pos
;
pos
.
x
=
i
;
pos
.
y
=
j
;
grid
.
set_cell
(
pos
,
item
->
cell_
state
);
grid
.
set_cell
(
pos
,
item
->
state
()
);
}
}
...
...
@@ -320,7 +329,7 @@ Structure GridView::selected_cells() const
Q_FOREACH
(
const
auto
&
item
,
m_scene
->
selectedItems
())
{
GridItem
*
cell
=
static_cast
<
GridItem
*>
(
item
);
cells
.
push_back
(
std
::
make_pair
(
cell
->
cell
_pos
-
origin
,
cell
->
cell_
state
));
cells
.
push_back
(
std
::
make_pair
(
cell
->
grid
_pos
()
-
origin
,
cell
->
state
()
));
}
return
Structure
(
cells
.
begin
(),
cells
.
end
());
}
...
...
@@ -357,9 +366,7 @@ void GridView::fill_selection(unsigned state)
for
(
auto
&
abstract_item
:
m_scene
->
selectedItems
())
{
GridItem
*
item
=
static_cast
<
GridItem
*>
(
abstract_item
);
item
->
setBrush
(
QBrush
(
stateColor_to_QColor
(
m_alph
.
getState
(
state
).
getColor
())));
item
->
cell_state
=
state
;
item
->
update
();
item
->
set_state
(
state
);
}
}
...
...
@@ -378,9 +385,7 @@ void GridView::paste_structure_at(Coord origin, const Structure &s)
GridItem
*
item
=
item_at
(
corrected
);
assert
(
item
!=
nullptr
);
item
->
cell_state
=
cell
.
second
;
item
->
setBrush
(
QBrush
(
stateColor_to_QColor
(
m_alph
.
getState
(
cell
.
second
).
getColor
())));
item
->
update
();
item
->
set_state
(
cell
.
second
);
}
}
...
...
@@ -391,10 +396,10 @@ Coord GridView::top_left_of_selection() const
Q_FOREACH
(
const
auto
&
item
,
m_scene
->
selectedItems
())
{
GridItem
*
cell
=
static_cast
<
GridItem
*>
(
item
);
if
(
cell
->
cell
_pos
.
x
<
selection_top_left
.
x
)
selection_top_left
.
x
=
cell
->
cell
_pos
.
x
;
if
(
cell
->
cell
_pos
.
y
<
selection_top_left
.
y
)
selection_top_left
.
y
=
cell
->
cell
_pos
.
y
;
if
(
cell
->
grid
_pos
()
.
x
<
selection_top_left
.
x
)
selection_top_left
.
x
=
cell
->
grid
_pos
()
.
x
;
if
(
cell
->
grid
_pos
()
.
y
<
selection_top_left
.
y
)
selection_top_left
.
y
=
cell
->
grid
_pos
()
.
y
;
}
return
selection_top_left
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment