Files

192 lines
6.4 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#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