From ccf82dd0f5d115e648293af209c4246cba25c611 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 28 Jun 2022 21:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E5=B9=BD=E7=81=B5=E8=BD=A6=E7=BB=88?= =?UTF-8?q?=E4=BA=8E=E8=B7=91=E8=B5=B7=E6=9D=A5=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/BusFCFSStrategy.h | 2 +- include/BusSCANStrategy.h | 2 +- include/BusSSTFStrategy.h | 2 +- include/BusStrategyBase.h | 30 +++++++++++-- src/BusFCFSStrategy.cpp | 17 ++------ src/BusSCANStrategy.cpp | 2 +- src/BusSSTFStrategy.cpp | 2 +- src/BusStrategyBase.cpp | 89 +++++++++++++++++++++++++++++++++++++- src/centralwidget.cpp | 24 +++++++--- src/header/centralwidget.h | 4 +- src/header/mainwindow.h | 2 + src/mainwindow.cpp | 14 ++++++ 12 files changed, 160 insertions(+), 30 deletions(-) diff --git a/include/BusFCFSStrategy.h b/include/BusFCFSStrategy.h index 1e5e657..cc1070e 100644 --- a/include/BusFCFSStrategy.h +++ b/include/BusFCFSStrategy.h @@ -12,7 +12,7 @@ class BusFCFSStrategy : public BusStrategyBase bus_query_t *GetTargetQuery(); - bus_query_t *HandleQuery(); + bus_query_t *HandleBTWQuery(); }; diff --git a/include/BusSCANStrategy.h b/include/BusSCANStrategy.h index c5f72d5..7a48cba 100644 --- a/include/BusSCANStrategy.h +++ b/include/BusSCANStrategy.h @@ -13,7 +13,7 @@ class BusSCANStrategy : public BusStrategyBase bus_query_t *GetTargetQuery(); - bus_query_t *HandleQuery(); + bus_query_t *HandleBTWQuery(); }; diff --git a/include/BusSSTFStrategy.h b/include/BusSSTFStrategy.h index a5bf298..35d8c41 100644 --- a/include/BusSSTFStrategy.h +++ b/include/BusSSTFStrategy.h @@ -12,7 +12,7 @@ class BusSSTFStrategy : public BusStrategyBase bus_query_t *GetTargetQuery(); - bus_query_t *HandleQuery(); + bus_query_t *HandleBTWQuery(); }; diff --git a/include/BusStrategyBase.h b/include/BusStrategyBase.h index 0f1c347..c375947 100644 --- a/include/BusStrategyBase.h +++ b/include/BusStrategyBase.h @@ -40,17 +40,17 @@ public: virtual bus_query_t *GetTargetQuery() = 0; /** - * 获取公交车现在可以处理的请求 + * 获取公交车现在可以顺便处理的请求 * @return 请求指针 */ - virtual bus_query_t *HandleQuery() = 0; + virtual bus_query_t *HandleBTWQuery() = 0; signals: /** * 删除请求信号 * @param query 需要删除请求的指针 */ - void DeleteQuerySignal(bus_query_t *query); + void DeleteQuerySignal(int query_type, int node_id); /** * 打印状态信号 @@ -58,8 +58,25 @@ public: */ void PrintStateSignal(QString string); + /** + * 获得公交车前进方向的信号 + */ + void GetBusDirectionSignal(); + + /** + * 运行公交车的信号 + * @param direction 公交车前进的方向 + * @param duration 前进需要的时间 + */ + void BusRunningSignal(int direction, int duration); + public slots: - void AppendQuerySlot(int query_type, int node_id) const; + /** + * 添加请求的槽函数 + * @param query_type 请求的类别 + * @param node_id 请求的站点ID + */ + void AppendQuerySlot(int query_type, int node_id); /** * 处理开始事件的槽函数 @@ -76,6 +93,10 @@ public slots: */ void OneTickSlot(int remaining_time); + void GetBusDirectionSlot(); + + void OnStopSlot(); + private: /** * 储存当前的状态 @@ -91,6 +112,7 @@ private: void SetConnection() const; + void HandleQuery(); }; diff --git a/src/BusFCFSStrategy.cpp b/src/BusFCFSStrategy.cpp index c81cf90..3d8493a 100644 --- a/src/BusFCFSStrategy.cpp +++ b/src/BusFCFSStrategy.cpp @@ -55,21 +55,12 @@ int BusFCFSStrategy::GetBusDirection() bus_query_t *BusFCFSStrategy::GetTargetQuery() { + // FCFS只处理第一个请求 return query_model->queries; } -bus_query_t *BusFCFSStrategy::HandleQuery() +bus_query_t *BusFCFSStrategy::HandleBTWQuery() { - bus_query_t *result = NULL; - bus_query_t *query = query_model->queries; - - if(query != nullptr) - { - if(bus_model->rail_pos == query->node) - { - result = query; - } - } - - return result; + // FCFS不进行顺便处理 + return nullptr; } diff --git a/src/BusSCANStrategy.cpp b/src/BusSCANStrategy.cpp index ba63e2e..036e25c 100644 --- a/src/BusSCANStrategy.cpp +++ b/src/BusSCANStrategy.cpp @@ -116,7 +116,7 @@ bus_query_t *BusSCANStrategy::GetTargetQuery() } } -bus_query_t *BusSCANStrategy::HandleQuery() +bus_query_t *BusSCANStrategy::HandleBTWQuery() { //获取公交车当前所在站点 rail_node_t *now_position = bus_model->rail_pos; diff --git a/src/BusSSTFStrategy.cpp b/src/BusSSTFStrategy.cpp index 3d87644..0d56a80 100644 --- a/src/BusSSTFStrategy.cpp +++ b/src/BusSSTFStrategy.cpp @@ -28,7 +28,7 @@ int BusSSTFStrategy::GetBusDirection() } } -bus_query_t *BusSSTFStrategy::HandleQuery() +bus_query_t *BusSSTFStrategy::HandleBTWQuery() { // 这里只是处理顺便服务的代码 diff --git a/src/BusStrategyBase.cpp b/src/BusStrategyBase.cpp index 3d21d54..e6c4241 100644 --- a/src/BusStrategyBase.cpp +++ b/src/BusStrategyBase.cpp @@ -11,6 +11,8 @@ BusStrategyBase::BusStrategyBase() bus_model = new BusModel; bus_tick = 0; + + SetConnection(); } BusStrategyBase::~BusStrategyBase() @@ -22,13 +24,20 @@ BusStrategyBase::~BusStrategyBase() void BusStrategyBase::SetConnection() const { - + QObject::connect(this, &BusStrategyBase::GetBusDirectionSignal, + this, &BusStrategyBase::GetBusDirectionSlot); } -void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) const +void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) { rail_node_t *node = rails_model->FindNode(node_id); query_model->CreateQuery(query_type, node); + + // 如果公交车停车且在系统在运行 + if(bus_model->direction == BUS_STOP && status == BUS_RUNNING) + { + emit GetBusDirectionSignal(); + } } void BusStrategyBase::OneTickSlot(int remaining_time) @@ -48,6 +57,8 @@ void BusStrategyBase::BusBeginSlot() // 在一开始先打印一下状态 QString str = PrintState(0); emit PrintStateSignal(str); + + emit GetBusDirectionSignal(); } void BusStrategyBase::BusEndSlot() @@ -58,6 +69,80 @@ void BusStrategyBase::BusEndSlot() bus_model->ResetBus(rails_model->rails); } +void BusStrategyBase::GetBusDirectionSlot() +{ + bus_model->target_query = GetTargetQuery(); + bus_model->direction = GetBusDirection(); + + // 如果没有目标请求,跳出循环 + if(bus_model->target_query == nullptr) + { + return; + } + + int duration = 0; + + switch (bus_model->direction) + { + case BUS_CLOCK_WISE: + duration = bus_model->rail_pos->next_node_distance / bus_model->velocity * 1000; + break; + case BUS_COUNTER_CLOCK_WISE: + duration = bus_model->rail_pos->last_node_distance / bus_model->velocity * 1000; + break; + case BUS_STOP: + duration = 1000; + break; + } + + emit BusRunningSignal(bus_model->direction, duration); +} + +void BusStrategyBase::HandleQuery() +{ + if(bus_model->target_query != nullptr) + { + if(bus_model->target_query->node == bus_model->rail_pos) + { + // 如果已经到站 + while (bus_model->target_query != nullptr and + bus_model->target_query->node == bus_model->rail_pos) + { + emit DeleteQuerySignal(bus_model->target_query->type, bus_model->target_query->node->id); + query_model->DeleteQuery(bus_model->target_query); + bus_model->target_query = GetTargetQuery(); + } + } + else + { + // 没有到站就进行顺便处理 + bus_query_t *query = HandleBTWQuery(); + + while(query != nullptr) + { + emit DeleteQuerySignal(query->type, query->node->id); + query_model->DeleteQuery(query); + query = HandleBTWQuery(); + } + } + } +} + +void BusStrategyBase::OnStopSlot() +{ + if(bus_model->direction == BUS_CLOCK_WISE) + { + bus_model->rail_pos = bus_model->rail_pos->next_node; + } + else if(bus_model->direction == BUS_COUNTER_CLOCK_WISE) + { + bus_model->rail_pos = bus_model->rail_pos->last_node; + } + HandleQuery(); + + emit GetBusDirectionSignal(); +} + QString BusStrategyBase::PrintState(int remaining_time) const { int count = 0; diff --git a/src/centralwidget.cpp b/src/centralwidget.cpp index 3c03e60..1808748 100644 --- a/src/centralwidget.cpp +++ b/src/centralwidget.cpp @@ -44,8 +44,13 @@ void CentralWidget::SetControlConnection() QObject::connect(controller, &BusStrategyBase::DeleteQuerySignal, this, &CentralWidget::DeleteQueryItemSlot); + // 设置打印状态事件的连接 QObject::connect(controller, &BusStrategyBase::PrintStateSignal, this, &CentralWidget::PrintStateSlot); + + QObject::connect(controller, &BusStrategyBase::BusRunningSignal, + this, &CentralWidget::BeginBusAnimationSlot); + } void CentralWidget::SetWidgetConnection() @@ -94,21 +99,25 @@ void CentralWidget::AppendQueryItemSlot(int query_type, int node_id) ui->query_list->setItemWidget(widget_item, item); } -void CentralWidget::DeleteQueryItemSlot(bus_query_t *query) +void CentralWidget::DeleteQueryItemSlot(int query_type, int node_id) { // 由于请求列表头的存在,编号从1开始 int query_id = 1; - for(auto itor = query_items.begin(); itor != query_items.end(); ++itor) + // 先排除表头的影响 + auto first_item = query_items.begin(); + first_item++; + + for(auto itor = first_item; itor != query_items.end(); ++itor) { - if((*itor)->query_type == query->type and (*itor)->target_node_id == query->node->id) + if((*itor)->query_type == query_type and (*itor)->target_node_id == node_id) { break; } query_id++; } - QListWidgetItem *deleted_item = ui->query_list->takeItem(query_id); + QListWidgetItem *deleted_item = ui->query_list->takeItem(query_id); auto pos = query_items.begin(); @@ -118,8 +127,8 @@ void CentralWidget::DeleteQueryItemSlot(bus_query_t *query) } delete deleted_item; - query_items.erase(pos); delete *pos; + query_items.erase(pos); } void CentralWidget::PrintStateSlot(const QString& string) @@ -132,6 +141,11 @@ void CentralWidget::PrintStateSlot(const QString& string) ui->text_output->setTextCursor(cursor); } +void CentralWidget::BeginBusAnimationSlot(int direction, int duration) +{ + scene_manager->BeginBusAnimation(direction, duration); +} + void CentralWidget::AddQueryButtonClicked() { int query_type = ui->query_type_combo->currentIndex(); diff --git a/src/header/centralwidget.h b/src/header/centralwidget.h index 3b15801..3a5350c 100644 --- a/src/header/centralwidget.h +++ b/src/header/centralwidget.h @@ -56,7 +56,7 @@ public slots: * 处理删除一个请求的槽函数 * @param query 请求指针 */ - void DeleteQueryItemSlot(bus_query_t *query); + void DeleteQueryItemSlot(int query_type, int node_id); /** * 处理点击添加请求按钮的槽函数 @@ -69,6 +69,8 @@ public slots: */ void PrintStateSlot(const QString& string); + void BeginBusAnimationSlot(int direction, int duration); + private: /** * UI控件 diff --git a/src/header/mainwindow.h b/src/header/mainwindow.h index 7a4f9de..5078487 100644 --- a/src/header/mainwindow.h +++ b/src/header/mainwindow.h @@ -79,6 +79,8 @@ public slots: */ void OneTickSlot(); + void BeginBusTimerSlot(int direction, int duration); + private: /** * 设置一时刻的长度,单位毫秒 diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a880831..91dc6fa 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -25,6 +25,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) //开始多线程事件循环 worker_thread->start(); + + // 设置计时器只工作一次 + bus_timer->setSingleShot(true); } MainWindow::~MainWindow() @@ -80,6 +83,12 @@ void MainWindow::SetControlConnection() // 每一tick连接 QObject::connect(this, &MainWindow::BusTickSignal, controller, &BusStrategyBase::OneTickSlot); + + QObject::connect(bus_timer, &QTimer::timeout, + controller, &BusStrategyBase::OnStopSlot); + + QObject::connect(controller, &BusStrategyBase::BusRunningSignal, + this, &MainWindow::BeginBusTimerSlot); } void MainWindow::ReadConfigFileButtonClicked() @@ -149,4 +158,9 @@ void MainWindow::OneTickSlot() void MainWindow::EndTickTimerSlot() { tick_timer->stop(); +} + +void MainWindow::BeginBusTimerSlot(int direction, int duration) +{ + bus_timer->start(duration); } \ No newline at end of file