DYTSrouce/src/ui/Menu/PlayManagerMenu.cpp

165 lines
5.2 KiB
C++

#include "PlayManagerMenu.h"
#include <QMessageBox>
#include "ui/MainFrame.h"
#include "common/SpdLogger.h"
#include "workspace/Timestep.h"
#include "workspace/WorkSpaceManager.h"
#include "ui_PlayManagerMenu.h"
PlayManagerMenu::PlayManagerMenu(QWidget* parent)
: QWidget(parent)
, ui(new Ui::PlayManagerMenu) {
ui->setupUi(this);
connect(&WorkSpaceManager::Get(), &WorkSpaceManager::WorkSpaceChanged,
this, &PlayManagerMenu::OnWorkspaceChange);
connect(ui->psbPlay, &QPushButton::clicked, this, &PlayManagerMenu::OnPlay);
connect(ui->psbStop, &QPushButton::clicked, this, &PlayManagerMenu::OnStop);
connect(ui->psbUp, &QPushButton::clicked, this, &PlayManagerMenu::OnUp);
connect(ui->psbDown, &QPushButton::clicked, this, &PlayManagerMenu::OnDown);
}
PlayManagerMenu::~PlayManagerMenu() {
delete ui;
}
void PlayManagerMenu::OnPlay() {
workSpace_ = WorkSpaceManager::Get().GetCurrent();
if (nullptr == workSpace_) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("has not workspace"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
Timestep* timestep = workSpace_->GetTimestep();
if (nullptr == timestep) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("not timestep"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
if (timestep->IsStoped()) {
timestep->Start();
ui->psbPlay->setText(tr("pause"));
}
else if (timestep->IsPause()) {
timestep->Resume();
ui->psbPlay->setText(tr("pause"));
} else {
timestep->Pause();
ui->psbPlay->setText(tr("play"));
}
}
void PlayManagerMenu::OnStop() {
workSpace_ = WorkSpaceManager::Get().GetCurrent();
if (nullptr == workSpace_) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("has not workspace"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
Timestep* timestep = workSpace_->GetTimestep();
if (nullptr == timestep) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("has not workspace"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
timestep->Stop();
}
void PlayManagerMenu::OnUp() {
workSpace_ = WorkSpaceManager::Get().GetCurrent();
if (nullptr == workSpace_) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("has not workspace"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
Timestep* timestep = workSpace_->GetTimestep();
if (nullptr == timestep) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("has not workspace"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
timestep->Up();
}
void PlayManagerMenu::OnDown() {
workSpace_ = WorkSpaceManager::Get().GetCurrent();
if (nullptr == workSpace_) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("has not workspace"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
Timestep* timestep = workSpace_->GetTimestep();
if (nullptr == timestep) {
QMessageBox::warning(&MainFrame::Get(), tr("warning"), tr("has not workspace"),
QMessageBox::Ok);
LOG_INFO("current is nullptr");
return;
}
timestep->Down();
}
void PlayManagerMenu::OnWorkspaceChange(WorkSpace* workSpace) {
if (nullptr == workSpace) {
LOG_WARN("current is nullptr");
return;
}
workSpace_ = workSpace;
connect(workSpace, &WorkSpace::TimestepChanged, this, &PlayManagerMenu::OnTimestepChanged);
}
void PlayManagerMenu::OnTimestepChanged(Timestep* timestep) {
double step = 0.0;
timestep->GetRange(minTime_, maxTime_, step);
ui->lbtime->setText(QString::number(minTime_, 'f', 3));
ui->lbtimeTotal->setText(QString("/%1(s)").arg(maxTime_));
ui->lbUp->setText(QString("x%1").arg(step));
ui->horizontalSlider->setRange((int)(minTime_ * 1000), (int)(maxTime_ * 1000));
connect(timestep, &Timestep::TimeChanged, [this](double dt) {
ui->lbtime->setText(QString::number(dt, 'f', 3));
dt *= 1000;
ui->horizontalSlider->setValue(int(dt));
}
);
connect(timestep, &Timestep::StepChanged, [this](double step) {
ui->lbUp->setText(QString("x%1").arg(step));
}
);
connect(timestep, &Timestep::StatusChanged, [this](int statue) {
Timestep::PlayStatus state = static_cast<Timestep::PlayStatus>(statue);
switch (state) {
case Timestep::PlayStatus::PS_Started:
ui->psbPlay->setText(tr("pause"));
break;
case Timestep::PlayStatus::PS_Stoped: {
ui->psbPlay->setText(tr("play"));
ui->lbtime->setText(QString::number(minTime_, 'f', 3));
ui->lbtimeTotal->setText(QString("/%1(s)").arg(maxTime_));
ui->horizontalSlider->setValue(0);
}
break;
case Timestep::PlayStatus::PS_Suspended:
ui->psbPlay->setText(tr("play"));
break;
default:
break;
}
}
);
}