-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.cpp
132 lines (108 loc) · 3.4 KB
/
dialog.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
#include <dialog.h>
Dialog::Dialog() {
board = new Board(50, 50, 25, 25, 25, 25, 0);
createHorizontalGroupBox();
createVillageArea();
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(horizontalGroupBox);
mainLayout->addWidget(villageGroupBox);
setLayout(mainLayout);
setWindowTitle("Demokratur");
}
Dialog::~Dialog() {
delete board;
delete horizontalGroupBox;
delete villageGroupBox;
delete labels;
delete[] buttons[2];
delete[] lineEdits[2];
delete[] exitAction;
delete thread;
delete worker;
}
void Dialog::setLabels(QLabel* pLabels) {
labels = pLabels;
}
QLabel* Dialog::getLabels() {
return labels;
}
void Dialog::createHorizontalGroupBox() {
horizontalGroupBox = new QGroupBox("Eingabe:");
QHBoxLayout* layout = new QHBoxLayout;
lineEdits[0] = new QLineEdit;
lineEdits[0]->setText("50");
layout->addWidget(lineEdits[0]);
lineEdits[1] = new QLineEdit;
lineEdits[1]->setText("50");
layout->addWidget(lineEdits[1]);
thread = new QThread;
worker = new Worker(board, 1000000000);
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(repaint()), this, SLOT(repaint()));
connect(worker, SIGNAL(finished()), this, SLOT(finished()));
connect(this, SIGNAL(setBoard(Board*)), worker, SLOT(setBoard(Board*)));
buttons[0] = new QPushButton("Start");
QObject::connect(buttons[0], &QPushButton::clicked,
[this] { handleStartButton(); });
layout->addWidget(buttons[0]);
buttons[1] = new QPushButton("Stop");
QObject::connect(buttons[1], &QPushButton::clicked,
[this] { handleStopButton(); });
layout->addWidget(buttons[1]);
buttons[1]->setEnabled(false);
horizontalGroupBox->setLayout(layout);
horizontalGroupBox->setFixedHeight(58);
}
void Dialog::createVillageArea() {
villageGroupBox = new QGroupBox("Dorf");
villageGroupBox->setMinimumHeight(600);
villageGroupBox->setMinimumWidth(600);
}
void Dialog::handleStartButton() {
int x = lineEdits[0]->text().toInt();
int y = lineEdits[1]->text().toInt();
board->reset(x, y, 25, 25, 25, 25, 0);
thread->start();
buttons[0]->setEnabled(false);
buttons[1]->setEnabled(true);
}
void Dialog::handleStopButton() {
thread->requestInterruption();
thread->quit();
buttons[0]->setEnabled(true);
buttons[1]->setEnabled(false);
}
void Dialog::finished() {
std::cout << "finished" << std::endl;
handleStopButton();
}
void Dialog::repaint() {
update();
}
void Dialog::paintEvent(QPaintEvent*) {
int x = 20;
int y = 90;
double oneWidth = (size().width() - 2 * x) / (double)board->getXDim();
double oneHeight = (size().height() - y - 20) / (double)board->getYDim();
QPainter painter(this);
Citizen* citizens = board->getCitizens();
for (int i = 0; i < board->getYDim(); i++) {
for (int j = 0; j < board->getXDim(); j++) {
Citizen citizen = citizens[i * board->getXDim() + j];
QRect rect =
QRect((j * oneWidth) + x, (i * oneHeight) + y, oneWidth, oneHeight);
painter.setPen(QPen(Qt::gray, 2));
painter.drawRect(rect);
if (citizen.getParty() == 0) {
painter.fillRect(rect, Qt::red);
} else if (citizen.getParty() == 1) {
painter.fillRect(rect, Qt::cyan);
} else if (citizen.getParty() == 2) {
painter.fillRect(rect, Qt::yellow);
} else {
painter.fillRect(rect, Qt::green);
}
}
}
}