-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeckItem.cpp
52 lines (40 loc) · 960 Bytes
/
DeckItem.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
#include "DeckItem.h"
#include "CardItem.h"
#include "MainWindow.h"
#include <QBrush>
#include <QGraphicsScene>
DeckItem::DeckItem(MainWindow* window, Deck& deck)
: GameItem{window}, m_deck{deck}
{
}
QRectF DeckItem::boundingRect() const
{
return QRectF{0, 0, CardItem::WIDTH, CardItem::HEIGHT};
}
void DeckItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
{
auto rect = boundingRect();
if(m_deck.isEmpty()) {
QPen pen{Qt::black};
painter->setPen(pen);
painter->drawRect(rect);
} else {
painter->fillRect(rect, QBrush(Qt::blue));
}
}
void DeckItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*)
{
if(m_deck.isEmpty()) {
window()->resetDeck();
update();
return;
}
auto card = m_deck.draw();
if(!card.isFlipped()) {
card.flip();
}
CardItem* item = new CardItem{window(), card};
item->moveBy(0, boundingRect().height() + 10);
scene()->addItem(item);
update();
}