diff --git a/include/mainScene.h b/include/mainScene.h index da14b27..c5d3be6 100644 --- a/include/mainScene.h +++ b/include/mainScene.h @@ -7,28 +7,58 @@ #include "QGraphicsScene" #include "QGraphicsPixmapItem" -#include "railsModel.h" + +class PosPair{ + +public: + int pos_x; + int pos_y; + + PosPair(); + /** + * 获取两个站点之间的距离 + * @param stop_number 站点的数量 + * @return 站点之间的距离 + */ + int GetStopSpaceLength(int stop_number) const; + /** + * 加上一定的距离 + * @param length 需要加上的距离 + */ + void AddLength(int length); + +private: + const int stop_begin_x = 100; + const int stop_begin_y = 80; + const int stop_rail_width = 300; + const int stop_rail_height = 200; + + int distance = 0; +}; class SceneManager { public: QGraphicsScene *scene; + /** + * 构造函数,同时生成站点 + * @param stop_node_number 站点的数量 + */ + explicit SceneManager(int stop_node_number); ~SceneManager(); - /** - * 初始化绘制场景 - */ - void init(); - - /** - * 初始化场景中的站点 - * @param rail_head 轨道的头节点 - */ - void initBusStop(rail_node_t *rail_head); - private: + /** + * 显示站点的像素图对象 + */ QGraphicsPixmapItem *pixmap_items; + /** + * 每个站点的所在位置 + */ + PosPair *stop_pos_pairs; + + QGraphicsRectItem *rect_item; }; #endif //AUTO_BUS_GUI_MAIN_SCENE_H diff --git a/src/centralwidget.cpp b/src/centralwidget.cpp index 0e39f07..a824f74 100644 --- a/src/centralwidget.cpp +++ b/src/centralwidget.cpp @@ -12,16 +12,13 @@ CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CentralWidget) { ui->setupUi(this); - - scene_manager = new SceneManager; - scene_manager->init(); - scene_manager->initBusStop(nullptr); + scene_manager = new SceneManager(20); ui->main_canva->setScene(scene_manager->scene); } CentralWidget::~CentralWidget() { - delete ui; delete scene_manager; + delete ui; } diff --git a/src/mainScene.cpp b/src/mainScene.cpp index 997cd04..f4b057a 100644 --- a/src/mainScene.cpp +++ b/src/mainScene.cpp @@ -4,23 +4,95 @@ #include "mainScene.h" -void SceneManager::init() +SceneManager::SceneManager(int stop_node_number) { scene = new QGraphicsScene; -} + pixmap_items = new QGraphicsPixmapItem[stop_node_number]; + stop_pos_pairs = new PosPair[stop_node_number]; + rect_item = new QGraphicsRectItem; -void SceneManager::initBusStop(rail_node_t *rail_head) -{ - pixmap_items = new QGraphicsPixmapItem[1]; + int stop_space_length = stop_pos_pairs->GetStopSpaceLength(stop_node_number); + double stop_scale = 0.15; + QTransform stop_transform; - pixmap_items->setPixmap(QPixmap(":/picture/bus.png")); - pixmap_items->setPos(100,100); + stop_transform.scale(stop_scale, stop_scale); - scene->addItem(pixmap_items); + // 为站点对象设置 + // 并将其添加到画图中 + for(size_t i = 0; i < stop_node_number; i++) + { + pixmap_items[i].setPixmap(QPixmap(":/picture/stop.png")); + pixmap_items[i].setTransform(stop_transform); + scene->addItem(&pixmap_items[i]); + } + + // 初始化站点的位置 + for(size_t i = 0; i < stop_node_number; i++) + { + // 对于后面的节点执行累加 + for(size_t j = i + 1; j < stop_node_number; j++) + { + 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; + } + + // 画一个描边的矩形框 + rect_item->setRect(0, 0, 595, 395); + scene->addItem(rect_item); } SceneManager::~SceneManager() { + delete []stop_pos_pairs; delete []pixmap_items; delete scene; -} \ No newline at end of file +} + +PosPair::PosPair() +{ + pos_x = stop_begin_x; + pos_y = stop_begin_y; +} + +int PosPair::GetStopSpaceLength(int stop_number) const +{ + return 2 * (stop_rail_width + stop_rail_height) / stop_number; +} + +void PosPair::AddLength(int length) +{ + distance += length; + + if(distance > 2 * stop_rail_width + stop_rail_height) + { + // 站点在左轨道 + + pos_x = stop_begin_x; + pos_y = stop_begin_y + (stop_rail_width + stop_rail_height) * 2 - distance; + } + else if(distance > stop_rail_width + stop_rail_height and + distance <= 2 * stop_rail_width + stop_rail_height) + { + // 站点在下轨道 + + pos_y = stop_begin_y + stop_rail_height; + pos_x = stop_begin_x + 2 * stop_rail_width + stop_rail_height - distance; + } + else if(distance > stop_rail_width and + distance <= stop_rail_width + stop_rail_height) + { + // 站点在右轨道 + + pos_x = stop_begin_x + stop_rail_width; + pos_y = stop_begin_y + stop_rail_width + stop_rail_height - distance; + } + else + { + // 站点在上轨道 + + pos_y = stop_begin_y; + pos_x = stop_begin_x + distance; + } +}