You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.8 KiB
C++
47 lines
1.8 KiB
C++
#include "nmVolumeImportTask.h"
|
|
#include "nmVolumeDataImporter.h"
|
|
#include "nmVolumeDataBuilder.h"
|
|
|
|
nmVolumeImportTask::nmVolumeImportTask(SourceType eSourceType, const QString& sFilePath, QObject* pParent)
|
|
: QThread(pParent), m_eSourceType(eSourceType), m_sFilePath(sFilePath),
|
|
m_nCancelRequested(0), m_nLastProgress(-1)
|
|
{
|
|
}
|
|
nmVolumeImportTask::~nmVolumeImportTask()
|
|
{
|
|
// 析构是最后一道生命周期保护,不允许销毁仍在执行 VTK 代码的线程。
|
|
requestCancel();
|
|
wait();
|
|
}
|
|
void nmVolumeImportTask::requestCancel() { m_nCancelRequested.fetchAndStoreRelease(1); }
|
|
bool nmVolumeImportTask::isCanceled() const { return m_nCancelRequested.fetchAndAddAcquire(0) != 0; }
|
|
void nmVolumeImportTask::reportProgress(int nPercent)
|
|
{
|
|
nPercent = qBound(0, nPercent, 100);
|
|
if(nPercent > m_nLastProgress) {
|
|
m_nLastProgress = nPercent;
|
|
emit progressChanged(nPercent);
|
|
}
|
|
}
|
|
void nmVolumeImportTask::run()
|
|
{
|
|
try {
|
|
// 两种来源都只发布只读候选,场景切换仍由 GUI 在线程结束后统一处理。
|
|
if(m_eSourceType == SliceTestModelSource) {
|
|
m_pResult = nmVolumeDataBuilder::buildSliceTestModel(&m_sError, this);
|
|
} else {
|
|
m_pResult = nmVolumeDataImporter::loadFile(m_sFilePath, &m_sError, this);
|
|
}
|
|
} catch(...) {
|
|
// C++ 异常不得越过 QThread::run 边界,否则一次加载可能终止整个进程。
|
|
m_pResult.clear();
|
|
m_sError = tr("An unexpected error occurred while loading the volume mesh.");
|
|
}
|
|
if(isCanceled()) m_pResult.clear();
|
|
}
|
|
QSharedPointer<const nmVolumeData> nmVolumeImportTask::getResult() const
|
|
{
|
|
return isFinished() && !isCanceled() ? m_pResult : QSharedPointer<const nmVolumeData>();
|
|
}
|
|
QString nmVolumeImportTask::getError() const { return isFinished() ? m_sError : QString(); }
|