diff --git a/include/BusWidget.h b/include/BusWidget.h new file mode 100644 index 0000000..69ab4cb --- /dev/null +++ b/include/BusWidget.h @@ -0,0 +1,29 @@ +// +// Created by ricardo on 2022/6/28. +// + +#ifndef AUTO_BUS_GUI_BUS_WIDGET_H +#define AUTO_BUS_GUI_BUS_WIDGET_H +#include "QGraphicsPixmapItem" +#include "QTransform" + +#include "PosPair.h" + +class BusWidget +{ +public: + QGraphicsPixmapItem *item; + + + explicit BusWidget(); + + ~BusWidget(); + + void ResetBusPos(PosPair *s); + +private: + PosPair *pos_pairs = nullptr; +}; + + +#endif //AUTO_BUS_GUI_BUS_WIDGET_H diff --git a/include/PosPair.h b/include/PosPair.h new file mode 100644 index 0000000..c135d48 --- /dev/null +++ b/include/PosPair.h @@ -0,0 +1,59 @@ +// +// Created by ricardo on 2022/6/28. +// + +#ifndef AUTO_BUS_GUI_POSPAIR_H +#define AUTO_BUS_GUI_POSPAIR_H + +/** + * 储存每个站点位置的类 + */ +class PosPair{ + +public: + /* + * 站点位置的x坐标 + */ + int pos_x; + /** + * 站点位置的y坐标 + */ + int pos_y; + + PosPair(); + /** + * 获取两个站点之间的距离 + * @param stop_number 站点的数量 + * @return 站点之间的距离 + */ + int GetStopSpaceLength(int stop_number) const; + + /** + * 加上一定的距离 + * @param length 需要加上的距离 + */ + void AddLength(int length); + + /** + * 获得该站点公交车停车的位置x坐标 + * @return + */ + int GetBusPosX() const; + + /** + * 获得该站点公交车停车位置的y坐标 + * @return + */ + int GetBusPosY() const; + +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; + const int stop_bus_distance = 30; + + int distance = 0; +}; + +#endif //AUTO_BUS_GUI_POSPAIR_H diff --git a/include/mainScene.h b/include/mainScene.h index 9c7967a..0cf1599 100644 --- a/include/mainScene.h +++ b/include/mainScene.h @@ -8,56 +8,8 @@ #include "QGraphicsScene" #include "QGraphicsPixmapItem" -/** - * 储存每个站点位置的类 - */ -class PosPair{ - -public: - /* - * 站点位置的x坐标 - */ - int pos_x; - /** - * 站点位置的y坐标 - */ - int pos_y; - - PosPair(); - /** - * 获取两个站点之间的距离 - * @param stop_number 站点的数量 - * @return 站点之间的距离 - */ - int GetStopSpaceLength(int stop_number) const; - - /** - * 加上一定的距离 - * @param length 需要加上的距离 - */ - void AddLength(int length); - - /** - * 获得该站点公交车停车的位置x坐标 - * @return - */ - int GetBusPosX() const; - - /** - * 获得该站点公交车停车位置的y坐标 - * @return - */ - int GetBusPosY() const; - -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; - const int stop_bus_distance = 20; - - int distance = 0; -}; +#include "PosPair.h" +#include "BusWidget.h" class SceneManager { @@ -82,11 +34,17 @@ private: * 显示站点的像素图对象 */ QGraphicsPixmapItem *pixmap_items; + /** * 每个站点的所在位置 */ PosPair *stop_pos_pairs; + /** + * 公交车对象 + */ + BusWidget *bus; + /** * 总共的站点数量 */ @@ -101,7 +59,7 @@ private: /** * 清除画面 */ - void ClearScene(); + void ClearStopScene(); }; #endif //AUTO_BUS_GUI_MAIN_SCENE_H diff --git a/picture/bus.png b/picture/bus.png index 91ff966..ffed124 100644 Binary files a/picture/bus.png and b/picture/bus.png differ diff --git a/src/BusWidget.cpp b/src/BusWidget.cpp new file mode 100644 index 0000000..75845dc --- /dev/null +++ b/src/BusWidget.cpp @@ -0,0 +1,28 @@ +// +// Created by ricardo on 2022/6/28. +// + +#include "BusWidget.h" + +BusWidget::BusWidget() +{ + item = new QGraphicsPixmapItem(QPixmap(":/picture/bus.png")); + + // 设置缩放 + double bus_scale = 0.1; + QTransform transform; + transform.scale(bus_scale, bus_scale); + item->setTransform(transform); +} + +BusWidget::~BusWidget() +{ + delete item; +} + +void BusWidget::ResetBusPos(PosPair *s) +{ + pos_pairs = s; + + item->setPos(s[0].GetBusPosX(), s[0].GetBusPosY()); +} diff --git a/src/PosPair.cpp b/src/PosPair.cpp new file mode 100644 index 0000000..f34ba30 --- /dev/null +++ b/src/PosPair.cpp @@ -0,0 +1,92 @@ +// +// Created by ricardo on 2022/6/28. +// + +#include "PosPair.h" + +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 + distance - stop_rail_width; + } + else + { + // 站点在上轨道 + + pos_y = stop_begin_y; + pos_x = stop_begin_x + distance; + } +} + +int PosPair::GetBusPosX() const +{ + int result; + + if(pos_x <= stop_begin_x) + { + result = stop_begin_x - stop_bus_distance; + } + else if(pos_x >= stop_begin_x + stop_rail_width) + { + result = pos_x + stop_bus_distance; + } + else + { + result = pos_x; + } + + return result; +} + +int PosPair::GetBusPosY() const +{ + int result; + + if(pos_y <= stop_begin_y) + { + result = stop_begin_y - stop_bus_distance; + } + else if(pos_y >= stop_begin_y + stop_rail_width) + { + result = pos_y + stop_bus_distance; + } + else + { + result = pos_y; + } + + return result; +} diff --git a/src/mainScene.cpp b/src/mainScene.cpp index 8ae36ef..2fa86e1 100644 --- a/src/mainScene.cpp +++ b/src/mainScene.cpp @@ -11,6 +11,7 @@ SceneManager::SceneManager() pixmap_items = nullptr; stop_pos_pairs = nullptr; rect_item = new QGraphicsRectItem; + bus = new BusWidget; // 画一个描边的矩形框 rect_item->setRect(0, 0, 595, 395); @@ -19,14 +20,20 @@ SceneManager::SceneManager() SceneManager::~SceneManager() { - ClearScene(); + // 清除站点图像 + ClearStopScene(); + + // 清除公交车图像 + scene->removeItem(bus->item); + delete bus; + delete scene; } void SceneManager::SetStopScene(int node_number) { // 先清除屏幕 - ClearScene(); + ClearStopScene(); stop_node_number = node_number; pixmap_items = new QGraphicsPixmapItem[stop_node_number]; @@ -57,9 +64,13 @@ void SceneManager::SetStopScene(int node_number) } pixmap_items[i].setPos(stop_pos_pairs[i].pos_x, stop_pos_pairs[i].pos_y); } + + // 设置公交车图像 + bus->ResetBusPos(stop_pos_pairs); + scene->addItem(bus->item); } -void SceneManager::ClearScene() +void SceneManager::ClearStopScene() { // 从画布中移除所有的站点图片 for(int i = 0; i < stop_node_number; i++) @@ -70,90 +81,3 @@ void SceneManager::ClearScene() delete []pixmap_items; delete []stop_pos_pairs; } - -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 + distance - stop_rail_width; - } - else - { - // 站点在上轨道 - - pos_y = stop_begin_y; - pos_x = stop_begin_x + distance; - } -} - -int PosPair::GetBusPosX() const -{ - int result; - - if(pos_x <= stop_begin_x) - { - result = stop_begin_x - stop_bus_distance; - } - else if(pos_x >= stop_begin_x + stop_rail_width) - { - result = pos_x + stop_bus_distance; - } - else - { - result = pos_x; - } - - return result; -} - -int PosPair::GetBusPosY() const -{ - int result; - - if(pos_y <= stop_begin_y) - { - result = stop_begin_y - stop_bus_distance; - } - else if(pos_y >= stop_begin_y + stop_rail_width) - { - result = pos_y + stop_bus_distance; - } - else - { - result = pos_y; - } - - return result; -}