diff --git a/include/mainScene.h b/include/mainScene.h index c5d3be6..91dff7f 100644 --- a/include/mainScene.h +++ b/include/mainScene.h @@ -45,9 +45,15 @@ public: * 构造函数,同时生成站点 * @param stop_node_number 站点的数量 */ - explicit SceneManager(int stop_node_number); + explicit SceneManager(); ~SceneManager(); + /** + * 生成站点画面 + * @param node_number + */ + void SetStopScene(int node_number); + private: /** * 显示站点的像素图对象 @@ -58,7 +64,21 @@ private: */ PosPair *stop_pos_pairs; + /** + * 总共的站点数量 + */ + int stop_node_number; + + /** + * 一个矩形的边框 + * 使无论中间存在多少站点,这个scene都能在view的中心 + */ QGraphicsRectItem *rect_item; + + /** + * 清除画面 + */ + void ClearScene(); }; #endif //AUTO_BUS_GUI_MAIN_SCENE_H diff --git a/src/busModel.cpp b/src/busModel.cpp index 279620b..fd14875 100644 --- a/src/busModel.cpp +++ b/src/busModel.cpp @@ -304,11 +304,11 @@ 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; rail_manager->CreateRails(node_space_length, total_station); + // 发射信号说明创建完毕 emit RailsCreated(total_station); } diff --git a/src/centralwidget.cpp b/src/centralwidget.cpp index e214b5f..3c35b9c 100644 --- a/src/centralwidget.cpp +++ b/src/centralwidget.cpp @@ -11,7 +11,7 @@ CentralWidget::CentralWidget(QWidget *parent, BusControllerModel *bus_controller) : QWidget(parent), ui(new Ui::CentralWidget) { ui->setupUi(this); - scene_manager = new SceneManager(20); + scene_manager = new SceneManager; ui->main_canva->setScene(scene_manager->scene); @@ -32,7 +32,9 @@ CentralWidget::~CentralWidget() void CentralWidget::SetControlConnection() { - + // 处理轨道重新设置事件 + QObject::connect(controller, &BusControllerModel::RailsCreated, + this, &CentralWidget::SetRailsScene); } void CentralWidget::SetWidgetConnection() @@ -96,4 +98,21 @@ void CentralWidget::DeleteQueryItem(int query_id) void CentralWidget::AddQueryButtonClicked() { +} + +void CentralWidget::SetRailsScene(int node_num) +{ + scene_manager->SetStopScene(node_num); + SetRailsComboBox(node_num); +} + +void CentralWidget::SetRailsComboBox(int node_num) +{ + ui->query_node_combo->clear(); + + for(int i = 1; i <= node_num; i++) + { + QString node_str = QString::asprintf("第%d站", i); + ui->query_node_combo->addItem(node_str); + } } \ No newline at end of file diff --git a/src/header/centralwidget.h b/src/header/centralwidget.h index e16c753..8ad11b6 100644 --- a/src/header/centralwidget.h +++ b/src/header/centralwidget.h @@ -49,6 +49,14 @@ public slots: */ void AddQueryButtonClicked(); + /** + * 处理轨道重新生成的槽函数 + * 重绘轨道画面 + * 重新设置站点选择下拉栏 + * @param node_num + */ + void SetRailsScene(int node_num); + private: /** * UI控件 @@ -90,6 +98,11 @@ private: * 清除展示对象占用的内存空间 */ void DeleteQueryList(); + + /** + * 设置站点选择下拉栏 + */ + void SetRailsComboBox(int node_num); }; diff --git a/src/mainScene.cpp b/src/mainScene.cpp index bc4c852..1dc2a45 100644 --- a/src/mainScene.cpp +++ b/src/mainScene.cpp @@ -4,12 +4,33 @@ #include "mainScene.h" -SceneManager::SceneManager(int stop_node_number) +SceneManager::SceneManager() { scene = new QGraphicsScene; + stop_node_number = 0; + pixmap_items = nullptr; + stop_pos_pairs = nullptr; + rect_item = new QGraphicsRectItem; + + // 画一个描边的矩形框 + rect_item->setRect(0, 0, 595, 395); + scene->addItem(rect_item); +} + +SceneManager::~SceneManager() +{ + ClearScene(); + delete scene; +} + +void SceneManager::SetStopScene(int node_number) +{ + // 先清除以下屏幕 + ClearScene(); + + stop_node_number = node_number; pixmap_items = new QGraphicsPixmapItem[stop_node_number]; stop_pos_pairs = new PosPair[stop_node_number]; - rect_item = new QGraphicsRectItem; int stop_space_length = stop_pos_pairs->GetStopSpaceLength(stop_node_number); double stop_scale = 0.15; @@ -36,17 +57,18 @@ SceneManager::SceneManager(int stop_node_number) } pixmap_items[i].setPos(stop_pos_pairs[i].pos_x, stop_pos_pairs[i].pos_y); } - - // 画一个描边的矩形框 - rect_item->setRect(0, 0, 595, 395); - scene->addItem(rect_item); } -SceneManager::~SceneManager() +void SceneManager::ClearScene() { - delete []stop_pos_pairs; + // 从画布中移除所有的站点图片 + for(int i = 0; i < stop_node_number; i++) + { + scene->removeItem(&pixmap_items[i]); + } + delete []pixmap_items; - delete scene; + delete []stop_pos_pairs; } PosPair::PosPair() @@ -85,7 +107,7 @@ void PosPair::AddLength(int length) // 站点在右轨道 pos_x = stop_begin_x + stop_rail_width; - pos_y = stop_begin_y + stop_rail_width + stop_rail_height - distance; + pos_y = stop_begin_y + distance - stop_rail_width; } else { diff --git a/src/railsModel.cpp b/src/railsModel.cpp index 283e002..12f9a30 100644 --- a/src/railsModel.cpp +++ b/src/railsModel.cpp @@ -12,7 +12,10 @@ RailsModel::RailsModel() RailsModel::~RailsModel() { - FreeRails(); + if(rails != nullptr) + { + FreeRails(); + } } rail_node_t *RailsModel::FindNode(int node_num) const