将PosPair类独立出来

添加了公交车显示类
This commit is contained in:
jackfiled 2022-06-28 11:10:49 +08:00
parent b2b043e227
commit 35569b093d
7 changed files with 231 additions and 141 deletions

29
include/BusWidget.h Normal file
View 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
View 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

View File

@ -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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 15 KiB

28
src/BusWidget.cpp Normal file
View 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
View 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;
}

View File

@ -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;
}