多线程开始
This commit is contained in:
parent
4709b1ad7c
commit
48cd806197
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
|
||||
}
|
|
@ -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<QueryListItem *> query_items;
|
||||
|
||||
/**
|
||||
* 设置同控制器的连接
|
||||
*/
|
||||
void SetControlConnection();
|
||||
|
||||
/**
|
||||
* 设置同控件的连接
|
||||
*/
|
||||
void SetWidgetConnection();
|
||||
|
||||
/**
|
||||
* 初始化请求展示列表
|
||||
* 展示表头的说明文字
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <QMainWindow>
|
||||
#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
|
||||
|
|
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user