Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6e1d433a14
Fetching contributors…

Cannot retrieve contributors at this time

220 lines (186 sloc) 6.468 kb
#include "../Headers/qt/emulator.h"
#include "../Headers/PoissRand.h"
#include "../Headers/internlib.h"
#include "../Headers/controller.h"
#include <QPushButton>
#include <QApplication>
#include <QGridLayout>
#include <QPainter>
#include <QKeyEvent>
/* 构造函数:构造 Qt 界面 */
Emulator::Emulator(QWidget* parent) : QWidget(parent) {
START = 0;
QLabel* labelP;
labelP = new QLabel("Elevator:", this);
int bias = 20;
labelP->move(40 + bias, 530);
nEle = new QLabel("0 ", this);
nEle->move(100 + bias, 530);
labelP = new QLabel("Passenger:", this);
labelP->move(150 + bias, 530);
nPas = new QLabel("0 ", this);
nPas->move(220 + bias, 530);
labelP = new QLabel("Average wait:", this);
labelP->move(260 + bias, 530);
avWt = new QLabel("0 ", this);
avWt ->move(345 + bias, 530);
labelP = new QLabel("Max wait:", this);
labelP->move(405 + bias, 530);
mxWt = new QLabel("0 ", this);
mxWt->move(470 + bias, 530);
labelP = new QLabel("Average duration:", this);
labelP->move(40 + bias, 550);
avDur = new QLabel("0 ", this);
avDur->move(160 + bias, 550);
labelP = new QLabel("Max duration:", this);
labelP->move(210 + bias, 550);
mxDur = new QLabel("0 ", this);
mxDur->move(305 + bias, 550);
labelP = new QLabel("Min duration:", this);
labelP->move(365 + bias, 550);
mnDur = new QLabel("0 ", this);
mnDur->move(465 + bias, 550);
labelP = new QLabel("Average throughput:", this);
labelP->move(40 + bias, 570);
avThr = new QLabel("0 ", this);
avThr->move(bias + 180, 570);
start = new QPushButton("START", this);
start->move(700, 550);
QPushButton *quit = new QPushButton("Quit ", this);
quit->move(700, 570);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(start, SIGNAL(clicked()), this, SLOT(startEmulationSlots()));
timer = 0;
startTimer(10);
}
/* 开始/暂停游戏 slots */
void Emulator::startEmulationSlots() {
startEmulator();
}
/* 开始/暂停模拟 */
void Emulator::startEmulator()
{
START = !START;
if (START == 0) {
start->setText(QString("START"));
} else {
start->setText(QString("PAUSE"));
}
}
/* 每计数器刷新事件,详见函数内注释 */
void Emulator::timerEvent(QTimerEvent *event)
{
if (START) {
timer = (timer + 1) % 100;
}
Q_UNUSED(event);
if(START) {
if (timer == 0) {
Born(); /* 产生乘客,电梯请求 */
Control(); /* 调用调度算法 */
}
Move(); /* 电梯依据指令或默认逻辑移动 */
Update(); /* 更新统计数据 */
repaint();
} else {
// Do nothing
}
}
/* Qt::绘制图形界面 */
void Emulator::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
QFont font("Courier", 40);
painter.setFont(font);
painter.setBrush(QBrush("#c56c00"));
int i = 0;
/* 绘制电梯 */
for(EleIter iter = elevators.begin(); iter != elevators.end(); iter++) {
i++;
painter.drawRect(150 + i * 50, height() - (128 + (*iter)->Get_fpos() * 50), 40, 40);
painter.setPen(Qt::white);
painter.drawText(150 + i * 50, height() - (95 + (*iter)->Get_fpos() * 50), QString::number((*iter)->Get_nPassenger()));//QString::number((*iter)->Get_inst()->size() ));
painter.setPen(QColor(Qt::black));
}
/* 绘制楼层 */
for (i = 0; i < nFloor; i++) {
painter.drawRect(60, height() - (84 + i * 50), 110, 4);
painter.setPen(QColor(Qt::gray));
painter.drawText(105, height() - (90 + i * 50), QString::number(floors[i].Get_nPassenger()));
painter.setPen(QColor(Qt::red));
if (floors[i].UpOrdered())
painter.drawText(145, height() - (90 + i * 50), "^");
if (floors[i].DownOrdered()) {
painter.drawText(145, height() - (90 + i * 50) + 5, "v");
}
painter.setPen(QColor(Qt::black));
}
QPen pen(Qt::gray, 2, Qt::DashDotLine);
painter.setPen(pen);
/* 打印统计数据 */
nEle->setText(QString::number(nElevator));
nPas->setText(QString::number(Passenger::Total()));
avWt->setText(QString::number(Passenger::AverWaitTime()));
avDur->setText(QString::number(Passenger::AverDuration()));
mxWt->setText(QString::number(Passenger::MaxWaitTime()));
mxDur->setText(QString::number(Passenger::MaxDuration()));
mnDur->setText(QString::number(Passenger::MinDuration()));
avThr->setText(QString::number(Elevator::AverThrput()));
/* 绘制楼层分界 */
for (i = 0; i < nFloor; i++) {
painter.drawLine(180, 67 + i * 50, 700, 67 + i * 50);
}
}
/* Qt::键盘响应事件映射 */
void Emulator::keyPressEvent(QKeyEvent* event)
{
switch (event->key()) {
case Qt::Key_Space: /* Space: 开始/暂停模拟 */
startEmulator();
break;
case Qt::Key_Escape: /* Esc: 退出程序 */
qApp->exit();
break;
default:
QWidget::keyPressEvent(event);
break;
}
}
/* 产生新的乘客,新的请求 */
void Emulator::Born() {
for(int i = 0; i < nFloor; i++) {
int nPassenger = PoissRand(lambda);
for(int j = 0; j < nPassenger; j++) {
Passenger *pass = new Passenger(i, floor(Rand(0, nFloor)));
floors[i].PushPassenger(pass);
pass->MakeAnOrder();
}
}
}
/* 依据算法调度 */
void Emulator::Control() {
if (elevatorType == "PFCFS") {
Controller::Fcfs();
} else if (elevatorType == "costflow") {
Controller::CostFLow();
}
}
/* 电梯依据指令或默认逻辑移动 */
void Emulator::Move() {
EleIter iter;
for(iter = elevators.begin(); iter != elevators.end(); iter++) {
(*iter)->Move();
}
}
/* 更新统计数据 */
void Emulator::Update() {
EleIter iter;
for (int i = 0; i < nFloor; i++) {
floors[i].LetThemWait();
}
for (iter = elevators.begin(); iter != elevators.end(); iter++) {
(*iter)->LetThemMove();
}
dual++;
}
Jump to Line
Something went wrong with that request. Please try again.