多线程开始
This commit is contained in:
parent
4709b1ad7c
commit
48cd806197
|
@ -13,6 +13,7 @@
|
||||||
#include "string"
|
#include "string"
|
||||||
#include "sstream"
|
#include "sstream"
|
||||||
#include "QObject"
|
#include "QObject"
|
||||||
|
#include "QDebug"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 控制公交车的类,继承了QObject
|
* 控制公交车的类,继承了QObject
|
||||||
|
@ -77,6 +78,24 @@ public slots:
|
||||||
*/
|
*/
|
||||||
void ReadConfigFileSlot(const QString& file_name);
|
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:
|
private:
|
||||||
|
|
|
@ -29,6 +29,17 @@ void BusControllerModel::ReadConfigFileSlot(const QString& file_name)
|
||||||
ReadConfigFile(file_name.toStdString());
|
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 BusControllerModel::GetBusPosition() const
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
@ -293,11 +304,12 @@ void BusControllerModel::ReadConfigFile(const std::string& file_name)
|
||||||
chosen_strategy = BUS_FCFS;
|
chosen_strategy = BUS_FCFS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
qDebug() << node_space_length << total_station;
|
||||||
// 得到轨道的总长度
|
// 得到轨道的总长度
|
||||||
total_distance = node_space_length * total_station;
|
total_distance = node_space_length * total_station;
|
||||||
// 重新生成轨道模型
|
rail_manager->CreateRails(node_space_length, total_station);
|
||||||
delete rail_manager;
|
|
||||||
rail_manager = new RailsModel(node_space_length, total_station);
|
emit RailsCreated(total_station);
|
||||||
}
|
}
|
||||||
|
|
||||||
int BusControllerModel::FCFSDirection() const
|
int BusControllerModel::FCFSDirection() const
|
||||||
|
|
|
@ -6,16 +6,19 @@
|
||||||
|
|
||||||
#include "header/moc_centralwidget.cpp"
|
#include "header/moc_centralwidget.cpp"
|
||||||
#include "form/ui_CentralWidget.h"
|
#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);
|
ui->setupUi(this);
|
||||||
scene_manager = new SceneManager(20);
|
scene_manager = new SceneManager(20);
|
||||||
|
|
||||||
ui->main_canva->setScene(scene_manager->scene);
|
ui->main_canva->setScene(scene_manager->scene);
|
||||||
|
|
||||||
|
controller = bus_controller;
|
||||||
|
|
||||||
|
SetControlConnection();
|
||||||
|
SetWidgetConnection();
|
||||||
SetupQueryList();
|
SetupQueryList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +30,17 @@ CentralWidget::~CentralWidget()
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CentralWidget::SetControlConnection()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void CentralWidget::SetWidgetConnection()
|
||||||
|
{
|
||||||
|
QObject::connect(ui->create_query_button, &QPushButton::clicked,
|
||||||
|
this, &CentralWidget::AddQueryButtonClicked);
|
||||||
|
}
|
||||||
|
|
||||||
void CentralWidget::SetupQueryList()
|
void CentralWidget::SetupQueryList()
|
||||||
{
|
{
|
||||||
// 设置请求列表的表头
|
// 设置请求列表的表头
|
||||||
|
@ -77,4 +91,9 @@ void CentralWidget::DeleteQueryItem(int query_id)
|
||||||
delete deleted_widget;
|
delete deleted_widget;
|
||||||
delete *itor;
|
delete *itor;
|
||||||
query_items.erase(itor);
|
query_items.erase(itor);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CentralWidget::AddQueryButtonClicked()
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
#include "mainScene.h"
|
#include "mainScene.h"
|
||||||
#include "queryListItem.h"
|
#include "queryListItem.h"
|
||||||
|
#include "busModel.h"
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui
|
namespace Ui
|
||||||
|
@ -25,7 +26,7 @@ class CentralWidget : public QWidget
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit CentralWidget(QWidget *parent = nullptr);
|
explicit CentralWidget(QWidget *parent = nullptr, BusControllerModel *bus_controller = nullptr);
|
||||||
|
|
||||||
~CentralWidget() override;
|
~CentralWidget() override;
|
||||||
|
|
||||||
|
@ -37,8 +38,17 @@ public slots:
|
||||||
*/
|
*/
|
||||||
void AppendQueryItem(int query_type, int node_id);
|
void AppendQueryItem(int query_type, int node_id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理删除一个请求的槽函数
|
||||||
|
* @param query_id 请求的编号
|
||||||
|
*/
|
||||||
void DeleteQueryItem(int query_id);
|
void DeleteQueryItem(int query_id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理点击添加请求按钮的槽函数
|
||||||
|
*/
|
||||||
|
void AddQueryButtonClicked();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* UI控件
|
* UI控件
|
||||||
|
@ -50,11 +60,26 @@ private:
|
||||||
*/
|
*/
|
||||||
SceneManager *scene_manager;
|
SceneManager *scene_manager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公交车控制器
|
||||||
|
*/
|
||||||
|
BusControllerModel *controller;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求列表中的对象
|
* 请求列表中的对象
|
||||||
*/
|
*/
|
||||||
std::list<QueryListItem *> query_items;
|
std::list<QueryListItem *> query_items;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置同控制器的连接
|
||||||
|
*/
|
||||||
|
void SetControlConnection();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置同控件的连接
|
||||||
|
*/
|
||||||
|
void SetWidgetConnection();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化请求展示列表
|
* 初始化请求展示列表
|
||||||
* 展示表头的说明文字
|
* 展示表头的说明文字
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include "QFileDialog"
|
#include "QFileDialog"
|
||||||
|
#include "QThread"
|
||||||
|
|
||||||
#include "centralwidget.h"
|
#include "centralwidget.h"
|
||||||
#include "busModel.h"
|
#include "busModel.h"
|
||||||
|
@ -35,17 +36,63 @@ signals:
|
||||||
*/
|
*/
|
||||||
void OpenConfigFileSignal(QString file_name);
|
void OpenConfigFileSignal(QString file_name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始运行公交车的信号
|
||||||
|
*/
|
||||||
|
void RunBusSignal();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 暂停运行公交车的信号
|
||||||
|
*/
|
||||||
|
void PauseBusSignal();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止运行公交车的信号
|
||||||
|
*/
|
||||||
|
void StopBusSignal();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
/**
|
/**
|
||||||
* 点击打开配置文件按钮的槽函数
|
* 点击打开配置文件按钮的槽函数
|
||||||
*/
|
*/
|
||||||
void ReadConfigFileButtonClicked();
|
void ReadConfigFileButtonClicked();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理点击运行公交车按钮的槽函数
|
||||||
|
*/
|
||||||
|
void RunBusClicked();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理点击暂停公交车按钮的槽函数
|
||||||
|
*/
|
||||||
|
void PauseBusClicked();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理点击停止公交车按钮的槽函数
|
||||||
|
*/
|
||||||
|
void StopBusClicked();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* UI控件
|
||||||
|
*/
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
|
/**
|
||||||
|
* 中间的显示框架
|
||||||
|
*/
|
||||||
CentralWidget *central_widget;
|
CentralWidget *central_widget;
|
||||||
|
/**
|
||||||
|
* 控制类
|
||||||
|
*/
|
||||||
BusControllerModel *controller;
|
BusControllerModel *controller;
|
||||||
|
|
||||||
|
QThread *worker_thread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置菜单栏的相关连接
|
||||||
|
*/
|
||||||
void SetMenuBarConnection();
|
void SetMenuBarConnection();
|
||||||
|
|
||||||
|
void SetControlConnection();
|
||||||
};
|
};
|
||||||
#endif //AUTO_BUS_GUI_MAIN_WINDOW_H
|
#endif //AUTO_BUS_GUI_MAIN_WINDOW_H
|
||||||
|
|
|
@ -6,32 +6,59 @@
|
||||||
|
|
||||||
#include "header/moc_mainwindow.cpp"
|
#include "header/moc_mainwindow.cpp"
|
||||||
#include "form/ui_MainWindow.h"
|
#include "form/ui_MainWindow.h"
|
||||||
#include "mainwindow.h"
|
|
||||||
|
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
|
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
|
||||||
{
|
{
|
||||||
ui = new Ui::MainWindow;
|
ui = new Ui::MainWindow;
|
||||||
central_widget = new CentralWidget;
|
|
||||||
controller = new BusControllerModel;
|
controller = new BusControllerModel;
|
||||||
|
worker_thread = new QThread;
|
||||||
|
|
||||||
|
// 开始多线程
|
||||||
|
controller->moveToThread(worker_thread);
|
||||||
|
worker_thread->start();
|
||||||
|
|
||||||
|
central_widget = new CentralWidget(nullptr, controller);
|
||||||
|
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
this->setCentralWidget(central_widget);
|
this->setCentralWidget(central_widget);
|
||||||
SetMenuBarConnection();
|
SetMenuBarConnection();
|
||||||
|
SetControlConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
{
|
{
|
||||||
|
worker_thread->quit();
|
||||||
|
|
||||||
delete ui;
|
delete ui;
|
||||||
delete central_widget;
|
delete central_widget;
|
||||||
delete controller;
|
delete controller;
|
||||||
|
delete worker_thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::SetMenuBarConnection()
|
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()
|
void MainWindow::ReadConfigFileButtonClicked()
|
||||||
|
@ -53,3 +80,18 @@ void MainWindow::ReadConfigFileButtonClicked()
|
||||||
emit OpenConfigFileSignal(file_name);
|
emit OpenConfigFileSignal(file_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::RunBusClicked()
|
||||||
|
{
|
||||||
|
emit RunBusSignal();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::PauseBusClicked()
|
||||||
|
{
|
||||||
|
emit PauseBusSignal();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::StopBusClicked()
|
||||||
|
{
|
||||||
|
emit StopBusSignal();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user