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,163 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cevo/FormBase.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QElapsedTimer>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class QCloseEvent;
|
||||
class QTimer;
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
class FormJobProgress;
|
||||
}
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
class Job;
|
||||
class FormJobProgress;
|
||||
|
||||
// Equivalent of JobProgressView.pas's TProgress. Safe to update from the
|
||||
// job's worker thread: setters only touch this object's own data (never
|
||||
// read back from another thread) and emit `changed()`, which Qt delivers
|
||||
// as a queued call to FormJobProgress's slot since that object lives on
|
||||
// the main thread - no mutex needed, unlike the original's TCriticalSection.
|
||||
// Push-based: unlike the original (where only SetValue invoked OnChange,
|
||||
// and SetMax/SetText only ever reached the UI via the 100ms poll timer),
|
||||
// all three setters emit `changed()` here for immediate, lag-free updates.
|
||||
class JobProgress : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using QObject::QObject;
|
||||
|
||||
int value() const { return m_value; }
|
||||
int max() const { return m_max; }
|
||||
QString text() const { return m_text; }
|
||||
|
||||
void setMax(int value);
|
||||
void setText(const QString &text);
|
||||
void setValue(int value);
|
||||
void increment();
|
||||
void reset();
|
||||
|
||||
signals:
|
||||
void changed();
|
||||
|
||||
private:
|
||||
int m_value = 0;
|
||||
int m_max = 100;
|
||||
QString m_text;
|
||||
};
|
||||
|
||||
// Equivalent of JobProgressView.pas's TJob.
|
||||
class Job {
|
||||
public:
|
||||
explicit Job(FormJobProgress *owner);
|
||||
|
||||
QString title;
|
||||
std::function<void(Job &)> method;
|
||||
bool noThreaded = false;
|
||||
// Kept for interface fidelity with TJob.WaitFor, which the original
|
||||
// itself never reads anywhere either (write-only field upstream too).
|
||||
bool waitFor = false;
|
||||
bool finished = false;
|
||||
QString resultString;
|
||||
QDateTime startTime;
|
||||
QDateTime endTime;
|
||||
JobProgress progress;
|
||||
|
||||
// Appends to the owning dialog's log, marshalling to the main thread
|
||||
// if called from the job's worker thread (equivalent of TJob.AddLogItem).
|
||||
void addLogItem(const QString &text);
|
||||
|
||||
bool isTerminateRequested() const { return m_terminateRequested; }
|
||||
void requestTerminate() { m_terminateRequested = true; }
|
||||
|
||||
private:
|
||||
FormJobProgress *m_owner;
|
||||
bool m_terminateRequested = false;
|
||||
};
|
||||
|
||||
// Equivalent of JobProgressView.pas's TFormJobProgressView + TJobProgressView
|
||||
// merged into one dialog (Qt's QDialog already is the "component that owns
|
||||
// the form" the original split across two classes for Lazarus-IDE-palette
|
||||
// reasons - RegisterComponents('Common', [TJobProgressView]) has no Qt
|
||||
// equivalent). NOT ported: the OwnerDraw/OnOwnerDraw hook (lets a caller
|
||||
// suppress the dialog and draw its own progress UI instead) - grepping the
|
||||
// whole perso/cevo tree found no actual consumer of TJobProgressView at
|
||||
// all, so this is dead infrastructure; drop it and simply don't call
|
||||
// start() if you want fully custom progress UI.
|
||||
class FormJobProgress : public FormBase {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit FormJobProgress(QWidget *parent = nullptr);
|
||||
~FormJobProgress() override;
|
||||
|
||||
bool autoClose() const { return m_autoClose; }
|
||||
void setAutoClose(bool value) { m_autoClose = value; }
|
||||
|
||||
// Delay, in ms, before the dialog actually shows itself once start() is
|
||||
// running (0 shows immediately). Equivalent of TJobProgressView.ShowDelay.
|
||||
int showDelay() const { return m_showDelay; }
|
||||
void setShowDelay(int value) { m_showDelay = value; }
|
||||
|
||||
// Equivalent of TJobProgressView.AddJob. Ownership stays with this
|
||||
// dialog; the returned pointer stays valid until clear() or destruction.
|
||||
Job *addJob(const QString &title, std::function<void(Job &)> method, bool noThreaded = false,
|
||||
bool waitFor = false);
|
||||
|
||||
void clear();
|
||||
|
||||
// Equivalent of TJobProgressView.Start: runs every added job in order,
|
||||
// blocking the caller until all jobs finish or stop() is called, while
|
||||
// pumping the Qt event loop (via Threading.h's runInThread()) so the
|
||||
// dialog stays responsive - matches the original's
|
||||
// Application.ProcessMessages-pumping wait loop.
|
||||
void start();
|
||||
|
||||
// Equivalent of TJobProgressView.Stop / the Terminate property setter:
|
||||
// cooperatively asks the current (and all pending) jobs to stop.
|
||||
void stop();
|
||||
bool isTerminateRequested() const { return m_terminateRequested; }
|
||||
|
||||
signals:
|
||||
void jobFinished(Job *job);
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void onTimerUpdate();
|
||||
|
||||
private:
|
||||
void reloadJobList();
|
||||
void updateProgress();
|
||||
void updateHeight();
|
||||
void appendLog(const QString &text);
|
||||
|
||||
friend class Job;
|
||||
|
||||
Ui::FormJobProgress *ui;
|
||||
QTimer *m_updateTimer;
|
||||
std::vector<std::unique_ptr<Job>> m_jobs;
|
||||
int m_currentJobIndex = -1;
|
||||
Job *m_currentJob = nullptr;
|
||||
bool m_finished = false;
|
||||
bool m_autoClose = true;
|
||||
int m_showDelay = 0;
|
||||
bool m_terminateRequested = false;
|
||||
QDateTime m_totalStartTime;
|
||||
QElapsedTimer m_partElapsed;
|
||||
QStringList m_log;
|
||||
};
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user