diff --git a/README.md b/README.md index 7df5be9..42061bc 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Auto Bus GUI -北京邮电大学计算机学院2021级《计算导论与程序设计》实践大作业的GUI分支。 +北京邮电大学计算机学院2021级《计算导论与程序设计》实践大作业“公交车调度”的GUI分支。 -## 项目构建 +## 构建 ### 构建环境 @@ -26,5 +26,24 @@ cmake .. -G "Visual Studio 17 2022" > > 现代VS已经支持用`cmake`管理`C/C++`项目 +## 使用 +![主界面截图](md_pic/main.png) +### 主界面 + +主界面由四个部分组成,分别是最上方的菜单栏,左侧的动画区,右侧的控制面板,下方的日志输出。 + +### 使用 + +首先使用`File-Read ConfigFile`读取一个配置文件,在读取结束后动画区会显示公交车与公交站台,在控制面板的上方会显示当前选择的策略种类。 + +使用`Run-Run Bus`和`Run-Stop Bus`控制公交的启动和停止。在开始运行之后,在日志输出区会打印当前的状态。 + +在控制面板的下方可以查看当前存在的请求与添加请求。 + +![运行时截图](md_pic/running.png) + +### 策略上的补充说明 + +由于GUI的性质,所有的请求都是立即调度的,不同于OJ版的等待一个clock结束之后在进行调度。 diff --git a/include/BusStrategyBase.h b/include/BusStrategyBase.h index df3fa78..2057e2f 100644 --- a/include/BusStrategyBase.h +++ b/include/BusStrategyBase.h @@ -16,11 +16,29 @@ class BusStrategyBase : public QObject { Q_OBJECT public: + /** + * 轨道模型 + */ RailsModel *rails_model; + + /** + * 请求模型 + */ QueryModel *query_model; + + /** + * 公交车模型 + */ BusModel *bus_model; + /** + * 当前的计时时刻 + */ int bus_tick; + + /** + * 当前的处理策略 + */ int strategy; BusStrategyBase(); @@ -94,8 +112,14 @@ public slots: */ void OneTickSlot(int remaining_time); + /** + * 获得公交车前进方向的槽函数 + */ void GetBusDirectionSlot(); + /** + * 处理到站事件的槽函数 + */ void OnStopSlot(); private: @@ -110,9 +134,14 @@ private: */ QString PrintState(int remaining_time) const; - + /** + * 设置各种连接 + */ void SetConnection() const; + /** + * 处理请求 + */ void HandleQuery(); }; diff --git a/include/busModel.h b/include/busModel.h index a44c93f..73e42ec 100644 --- a/include/busModel.h +++ b/include/busModel.h @@ -19,6 +19,9 @@ class BusModel { public: + /** + * 公交车当前所在的站点 + */ rail_node_t *rail_pos; /** @@ -46,6 +49,11 @@ public: */ void ResetBus(rail_node_t *head); + /** + * 获得公交车当前所在位置 + * @param remaining_time 行驶计时器剩下的时间 + * @return 到起点的距离 + */ double GetBusPosition(int remaining_time); /** diff --git a/md_pic/main.png b/md_pic/main.png new file mode 100644 index 0000000..d209e3f Binary files /dev/null and b/md_pic/main.png differ diff --git a/md_pic/running.png b/md_pic/running.png new file mode 100644 index 0000000..8eb6439 Binary files /dev/null and b/md_pic/running.png differ diff --git a/src/busModel.cpp b/src/busModel.cpp index ce8e109..bcb8e6d 100644 --- a/src/busModel.cpp +++ b/src/busModel.cpp @@ -29,6 +29,7 @@ void BusModel::ResetBus(rail_node_t *head) double BusModel::GetBusPosition(int remaining_time) { double result = 0; + double length = 0; rail_node_t *now_pos = rail_pos; rail_node_t *node = rail_head; @@ -39,23 +40,43 @@ double BusModel::GetBusPosition(int remaining_time) node = node->next_node; } while (node != now_pos); - // 如果就在起始点,距离为0 - if(now_pos == rail_head) - { - result = 0; - } - + // 获得可能存在的偏移量 if(remaining_time > 0) { if(direction == BUS_CLOCK_WISE) { - double length = now_pos->next_node_distance - (double)remaining_time / 1000.0 * velocity; - result = result + length; + length = now_pos->next_node_distance - (double)remaining_time / 1000.0 * velocity; } else if(direction == BUS_COUNTER_CLOCK_WISE) { - double length = now_pos->last_node_distance - (double)remaining_time / 1000.0 * velocity; - result = result - length; + length = now_pos->last_node_distance - (double)remaining_time / 1000.0 * velocity; + length = -length; + } + } + + if(now_pos == rail_head) + { + // 在起点 + if(length >= 0) + { + result = length; + } + else + { + result = result + length; + } + } + else + { + // 在其他点 + if(now_pos == rail_head->last_node and length == -rail_head->last_node_distance) + { + // 恰好即将回到出发点的情况 + result = 0; + } + else + { + result = result + length; } } diff --git a/src/centralwidget.cpp b/src/centralwidget.cpp index 08a5fe2..87976cb 100644 --- a/src/centralwidget.cpp +++ b/src/centralwidget.cpp @@ -139,6 +139,26 @@ void CentralWidget::DeleteQueryList() void CentralWidget::AppendQueryItemSlot(int query_type, int node_id) { + // 判断是否存在相同的请求 + // 如果存在就不再添加 + bool is_exist = false; + 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 == node_id) + { + is_exist = true; + break; + } + } + + if(is_exist) + { + return; + } + QueryListItem *item = new QueryListItem(query_type, node_id); query_items.push_back(item);