initial port for Cevo game in C++ (libs)
This commit is contained in:
commit
8436242032
375 files changed
+46282
No files matched your search
@@ -0,0 +1,306 @@
|
||||
#include "Cevo/FormJobProgress.h"
|
||||
#include "Cevo/Threading.h"
|
||||
#include "ui_FormJobProgress.h"
|
||||
|
||||
#include <QCloseEvent>
|
||||
#include <QCoreApplication>
|
||||
#include <QListWidgetItem>
|
||||
#include <QStyle>
|
||||
#include <QTimer>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
namespace {
|
||||
constexpr int UpdateIntervalMs = 100;
|
||||
constexpr int EstimatedTimeShowThreshold = 4;
|
||||
constexpr int EstimatedTimeShowThresholdTotal = 1;
|
||||
|
||||
QString formatDuration(qint64 milliseconds)
|
||||
{
|
||||
if (milliseconds < 0)
|
||||
milliseconds = 0;
|
||||
const qint64 totalSeconds = milliseconds / 1000;
|
||||
return QStringLiteral("%1:%2:%3")
|
||||
.arg(totalSeconds / 3600, 2, 10, QLatin1Char('0'))
|
||||
.arg((totalSeconds % 3600) / 60, 2, 10, QLatin1Char('0'))
|
||||
.arg(totalSeconds % 60, 2, 10, QLatin1Char('0'));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void JobProgress::setMax(int value)
|
||||
{
|
||||
m_max = std::max(value, 1);
|
||||
if (m_value >= m_max)
|
||||
m_value = m_max;
|
||||
emit changed();
|
||||
}
|
||||
|
||||
void JobProgress::setText(const QString &text)
|
||||
{
|
||||
if (m_text == text)
|
||||
return;
|
||||
m_text = text;
|
||||
emit changed();
|
||||
}
|
||||
|
||||
void JobProgress::setValue(int value)
|
||||
{
|
||||
if (value < m_max && value != m_value) {
|
||||
m_value = value;
|
||||
emit changed();
|
||||
}
|
||||
}
|
||||
|
||||
void JobProgress::increment()
|
||||
{
|
||||
setValue(m_value + 1);
|
||||
}
|
||||
|
||||
void JobProgress::reset()
|
||||
{
|
||||
// Matches the original: silent, does not emit changed().
|
||||
m_value = 0;
|
||||
}
|
||||
|
||||
Job::Job(FormJobProgress *owner) : m_owner(owner)
|
||||
{
|
||||
}
|
||||
|
||||
void Job::addLogItem(const QString &text)
|
||||
{
|
||||
synchronizeToMainThread([this, text] { m_owner->appendLog(text); });
|
||||
}
|
||||
|
||||
FormJobProgress::FormJobProgress(QWidget *parent) : FormBase(parent), ui(new Ui::FormJobProgress)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
m_updateTimer = new QTimer(this);
|
||||
connect(m_updateTimer, &QTimer::timeout, this, &FormJobProgress::onTimerUpdate);
|
||||
reloadJobList();
|
||||
}
|
||||
|
||||
FormJobProgress::~FormJobProgress()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
Job *FormJobProgress::addJob(const QString &title, std::function<void(Job &)> method, bool noThreaded,
|
||||
bool waitFor)
|
||||
{
|
||||
auto job = std::make_unique<Job>(this);
|
||||
job->title = title;
|
||||
job->method = std::move(method);
|
||||
job->noThreaded = noThreaded;
|
||||
job->waitFor = waitFor;
|
||||
job->progress.setMax(100);
|
||||
job->progress.reset();
|
||||
Job *raw = job.get();
|
||||
connect(&raw->progress, &JobProgress::changed, this, [this, raw] {
|
||||
if (raw == m_currentJob)
|
||||
updateProgress();
|
||||
});
|
||||
m_jobs.push_back(std::move(job));
|
||||
reloadJobList();
|
||||
return raw;
|
||||
}
|
||||
|
||||
void FormJobProgress::clear()
|
||||
{
|
||||
m_jobs.clear();
|
||||
m_log.clear();
|
||||
m_currentJob = nullptr;
|
||||
m_currentJobIndex = -1;
|
||||
reloadJobList();
|
||||
}
|
||||
|
||||
void FormJobProgress::start()
|
||||
{
|
||||
m_terminateRequested = false;
|
||||
m_finished = false;
|
||||
setWindowTitle(tr("Please wait..."));
|
||||
|
||||
m_log.clear();
|
||||
ui->memoLog->clear();
|
||||
ui->labelText->clear();
|
||||
ui->labelEstimatedTimePart->setVisible(false);
|
||||
ui->labelEstimatedTimeTotal->setVisible(false);
|
||||
|
||||
m_currentJob = nullptr;
|
||||
m_currentJobIndex = -1;
|
||||
|
||||
if (m_showDelay == 0) {
|
||||
m_updateTimer->start(UpdateIntervalMs);
|
||||
onTimerUpdate();
|
||||
} else {
|
||||
m_updateTimer->start(m_showDelay);
|
||||
}
|
||||
|
||||
m_totalStartTime = QDateTime::currentDateTime();
|
||||
ui->progressBarTotal->setValue(0);
|
||||
ui->progressBarTotal->setVisible(false);
|
||||
|
||||
std::size_t i = 0;
|
||||
while (i < m_jobs.size()) {
|
||||
Job *job = m_jobs[i].get();
|
||||
m_currentJobIndex = static_cast<int>(i);
|
||||
m_currentJob = job;
|
||||
job->startTime = QDateTime::currentDateTime();
|
||||
m_partElapsed.start();
|
||||
ui->labelEstimatedTimePart->setText(tr("Estimated time:"));
|
||||
ui->progressBarPart->setValue(0);
|
||||
ui->progressBarPart->setVisible(false);
|
||||
reloadJobList();
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
try {
|
||||
if (job->noThreaded)
|
||||
job->method(*job);
|
||||
else
|
||||
runInThread([job] { job->method(*job); });
|
||||
} catch (const std::exception &e) {
|
||||
// An uncaught exception on a worker QThread would otherwise
|
||||
// terminate the whole process - caught here and treated like
|
||||
// the original's Terminate-and-stop-iterating path (disclosed
|
||||
// safety fix, not just a literal port of the Pascal try/except).
|
||||
job->resultString = QString::fromUtf8(e.what());
|
||||
job->addLogItem(job->resultString);
|
||||
m_terminateRequested = true;
|
||||
}
|
||||
|
||||
ui->progressBarPart->hide();
|
||||
emit jobFinished(job);
|
||||
if (m_terminateRequested)
|
||||
break;
|
||||
job->endTime = QDateTime::currentDateTime();
|
||||
job->finished = true;
|
||||
++i;
|
||||
}
|
||||
|
||||
m_currentJob = nullptr;
|
||||
m_updateTimer->stop();
|
||||
ui->memoLog->setPlainText(m_log.join(QLatin1Char('\n')));
|
||||
if (m_log.isEmpty() && m_autoClose)
|
||||
hide();
|
||||
if (!isVisible())
|
||||
clear();
|
||||
setWindowTitle(tr("Finished"));
|
||||
m_finished = true;
|
||||
m_currentJobIndex = -1;
|
||||
reloadJobList();
|
||||
}
|
||||
|
||||
void FormJobProgress::stop()
|
||||
{
|
||||
m_terminateRequested = true;
|
||||
for (const auto &job : m_jobs)
|
||||
job->requestTerminate();
|
||||
}
|
||||
|
||||
void FormJobProgress::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
if (m_finished) {
|
||||
event->accept();
|
||||
} else {
|
||||
event->ignore();
|
||||
stop();
|
||||
setWindowTitle(tr("Please wait...") + tr("Termination"));
|
||||
}
|
||||
}
|
||||
|
||||
void FormJobProgress::onTimerUpdate()
|
||||
{
|
||||
updateProgress();
|
||||
if (!isVisible()) {
|
||||
m_updateTimer->setInterval(UpdateIntervalMs);
|
||||
show();
|
||||
}
|
||||
}
|
||||
|
||||
void FormJobProgress::reloadJobList()
|
||||
{
|
||||
ui->listViewJobs->clear();
|
||||
for (int i = 0; i < static_cast<int>(m_jobs.size()); ++i) {
|
||||
Job *job = m_jobs[static_cast<std::size_t>(i)].get();
|
||||
auto *item = new QListWidgetItem(job->title, ui->listViewJobs);
|
||||
if (i == m_currentJobIndex)
|
||||
item->setIcon(style()->standardIcon(QStyle::SP_ArrowRight));
|
||||
else if (job->finished)
|
||||
item->setIcon(style()->standardIcon(QStyle::SP_DialogApplyButton));
|
||||
}
|
||||
updateHeight();
|
||||
}
|
||||
|
||||
void FormJobProgress::updateProgress()
|
||||
{
|
||||
if (!m_currentJob)
|
||||
return;
|
||||
Job &job = *m_currentJob;
|
||||
|
||||
ui->progressBarPart->setMaximum(job.progress.max());
|
||||
ui->progressBarPart->setValue(job.progress.value());
|
||||
ui->labelText->setText(job.progress.text());
|
||||
if (job.progress.value() > 0)
|
||||
ui->progressBarPart->setVisible(true);
|
||||
|
||||
qint64 estimatedPartMs = 0;
|
||||
if (job.progress.value() >= EstimatedTimeShowThreshold) {
|
||||
estimatedPartMs = m_partElapsed.elapsed() * (job.progress.max() - job.progress.value()) / job.progress.value();
|
||||
ui->labelEstimatedTimePart->setText(tr("Estimated time: %1").arg(formatDuration(estimatedPartMs)));
|
||||
ui->labelEstimatedTimePart->setVisible(true);
|
||||
}
|
||||
|
||||
const int totalMax = static_cast<int>(m_jobs.size()) * 100;
|
||||
const int totalValue = m_currentJobIndex * 100 +
|
||||
(job.progress.max() > 0 ? (job.progress.value() * 100 / job.progress.max()) : 0);
|
||||
ui->progressBarTotal->setMaximum(totalMax);
|
||||
ui->progressBarTotal->setValue(totalValue);
|
||||
|
||||
if (totalValue >= EstimatedTimeShowThresholdTotal) {
|
||||
const qint64 elapsedTotalMs = m_totalStartTime.msecsTo(QDateTime::currentDateTime());
|
||||
const qint64 remainingMs = estimatedPartMs +
|
||||
(elapsedTotalMs + estimatedPartMs) * ((static_cast<int>(m_jobs.size()) - 1 - m_currentJobIndex) * 100) /
|
||||
std::max(1, (m_currentJobIndex + 1) * 100);
|
||||
if (remainingMs > 0) {
|
||||
ui->labelEstimatedTimeTotal->setText(tr("Total estimated time: %1").arg(formatDuration(remainingMs)));
|
||||
ui->labelEstimatedTimeTotal->setVisible(true);
|
||||
} else {
|
||||
ui->labelEstimatedTimeTotal->setVisible(false);
|
||||
}
|
||||
}
|
||||
updateHeight();
|
||||
}
|
||||
|
||||
void FormJobProgress::updateHeight()
|
||||
{
|
||||
// Unlike the original's manual pixel-math UpdateHeight(), this just
|
||||
// toggles widget visibility and lets Qt's own layout system (QVBoxLayout
|
||||
// skips hidden widgets automatically) + adjustSize() compute the actual
|
||||
// dialog size - a disclosed simplification, not a literal port.
|
||||
const bool hasJobs = !m_jobs.empty();
|
||||
ui->labelOperation->setVisible(hasJobs);
|
||||
ui->listViewJobs->setVisible(hasJobs);
|
||||
|
||||
const bool running = hasJobs && !m_finished;
|
||||
ui->labelEstimatedTimePart->setVisible(running && ui->labelEstimatedTimePart->isVisible());
|
||||
ui->progressBarPart->setVisible(running && ui->progressBarPart->isVisible());
|
||||
|
||||
const bool runningMultiple = running && m_jobs.size() > 1;
|
||||
ui->labelEstimatedTimeTotal->setVisible(runningMultiple && ui->labelEstimatedTimeTotal->isVisible());
|
||||
ui->progressBarTotal->setVisible(runningMultiple);
|
||||
|
||||
ui->labelText->setVisible(!ui->labelText->text().isEmpty());
|
||||
ui->memoLog->setVisible(!m_log.isEmpty());
|
||||
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
void FormJobProgress::appendLog(const QString &text)
|
||||
{
|
||||
m_log.append(text);
|
||||
ui->memoLog->appendPlainText(text);
|
||||
updateHeight();
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user