-
Notifications
You must be signed in to change notification settings - Fork 3
/
cardlist.cpp
154 lines (135 loc) · 3.17 KB
/
cardlist.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
80
81
82
83
84
85
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "cardlist.h"
#include "constant.h"
#include <QPainter>
#include <QDebug>
CardList::CardList()
{
lastCard = NULL;
setData(GD_Type,CLIST); //用于识别碰撞检测
}
bool CardList::enableCard(Card *card)
{
if(lastCard == NULL)
{
return true;
}
else
{
Card *t = lastCard;
if(int(t->getCardNum()) == int(card->getCardNum() + 1))
{
return (t->isBlackCard() != card->isBlackCard()); //花色不同
}
return false;
}
}
void CardList::insertCard(Card *card)
{
qDebug() << "insertCard()";
cardlist.append(card);
// for(int i = 0; i < cardlist.size()-1; i ++)
// {
// //把前面的卡片都设置成不可拖动
// cardlist[i]->setFlag(ItemIsMovable,false);
// cardlist[i]->setTop(false);
// }
if(lastCard != NULL)
card->setZValue(lastCard->zValue() + 1);
lastCard = card;
update();
}
void CardList::removeCard() //清理被移除的最后一张纸牌
{
qDebug() << "removeCard()";
cardlist.removeLast();
if(cardlist.size() == 0)
lastCard = NULL;
else
{
lastCard = cardlist.last();
qDebug() << "after remove, old cardlist's last Card:" << lastCard->cardNum;
lastCard->setFlag(ItemIsMovable,true);
//lastCard->setTop(true);
lastCard->nextCard = NULL;
}
update();
}
void CardList::removeCardLink(Card *begincard) //清理被移除的整个纸牌链
{
qDebug() << "removeCardLink";
int beginIndex = 0;
for(int i = 0; i < cardlist.size(); i++)
{
if(cardlist.at(i) == begincard)
{
beginIndex = i;
break;
}
}
if(beginIndex == 0)
{
cardlist.clear();
lastCard = NULL;
}
else
{
int nums = cardlist.size() - beginIndex;
while(nums--)
{
cardlist.removeLast();
}
lastCard = cardlist.last();
lastCard->setFlag(ItemIsMovable,true);
//lastCard->setTop(true);
lastCard->nextCard = NULL;
}
update();
}
Card *CardList::getLastCard()
{
return lastCard;
}
void CardList::appendCard(Card *newCard)
{
cardlist.append(newCard);
}
Card *CardList::getCard(int i)
{
if(i > cardlist.size())
return NULL;
return cardlist[i];
}
int CardList::getCardNums() const
{
return cardlist.size();
}
void CardList::initCardsZValue()
{
for(int i = 0; i < cardlist.size(); i++)
{
cardlist[i]->setZValue(i+1);
// qDebug() <<"z:" <<cardlist[i]->zValue();
}
}
QRectF CardList::boundingRect() const
{
qreal ht = CARD_HEIGHT + cardlist.size() * CARD_LIST_LEAP;
qreal hw = CARD_WIDTH;
return QRectF(0,0,hw,ht);
}
void CardList::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
painter->setPen(QPen(QColor(255,180,100),6));
painter->drawRect(0,0,CARD_WIDTH,CARD_HEIGHT);
painter->restore();
}
// 用于碰撞检测
QPainterPath CardList::shape() const
{
QPainterPath path;
path.addRect(0,cardlist.size()*CARD_LIST_LEAP,CARD_WIDTH,CARD_HEIGHT);
return path;
}