-
Notifications
You must be signed in to change notification settings - Fork 3
/
myitem.cpp
79 lines (67 loc) · 2.52 KB
/
myitem.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "myitem.h"
myItem::myItem()
{
this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setAcceptedMouseButtons(Qt::AllButtons);
updateItem();
}
void myItem::simulateLeftClick(){
if(!sweeped&&!mark){
setSweeped(true);
if(mine){
stepMine=true;
updateItem();
emit stepOnMine(row,col);
}
else if(numOfMines==0)emit stepOnNone(row,col);
}
}
void myItem::mousePressEvent(QGraphicsSceneMouseEvent *event){
qDebug()<<"检测到按下!"<<row<<","<<col;
qDebug()<<event->button();
qDebug()<<event->buttons();
if(event->buttons()==(Qt::LeftButton|Qt::RightButton)){
qDebug()<<"左右键同时按下";
if((sweeped&&numOfMines!=0)||(!sweeped&&mark)){//已被挖掘且有数字/未挖掘被标记
emit doubleClickSignal(row,col);
}
}else if(event->button()==Qt::LeftButton){//左键点击
qDebug()<<"左键按下";
simulateLeftClick();
}else if(event->button()==Qt::RightButton){
qDebug()<<"右键按下";
if(!sweeped){
setMark(!mark);
qDebug()<<"标记改变!";
emit markChangedSignal();
}
}
emit checkSignal();
}
void myItem::updateItem(){
if(stepMine)this->setPixmap(QPixmap(":/mine/pic/mine_step.jpg"));
else if(!sweeped){
if(!mark)this->setPixmap(QPixmap(":/mine/pic/notSweep.jpg"));
else this->setPixmap(QPixmap(":/mine/pic/flag.jpg"));
}
else if(mine){
this->setPixmap(QPixmap(":/mine/pic/mine.jpg"));
}else{
if(numOfMines==0)this->setPixmap(QPixmap(":/mine/pic/mine0.jpg"));
else if(numOfMines==1)this->setPixmap(QPixmap(":/mine/pic/mine1.jpg"));
else if(numOfMines==2)this->setPixmap(QPixmap(":/mine/pic/mine2.jpg"));
else if(numOfMines==3)this->setPixmap(QPixmap(":/mine/pic/mine3.jpg"));
else if(numOfMines==4)this->setPixmap(QPixmap(":/mine/pic/mine4.jpg"));
else if(numOfMines==5)this->setPixmap(QPixmap(":/mine/pic/mine5.jpg"));
else if(numOfMines==6)this->setPixmap(QPixmap(":/mine/pic/mine6.jpg"));
else if(numOfMines==7)this->setPixmap(QPixmap(":/mine/pic/mine7.jpg"));
else if(numOfMines==8)this->setPixmap(QPixmap(":/mine/pic/mine8.jpg"));
}
}
void myItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QStyleOptionGraphicsItem op;
op.initFrom(widget);
op.state = QStyle::State_None;
return QGraphicsPixmapItem::paint(painter, &op, widget);
}