From 48cd806197b899ea1411067c5df6ae9e6343ea21 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 26 Jun 2022 13:57:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E5=BC=80=E5=A7=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/busModel.h | 19 ++++++++++++++ src/busModel.cpp | 18 ++++++++++--- src/centralwidget.cpp | 23 +++++++++++++++-- src/header/centralwidget.h | 27 +++++++++++++++++++- src/header/mainwindow.h | 47 ++++++++++++++++++++++++++++++++++ src/mainwindow.cpp | 52 ++++++++++++++++++++++++++++++++++---- 6 files changed, 175 insertions(+), 11 deletions(-) diff --git a/include/busModel.h b/include/busModel.h index 851c0a9..b4db775 100644 --- a/include/busModel.h +++ b/include/busModel.h @@ -13,6 +13,7 @@ #include "string" #include "sstream" #include "QObject" +#include "QDebug" /** * 控制公交车的类,继承了QObject @@ -77,6 +78,24 @@ public slots: */ void ReadConfigFileSlot(const QString& file_name); + /** + * 创建上下车请求的槽函数 + * @param query_type 请求的类型 + * @param node_id 请求站点的id + */ + void AddQuerySlot(int query_type, int node_id) const; + +signals: + /** + * 创建轨道链表完成的信号 + * @param node_num 轨道上节点的个数 + */ + void RailsCreated(int node_num); + + /** + * 请求发生修改的槽函数 + */ + void QueryChangedSignal(); private: diff --git a/src/busModel.cpp b/src/busModel.cpp index e21c554..279620b 100644 --- a/src/busModel.cpp +++ b/src/busModel.cpp @@ -29,6 +29,17 @@ void BusControllerModel::ReadConfigFileSlot(const QString& file_name) ReadConfigFile(file_name.toStdString()); } +void BusControllerModel::AddQuerySlot(int query_type, int node_id) const +{ + rail_node_t *node_pos = rail_manager->FindNode(node_id); + query_manager->CreateQuery(query_type, node_pos); +} + +/* + * 以下函数的实现移植自auto_pilot_bus + * 源程序采用C写成 + */ + int BusControllerModel::GetBusPosition() const { int result = 0; @@ -293,11 +304,12 @@ void BusControllerModel::ReadConfigFile(const std::string& file_name) chosen_strategy = BUS_FCFS; } + qDebug() << node_space_length << total_station; // 得到轨道的总长度 total_distance = node_space_length * total_station; - // 重新生成轨道模型 - delete rail_manager; - rail_manager = new RailsModel(node_space_length, total_station); + rail_manager->CreateRails(node_space_length, total_station); + + emit RailsCreated(total_station); } int BusControllerModel::FCFSDirection() const diff --git a/src/centralwidget.cpp b/src/centralwidget.cpp index 7f30ff2..e214b5f 100644 --- a/src/centralwidget.cpp +++ b/src/centralwidget.cpp @@ -6,16 +6,19 @@ #include "header/moc_centralwidget.cpp" #include "form/ui_CentralWidget.h" -#include "centralwidget.h" -CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CentralWidget) +CentralWidget::CentralWidget(QWidget *parent, BusControllerModel *bus_controller) : QWidget(parent), ui(new Ui::CentralWidget) { ui->setupUi(this); scene_manager = new SceneManager(20); ui->main_canva->setScene(scene_manager->scene); + controller = bus_controller; + + SetControlConnection(); + SetWidgetConnection(); SetupQueryList(); } @@ -27,6 +30,17 @@ CentralWidget::~CentralWidget() delete ui; } +void CentralWidget::SetControlConnection() +{ + +} + +void CentralWidget::SetWidgetConnection() +{ + QObject::connect(ui->create_query_button, &QPushButton::clicked, + this, &CentralWidget::AddQueryButtonClicked); +} + void CentralWidget::SetupQueryList() { // 设置请求列表的表头 @@ -77,4 +91,9 @@ void CentralWidget::DeleteQueryItem(int query_id) delete deleted_widget; delete *itor; query_items.erase(itor); +} + +void CentralWidget::AddQueryButtonClicked() +{ + } \ No newline at end of file diff --git a/src/header/centralwidget.h b/src/header/centralwidget.h index ee6a496..e16c753 100644 --- a/src/header/centralwidget.h +++ b/src/header/centralwidget.h @@ -12,6 +12,7 @@ #include "mainScene.h" #include "queryListItem.h" +#include "busModel.h" QT_BEGIN_NAMESPACE namespace Ui @@ -25,7 +26,7 @@ class CentralWidget : public QWidget Q_OBJECT public: - explicit CentralWidget(QWidget *parent = nullptr); + explicit CentralWidget(QWidget *parent = nullptr, BusControllerModel *bus_controller = nullptr); ~CentralWidget() override; @@ -37,8 +38,17 @@ public slots: */ void AppendQueryItem(int query_type, int node_id); + /** + * 处理删除一个请求的槽函数 + * @param query_id 请求的编号 + */ void DeleteQueryItem(int query_id); + /** + * 处理点击添加请求按钮的槽函数 + */ + void AddQueryButtonClicked(); + private: /** * UI控件 @@ -50,11 +60,26 @@ private: */ SceneManager *scene_manager; + /** + * 公交车控制器 + */ + BusControllerModel *controller; + /** * 请求列表中的对象 */ std::list query_items; + /** + * 设置同控制器的连接 + */ + void SetControlConnection(); + + /** + * 设置同控件的连接 + */ + void SetWidgetConnection(); + /** * 初始化请求展示列表 * 展示表头的说明文字 diff --git a/src/header/mainwindow.h b/src/header/mainwindow.h index 99b67e6..effdb75 100644 --- a/src/header/mainwindow.h +++ b/src/header/mainwindow.h @@ -7,6 +7,7 @@ #include #include "QFileDialog" +#include "QThread" #include "centralwidget.h" #include "busModel.h" @@ -35,17 +36,63 @@ signals: */ void OpenConfigFileSignal(QString file_name); + /** + * 开始运行公交车的信号 + */ + void RunBusSignal(); + + /** + * 暂停运行公交车的信号 + */ + void PauseBusSignal(); + + /** + * 停止运行公交车的信号 + */ + void StopBusSignal(); + public slots: /** * 点击打开配置文件按钮的槽函数 */ void ReadConfigFileButtonClicked(); + /** + * 处理点击运行公交车按钮的槽函数 + */ + void RunBusClicked(); + + /** + * 处理点击暂停公交车按钮的槽函数 + */ + void PauseBusClicked(); + + /** + * 处理点击停止公交车按钮的槽函数 + */ + void StopBusClicked(); + private: + /** + * UI控件 + */ Ui::MainWindow *ui; + /** + * 中间的显示框架 + */ CentralWidget *central_widget; + /** + * 控制类 + */ BusControllerModel *controller; + QThread *worker_thread; + + /** + * 设置菜单栏的相关连接 + */ void SetMenuBarConnection(); + + void SetControlConnection(); }; #endif //AUTO_BUS_GUI_MAIN_WINDOW_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 31d6003..7c1c1f9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -6,32 +6,59 @@ #include "header/moc_mainwindow.cpp" #include "form/ui_MainWindow.h" -#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ui = new Ui::MainWindow; - central_widget = new CentralWidget; controller = new BusControllerModel; + worker_thread = new QThread; + + // 开始多线程 + controller->moveToThread(worker_thread); + worker_thread->start(); + + central_widget = new CentralWidget(nullptr, controller); ui->setupUi(this); this->setCentralWidget(central_widget); SetMenuBarConnection(); - + SetControlConnection(); } MainWindow::~MainWindow() { + worker_thread->quit(); + delete ui; delete central_widget; delete controller; + delete worker_thread; } void MainWindow::SetMenuBarConnection() { - QObject::connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close())); - QObject::connect(ui->actionRead_ConfigFile, &QAction::triggered, this, &MainWindow::ReadConfigFileButtonClicked); + // 连接退出按钮 + QObject::connect(ui->actionExit, SIGNAL(triggered()), + this, SLOT(close())); + // 连接读取配置文件操作 + QObject::connect(ui->actionRead_ConfigFile, &QAction::triggered, + this, &MainWindow::ReadConfigFileButtonClicked); + // 连接运行公交车按钮 + QObject::connect(ui->actionRun_Bus, &QAction::triggered, + this, &MainWindow::RunBusClicked); + // 连接暂停公交车按钮 + QObject::connect(ui->actionPause_Bus, &QAction::triggered, + this, &MainWindow::PauseBusClicked); + // 连接停止公交车按钮 + QObject::connect(ui->actionStop_Bus, &QAction::triggered, + this, &MainWindow::StopBusClicked); +} + +void MainWindow::SetControlConnection() +{ + QObject::connect(this, &MainWindow::OpenConfigFileSignal, + controller, &BusControllerModel::ReadConfigFileSlot); } void MainWindow::ReadConfigFileButtonClicked() @@ -53,3 +80,18 @@ void MainWindow::ReadConfigFileButtonClicked() emit OpenConfigFileSignal(file_name); } } + +void MainWindow::RunBusClicked() +{ + emit RunBusSignal(); +} + +void MainWindow::PauseBusClicked() +{ + emit PauseBusSignal(); +} + +void MainWindow::StopBusClicked() +{ + emit StopBusSignal(); +} \ No newline at end of file