-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairplanewidget.cpp
154 lines (125 loc) · 4.2 KB
/
airplanewidget.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
153
154
#include "airplanewidget.h"
const QColor AirPlaneWidget::m_paperColor(255,255,153);
AirPlaneWidget::AirPlaneWidget(QWidget *parent,
DataBase *db) :
QGraphicsView(parent),
m_scene(new QGraphicsScene(this)),
m_dataBase(db)
{
m_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
m_scene->setSceneRect(-100, -350, 300, 550);
setScene(m_scene);
setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing);
setTransformationAnchor(AnchorUnderMouse);
setMinimumSize(400, 600);
drawAirPlane();
// signal from DB - data has been modified from outside
connect(m_dataBase, SIGNAL(dataChanged(QString)),
this, SLOT(seatChanged(QString)) );
}
AirPlaneWidget::~AirPlaneWidget()
{
}
void AirPlaneWidget::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
/// @todo draw a real airplane
painter->fillRect(m_scene->sceneRect(), AirPlaneWidget::m_paperColor);
painter->setBrush(Qt::NoBrush);
painter->drawRect(m_scene->sceneRect());
}
void AirPlaneWidget::seatChanged(Seat *seat)
{
QString id(idOfSeat(seat));
QString msg = QString("Seat %1 has been ").arg(id);
msg.append(seat->taken() ? "reserved" : "cancelled");
emit notification(msg);
if (!m_dataBase->writeData(id, seat->taken())) {
// Couldn't reserve seat, reverse state
seat->setTaken(!seat->taken());
}
}
// signal from DB - data has been modified from outside
void AirPlaneWidget::seatChanged(QString id)
{
QString msg = QString("Seat %1 has been changed - from DataBase").arg(id);
emit notification(msg);
bool taken;
if (!m_dataBase->readData(id,taken)) {
// Database read error, leaving the seat as it is
} else {
m_seats[id]->setTaken(taken);
}
}
/// @todo too many magic numbers
void AirPlaneWidget::drawAirPlane()
{
/** @note new QGraphicsTextItem(0) sounds like memory leak,
* since it has no parent to take care of the destruction,
* but after adding it to the scene, the scene will delete it
*/
// print help text
QGraphicsTextItem *helpText = new QGraphicsTextItem(0);
m_scene->addItem(helpText);
helpText->setDefaultTextColor(Qt::black);
helpText->setPlainText("'Ctrl' + click to cancel reservation");
helpText->setPos(-80, -330);
for (int column = 0; column < 6; column++) {
// print column letters: a,b,c d,e,f
QGraphicsTextItem *columnLetter = new QGraphicsTextItem(0);
m_scene->addItem(columnLetter);
columnLetter->setDefaultTextColor(Qt::black);
columnLetter->setPlainText(QString(QChar(column+97)));
qreal x(-16 + column * 23);
if (column > 2 ) {
x += 28;
}
columnLetter->setPos(x, -280);
for (int row = 0; row < 20; row++) {
// print row numbers
QGraphicsTextItem *rowNumber = new QGraphicsTextItem(0);
m_scene->addItem(rowNumber);
rowNumber->setDefaultTextColor(Qt::black);
rowNumber->setPlainText(QString(" %1").arg(row+1,2));
rowNumber->setPos(-50, -257 + row * 22);
// draw seat
drawSeat(row, column);
}
}
}
void AirPlaneWidget::drawSeat(const int row,
const int column)
{
Seat *seat = new Seat(this);
m_scene->addItem(seat);
QString id(QString("%1%2").arg(row+1).arg(QChar(97+column)));
m_seats[id] = seat;
qreal x(-10 + column * 22);
if (column > 2 ) {
x += 30;
}
qreal y(-250 + row * 22);
seat->setPos(x,y);
// read state from DB
bool taken;
if (!m_dataBase->readData(id,taken)) {
// Database read error
m_seats[id]->setTaken(false);
} else {
m_seats[id]->setTaken(taken);
}
connect(seat, SIGNAL(clicked(Seat*)),
this, SLOT(seatChanged(Seat*)));
}
QString AirPlaneWidget::idOfSeat(const Seat *seat) const
{
QMap<QString, Seat*>::const_iterator it;
for (it = m_seats.begin(); it != m_seats.end(); it++) {
if (it.value() == seat)
return it.key();
}
// impossible
return QString("Seat not found");
}