-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardStack.cpp
111 lines (92 loc) · 2.26 KB
/
CardStack.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
// Author: Annie Berend (5033782) - Jonathan Verbeek (5058288)
#include "CardStack.h"
#include <QDebug>
CCardStack::CCardStack(QWidget *parent)
: QWidget(parent)
{
// Connect the event
QObject::connect(this, &CCardStack::onCardsChanged, this, &CCardStack::handleCardsChanged);
// Enable dropping elements
setAcceptDrops(true);
}
void CCardStack::addCard(CCard *cardToAdd)
{
// Make sure the card is valid
if (cardToAdd)
{
// Add it to the list
cards.push_back(cardToAdd);
// Set the card's stack to this one
cardToAdd->setCardStack(this);
// Notify the handler
emit onCardsChanged();
}
}
void CCardStack::insertCardAt(int insertAt, CCard* cardToInsert)
{
// Make sure the card is valid
if (cardToInsert)
{
// Add it to the list
cards.insert(insertAt, cardToInsert);
// Set the card's stack to this one
cardToInsert->setCardStack(this);
// Notify the handler
emit onCardsChanged();
}
}
void CCardStack::removeCard(CCard *cardToRemove)
{
// Make sure the card is valid
if (cardToRemove)
{
// Remove it from the list
cards.removeOne(cardToRemove);
// Clear the card's stack
cardToRemove->setCardStack(nullptr);
// Notify the handler
emit onCardsChanged();
}
}
void CCardStack::removeCardAt(int cardIndexToRemove)
{
// Make sure the index is valid
if (cardIndexToRemove >= 0 && cardIndexToRemove < cards.length())
{
// Remove it from the list
removeCard(cards[cardIndexToRemove]);
}
}
int CCardStack::getNumCards() const
{
// Return the length of the cards list
return cards.length();
}
CCard* CCardStack::getTopCard()
{
// Make sure we have cards
if (cards.length() > 0)
{
// Return the last card in the list, as that will be the topmost card
return cards.last();
}
else
{
// Return nothing
return nullptr;
}
}
bool CCardStack::canDropCard(CCard* cardToDrop)
{
Q_UNUSED(cardToDrop);
// By default, allow dropping
return true;
}
int CCardStack::getIndexOfCard(CCard* card)
{
// Find the card
return cards.indexOf(card);
}
void CCardStack::handleCardsChanged()
{
}