日志的打印完成
控制开始结束的流程完成
This commit is contained in:
parent
112299b02f
commit
ab830fcd9e
|
@ -19,8 +19,6 @@ public:
|
|||
RailsModel *rails_model;
|
||||
QueryModel *query_model;
|
||||
BusModel *bus_model;
|
||||
QTimer *tick_timer;
|
||||
|
||||
|
||||
int bus_tick;
|
||||
|
||||
|
@ -28,6 +26,7 @@ public:
|
|||
|
||||
virtual ~BusStrategyBase();
|
||||
|
||||
|
||||
/**
|
||||
* 获得当前公交车应该前进的方向
|
||||
* @return 公交车前进的方向
|
||||
|
@ -47,14 +46,50 @@ public:
|
|||
virtual bus_query_t *HandleQuery() = 0;
|
||||
|
||||
signals:
|
||||
/**
|
||||
* 删除请求信号
|
||||
* @param query 需要删除请求的指针
|
||||
*/
|
||||
void DeleteQuerySignal(bus_query_t *query);
|
||||
|
||||
/**
|
||||
* 打印状态信号
|
||||
* @param string 状态字符串
|
||||
*/
|
||||
void PrintStateSignal(QString string);
|
||||
|
||||
public slots:
|
||||
void AppendQuerySlot(int query_type, int node_id) const;
|
||||
|
||||
/**
|
||||
* 处理开始事件的槽函数
|
||||
*/
|
||||
void BusBeginSlot();
|
||||
|
||||
/**
|
||||
* 处理结束事件的槽函数
|
||||
*/
|
||||
void BusEndSlot();
|
||||
|
||||
/**
|
||||
* 处理tick事件的槽函数
|
||||
*/
|
||||
void OneTickSlot(int remaining_time);
|
||||
|
||||
private:
|
||||
QString PrintState() const;
|
||||
/**
|
||||
* 储存当前的状态
|
||||
*/
|
||||
int status = BUS_END;
|
||||
|
||||
/**
|
||||
* 打印当前状态
|
||||
* @return 表示当前状态的字符串
|
||||
*/
|
||||
QString PrintState(int remaining_time) const;
|
||||
|
||||
|
||||
void SetConnection() const;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -36,8 +36,6 @@ public:
|
|||
*/
|
||||
bus_query_t *target_query;
|
||||
|
||||
QTimer *bus_timer;
|
||||
|
||||
explicit BusModel();
|
||||
|
||||
~BusModel();
|
||||
|
@ -48,7 +46,7 @@ public:
|
|||
*/
|
||||
void ResetBus(rail_node_t *head);
|
||||
|
||||
double GetBusPosition();
|
||||
double GetBusPosition(int remaining_time);
|
||||
|
||||
/**
|
||||
* 给出在指定的方向下,指定的请求于公交车当前位置的距离
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
#define BUS_COUNTER_CLOCK_WISE 1 // 逆时针
|
||||
#define BUS_TARGET 2 // 目标
|
||||
#define BUS_STOP 2 // 停止
|
||||
#define IO_CLOCK 0 // 读取时钟指令
|
||||
#define IO_READING 1 // 读取请求指令
|
||||
#define IO_END 2 // 读取结束指令
|
||||
#define BUS_FCFS 0 // 先来先服务
|
||||
#define BUS_SSTF 1 // 最短寻找时间优先
|
||||
#define BUS_SCAN 2 // 顺便服务
|
||||
#define BUS_RUNNING 0
|
||||
#define BUS_PAUSING 1
|
||||
#define BUS_END 2
|
||||
|
||||
#endif //AUTO_BUS_GUI_DEFINE_H
|
||||
|
|
|
@ -29,6 +29,13 @@ public:
|
|||
*/
|
||||
void SetStopScene(int node_number);
|
||||
|
||||
/**
|
||||
* 开始公交车动画
|
||||
* @param direction 公交车前进的方向
|
||||
* @param duration 前进持续的时间
|
||||
*/
|
||||
void BeginBusAnimation(int direction, int duration);
|
||||
|
||||
private:
|
||||
/**
|
||||
* 显示站点的像素图对象
|
||||
|
|
|
@ -57,7 +57,7 @@ public:
|
|||
* @param id 需要查找的站点编号
|
||||
* @return 需要查找站点指针
|
||||
*/
|
||||
rail_node_t *FindNode(int node_num) const;
|
||||
rail_node_t *FindNode(int node_id) const;
|
||||
|
||||
/**
|
||||
* 创建轨道链表
|
||||
|
|
|
@ -8,8 +8,7 @@ BusStrategyBase::BusStrategyBase()
|
|||
{
|
||||
rails_model = new RailsModel;
|
||||
query_model = new QueryModel;
|
||||
bus_model = new BusModel();
|
||||
tick_timer = new QTimer;
|
||||
bus_model = new BusModel;
|
||||
|
||||
bus_tick = 0;
|
||||
}
|
||||
|
@ -19,7 +18,11 @@ BusStrategyBase::~BusStrategyBase()
|
|||
delete rails_model;
|
||||
delete query_model;
|
||||
delete bus_model;
|
||||
delete tick_timer;
|
||||
}
|
||||
|
||||
void BusStrategyBase::SetConnection() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) const
|
||||
|
@ -28,7 +31,34 @@ void BusStrategyBase::AppendQuerySlot(int query_type, int node_id) const
|
|||
query_model->CreateQuery(query_type, node);
|
||||
}
|
||||
|
||||
QString BusStrategyBase::PrintState() const
|
||||
void BusStrategyBase::OneTickSlot(int remaining_time)
|
||||
{
|
||||
// 时间流动
|
||||
bus_tick++;
|
||||
|
||||
// 打印状态
|
||||
QString str = PrintState(remaining_time);
|
||||
emit PrintStateSignal(str);
|
||||
}
|
||||
|
||||
void BusStrategyBase::BusBeginSlot()
|
||||
{
|
||||
status = BUS_RUNNING;
|
||||
|
||||
// 在一开始先打印一下状态
|
||||
QString str = PrintState(0);
|
||||
emit PrintStateSignal(str);
|
||||
}
|
||||
|
||||
void BusStrategyBase::BusEndSlot()
|
||||
{
|
||||
status = BUS_END;
|
||||
bus_tick = 0;
|
||||
|
||||
bus_model->ResetBus(rails_model->rails);
|
||||
}
|
||||
|
||||
QString BusStrategyBase::PrintState(int remaining_time) const
|
||||
{
|
||||
int count = 0;
|
||||
rail_node_t *node = rails_model->rails;
|
||||
|
@ -78,7 +108,7 @@ QString BusStrategyBase::PrintState() const
|
|||
|
||||
QString str = QString::asprintf("Time:%d\n", bus_tick);
|
||||
str += "BUS:\n";
|
||||
str += QString::asprintf("position:%lf\n", bus_model->GetBusPosition());
|
||||
str += QString::asprintf("position:%lf\n", bus_model->GetBusPosition(remaining_time));
|
||||
str = str + "target:" + target + "\n";
|
||||
str += "STATION:\n";
|
||||
str = str + "clockwise" + clockwise + "\n";
|
||||
|
|
|
@ -10,13 +10,11 @@ BusModel::BusModel()
|
|||
direction = BUS_STOP;
|
||||
target_query = nullptr;
|
||||
rail_head = nullptr;
|
||||
|
||||
bus_timer = new QTimer;
|
||||
}
|
||||
|
||||
BusModel::~BusModel()
|
||||
{
|
||||
delete bus_timer;
|
||||
|
||||
}
|
||||
|
||||
void BusModel::ResetBus(rail_node_t *head)
|
||||
|
@ -28,7 +26,7 @@ void BusModel::ResetBus(rail_node_t *head)
|
|||
rail_pos = rail_head;
|
||||
}
|
||||
|
||||
double BusModel::GetBusPosition()
|
||||
double BusModel::GetBusPosition(int remaining_time)
|
||||
{
|
||||
double result = 0;
|
||||
rail_node_t *now_pos = rail_pos;
|
||||
|
@ -41,7 +39,11 @@ double BusModel::GetBusPosition()
|
|||
node = node->next_node;
|
||||
} while (node != now_pos);
|
||||
|
||||
int remaining_time = bus_timer->remainingTime();
|
||||
// 如果就在起始点,距离为0
|
||||
if(now_pos == rail_head)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
|
||||
if(remaining_time > 0)
|
||||
{
|
||||
|
|
|
@ -43,6 +43,9 @@ void CentralWidget::SetControlConnection()
|
|||
// 设置删除请求事件的连接
|
||||
QObject::connect(controller, &BusStrategyBase::DeleteQuerySignal,
|
||||
this, &CentralWidget::DeleteQueryItemSlot);
|
||||
|
||||
QObject::connect(controller, &BusStrategyBase::PrintStateSignal,
|
||||
this, &CentralWidget::PrintStateSlot);
|
||||
}
|
||||
|
||||
void CentralWidget::SetWidgetConnection()
|
||||
|
@ -119,6 +122,16 @@ void CentralWidget::DeleteQueryItemSlot(bus_query_t *query)
|
|||
delete *pos;
|
||||
}
|
||||
|
||||
void CentralWidget::PrintStateSlot(const QString& string)
|
||||
{
|
||||
ui->text_output->insertPlainText(string);
|
||||
|
||||
// 设置光标在最后一行
|
||||
QTextCursor cursor = ui->text_output->textCursor();
|
||||
cursor.movePosition(QTextCursor::End);
|
||||
ui->text_output->setTextCursor(cursor);
|
||||
}
|
||||
|
||||
void CentralWidget::AddQueryButtonClicked()
|
||||
{
|
||||
int query_type = ui->query_type_combo->currentIndex();
|
||||
|
|
|
@ -46,7 +46,6 @@
|
|||
<string>Run</string>
|
||||
</property>
|
||||
<addaction name="actionRun_Bus"/>
|
||||
<addaction name="actionPause_Bus"/>
|
||||
<addaction name="actionStop_Bus"/>
|
||||
</widget>
|
||||
<addaction name="menuFile"/>
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "string"
|
||||
#include "list"
|
||||
#include "QMessageBox"
|
||||
#include "QTextCursor"
|
||||
|
||||
#include "mainScene.h"
|
||||
#include "queryListItem.h"
|
||||
|
@ -62,6 +63,12 @@ public slots:
|
|||
*/
|
||||
void AddQueryButtonClicked();
|
||||
|
||||
/**
|
||||
* 处理打印状态事件的槽函数
|
||||
* @param string 状态字符串
|
||||
*/
|
||||
void PrintStateSlot(const QString& string);
|
||||
|
||||
private:
|
||||
/**
|
||||
* UI控件
|
||||
|
|
|
@ -32,22 +32,22 @@ public:
|
|||
~MainWindow() override;
|
||||
|
||||
signals:
|
||||
|
||||
/**
|
||||
* 开始运行公交车的信号
|
||||
*/
|
||||
void RunBusSignal();
|
||||
|
||||
/**
|
||||
* 暂停运行公交车的信号
|
||||
*/
|
||||
void PauseBusSignal();
|
||||
|
||||
/**
|
||||
* 停止运行公交车的信号
|
||||
*/
|
||||
void StopBusSignal();
|
||||
|
||||
/**
|
||||
* 时钟信号
|
||||
* @param remain_time bus_timer计时器剩余的时间
|
||||
*/
|
||||
void BusTickSignal(int remain_time);
|
||||
|
||||
public slots:
|
||||
/**
|
||||
* 点击打开配置文件按钮的槽函数
|
||||
|
@ -59,21 +59,37 @@ public slots:
|
|||
*/
|
||||
void RunBusClicked();
|
||||
|
||||
/**
|
||||
* 处理点击暂停公交车按钮的槽函数
|
||||
*/
|
||||
void PauseBusClicked();
|
||||
|
||||
/**
|
||||
* 处理点击停止公交车按钮的槽函数
|
||||
*/
|
||||
void StopBusClicked();
|
||||
|
||||
/**
|
||||
* 开始全局计时器的槽函数
|
||||
*/
|
||||
void BeginTickTimerSlot();
|
||||
|
||||
/**
|
||||
* 结束全局计时器的槽函数
|
||||
*/
|
||||
void EndTickTimerSlot();
|
||||
|
||||
/**
|
||||
* 每一tick的槽函数
|
||||
*/
|
||||
void OneTickSlot();
|
||||
|
||||
private:
|
||||
/**
|
||||
* 设置一时刻的长度,单位毫秒
|
||||
*/
|
||||
const int tick = 1000;
|
||||
|
||||
/**
|
||||
* UI控件
|
||||
*/
|
||||
Ui::MainWindow *ui;
|
||||
|
||||
/**
|
||||
* 中间的显示框架
|
||||
*/
|
||||
|
@ -83,10 +99,15 @@ private:
|
|||
|
||||
BusStrategyBase *controller;
|
||||
|
||||
QTimer *tick_timer;
|
||||
|
||||
QTimer *bus_timer;
|
||||
|
||||
|
||||
/**
|
||||
* 设置菜单栏的相关连接
|
||||
*/
|
||||
void SetMenuBarConnection();
|
||||
void SetWidgetConnection();
|
||||
|
||||
/**
|
||||
* 设置控制器的相关连接
|
||||
|
|
|
@ -81,3 +81,8 @@ void SceneManager::ClearStopScene()
|
|||
delete []pixmap_items;
|
||||
delete []stop_pos_pairs;
|
||||
}
|
||||
|
||||
void SceneManager::BeginBusAnimation(int direction, int duration)
|
||||
{
|
||||
bus->StartAnimation(direction, duration);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
|
|||
{
|
||||
ui = new Ui::MainWindow;
|
||||
worker_thread = new QThread;
|
||||
tick_timer = new QTimer;
|
||||
bus_timer = new QTimer;
|
||||
controller = nullptr;
|
||||
|
||||
central_widget = new CentralWidget(nullptr);
|
||||
|
@ -19,7 +21,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
|
|||
ui->setupUi(this);
|
||||
this->setCentralWidget(central_widget);
|
||||
|
||||
SetMenuBarConnection();
|
||||
SetWidgetConnection();
|
||||
|
||||
//开始多线程事件循环
|
||||
worker_thread->start();
|
||||
|
@ -33,9 +35,11 @@ MainWindow::~MainWindow()
|
|||
delete ui;
|
||||
delete central_widget;
|
||||
delete worker_thread;
|
||||
delete tick_timer;
|
||||
delete bus_timer;
|
||||
}
|
||||
|
||||
void MainWindow::SetMenuBarConnection()
|
||||
void MainWindow::SetWidgetConnection()
|
||||
{
|
||||
// 连接退出按钮
|
||||
QObject::connect(ui->actionExit, SIGNAL(triggered()),
|
||||
|
@ -46,17 +50,36 @@ void MainWindow::SetMenuBarConnection()
|
|||
// 连接运行公交车按钮
|
||||
QObject::connect(ui->actionRun_Bus, &QAction::triggered,
|
||||
this, &MainWindow::RunBusClicked);
|
||||
// 连接暂停公交车按钮
|
||||
QObject::connect(ui->actionPause_Bus, &QAction::triggered,
|
||||
this, &MainWindow::PauseBusClicked);
|
||||
|
||||
// 连接停止公交车按钮
|
||||
QObject::connect(ui->actionStop_Bus, &QAction::triggered,
|
||||
this, &MainWindow::StopBusClicked);
|
||||
|
||||
// 开始全局计时器连接
|
||||
QObject::connect(this, &MainWindow::RunBusSignal,
|
||||
this, &MainWindow::BeginTickTimerSlot);
|
||||
|
||||
// 结束全局计时器连接
|
||||
QObject::connect(this, &MainWindow::StopBusSignal,
|
||||
this, &MainWindow::EndTickTimerSlot);
|
||||
|
||||
// 处理计时器tick连接
|
||||
QObject::connect(tick_timer, &QTimer::timeout,
|
||||
this, &MainWindow::OneTickSlot);
|
||||
}
|
||||
|
||||
void MainWindow::SetControlConnection()
|
||||
{
|
||||
// 开始运行连接
|
||||
QObject::connect(this, &MainWindow::RunBusSignal,
|
||||
controller, &BusStrategyBase::BusBeginSlot);
|
||||
|
||||
QObject::connect(this, &MainWindow::StopBusSignal,
|
||||
controller, &BusStrategyBase::BusEndSlot);
|
||||
|
||||
// 每一tick连接
|
||||
QObject::connect(this, &MainWindow::BusTickSignal,
|
||||
controller, &BusStrategyBase::OneTickSlot);
|
||||
}
|
||||
|
||||
void MainWindow::ReadConfigFileButtonClicked()
|
||||
|
@ -81,9 +104,13 @@ void MainWindow::ReadConfigFileButtonClicked()
|
|||
}
|
||||
|
||||
controller = StrategyFactory::GetStrategy(file_name);
|
||||
// 开始多线程
|
||||
BeginThread();
|
||||
// 设置controller相关连接
|
||||
SetControlConnection();
|
||||
central_widget->SetController(controller);
|
||||
// 重设公交车的状态
|
||||
controller->bus_model->ResetBus(controller->rails_model->rails);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,11 +119,6 @@ void MainWindow::RunBusClicked()
|
|||
emit RunBusSignal();
|
||||
}
|
||||
|
||||
void MainWindow::PauseBusClicked()
|
||||
{
|
||||
emit PauseBusSignal();
|
||||
}
|
||||
|
||||
void MainWindow::StopBusClicked()
|
||||
{
|
||||
emit StopBusSignal();
|
||||
|
@ -108,4 +130,23 @@ void MainWindow::BeginThread()
|
|||
{
|
||||
controller->moveToThread(worker_thread);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::BeginTickTimerSlot()
|
||||
{
|
||||
tick_timer->setInterval(tick);
|
||||
|
||||
tick_timer->start();
|
||||
}
|
||||
|
||||
void MainWindow::OneTickSlot()
|
||||
{
|
||||
int time = bus_timer->remainingTime();
|
||||
|
||||
emit BusTickSignal(time);
|
||||
}
|
||||
|
||||
void MainWindow::EndTickTimerSlot()
|
||||
{
|
||||
tick_timer->stop();
|
||||
}
|
|
@ -19,7 +19,7 @@ RailsModel::~RailsModel()
|
|||
}
|
||||
}
|
||||
|
||||
rail_node_t *RailsModel::FindNode(int node_num) const
|
||||
rail_node_t *RailsModel::FindNode(int node_id) const
|
||||
{
|
||||
if (rails == nullptr)
|
||||
{
|
||||
|
@ -31,7 +31,7 @@ rail_node_t *RailsModel::FindNode(int node_num) const
|
|||
|
||||
do
|
||||
{
|
||||
if(node->id == node_num)
|
||||
if(node->id == node_id)
|
||||
{
|
||||
result = node;
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue
Block a user