forked from kevinsleep/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.h
201 lines (151 loc) · 4.04 KB
/
MainWindow.h
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
#pragma once
#include <QtWidgets/QMainWindow>
#include <QApplication>
#include <QWidget>
#include <QTextEdit>
#include <QLabel>
#include <QDockWidget>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <qpainter.h>
#include <QCheckBox>
#include <QSpinBox>
#include <QPushButton>
#include <QHBoxLayout>
#include <QToolBar>
#include <QStatusBar>
#include <random>
#include <stack>
#include <QThread>
#include <QImage>
#include <string>
#include <set>
#include <queue>
#include <QComboBox>
#include "AdjacencyList.h"
//#include "ui_QtWidgetsApplication1.h"
enum class Generate_method
{
DeepFirstSearch = 0,
Prim = 1,
Kruskal = 2
};
enum class FindPath_method
{
DeepFirstSearch = 0,
BreadthFirstSearch = 1,
Dijkstra = 2,
AStar = 3
};
extern const char* generate_method_str[];
/*
* MainWindow是该程序的主体窗口
* 包含一个迷宫绘制窗口MazeWidget和一个显示信息窗口ShowInfoWidget
* 还包含一个DrawThread线程,用于生成迷宫
*/
class MazeWidget;
class ShowInfoWidget;
class DrawThread;
class MazeWidget : public QWidget
{
Q_OBJECT
public:
Generate_method method = Generate_method::DeepFirstSearch;
AdjacencyList* maze; //用邻接表表示迷宫间格子的连接关系
MazeWidget(QWidget* parent = nullptr);
void paintEvent(QPaintEvent* event) override;
//下面三个方法已被废弃,其功能由DrawThread类中的方法替代
//void paintMaze(QPainter& painter);
//void generateMaze(int row, int column);
//void generateMazeByDeepFirstSearch(int row, int column);
std::shared_ptr<QImage> image = nullptr;
};
class ShowInfoWidget : public QWidget
{
Q_OBJECT
public:
ShowInfoWidget(QWidget* parent = nullptr);
void paintEvent(QPaintEvent* event) override;
QCheckBox* checkbox_show_wall;
QCheckBox* checkbox_show_path;
QCheckBox* checkbox_show_solution;
QCheckBox* checkbox_show_accessed;
private:
QLabel* label_show;
QLabel* label_wall;
QLabel* label_path;
QLabel* label_solution;
QLabel* label_accessed;
};
class DrawThread : public QThread
{
Q_OBJECT
public:
Generate_method method = Generate_method::DeepFirstSearch;
FindPath_method findPathMethod = FindPath_method::DeepFirstSearch;
AdjacencyList* maze; //用邻接表表示迷宫间格子的连接关系
std::vector<std::pair<int, int>>* path;
std::vector<std::pair<int, int>>* unaccessed;
QImage paintMaze(); //绘制迷宫的方法
QImage paintPath(); //绘制路径的方法
//~MazeWidget();
void generateMaze(int row, int column); //生成迷宫的方法
void generateMazeByDeepFirstSearch(int row, int column);
void generateMazeByPrim(int row, int column);
void generateMazeByKruskal(int row, int column);
void findPath(int start, int end); //寻找路径的方法
void findPathByDeepFirstSearch(int start, int end);
void findPathByBreadthFirstSearch(int start, int end);
void findPathByDijkstra(int start, int end);
void findPathByAStar(int start, int end);
void run() override;
bool isDrawing = false;
bool isGenerated = false;
bool isFindingPath = false;
bool showWall = true;
bool showPath = false;
bool showSolution = true;
int row;
int column;
QImage BaseImage;
DrawThread(
MazeWidget* mazeWidget,
int row, int column,
Generate_method method = Generate_method::DeepFirstSearch,
FindPath_method findPathMethod = FindPath_method::DeepFirstSearch
);
signals:
void imageReady(const QImage &image);
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
signals:
void methodChanged(Generate_method method);
public slots:
void onImageReady(const QImage& image) {
mazeWidget->image = std::shared_ptr<QImage>(new QImage(image));
mazeWidget->update();
}
void onMethodChanged(Generate_method method);
void onComboboxFindPathMethodChanged(int index);
private:
//Ui::QtWidgetsApplication1Class ui;
QSpinBox* row_cnt;
QSpinBox* col_cnt;
QLabel* label_row;
QLabel* label_col;
QLabel* label_generate_method;
QPushButton* button_generate;
QPushButton* button_findpath;
QComboBox* combobox_findpath_method;
MazeWidget* mazeWidget;
ShowInfoWidget* showInfoWidget;
DrawThread* drawThread;
void generateButtonClicked();
void findPathButtonClicked();
};