-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare.cpp
84 lines (75 loc) · 1.86 KB
/
square.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
#include "square.hh"
Square::Square(QWidget *parent) :
Square(SquareState::EMPTY, parent)
{
}
Square::Square(SquareState correctState, QWidget *parent) :
QWidget(parent),
m_state(SquareState::EMPTY),
m_correctState(correctState)
{
setAutoFillBackground(true);
updateColor();
}
SquareState Square::getState() const
{
return m_state;
}
void Square::setState(SquareState state)
{
if (state != m_state) {
m_state = state;
updateColor();
emit stateChanged();
}
}
SquareState Square::getCorrectState() const
{
return m_correctState;
}
void Square::setCorrectState(SquareState correctState)
{
m_correctState = correctState;
}
bool Square::isCorrectState() const
{
if (getCorrectState() == SquareState::FILLED) {
return getState() == SquareState::FILLED;
}
else {
return getState() != SquareState::FILLED;
}
}
void Square::updateColor()
{
QPalette pal = palette();
switch (getState()) {
case SquareState::EMPTY:
pal.setColor(QPalette::Background, Qt::white);
break;
case SquareState::FLAGGED:
pal.setColor(QPalette::Background, Qt::yellow);
break;
case SquareState::FILLED:
pal.setColor(QPalette::Background, Qt::black);
break;
}
setPalette(pal);
}
void Square::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (getState() == SquareState::FILLED)
setState(SquareState::EMPTY);
else if (getState() == SquareState::EMPTY)
setState(SquareState::FILLED);
event->accept();
}
else if (event->button() == Qt::RightButton) {
if (getState() == SquareState::FLAGGED)
setState(SquareState::EMPTY);
else if (getState() == SquareState::EMPTY)
setState(SquareState::FLAGGED);
event->accept();
}
}