diff --git a/src/Insight/InsightWorker/Tasks/CaptureMemorySnapshot.cpp b/src/Insight/InsightWorker/Tasks/CaptureMemorySnapshot.cpp index a6b6b55d..f6cdfd35 100644 --- a/src/Insight/InsightWorker/Tasks/CaptureMemorySnapshot.cpp +++ b/src/Insight/InsightWorker/Tasks/CaptureMemorySnapshot.cpp @@ -68,7 +68,7 @@ namespace Bloom ); std::move(dataSegment.begin(), dataSegment.end(), std::back_inserter(*this->data)); - emit this->progressUpdate(static_cast( + this->setProgressPercentage(static_cast( (static_cast(i) + 1) / (static_cast(readsRequired + 1) / 100) )); } diff --git a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp index c5c9e5f8..45712f95 100644 --- a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp +++ b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp @@ -14,8 +14,11 @@ namespace Bloom try { this->state = InsightWorkerTaskState::STARTED; emit this->started(); + this->run(targetControllerService); + this->state = InsightWorkerTaskState::COMPLETED; + this->setProgressPercentage(100); emit this->completed(); } catch (std::exception& exception) { @@ -26,4 +29,9 @@ namespace Bloom emit this->finished(); } + + void InsightWorkerTask::setProgressPercentage(std::uint8_t percentage) { + this->progressPercentage = percentage; + emit this->progressUpdate(this->progressPercentage); + } } diff --git a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp index 92d9e361..c24679ae 100644 --- a/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp +++ b/src/Insight/InsightWorker/Tasks/InsightWorkerTask.hpp @@ -18,6 +18,9 @@ namespace Bloom COMPLETED, }; + static_assert(std::atomic::is_always_lock_free); + static_assert(std::atomic::is_always_lock_free); + class InsightWorkerTask: public QObject { Q_OBJECT @@ -25,7 +28,8 @@ namespace Bloom public: using IdType = std::uint64_t; const InsightWorkerTask::IdType id = ++(InsightWorkerTask::lastId); - InsightWorkerTaskState state = InsightWorkerTaskState::CREATED; + std::atomic state = InsightWorkerTaskState::CREATED; + std::atomic progressPercentage = 0; InsightWorkerTask(); @@ -55,7 +59,7 @@ namespace Bloom * @param progressPercentage * The task's current progress. */ - void progressUpdate(int progressPercentage); + void progressUpdate(std::uint8_t progressPercentage); /** * The InsightWorkerTask::completed() signal will be emitted once the task has successfully completed. @@ -76,6 +80,7 @@ namespace Bloom protected: virtual void run(Services::TargetControllerService& targetControllerService) = 0; + void setProgressPercentage(std::uint8_t percentage); private: static inline std::atomic lastId = 0; diff --git a/src/Insight/InsightWorker/Tasks/ReadTargetMemory.cpp b/src/Insight/InsightWorker/Tasks/ReadTargetMemory.cpp index 927622f7..70a368dc 100644 --- a/src/Insight/InsightWorker/Tasks/ReadTargetMemory.cpp +++ b/src/Insight/InsightWorker/Tasks/ReadTargetMemory.cpp @@ -49,7 +49,7 @@ namespace Bloom ); std::move(dataSegment.begin(), dataSegment.end(), std::back_inserter(data)); - emit this->progressUpdate(static_cast( + this->setProgressPercentage(static_cast( (static_cast(i) + 1) / (static_cast(readsRequired) / 100) )); } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.cpp index 4e0e95b3..c33c2fa3 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.cpp @@ -20,16 +20,23 @@ namespace Bloom::Widgets QObject::connect( this->task.get(), - &InsightWorkerTask::progressUpdate, + &InsightWorkerTask::started, this, - &TaskProgressIndicator::onTaskProgressUpdate + &TaskProgressIndicator::onTaskStateChanged + ); + + QObject::connect( + this->task.get(), + &InsightWorkerTask::finished, + this, + &TaskProgressIndicator::onTaskStateChanged ); QObject::connect( this->task.get(), &InsightWorkerTask::failed, this, - &TaskProgressIndicator::onTaskFailed + &TaskProgressIndicator::onTaskStateChanged ); QObject::connect( @@ -38,6 +45,13 @@ namespace Bloom::Widgets this, &TaskProgressIndicator::onTaskFinished ); + + QObject::connect( + this->task.get(), + &InsightWorkerTask::progressUpdate, + this, + &TaskProgressIndicator::onTaskProgressUpdate + ); } void TaskProgressIndicator::paintEvent(QPaintEvent* event) { @@ -56,12 +70,20 @@ namespace Bloom::Widgets static constexpr auto barHeight = 3; const auto barYPosition = size.height() - barHeight - 3; - const auto percentage = std::max(this->taskProgressPercentage, 2); + const auto percentage = std::max(static_cast(this->task->progressPercentage), 2); + + const auto status = QString( + this->task->state == InsightWorkerTaskState::FAILED + ? " - Failed" + : this->task->state == InsightWorkerTaskState::COMPLETED + ? " - Completed" + : "" + ); painter.drawText( 0, barYPosition - 5, - this->task->brief() + QString(this->taskFailed ? " - Failed" : (percentage == 100 ? " - Completed" : "")) + this->task->brief() + status ); painter.setPen(Qt::PenStyle::NoPen); @@ -84,20 +106,15 @@ namespace Bloom::Widgets ); } - void TaskProgressIndicator::onTaskProgressUpdate(int taskProgressPercentage) { - this->taskProgressPercentage = taskProgressPercentage; + void TaskProgressIndicator::onTaskProgressUpdate() { this->update(); } - void TaskProgressIndicator::onTaskFailed() { - this->taskFailed = true; + void TaskProgressIndicator::onTaskStateChanged() { this->update(); } void TaskProgressIndicator::onTaskFinished() { - this->taskProgressPercentage = 100; - this->update(); - auto* finishedSignalTimer = new QTimer(); finishedSignalTimer->setSingleShot(true); finishedSignalTimer->setInterval(2500); diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.hpp index 6dd794f3..94134936 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TaskProgressIndicator/TaskProgressIndicator.hpp @@ -25,11 +25,9 @@ namespace Bloom::Widgets private: QSharedPointer task; - int taskProgressPercentage = 0; - bool taskFailed = false; - void onTaskProgressUpdate(int progressPercentage); - void onTaskFailed(); + void onTaskProgressUpdate(); + void onTaskStateChanged(); void onTaskFinished(); }; }