显示了站点的图片
This commit is contained in:
parent
5b2045675d
commit
f18f545618
|
@ -7,28 +7,58 @@
|
||||||
|
|
||||||
#include "QGraphicsScene"
|
#include "QGraphicsScene"
|
||||||
#include "QGraphicsPixmapItem"
|
#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
|
class SceneManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QGraphicsScene *scene;
|
QGraphicsScene *scene;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数,同时生成站点
|
||||||
|
* @param stop_node_number 站点的数量
|
||||||
|
*/
|
||||||
|
explicit SceneManager(int stop_node_number);
|
||||||
~SceneManager();
|
~SceneManager();
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化绘制场景
|
|
||||||
*/
|
|
||||||
void init();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化场景中的站点
|
|
||||||
* @param rail_head 轨道的头节点
|
|
||||||
*/
|
|
||||||
void initBusStop(rail_node_t *rail_head);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* 显示站点的像素图对象
|
||||||
|
*/
|
||||||
QGraphicsPixmapItem *pixmap_items;
|
QGraphicsPixmapItem *pixmap_items;
|
||||||
|
/**
|
||||||
|
* 每个站点的所在位置
|
||||||
|
*/
|
||||||
|
PosPair *stop_pos_pairs;
|
||||||
|
|
||||||
|
QGraphicsRectItem *rect_item;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //AUTO_BUS_GUI_MAIN_SCENE_H
|
#endif //AUTO_BUS_GUI_MAIN_SCENE_H
|
||||||
|
|
|
@ -12,16 +12,13 @@
|
||||||
CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CentralWidget)
|
CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CentralWidget)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
scene_manager = new SceneManager(20);
|
||||||
scene_manager = new SceneManager;
|
|
||||||
scene_manager->init();
|
|
||||||
scene_manager->initBusStop(nullptr);
|
|
||||||
|
|
||||||
ui->main_canva->setScene(scene_manager->scene);
|
ui->main_canva->setScene(scene_manager->scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
CentralWidget::~CentralWidget()
|
CentralWidget::~CentralWidget()
|
||||||
{
|
{
|
||||||
delete ui;
|
|
||||||
delete scene_manager;
|
delete scene_manager;
|
||||||
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,23 +4,95 @@
|
||||||
|
|
||||||
#include "mainScene.h"
|
#include "mainScene.h"
|
||||||
|
|
||||||
void SceneManager::init()
|
SceneManager::SceneManager(int stop_node_number)
|
||||||
{
|
{
|
||||||
scene = new QGraphicsScene;
|
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)
|
int stop_space_length = stop_pos_pairs->GetStopSpaceLength(stop_node_number);
|
||||||
{
|
double stop_scale = 0.15;
|
||||||
pixmap_items = new QGraphicsPixmapItem[1];
|
QTransform stop_transform;
|
||||||
|
|
||||||
pixmap_items->setPixmap(QPixmap(":/picture/bus.png"));
|
stop_transform.scale(stop_scale, stop_scale);
|
||||||
pixmap_items->setPos(100,100);
|
|
||||||
|
|
||||||
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()
|
SceneManager::~SceneManager()
|
||||||
{
|
{
|
||||||
|
delete []stop_pos_pairs;
|
||||||
delete []pixmap_items;
|
delete []pixmap_items;
|
||||||
delete scene;
|
delete scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user