-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindowview.cpp
206 lines (159 loc) · 6.53 KB
/
mainwindowview.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <QTime>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QMessageBox>
#include <QDesktopServices>
#include "mainwindowview.h"
#include "ui_mainwindowview.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, seedStatusLabel(new QLabel)
, algoStatusLabel(new QLabel)
, sizeStatusLabel(new QLabel)
, dockWindow(new DockWindow(this))
, saveGame(new SaveGameDialog(this))
{
ui->setupUi(this);
// Let playground has same size as ui->gameWindow
QHBoxLayout *layout = new QHBoxLayout;
ui->gameWindow->setLayout(layout);
//set statusbar
size_t gameSeed = QRandomGenerator::global()->bounded(0, INT_MAX);
seedStatusLabel->setText(QString::number(gameSeed));
algoStatusLabel->setText("Depth");
sizeStatusLabel->setText("7x7");
ui->statusBar->addWidget(this->sizeStatusLabel);
ui->statusBar->addWidget(this->seedStatusLabel);
ui->statusBar->addWidget(this->algoStatusLabel);
this->addDockWidget(Qt::RightDockWidgetArea, this->dockWindow);
this->gameModel = new GameModel(7, gameSeed);
this->gameView = new GameView(this->gameModel, ui->gameWindow);
connect(this->gameModel, &GameModel::sendGameSeed, this->seedStatusLabel, &QLabel::setText);
ui->gameWindow->layout()->addWidget(this->gameView);
// pause buttom
connect(this->dockWindow, &DockWindow::changedGameStarted, this->gameModel, &GameModel::changeGameStarted);
// solution button
connect(this->dockWindow, &DockWindow::clickedSolutionBtn, this->gameModel, &GameModel::showSolution);
// reset button
connect(this->dockWindow, &DockWindow::clickedResetBtn, this->gameModel, &GameModel::resetGame);
// new game button
connect(this->dockWindow, &DockWindow::clickedNewGameBtn, this, [=]() {
NewGameDialog dialog(this->gameModel->getSize(), this->gameModel->getAlgoType() , this);
if (dialog.exec() == QDialog::Accepted) {
// get alto type of new game
int algoType = 0;
if (dialog.getAlgoType() == "Depth")
algoType = GameModel::Depth;
else if (dialog.getAlgoType() == "Prim")
algoType = GameModel::Prim;
else
throw "unknown game type";
// get and set new game size
this->gameModel->setSize(dialog.getSize());
// get and set new game seed
this->gameModel->setGameSeed(dialog.getSeed());
// set algo type
this->gameModel->initializeGame(algoType);
// set everything in dock window to default.
this->dockWindow->newGameStarted();
// set game status to statusbar
this->sizeStatusLabel->setText(QString("%1x%2").arg(dialog.getSize()).arg(dialog.getSize()));
this->algoStatusLabel->setText(dialog.getAlgoType());
}
});
// hint button
connect(this->dockWindow, &DockWindow::clickedHintBtn, this->gameModel, &GameModel::showSolutionOnRandomTile);
// connect hint action and hint button
connect(this->dockWindow, &DockWindow::hintBtnEnabledChanged, ui->hintAction, &QAction::setEnabled);
// connect solution action and solution button
connect(this->dockWindow, &DockWindow::solutionBtnEnabledChanged, ui->solutionAction, &QAction::setEnabled);
// connect pause action and pause button
connect(this->dockWindow, &DockWindow::pauseBtnEnabledChanged, ui->pauseAction, &QAction::setEnabled);
connect(this->dockWindow, &DockWindow::pauseBtnTextChanged, ui->pauseAction, &QAction::setText);
// count how many times HINT has been used.
// connect(this->gameView, &GameView::hintSucceeded, this->dockWindow, &DockWindow::setHintCount);
// send game started from GameModel
connect(this->gameModel, &GameModel::gameStart, this->dockWindow, &DockWindow::gameStarted);
// set text to totalSteps
connect(this->gameModel, &GameModel::sendSteps, this->dockWindow, &DockWindow::setStep);
// Timer
connect(this->gameModel, &GameModel::sendTime, this->dockWindow, &DockWindow::setTime);
// can not click hint button if the game has ended
connect(this->gameModel, &GameModel::onGameStatus, this, [=](bool status) {
if (status) // if there is a solution
this->dockWindow->setHintBtnEnabled(!status); // set hint button to disable
});
// can not click solution button if the game has ended
connect(this->gameModel, &GameModel::onGameStatus, this, [=](bool status) {
if (status) // if there is a solution
this->dockWindow->setSolutionBtnEnabled(!status); // set solution button to disable
});
// can not click solution button if the game has ended
connect(this->gameModel, &GameModel::onGameStatus, this, [=](bool status) {
if (status) // if there is a solution
this->dockWindow->setPauseBtnEnabled(!status); // set pause button to disable
});
}
// new game action
void MainWindow::on_newGameAction_triggered()
{
this->dockWindow->clickNewGameBtn();
}
// save game action
void MainWindow::on_saveGameAction_triggered()
{
// TODO
saveGame->exec();
}
// load game action
void MainWindow::on_loadGameAction_triggered()
{
// TODO
}
// pause action
void MainWindow::on_pauseAction_triggered()
{
this->dockWindow->clickPauseBtn();
}
// reset action
void MainWindow::on_resetAction_triggered()
{
this->dockWindow->clickResetBtn();
}
// exit action
void MainWindow::on_exitAction_triggered()
{
this->close();
}
// Hint
void MainWindow::on_hintAction_triggered()
{
this->dockWindow->clickHintBtn();
}
// Solution action
void MainWindow::on_solutionAction_triggered()
{
this->dockWindow->clickSolutionBtn();
}
// About action
void MainWindow::on_actionAbout_2_triggered()
{
QMessageBox::about(this, "About", "<h1>Netzwerkpuzzle</h1>"
"A Project at BHT, 2022<br><br>"
"<u><b>Autoren</b></u><br>"
"Xuatong Pan<br>"
"Paul Matti Meinhold<br>"
"Parfait R. Fejou<br>"
"Erdenetuya Undral<br>");
}
// Help Page action
void MainWindow::on_actionHelp_Page_triggered()
{
QString link = "https://github.com/FettGoenner/coma3/tree/Milestone#readme";
QDesktopServices::openUrl(QUrl(link));
}
MainWindow::~MainWindow()
{
delete ui;
}