-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCellItem.cpp
55 lines (42 loc) · 1.19 KB
/
CellItem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "CellItem.hpp"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSimpleTextItem>
#include <QPainter>
#include "Cell.hpp"
const int cellSize = 32;
CellItem::CellItem(Cell *cell) :
QGraphicsItem()
{
m_cell = cell;
m_text = new QGraphicsSimpleTextItem(this);
QFont f = m_text->font();
f.setPixelSize(cellSize - 4);
m_text->setFont(f);
setPos(m_cell->x() * cellSize, m_cell->y() * cellSize);
}
QRectF CellItem::boundingRect() const
{
return QRectF(0, 0, cellSize, cellSize);
}
void CellItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::HighQualityAntialiasing);
painter->drawRect(0, 0, cellSize, cellSize);
if (m_cell->haveMine()) {
m_text->setText("+");
} else if (m_cell->minesAround() > 0) {
m_text->setText(QString::number(m_cell->minesAround()));
}
}
void CellItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
event->accept();
}
void CellItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_cell->open();
}
update();
}