重新设计控制器同GUI之间的关系
重写了GetBusDirection函数
This commit is contained in:
		| @@ -8,7 +8,7 @@ | ||||
|  | ||||
| class BusFCFSStrategy : public BusStrategyBase | ||||
| { | ||||
|     int GetBusDirection(bus_query_t *query); | ||||
|     int GetBusDirection(); | ||||
|  | ||||
|     bus_query_t *GetTargetQuery(); | ||||
|  | ||||
|   | ||||
| @@ -9,7 +9,7 @@ | ||||
|  | ||||
| class BusSCANStrategy : public BusStrategyBase | ||||
| { | ||||
|     int GetBusDirection(bus_query_t *query); | ||||
|     int GetBusDirection(); | ||||
|  | ||||
|     bus_query_t *GetTargetQuery(); | ||||
|  | ||||
|   | ||||
| @@ -8,7 +8,7 @@ | ||||
|  | ||||
| class BusSSTFStrategy : public BusStrategyBase | ||||
| { | ||||
|     int GetBusDirection(bus_query_t *query); | ||||
|     int GetBusDirection(); | ||||
|  | ||||
|     bus_query_t *GetTargetQuery(); | ||||
|  | ||||
|   | ||||
| @@ -6,6 +6,7 @@ | ||||
| #define AUTO_BUS_GUI_BUS_CONTROLLER_BASE_H | ||||
| #include "QObject" | ||||
| #include "QString" | ||||
| #include "QTimer" | ||||
|  | ||||
| #include "railsModel.h" | ||||
| #include "queryModel.h" | ||||
| @@ -18,6 +19,8 @@ public: | ||||
|     RailsModel *rails_model; | ||||
|     QueryModel *query_model; | ||||
|     BusModel *bus_model; | ||||
|     QTimer *tick_timer; | ||||
|  | ||||
|  | ||||
|     int bus_tick; | ||||
|  | ||||
| @@ -25,12 +28,31 @@ public: | ||||
|  | ||||
|     virtual ~BusStrategyBase(); | ||||
|  | ||||
|     virtual int GetBusDirection(bus_query_t *query) = 0; | ||||
|     /** | ||||
|      * 获得当前公交车应该前进的方向 | ||||
|      * @return 公交车前进的方向 | ||||
|      */ | ||||
|     virtual int GetBusDirection() = 0; | ||||
|  | ||||
|     /** | ||||
|      * 获得公交车在当前指定的策略下应该处理的请求 | ||||
|      * @return 请求指针 | ||||
|      */ | ||||
|     virtual bus_query_t *GetTargetQuery() = 0; | ||||
|  | ||||
|     /** | ||||
|      * 获取公交车现在可以处理的请求 | ||||
|      * @return 请求指针 | ||||
|      */ | ||||
|     virtual bus_query_t *HandleQuery() = 0; | ||||
|  | ||||
|     signals: | ||||
|     void DeleteQuerySignal(bus_query_t *query); | ||||
|  | ||||
| public slots: | ||||
|     void AppendQuerySlot(int query_type, int node_id) const; | ||||
|  | ||||
|  | ||||
| private: | ||||
|     QString PrintState() const; | ||||
|  | ||||
|   | ||||
| @@ -9,9 +9,6 @@ | ||||
| #include "queryModel.h" | ||||
| #include "define.h" | ||||
|  | ||||
| #include "cstdio" | ||||
| #include "string" | ||||
| #include "sstream" | ||||
| #include "QObject" | ||||
| #include "QDebug" | ||||
| #include "QTimer" | ||||
| @@ -34,6 +31,11 @@ public: | ||||
|      */ | ||||
|     int direction; | ||||
|  | ||||
|     /** | ||||
|      * 当前指定处理的请求 | ||||
|      */ | ||||
|     bus_query_t *target_query; | ||||
|  | ||||
|     QTimer *bus_timer; | ||||
|  | ||||
|     explicit BusModel(); | ||||
| @@ -61,11 +63,6 @@ private: | ||||
|      * 轨道的头节点 | ||||
|      */ | ||||
|     rail_node_t *rail_head; | ||||
|  | ||||
|     /** | ||||
|      * 当前指定处理的请求 | ||||
|      */ | ||||
|     bus_query_t *target_query; | ||||
| }; | ||||
|  | ||||
| #endif //AUTO_BUS_GUI_BUS_MODEL_H | ||||
|   | ||||
| @@ -4,9 +4,9 @@ | ||||
|  | ||||
| #include "BusFCFSStrategy.h" | ||||
|  | ||||
| int BusFCFSStrategy::GetBusDirection(bus_query_t *query) | ||||
| int BusFCFSStrategy::GetBusDirection() | ||||
| { | ||||
|     bus_query_t *p = query; | ||||
|     bus_query_t *p = bus_model->target_query; | ||||
|  | ||||
|     if(p == NULL) | ||||
|     { | ||||
|   | ||||
| @@ -4,9 +4,10 @@ | ||||
|  | ||||
| #include "BusSCANStrategy.h" | ||||
|  | ||||
| int BusSCANStrategy::GetBusDirection(bus_query_t *query) | ||||
| int BusSCANStrategy::GetBusDirection() | ||||
| { | ||||
|     int orientation = bus_model->direction; | ||||
|     bus_query_t *query = bus_model->target_query; | ||||
|  | ||||
|     if(query == NULL) | ||||
|     { | ||||
|   | ||||
| @@ -4,8 +4,10 @@ | ||||
|  | ||||
| #include "BusSSTFStrategy.h" | ||||
|  | ||||
| int BusSSTFStrategy::GetBusDirection(bus_query_t *query) | ||||
| int BusSSTFStrategy::GetBusDirection() | ||||
| { | ||||
|     bus_query_t *query = bus_model->target_query; | ||||
|  | ||||
|     if (query == NULL) | ||||
|     { | ||||
|         return BUS_STOP; | ||||
|   | ||||
| @@ -9,6 +9,7 @@ BusStrategyBase::BusStrategyBase() | ||||
|     rails_model = new RailsModel; | ||||
|     query_model = new QueryModel; | ||||
|     bus_model = new BusModel(); | ||||
|     tick_timer = new QTimer; | ||||
|  | ||||
|     bus_tick = 0; | ||||
| } | ||||
| @@ -18,6 +19,13 @@ BusStrategyBase::~BusStrategyBase() | ||||
|     delete rails_model; | ||||
|     delete query_model; | ||||
|     delete bus_model; | ||||
|     delete tick_timer; | ||||
| } | ||||
|  | ||||
| void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) const | ||||
| { | ||||
|     rail_node_t *node = rails_model->FindNode(node_id); | ||||
|     query_model->CreateQuery(query_type, node); | ||||
| } | ||||
|  | ||||
| QString BusStrategyBase::PrintState() const | ||||
|   | ||||
| @@ -15,7 +15,6 @@ CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), ui(new Ui::Cent | ||||
|  | ||||
|     ui->main_canva->setScene(scene_manager->scene); | ||||
|  | ||||
|     SetControlConnection(); | ||||
|     SetWidgetConnection(); | ||||
|     SetupQueryList(); | ||||
| } | ||||
| @@ -28,9 +27,22 @@ CentralWidget::~CentralWidget() | ||||
|     delete ui; | ||||
| } | ||||
|  | ||||
| void CentralWidget::SetController(BusStrategyBase *c) | ||||
| { | ||||
|     controller = c; | ||||
|     SetControlConnection(); | ||||
|     SetRailsScene(controller->rails_model->node_num); | ||||
| } | ||||
|  | ||||
| void CentralWidget::SetControlConnection() | ||||
| { | ||||
|     // 设置添加请求事件的连接 | ||||
|     QObject::connect(this, &CentralWidget::AppendQuerySignal, | ||||
|                      controller, &BusStrategyBase::AppendQuerySlot); | ||||
|  | ||||
|     // 设置删除请求事件的连接 | ||||
|     QObject::connect(controller, &BusStrategyBase::DeleteQuerySignal, | ||||
|                      this, &CentralWidget::DeleteQueryItemSlot); | ||||
| } | ||||
|  | ||||
| void CentralWidget::SetWidgetConnection() | ||||
|   | ||||
| @@ -13,7 +13,7 @@ | ||||
|  | ||||
| #include "mainScene.h" | ||||
| #include "queryListItem.h" | ||||
| #include "busModel.h" | ||||
| #include "BusStrategyBase.h" | ||||
|  | ||||
| QT_BEGIN_NAMESPACE | ||||
| namespace Ui | ||||
| @@ -31,6 +31,12 @@ public: | ||||
|  | ||||
|     ~CentralWidget() override; | ||||
|  | ||||
|     /** | ||||
|      * 重新设置控制器 | ||||
|      * @param c 控制器指针 | ||||
|      */ | ||||
|     void SetController(BusStrategyBase *c); | ||||
|  | ||||
| signals: | ||||
|     /** | ||||
|      * 添加一个请求的信号 | ||||
| @@ -47,7 +53,7 @@ public slots: | ||||
|  | ||||
|     /** | ||||
|      * 处理删除一个请求的槽函数 | ||||
|      * @param query 请求的编号 | ||||
|      * @param query 请求指针 | ||||
|      */ | ||||
|     void DeleteQueryItemSlot(bus_query_t *query); | ||||
|  | ||||
| @@ -56,14 +62,6 @@ public slots: | ||||
|      */ | ||||
|     void AddQueryButtonClicked(); | ||||
|  | ||||
|     /** | ||||
|      * 处理轨道重新生成的槽函数 | ||||
|      * 重绘轨道画面 | ||||
|      * 重新设置站点选择下拉栏 | ||||
|      * @param node_num | ||||
|      */ | ||||
|     void SetRailsScene(int node_num); | ||||
|  | ||||
| private: | ||||
|     /** | ||||
|      * UI控件 | ||||
| @@ -75,6 +73,8 @@ private: | ||||
|      */ | ||||
|     SceneManager *scene_manager; | ||||
|  | ||||
|     BusStrategyBase *controller = nullptr; | ||||
|  | ||||
|     /** | ||||
|      * 请求列表中的对象 | ||||
|      */ | ||||
| @@ -105,6 +105,14 @@ private: | ||||
|      * 设置站点选择下拉栏 | ||||
|      */ | ||||
|     void SetRailsComboBox(int node_num); | ||||
|  | ||||
|     /** | ||||
|      * 处理轨道重新生成的槽函数 | ||||
|      * 重绘轨道画面 | ||||
|      * 重新设置站点选择下拉栏 | ||||
|      * @param node_num | ||||
|      */ | ||||
|     void SetRailsScene(int node_num); | ||||
| }; | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -11,7 +11,8 @@ | ||||
| #include "QMessageBox" | ||||
|  | ||||
| #include "centralwidget.h" | ||||
| #include "busModel.h" | ||||
| #include "BusStrategyBase.h" | ||||
| #include "StrategyFactory.h" | ||||
|  | ||||
|  | ||||
| QT_BEGIN_NAMESPACE | ||||
| @@ -31,11 +32,6 @@ public: | ||||
|     ~MainWindow() override; | ||||
|  | ||||
| signals: | ||||
|     /** | ||||
|      * 打开配置文件的信号 | ||||
|      * @param file_name 配置文件的文件路径 | ||||
|      */ | ||||
|     void OpenConfigFileSignal(QString file_name); | ||||
|  | ||||
|     /** | ||||
|      * 开始运行公交车的信号 | ||||
| @@ -85,11 +81,18 @@ private: | ||||
|  | ||||
|     QThread *worker_thread; | ||||
|  | ||||
|     BusStrategyBase *controller; | ||||
|  | ||||
|     /** | ||||
|      * 设置菜单栏的相关连接 | ||||
|      */ | ||||
|     void SetMenuBarConnection(); | ||||
|  | ||||
|     /** | ||||
|      * 设置控制器的相关连接 | ||||
|      */ | ||||
|     void SetControlConnection(); | ||||
|  | ||||
|     void BeginThread(); | ||||
| }; | ||||
| #endif //AUTO_BUS_GUI_MAIN_WINDOW_H | ||||
|   | ||||
| @@ -12,20 +12,22 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) | ||||
| { | ||||
|     ui = new Ui::MainWindow; | ||||
|     worker_thread = new QThread; | ||||
|  | ||||
|     // 开始多线程 | ||||
|     worker_thread->start(); | ||||
|     controller = nullptr; | ||||
|  | ||||
|     central_widget = new CentralWidget(nullptr); | ||||
|  | ||||
|     ui->setupUi(this); | ||||
|     this->setCentralWidget(central_widget); | ||||
|  | ||||
|     SetMenuBarConnection(); | ||||
|     SetControlConnection(); | ||||
|  | ||||
|     //开始多线程事件循环 | ||||
|     worker_thread->start(); | ||||
| } | ||||
|  | ||||
| MainWindow::~MainWindow() | ||||
| { | ||||
|     controller->deleteLater(); | ||||
|     worker_thread->quit(); | ||||
|  | ||||
|     delete ui; | ||||
| @@ -68,12 +70,20 @@ void MainWindow::ReadConfigFileButtonClicked() | ||||
|  | ||||
|     if(file_name.isEmpty()) | ||||
|     { | ||||
|         QMessageBox::warning(this, "警告", "文件名错误"); | ||||
|         QMessageBox::warning(this, "警告", "文件错误"); | ||||
|         return; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         emit OpenConfigFileSignal(file_name); | ||||
|         if(controller != nullptr) | ||||
|         { | ||||
|             controller->deleteLater(); | ||||
|         } | ||||
|  | ||||
|         controller = StrategyFactory::GetStrategy(file_name); | ||||
|         BeginThread(); | ||||
|         SetControlConnection(); | ||||
|         central_widget->SetController(controller); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -90,4 +100,12 @@ void MainWindow::PauseBusClicked() | ||||
| void MainWindow::StopBusClicked() | ||||
| { | ||||
|     emit StopBusSignal(); | ||||
| } | ||||
|  | ||||
| void MainWindow::BeginThread() | ||||
| { | ||||
|     if(controller != nullptr) | ||||
|     { | ||||
|         controller->moveToThread(worker_thread); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user