添加了轨道和位置点
This commit is contained in:
parent
73b096aa3d
commit
dc7635d8d0
|
@ -11,6 +11,26 @@
|
|||
class PosPair{
|
||||
|
||||
public:
|
||||
/**
|
||||
* 设定的轨道开始坐标x
|
||||
*/
|
||||
static const int stop_begin_x = 100;
|
||||
|
||||
/**
|
||||
* 设定的轨道开始坐标y
|
||||
*/
|
||||
static const int stop_begin_y = 80;
|
||||
|
||||
/**
|
||||
* 轨道的宽度
|
||||
*/
|
||||
static const int stop_rail_width = 300;
|
||||
|
||||
/**
|
||||
* 轨道的高度
|
||||
*/
|
||||
static const int stop_rail_height = 200;
|
||||
|
||||
/*
|
||||
* 站点位置的x坐标
|
||||
*/
|
||||
|
@ -48,25 +68,6 @@ public:
|
|||
int GetBusPosY() const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* 设定的轨道开始坐标x
|
||||
*/
|
||||
const int stop_begin_x = 100;
|
||||
|
||||
/**
|
||||
* 设定的轨道开始坐标y
|
||||
*/
|
||||
const int stop_begin_y = 80;
|
||||
|
||||
/**
|
||||
* 轨道的宽度
|
||||
*/
|
||||
const int stop_rail_width = 300;
|
||||
|
||||
/**
|
||||
* 轨道的高度
|
||||
*/
|
||||
const int stop_rail_height = 200;
|
||||
|
||||
/**
|
||||
* 公交车到站点的距离
|
||||
|
|
|
@ -34,7 +34,7 @@ public:
|
|||
* 生成站点画面
|
||||
* @param node_number
|
||||
*/
|
||||
void SetStopScene(int node_number);
|
||||
void SetStopScene(int node_number, int node_distance);
|
||||
|
||||
/**
|
||||
* 重设公交车的位置
|
||||
|
@ -59,6 +59,16 @@ private:
|
|||
*/
|
||||
QGraphicsSimpleTextItem *name_items;
|
||||
|
||||
/**
|
||||
* 轨道对象
|
||||
*/
|
||||
QGraphicsPathItem *rail_path;
|
||||
|
||||
/**
|
||||
* 位置点对象数组
|
||||
*/
|
||||
QGraphicsEllipseItem *pos_spots;
|
||||
|
||||
/**
|
||||
* 每个站点的所在位置
|
||||
*/
|
||||
|
@ -74,6 +84,11 @@ private:
|
|||
*/
|
||||
int stop_node_number;
|
||||
|
||||
/**
|
||||
* 站点之间的距离
|
||||
*/
|
||||
int stop_node_distance;
|
||||
|
||||
/**
|
||||
* 一个矩形的边框
|
||||
* 使无论中间存在多少站点,这个scene都能在view的中心
|
||||
|
|
|
@ -31,7 +31,8 @@ void CentralWidget::SetController(BusStrategyBase *c)
|
|||
{
|
||||
controller = c;
|
||||
SetControlConnection();
|
||||
SetRailsScene(controller->rails_model->node_num);
|
||||
SetRailsScene(controller->rails_model->node_num,
|
||||
controller->rails_model->total_distance / controller->rails_model->node_num);
|
||||
}
|
||||
|
||||
void CentralWidget::SetStrategyLabel(int strategy)
|
||||
|
@ -117,13 +118,13 @@ void CentralWidget::SetWidgetConnection()
|
|||
void CentralWidget::SetupQueryList()
|
||||
{
|
||||
// 设置请求列表的表头
|
||||
QueryListItem *column_name = new QueryListItem(1,1);
|
||||
auto *column_name = new QueryListItem(1,1);
|
||||
column_name->SetColumnName();
|
||||
|
||||
query_items.push_back(column_name);
|
||||
|
||||
// 在列表中显示
|
||||
QListWidgetItem *widget_item = new QListWidgetItem;
|
||||
auto *widget_item = new QListWidgetItem;
|
||||
ui->query_list->addItem(widget_item);
|
||||
ui->query_list->setItemWidget(widget_item, column_name);
|
||||
}
|
||||
|
@ -159,12 +160,12 @@ void CentralWidget::AppendQueryItemSlot(int query_type, int node_id)
|
|||
return;
|
||||
}
|
||||
|
||||
QueryListItem *item = new QueryListItem(query_type, node_id);
|
||||
auto *item = new QueryListItem(query_type, node_id);
|
||||
|
||||
query_items.push_back(item);
|
||||
|
||||
// 在列表中显示
|
||||
QListWidgetItem *widget_item = new QListWidgetItem;
|
||||
auto *widget_item = new QListWidgetItem;
|
||||
ui->query_list->addItem(widget_item);
|
||||
ui->query_list->setItemWidget(widget_item, item);
|
||||
}
|
||||
|
@ -231,9 +232,9 @@ void CentralWidget::AddQueryButtonClicked()
|
|||
emit AppendQuerySignal(query_type, node_id);
|
||||
}
|
||||
|
||||
void CentralWidget::SetRailsScene(int node_num)
|
||||
void CentralWidget::SetRailsScene(int node_num, int node_distance)
|
||||
{
|
||||
scene_manager->SetStopScene(node_num);
|
||||
scene_manager->SetStopScene(node_num, node_distance);
|
||||
SetRailsComboBox(node_num);
|
||||
}
|
||||
|
||||
|
|
|
@ -143,7 +143,7 @@ private:
|
|||
* 重新设置站点选择下拉栏
|
||||
* @param node_num
|
||||
*/
|
||||
void SetRailsScene(int node_num);
|
||||
void SetRailsScene(int node_num, int node_distance);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -8,9 +8,12 @@ SceneManager::SceneManager()
|
|||
{
|
||||
scene = new QGraphicsScene;
|
||||
stop_node_number = 0;
|
||||
stop_node_distance = 0;
|
||||
pixmap_items = nullptr;
|
||||
stop_pos_pairs = nullptr;
|
||||
name_items = nullptr;
|
||||
pos_spots = nullptr;
|
||||
rail_path = nullptr;
|
||||
rect_item = new QGraphicsRectItem;
|
||||
bus = new BusWidget;
|
||||
|
||||
|
@ -24,24 +27,31 @@ SceneManager::~SceneManager()
|
|||
delete []name_items;
|
||||
delete []pixmap_items;
|
||||
delete []stop_pos_pairs;
|
||||
delete []pos_spots;
|
||||
delete rail_path;
|
||||
delete bus;
|
||||
delete rect_item;
|
||||
delete scene;
|
||||
}
|
||||
|
||||
void SceneManager::SetStopScene(int node_number)
|
||||
void SceneManager::SetStopScene(int node_number, int node_distance)
|
||||
{
|
||||
// 先清除屏幕
|
||||
ClearStopScene();
|
||||
|
||||
stop_node_number = node_number;
|
||||
stop_node_distance = node_distance;
|
||||
|
||||
pixmap_items = new QGraphicsPixmapItem[stop_node_number];
|
||||
stop_pos_pairs = new PosPair[stop_node_number];
|
||||
name_items = new QGraphicsSimpleTextItem[stop_node_number];
|
||||
pos_spots = new QGraphicsEllipseItem[stop_node_number * (node_distance - 1)];
|
||||
rail_path = new QGraphicsPathItem;
|
||||
|
||||
int stop_space_length = stop_pos_pairs->GetStopSpaceLength(stop_node_number);
|
||||
|
||||
double stop_scale = 0.15;
|
||||
QTransform stop_transform;
|
||||
|
||||
stop_transform.scale(stop_scale, stop_scale);
|
||||
|
||||
// 为站点对象设置
|
||||
|
@ -64,6 +74,7 @@ void SceneManager::SetStopScene(int node_number)
|
|||
pixmap_items[i].setPos(stop_pos_pairs[i].pos_x, stop_pos_pairs[i].pos_y);
|
||||
}
|
||||
|
||||
// 初始化站点编号
|
||||
for(int i = 0; i < stop_node_number; i++)
|
||||
{
|
||||
name_items[i].setText(QString::number(i + 1));
|
||||
|
@ -72,6 +83,60 @@ void SceneManager::SetStopScene(int node_number)
|
|||
scene->addItem(&name_items[i]);
|
||||
}
|
||||
|
||||
// 设置画点的画笔
|
||||
QPen pen = QPen();
|
||||
pen.setWidth(2);
|
||||
pen.setColor(Qt::black);
|
||||
// 设置画轨道的画笔
|
||||
QPen rail_pen = QPen();
|
||||
rail_pen.setWidth(1);
|
||||
rail_pen.setColor(Qt::black);
|
||||
|
||||
// 画轨道
|
||||
QPainterPath path;
|
||||
path.moveTo(PosPair::stop_begin_x, PosPair::stop_begin_y);
|
||||
|
||||
// 画位置点
|
||||
int pos;
|
||||
for(int i = 0; i < stop_node_number; i++)
|
||||
{
|
||||
path.lineTo(stop_pos_pairs[i].pos_x, stop_pos_pairs[i].pos_y);
|
||||
|
||||
// 处理在画最后一个点的时候数组越界的问题
|
||||
int next_id = i + 1;
|
||||
if(next_id == stop_node_number)
|
||||
{
|
||||
next_id = 0;
|
||||
}
|
||||
|
||||
int x_distance = (stop_pos_pairs[next_id].pos_x - stop_pos_pairs[i].pos_x) / node_distance;
|
||||
int y_distance = (stop_pos_pairs[next_id].pos_y - stop_pos_pairs[i].pos_y) / node_distance;
|
||||
|
||||
int spot_pos_x = stop_pos_pairs[i].pos_x;
|
||||
int spot_pos_y = stop_pos_pairs[i].pos_y;
|
||||
|
||||
for(int j = 0; j < node_distance - 1; j++)
|
||||
{
|
||||
pos = i * (node_distance - 1) + j;
|
||||
|
||||
spot_pos_x += x_distance;
|
||||
spot_pos_y += y_distance;
|
||||
|
||||
pos_spots[pos].setPen(pen);
|
||||
pos_spots[pos].setBrush(QBrush(Qt::black));
|
||||
|
||||
pos_spots[pos].setRect(spot_pos_x - 1, spot_pos_y - 1, 2, 2);
|
||||
|
||||
scene->addItem(&pos_spots[pos]);
|
||||
}
|
||||
}
|
||||
|
||||
// 最后在连回起点
|
||||
path.lineTo(PosPair::stop_begin_x, PosPair::stop_begin_y);
|
||||
rail_path->setPath(path);
|
||||
rail_path->setPen(rail_pen);
|
||||
scene->addItem(rail_path);
|
||||
|
||||
// 设置公交车图像
|
||||
bus->ResetBusPos(stop_pos_pairs, node_number);
|
||||
scene->addItem(bus->item);
|
||||
|
@ -79,7 +144,6 @@ void SceneManager::SetStopScene(int node_number)
|
|||
|
||||
void SceneManager::ResetBus()
|
||||
{
|
||||
|
||||
bus->ResetBusPos(stop_pos_pairs, stop_node_number);
|
||||
}
|
||||
|
||||
|
@ -91,14 +155,29 @@ void SceneManager::ClearStopScene()
|
|||
scene->removeItem(&pixmap_items[i]);
|
||||
}
|
||||
|
||||
// 移除站点编号
|
||||
for(int i = 0; i < stop_node_number; i++)
|
||||
{
|
||||
scene->removeItem(&name_items[i]);
|
||||
}
|
||||
|
||||
// 移除位置点
|
||||
for(int i = 0; i < stop_node_number * (stop_node_distance - 1); i++)
|
||||
{
|
||||
scene->removeItem(&pos_spots[i]);
|
||||
}
|
||||
|
||||
// 移除轨道
|
||||
if(rail_path != nullptr)
|
||||
{
|
||||
scene->removeItem(rail_path);
|
||||
}
|
||||
|
||||
delete []name_items;
|
||||
delete []pixmap_items;
|
||||
delete []pos_spots;
|
||||
delete []stop_pos_pairs;
|
||||
delete rail_path;
|
||||
}
|
||||
|
||||
void SceneManager::BeginBusAnimation(int direction, int duration)
|
||||
|
|
Loading…
Reference in New Issue
Block a user