将PosPair类独立出来
添加了公交车显示类
This commit is contained in:
parent
b2b043e227
commit
35569b093d
29
include/BusWidget.h
Normal file
29
include/BusWidget.h
Normal file
|
@ -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
|
59
include/PosPair.h
Normal file
59
include/PosPair.h
Normal file
|
@ -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
|
|
@ -8,56 +8,8 @@
|
||||||
#include "QGraphicsScene"
|
#include "QGraphicsScene"
|
||||||
#include "QGraphicsPixmapItem"
|
#include "QGraphicsPixmapItem"
|
||||||
|
|
||||||
/**
|
#include "PosPair.h"
|
||||||
* 储存每个站点位置的类
|
#include "BusWidget.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 = 20;
|
|
||||||
|
|
||||||
int distance = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SceneManager
|
class SceneManager
|
||||||
{
|
{
|
||||||
|
@ -82,11 +34,17 @@ private:
|
||||||
* 显示站点的像素图对象
|
* 显示站点的像素图对象
|
||||||
*/
|
*/
|
||||||
QGraphicsPixmapItem *pixmap_items;
|
QGraphicsPixmapItem *pixmap_items;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 每个站点的所在位置
|
* 每个站点的所在位置
|
||||||
*/
|
*/
|
||||||
PosPair *stop_pos_pairs;
|
PosPair *stop_pos_pairs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公交车对象
|
||||||
|
*/
|
||||||
|
BusWidget *bus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 总共的站点数量
|
* 总共的站点数量
|
||||||
*/
|
*/
|
||||||
|
@ -101,7 +59,7 @@ private:
|
||||||
/**
|
/**
|
||||||
* 清除画面
|
* 清除画面
|
||||||
*/
|
*/
|
||||||
void ClearScene();
|
void ClearStopScene();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //AUTO_BUS_GUI_MAIN_SCENE_H
|
#endif //AUTO_BUS_GUI_MAIN_SCENE_H
|
||||||
|
|
BIN
picture/bus.png
BIN
picture/bus.png
Binary file not shown.
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 15 KiB |
28
src/BusWidget.cpp
Normal file
28
src/BusWidget.cpp
Normal file
|
@ -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());
|
||||||
|
}
|
92
src/PosPair.cpp
Normal file
92
src/PosPair.cpp
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ SceneManager::SceneManager()
|
||||||
pixmap_items = nullptr;
|
pixmap_items = nullptr;
|
||||||
stop_pos_pairs = nullptr;
|
stop_pos_pairs = nullptr;
|
||||||
rect_item = new QGraphicsRectItem;
|
rect_item = new QGraphicsRectItem;
|
||||||
|
bus = new BusWidget;
|
||||||
|
|
||||||
// 画一个描边的矩形框
|
// 画一个描边的矩形框
|
||||||
rect_item->setRect(0, 0, 595, 395);
|
rect_item->setRect(0, 0, 595, 395);
|
||||||
|
@ -19,14 +20,20 @@ SceneManager::SceneManager()
|
||||||
|
|
||||||
SceneManager::~SceneManager()
|
SceneManager::~SceneManager()
|
||||||
{
|
{
|
||||||
ClearScene();
|
// 清除站点图像
|
||||||
|
ClearStopScene();
|
||||||
|
|
||||||
|
// 清除公交车图像
|
||||||
|
scene->removeItem(bus->item);
|
||||||
|
delete bus;
|
||||||
|
|
||||||
delete scene;
|
delete scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SceneManager::SetStopScene(int node_number)
|
void SceneManager::SetStopScene(int node_number)
|
||||||
{
|
{
|
||||||
// 先清除屏幕
|
// 先清除屏幕
|
||||||
ClearScene();
|
ClearStopScene();
|
||||||
|
|
||||||
stop_node_number = node_number;
|
stop_node_number = node_number;
|
||||||
pixmap_items = new QGraphicsPixmapItem[stop_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);
|
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++)
|
for(int i = 0; i < stop_node_number; i++)
|
||||||
|
@ -70,90 +81,3 @@ void SceneManager::ClearScene()
|
||||||
delete []pixmap_items;
|
delete []pixmap_items;
|
||||||
delete []stop_pos_pairs;
|
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;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user