diff --git a/include/busModel.h b/include/busModel.h index e619fb0..851c0a9 100644 --- a/include/busModel.h +++ b/include/busModel.h @@ -12,9 +12,14 @@ #include "cstdio" #include "string" #include "sstream" +#include "QObject" -class BusControllerModel +/** + * 控制公交车的类,继承了QObject + */ +class BusControllerModel : public QObject { + Q_OBJECT public: /** * 指向公交车所在站点的指针 @@ -32,7 +37,7 @@ public: int direction; /** - * 开始运行的时间 + * 公交车的计时时刻 */ int bus_time; @@ -65,6 +70,15 @@ public: std::string PrintState(); bool JudgeOnStation(); +public slots: + /** + * 处理打开配置文件信号的槽函数 + * @param file_name 配置文件名称 + */ + void ReadConfigFileSlot(const QString& file_name); + + + private: /** * 轨道的总长度 diff --git a/src/busControl.cpp b/src/busControl.cpp deleted file mode 100644 index 223f120..0000000 --- a/src/busControl.cpp +++ /dev/null @@ -1,284 +0,0 @@ -// -// Created by ricardo on 2022/6/17. -// -#include "busModel.h" - -int BusControllerModel::FCFSDirection() const -{ - bus_query_t *p = query_manager->queries; - - if(p == nullptr) - { - return BUS_STOP; - } //如果没有请求,公交车停止 - - else - { - int clockwise = 0; - int counterclockwise = 0; //用于顺,逆时针方向所经站台计数 - - /** - * 公交车当前所在位置 - */ - rail_node_t *now_position = rail_pos; - /** - * 公交车应该前进的方向 - */ - rail_node_t *target_position = p->node; - - rail_node_t *pos = now_position; - while (pos != target_position) //顺时针计数 - { - clockwise++; - pos = pos->next_node; - } - - pos = now_position; - while (pos != target_position) //逆时针计数 - { - counterclockwise++; - pos = pos->last_node; - } - - if(clockwise <= counterclockwise) - { - return BUS_CLOCK_WISE; - }//若顺时针距离短(或顺逆相等),公交车顺时针运行 - else - { - return BUS_COUNTER_CLOCK_WISE; - }//若逆时针距离短,公交车逆时针运行 - } -} - -bus_query_t *BusControllerModel::FCFSQuery() const -{ - bus_query_t *result = nullptr; - - if(query_manager->queries != nullptr) - { - if(rail_pos == query_manager->queries->node) - { - result = query_manager->queries; - } - } - - return result; -} - -bus_query_t *BusControllerModel::SSTFGetQuery() -{ - // 当前没有请求 - if(query_manager->queries == nullptr) - { - return nullptr; - } - int length = 9999; - bus_query_t *query = nullptr; - bus_query_t *p = query_manager->queries; - - // 遍历顺时针方向 - // 在两个方向路程相同时选择顺时针方向 - // 所以先遍历顺时针方向 - while (p != nullptr) - { - int temp = GetQueryDistance(p, BUS_CLOCK_WISE); - if(temp < length) - { - length = temp; - query = p; - } - p = p->next_node; - } - - // 遍历逆时针方向 - p = query_manager->queries; - while (p != nullptr) - { - int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); - if(temp < length) - { - length = temp; - query = p; - } - p = p->next_node; - } - - return query; -} - -int BusControllerModel::SSTFDirection(bus_query_t *query) -{ - if (query == nullptr) - { - return BUS_STOP; - } - - int length = GetQueryDistance(query, BUS_CLOCK_WISE); - if(length > total_distance / 2) - { - return BUS_COUNTER_CLOCK_WISE; - } - else if(length == 0) - { - return BUS_STOP; - } - else - { - return BUS_CLOCK_WISE; - } -} - -bus_query_t *BusControllerModel::SSTFBTWQuery() const -{ - bus_query_t *query = query_manager->queries; - bus_query_t *allow_query = nullptr; - rail_node_t *now_node = rail_pos; - - while (query != nullptr) - { - if(query->node == now_node) - { - // 这里是设计上的缺陷,在bus_time显示时间的前一秒,公交车就实际上到达站台了 - if(query->time < bus_time - 1) - { - if(query->type == direction || query->type == BUS_TARGET) - { - allow_query = query; - break; - } - } - } - query = query->next_node; - } - - return allow_query; -} - -bus_query_t *BusControllerModel::SCANGetQuery() -{ - // 当前没有请求 - if(query_manager->queries == nullptr) - { - return nullptr; - } - - if(direction == BUS_STOP) - { - // 在停止的状态下第一次开始选择方向 - int length = 9999; - bus_query_t *query = nullptr; - bus_query_t *p = query_manager->queries; - - // 遍历顺时针方向 - // 在两个方向路程相同时选择顺时针方向 - // 所以先遍历顺时针方向 - while (p != nullptr) - { - int temp = GetQueryDistance(p, BUS_CLOCK_WISE); - if(temp < length) - { - length = temp; - query = p; - } - p = p->next_node; - } - - // 遍历逆时针方向 - p = query_manager->queries; - while (p != nullptr) - { - int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); - if(temp < length) - { - length = temp; - query = p; - } - p = p->next_node; - } - - return query; - } - else - { - // 在已经有方向的情况下处理方向 - int length = 9999; - bus_query_t *query = nullptr; - bus_query_t *p = query_manager->queries; - - while (p != nullptr) - { - int temp = GetQueryDistance(p, direction); - if(temp < length) - { - query = p; - length = temp; - } - p = p->next_node; - } - - return query; - } -} - -int BusControllerModel::SCANDirection(bus_query_t *query) -{ - if(query == nullptr) - { - return BUS_STOP; - } - - if(direction == BUS_STOP) - { - int length = GetQueryDistance(query, BUS_CLOCK_WISE); - if(length > total_distance / 2) - { - return BUS_COUNTER_CLOCK_WISE; - } - else - { - return BUS_CLOCK_WISE; - } - } - else - { - int length = GetQueryDistance(query, direction); - if(length > total_distance / 2) - { - if(direction == BUS_CLOCK_WISE) - { - return BUS_COUNTER_CLOCK_WISE; - } - else - { - return BUS_CLOCK_WISE; - } - } - else - { - return direction; - } - } -} - -bus_query_t *BusControllerModel::SCANBTWQuery() const -{ - rail_node_t *now_position = rail_pos; - //获取公交车当前所在站点 - bus_query_t *p = query_manager->queries; - - while(p != nullptr) - { - if(p->node == now_position) - { - if(p->time < bus_time - 1) - { - return p; - } - } - p = p->next_node; - }//遍历请求链表,判断是否有可以顺便处理的请求 - - return nullptr; -} - diff --git a/src/busModel.cpp b/src/busModel.cpp index c43b763..e21c554 100644 --- a/src/busModel.cpp +++ b/src/busModel.cpp @@ -2,7 +2,7 @@ // Created by ricardo on 2022/6/10. // -#include "busModel.h" +#include "moc_busModel.cpp" BusControllerModel::BusControllerModel() { @@ -13,6 +13,9 @@ BusControllerModel::BusControllerModel() rail_pos = rail_manager->rails; distance = 0; direction = BUS_STOP; + + // 设置初始时间 + bus_time = 0; } BusControllerModel::~BusControllerModel() @@ -21,6 +24,11 @@ BusControllerModel::~BusControllerModel() delete(query_manager); } +void BusControllerModel::ReadConfigFileSlot(const QString& file_name) +{ + ReadConfigFile(file_name.toStdString()); +} + int BusControllerModel::GetBusPosition() const { int result = 0; @@ -291,3 +299,284 @@ void BusControllerModel::ReadConfigFile(const std::string& file_name) delete rail_manager; rail_manager = new RailsModel(node_space_length, total_station); } + +int BusControllerModel::FCFSDirection() const +{ + bus_query_t *p = query_manager->queries; + + if(p == nullptr) + { + return BUS_STOP; + } //如果没有请求,公交车停止 + + else + { + int clockwise = 0; + int counterclockwise = 0; //用于顺,逆时针方向所经站台计数 + + /** + * 公交车当前所在位置 + */ + rail_node_t *now_position = rail_pos; + /** + * 公交车应该前进的方向 + */ + rail_node_t *target_position = p->node; + + rail_node_t *pos = now_position; + while (pos != target_position) //顺时针计数 + { + clockwise++; + pos = pos->next_node; + } + + pos = now_position; + while (pos != target_position) //逆时针计数 + { + counterclockwise++; + pos = pos->last_node; + } + + if(clockwise <= counterclockwise) + { + return BUS_CLOCK_WISE; + }//若顺时针距离短(或顺逆相等),公交车顺时针运行 + else + { + return BUS_COUNTER_CLOCK_WISE; + }//若逆时针距离短,公交车逆时针运行 + } +} + +bus_query_t *BusControllerModel::FCFSQuery() const +{ + bus_query_t *result = nullptr; + + if(query_manager->queries != nullptr) + { + if(rail_pos == query_manager->queries->node) + { + result = query_manager->queries; + } + } + + return result; +} + +bus_query_t *BusControllerModel::SSTFGetQuery() +{ + // 当前没有请求 + if(query_manager->queries == nullptr) + { + return nullptr; + } + int length = 9999; + bus_query_t *query = nullptr; + bus_query_t *p = query_manager->queries; + + // 遍历顺时针方向 + // 在两个方向路程相同时选择顺时针方向 + // 所以先遍历顺时针方向 + while (p != nullptr) + { + int temp = GetQueryDistance(p, BUS_CLOCK_WISE); + if(temp < length) + { + length = temp; + query = p; + } + p = p->next_node; + } + + // 遍历逆时针方向 + p = query_manager->queries; + while (p != nullptr) + { + int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); + if(temp < length) + { + length = temp; + query = p; + } + p = p->next_node; + } + + return query; +} + +int BusControllerModel::SSTFDirection(bus_query_t *query) +{ + if (query == nullptr) + { + return BUS_STOP; + } + + int length = GetQueryDistance(query, BUS_CLOCK_WISE); + if(length > total_distance / 2) + { + return BUS_COUNTER_CLOCK_WISE; + } + else if(length == 0) + { + return BUS_STOP; + } + else + { + return BUS_CLOCK_WISE; + } +} + +bus_query_t *BusControllerModel::SSTFBTWQuery() const +{ + bus_query_t *query = query_manager->queries; + bus_query_t *allow_query = nullptr; + rail_node_t *now_node = rail_pos; + + while (query != nullptr) + { + if(query->node == now_node) + { + // 这里是设计上的缺陷,在bus_time显示时间的前一秒,公交车就实际上到达站台了 + if(query->time < bus_time - 1) + { + if(query->type == direction || query->type == BUS_TARGET) + { + allow_query = query; + break; + } + } + } + query = query->next_node; + } + + return allow_query; +} + +bus_query_t *BusControllerModel::SCANGetQuery() +{ + // 当前没有请求 + if(query_manager->queries == nullptr) + { + return nullptr; + } + + if(direction == BUS_STOP) + { + // 在停止的状态下第一次开始选择方向 + int length = 9999; + bus_query_t *query = nullptr; + bus_query_t *p = query_manager->queries; + + // 遍历顺时针方向 + // 在两个方向路程相同时选择顺时针方向 + // 所以先遍历顺时针方向 + while (p != nullptr) + { + int temp = GetQueryDistance(p, BUS_CLOCK_WISE); + if(temp < length) + { + length = temp; + query = p; + } + p = p->next_node; + } + + // 遍历逆时针方向 + p = query_manager->queries; + while (p != nullptr) + { + int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); + if(temp < length) + { + length = temp; + query = p; + } + p = p->next_node; + } + + return query; + } + else + { + // 在已经有方向的情况下处理方向 + int length = 9999; + bus_query_t *query = nullptr; + bus_query_t *p = query_manager->queries; + + while (p != nullptr) + { + int temp = GetQueryDistance(p, direction); + if(temp < length) + { + query = p; + length = temp; + } + p = p->next_node; + } + + return query; + } +} + +int BusControllerModel::SCANDirection(bus_query_t *query) +{ + if(query == nullptr) + { + return BUS_STOP; + } + + if(direction == BUS_STOP) + { + int length = GetQueryDistance(query, BUS_CLOCK_WISE); + if(length > total_distance / 2) + { + return BUS_COUNTER_CLOCK_WISE; + } + else + { + return BUS_CLOCK_WISE; + } + } + else + { + int length = GetQueryDistance(query, direction); + if(length > total_distance / 2) + { + if(direction == BUS_CLOCK_WISE) + { + return BUS_COUNTER_CLOCK_WISE; + } + else + { + return BUS_CLOCK_WISE; + } + } + else + { + return direction; + } + } +} + +bus_query_t *BusControllerModel::SCANBTWQuery() const +{ + rail_node_t *now_position = rail_pos; + //获取公交车当前所在站点 + bus_query_t *p = query_manager->queries; + + while(p != nullptr) + { + if(p->node == now_position) + { + if(p->time < bus_time - 1) + { + return p; + } + } + p = p->next_node; + }//遍历请求链表,判断是否有可以顺便处理的请求 + + return nullptr; +} + + diff --git a/src/header/mainwindow.h b/src/header/mainwindow.h index 6f76745..99b67e6 100644 --- a/src/header/mainwindow.h +++ b/src/header/mainwindow.h @@ -6,7 +6,10 @@ #define AUTO_BUS_GUI_MAIN_WINDOW_H #include +#include "QFileDialog" + #include "centralwidget.h" +#include "busModel.h" QT_BEGIN_NAMESPACE @@ -25,9 +28,23 @@ public: ~MainWindow() override; +signals: + /** + * 打开配置文件的信号 + * @param file_name 配置文件的文件路径 + */ + void OpenConfigFileSignal(QString file_name); + +public slots: + /** + * 点击打开配置文件按钮的槽函数 + */ + void ReadConfigFileButtonClicked(); + private: Ui::MainWindow *ui; CentralWidget *central_widget; + BusControllerModel *controller; void SetMenuBarConnection(); }; diff --git a/src/mainScene.cpp b/src/mainScene.cpp index f4b057a..bc4c852 100644 --- a/src/mainScene.cpp +++ b/src/mainScene.cpp @@ -35,7 +35,6 @@ SceneManager::SceneManager(int stop_node_number) stop_pos_pairs[j].AddLength(stop_space_length); } pixmap_items[i].setPos(stop_pos_pairs[i].pos_x, stop_pos_pairs[i].pos_y); - qDebug() << i << " " << stop_pos_pairs[i].pos_x << " " << stop_pos_pairs[i].pos_y; } // 画一个描边的矩形框 diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 15ec911..31d6003 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -6,12 +6,14 @@ #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; ui->setupUi(this); this->setCentralWidget(central_widget); @@ -23,9 +25,31 @@ MainWindow::~MainWindow() { delete ui; delete central_widget; + delete controller; } void MainWindow::SetMenuBarConnection() { QObject::connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close())); + QObject::connect(ui->actionRead_ConfigFile, &QAction::triggered, this, &MainWindow::ReadConfigFileButtonClicked); +} + +void MainWindow::ReadConfigFileButtonClicked() +{ + QString file_name = QFileDialog::getOpenFileName( + this, + "打开配置文件", + "C:/", + "Config Files(*.dic)" + ); + + if(file_name.isEmpty()) + { + qDebug() << "文件名为空"; + } + else + { + qDebug() << file_name; + emit OpenConfigFileSignal(file_name); + } }