-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseat.cpp
96 lines (81 loc) · 1.93 KB
/
seat.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
#include "seat.h"
#include <QStyleOption>
#include <QMessageBox>
#include <QApplication>
Seat::Seat(AirPlaneWidget *airPlaneWidget,
const bool taken) :
m_airPlaneWidget(airPlaneWidget),
m_taken(taken)
{
setAcceptHoverEvents(true);
}
Seat::~Seat()
{
}
bool Seat::taken() const
{
return m_taken;
}
void Seat::setTaken(const bool taken)
{
m_taken = taken;
setAcceptHoverEvents(!taken);
update();
}
void Seat::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *w)
{
Q_UNUSED(w);
painter->setPen(Qt::NoPen);
// taken seats are gray
// free seats are blue
// a free seat when the mouse is over it is red
if (m_taken) {
painter->setBrush(Qt::gray);
} else {
if (option->state & QStyle::State_MouseOver) {
painter->setBrush(Qt::red);
} else {
painter->setBrush(Qt::blue);
}
}
painter->drawRect(QRect(-7,-7,20,20));
}
QRectF Seat::boundingRect() const
{
return QRectF(-7,-7,20,20);
}
void Seat::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
if (m_taken) {
if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
cancelReservationDialog();
}
return;
}
setTaken(true);
emit clicked(this);
}
void Seat::cancelReservationDialog()
{
QMessageBox dialog(m_airPlaneWidget);
dialog.setText("Really cancel reservation?");
dialog.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
dialog.setDefaultButton(QMessageBox::No);
int ret = dialog.exec();
switch (ret) {
case QMessageBox::Yes:
// Save was clicked
setTaken(false);
emit clicked(this);
return;
case QMessageBox::No:
// Don't Save was clicked
return;
default:
// should never be reached
return;
}
}