Improved containment of target package widget functionality - it's now less tightly coupled.

This commit is contained in:
Nav
2021-09-02 21:19:46 +01:00
parent 8c08493122
commit 1dc184edbc
17 changed files with 221 additions and 94 deletions

View File

@@ -140,9 +140,10 @@ add_executable(Bloom
# Insight worker tasks
src/Insight/InsightWorker/Tasks/InsightWorkerTask.cpp
src/Insight/InsightWorker/Tasks/ReadTargetRegisters.cpp
src/Insight/InsightWorker/Tasks/RefreshTargetPinStates.cpp
# Target package widgets
src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.hpp
src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPinWidget.hpp
src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPinBodyWidget.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/DIP/DualInlinePackageWidget.cpp

View File

@@ -3,7 +3,6 @@
#include <typeindex>
#include <QTimer>
#include "InsightWorker.hpp"
#include "src/Helpers/Paths.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/InvalidConfig.hpp"
@@ -69,36 +68,31 @@ void Insight::startup() {
* This allows us to use Qt's event loop whilst still being able to process our own events.
*/
auto eventDispatchTimer = new QTimer(&(this->application));
connect(eventDispatchTimer, &QTimer::timeout, this, &Insight::dispatchEvents);
this->connect(eventDispatchTimer, &QTimer::timeout, this, &Insight::dispatchEvents);
eventDispatchTimer->start(100);
this->connect(this->insightWorker, &InsightWorker::targetControllerSuspended, this->mainWindow, &InsightWindow::onTargetControllerSuspended);
this->connect(this->insightWorker, &InsightWorker::targetControllerResumed, this->mainWindow, &InsightWindow::onTargetControllerResumed);
this->connect(this->insightWorker, &InsightWorker::targetStateUpdated, this->mainWindow, &InsightWindow::onTargetStateUpdate);
this->connect(this->insightWorker, &InsightWorker::targetProgramCounterUpdated, this->mainWindow, &InsightWindow::onTargetProgramCounterUpdate);
this->connect(this->insightWorker, &InsightWorker::targetIoPortsUpdated, this->mainWindow, &InsightWindow::onTargetIoPortsUpdate);
this->connect(this->mainWindow, &InsightWindow::refreshTargetPinStates, this->insightWorker, &InsightWorker::requestPinStates);
this->connect(this->mainWindow, &InsightWindow::setTargetPinState, this->insightWorker, &InsightWorker::requestPinStateUpdate);
this->mainWindow->setInsightConfig(this->insightConfig);
this->mainWindow->setEnvironmentConfig(this->environmentConfig);
this->mainWindow->init(targetDescriptor);
// Prepare worker thread
this->workerThread = new QThread();
this->workerThread->setObjectName("IW");
this->insightWorker->moveToThread(this->workerThread);
connect(this->workerThread, &QThread::started, this->insightWorker, &InsightWorker::startup);
connect(this->workerThread, &QThread::finished, this->insightWorker, &QObject::deleteLater);
connect(this->workerThread, &QThread::finished, this->workerThread, &QThread::deleteLater);
this->connect(this->workerThread, &QThread::started, this->insightWorker, &InsightWorker::startup);
this->connect(this->workerThread, &QThread::finished, this->insightWorker, &QObject::deleteLater);
this->connect(this->workerThread, &QThread::finished, this->workerThread, &QThread::deleteLater);
connect(this->insightWorker, &InsightWorker::targetControllerSuspended, &(this->mainWindow), &InsightWindow::onTargetControllerSuspended);
connect(this->insightWorker, &InsightWorker::targetControllerResumed, &(this->mainWindow), &InsightWindow::onTargetControllerResumed);
connect(this->insightWorker, &InsightWorker::targetStateUpdated, &(this->mainWindow), &InsightWindow::onTargetStateUpdate);
connect(this->insightWorker, &InsightWorker::targetProgramCounterUpdated, &(this->mainWindow), &InsightWindow::onTargetProgramCounterUpdate);
connect(this->insightWorker, &InsightWorker::targetPinStatesUpdated, &(this->mainWindow), &InsightWindow::onTargetPinStatesUpdate);
connect(this->insightWorker, &InsightWorker::targetIoPortsUpdated, &(this->mainWindow), &InsightWindow::onTargetIoPortsUpdate);
connect(&(this->mainWindow), &InsightWindow::refreshTargetPinStates, this->insightWorker, &InsightWorker::requestPinStates);
connect(&(this->mainWindow), &InsightWindow::setTargetPinState, this->insightWorker, &InsightWorker::requestPinStateUpdate);
this->mainWindow.setInsightConfig(this->insightConfig);
this->mainWindow.setEnvironmentConfig(this->environmentConfig);
this->mainWindow.init(
this->application,
*(this->insightWorker),
targetDescriptor
);
this->mainWindow.show();
this->mainWindow->show();
}
void Insight::shutdown() {
@@ -107,7 +101,7 @@ void Insight::shutdown() {
}
Logger::info("Shutting down Insight");
this->mainWindow.close();
this->mainWindow->close();
if (this->workerThread != nullptr && this->workerThread->isRunning()) {
this->workerThread->quit();

View File

@@ -4,6 +4,7 @@
#include <QApplication>
#include "src/Helpers/Thread.hpp"
#include "InsightWorker/InsightWorker.hpp"
#include "src/ApplicationConfig.hpp"
#include "src/EventManager/EventManager.hpp"
#include "src/EventManager/EventListener.hpp"
@@ -37,7 +38,7 @@ namespace Bloom
QApplication application;
InsightWorker* insightWorker = new InsightWorker(this->eventManager);
InsightWindow mainWindow;
InsightWindow* mainWindow = new InsightWindow(this->application, *(this->insightWorker));
TargetControllerConsole targetControllerConsole = TargetControllerConsole(
this->eventManager,

View File

@@ -24,8 +24,7 @@ namespace Bloom
public:
InsightWorkerTaskState state;
InsightWorkerTask() = default;
InsightWorkerTask(QObject* parent): QObject(parent) {};
InsightWorkerTask(): QObject(nullptr) {};
void execute(TargetControllerConsole& targetControllerConsole);

View File

@@ -18,8 +18,8 @@ namespace Bloom
void run(TargetControllerConsole& targetControllerConsole) override;
public:
ReadTargetRegisters(const Targets::TargetRegisterDescriptors& descriptors, QObject* parent):
InsightWorkerTask(nullptr), descriptors(descriptors) {}
ReadTargetRegisters(const Targets::TargetRegisterDescriptors& descriptors):
InsightWorkerTask(), descriptors(descriptors) {}
signals:
void targetRegistersRead(Targets::TargetRegisters registers);

View File

@@ -0,0 +1,7 @@
#include "RefreshTargetPinStates.hpp"
using namespace Bloom;
void RefreshTargetPinStates::run(TargetControllerConsole& targetControllerConsole) {
emit this->targetPinStatesRetrieved(targetControllerConsole.getPinStates(this->variantId));
}

View File

@@ -0,0 +1,27 @@
#pragma once
#include <QObject>
#include <QString>
#include "InsightWorkerTask.hpp"
#include "src/Targets/TargetVariant.hpp"
#include "src/Targets/TargetPinDescriptor.hpp"
namespace Bloom
{
class RefreshTargetPinStates: public InsightWorkerTask
{
Q_OBJECT
private:
int variantId;
protected:
void run(TargetControllerConsole& targetControllerConsole) override;
public:
RefreshTargetPinStates(int variantId): InsightWorkerTask(), variantId(variantId) {}
signals:
void targetPinStatesRetrieved(Bloom::Targets::TargetPinStateMappingType pinStatesByNumber);
};
}

View File

@@ -6,10 +6,13 @@
#include "AboutWindow.hpp"
#include "Widgets/TargetWidgets/DIP/DualInlinePackageWidget.hpp"
#include "Widgets/TargetWidgets/QFP/QuadFlatPackageWidget.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp"
#include "src/Targets/TargetDescriptor.hpp"
#include "src/Helpers/Paths.hpp"
#include "src/Targets/TargetDescriptor.hpp"
#include "AboutWindow.hpp"
using namespace Bloom;
using namespace Bloom::Exceptions;
@@ -22,12 +25,10 @@ using Bloom::Targets::TargetVariant;
using Bloom::Targets::TargetPackage;
using Bloom::Targets::TargetPinDescriptor;
void InsightWindow::init(
InsightWindow::InsightWindow(
QApplication& application,
TargetDescriptor targetDescriptor
) {
this->targetDescriptor = std::move(targetDescriptor);
InsightWorker& insightWorker
): QObject(&application), insightWorker(insightWorker) {
auto mainWindowUiFile = QFile(
QString::fromStdString(Paths::compiledResourcesPath()
+ "/src/Insight/UserInterfaces/InsightWindow/UiFiles/InsightWindow.ui"
@@ -85,9 +86,11 @@ void InsightWindow::init(
this->footer = this->mainWindowWidget->findChild<QWidget*>("footer");
this->targetStatusLabel = this->footer->findChild<QLabel*>("target-state");
this->programCounterValueLabel = this->footer->findChild<QLabel*>("target-program-counter-value");
}
void InsightWindow::init(TargetDescriptor targetDescriptor) {
this->targetDescriptor = std::move(targetDescriptor);
this->activate();
/*
* Do not delete svgWidget. It seems like it's absolutely pointless, but it's really not. I know this is gross but
* I don't seem to have any other option.
@@ -316,6 +319,7 @@ void InsightWindow::selectVariant(const TargetVariant* variant) {
this->targetPackageWidget = new InsightTargetWidgets::Dip::DualInlinePackageWidget(
*variant,
this,
this->insightWorker,
this->ioContainerWidget
);
@@ -323,6 +327,7 @@ void InsightWindow::selectVariant(const TargetVariant* variant) {
this->targetPackageWidget = new InsightTargetWidgets::Qfp::QuadFlatPackageWidget(
*variant,
this,
this->insightWorker,
this->ioContainerWidget
);
}
@@ -356,10 +361,6 @@ void InsightWindow::toggleUi(bool disable) {
this->refreshIoInspectionButton->repaint();
}
if (this->ioContainerWidget != nullptr) {
this->ioContainerWidget->setDisabled(disable);
this->ioContainerWidget->repaint();
}
}
void InsightWindow::onTargetControllerSuspended() {
@@ -418,14 +419,10 @@ void InsightWindow::onTargetStateUpdate(TargetState newState) {
if (newState == TargetState::RUNNING) {
this->targetStatusLabel->setText("Running");
this->programCounterValueLabel->setText("-");
this->toggleUi(true);
} else if (newState == TargetState::STOPPED) {
this->targetStatusLabel->setText("Stopped");
if (this->selectedVariant != nullptr) {
emit this->refreshTargetPinStates(this->selectedVariant->id);
}
this->toggleUi(false);
} else {
this->targetStatusLabel->setText("Unknown");
@@ -444,21 +441,6 @@ void InsightWindow::onTargetIoPortsUpdate() {
}
}
void InsightWindow::onTargetPinStatesUpdate(int variantId, Bloom::Targets::TargetPinStateMappingType pinStatesByNumber) {
if (this->targetPackageWidget != nullptr
&& this->selectedVariant != nullptr
&& this->selectedVariant->id == variantId
) {
this->targetPackageWidget->updatePinStates(pinStatesByNumber);
if (this->targetState == TargetState::STOPPED && this->uiDisabled) {
this->toggleUi(false);
} else {
this->targetPackageWidget->repaint();
}
}
}
void InsightWindow::togglePinIoState(InsightTargetWidgets::TargetPinWidget* pinWidget) {
auto pinState = pinWidget->getPinState();

View File

@@ -6,13 +6,16 @@
#include <memory>
#include <optional>
#include "AboutWindow.hpp"
#include "src/ApplicationConfig.hpp"
#include "Widgets/TargetWidgets/TargetPackageWidget.hpp"
#include "src/Insight/InsightWorker/InsightWorker.hpp"
#include "src/Targets/TargetState.hpp"
#include "src/Targets/TargetDescriptor.hpp"
#include "src/Targets/TargetVariant.hpp"
#include "Widgets/TargetWidgets/TargetPackageWidget.hpp"
#include "AboutWindow.hpp"
namespace Bloom
{
class InsightWindow: public QObject
@@ -23,6 +26,8 @@ namespace Bloom
EnvironmentConfig environmentConfig;
TargetConfig targetConfig;
InsightWorker& insightWorker;
bool activated = false;
Targets::TargetDescriptor targetDescriptor;
@@ -57,7 +62,7 @@ namespace Bloom
void deactivate();
public:
InsightWindow() = default;
InsightWindow(QApplication& application, InsightWorker& insightWorker);
void setEnvironmentConfig(const EnvironmentConfig& environmentConfig) {
this->environmentConfig = environmentConfig;
@@ -68,17 +73,13 @@ namespace Bloom
this->insightConfig = insightConfig;
}
void init(
QApplication& application,
Targets::TargetDescriptor targetDescriptor
);
void init(Targets::TargetDescriptor targetDescriptor);
void show();
public slots:
void onTargetControllerSuspended();
void onTargetControllerResumed(const Bloom::Targets::TargetDescriptor& targetDescriptor);
void onTargetPinStatesUpdate(int variantId, Bloom::Targets::TargetPinStateMappingType pinStatesByNumber);
void onTargetStateUpdate(Targets::TargetState newState);
void onTargetProgramCounterUpdate(quint32 programCounter);
void onTargetIoPortsUpdate();

View File

@@ -1,3 +1,5 @@
#include "DualInlinePackageWidget.hpp"
#include <QPainter>
#include <QVBoxLayout>
#include <QHBoxLayout>
@@ -6,7 +8,6 @@
#include <QFile>
#include "../../../InsightWindow.hpp"
#include "DualInlinePackageWidget.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp"
#include "src/Helpers/Paths.hpp"
@@ -21,8 +22,9 @@ using Bloom::Targets::TargetVariant;
DualInlinePackageWidget::DualInlinePackageWidget(
const TargetVariant& targetVariant,
QObject* insightWindowObj,
InsightWorker& insightWorker,
QWidget* parent
): TargetPackageWidget(targetVariant, insightWindowObj, parent) {
): TargetPackageWidget(targetVariant, insightWorker, parent) {
auto stylesheetFile = QFile(QString::fromStdString(
Paths::compiledResourcesPath()
+ "/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/DIP/Stylesheets/DualInlinePackage.qss"

View File

@@ -30,6 +30,11 @@ namespace Bloom::Widgets::InsightTargetWidgets::Dip
void drawWidget(QPainter& painter);
public:
DualInlinePackageWidget(const Targets::TargetVariant& targetVariant, QObject* insightWindowObj, QWidget* parent);
DualInlinePackageWidget(
const Targets::TargetVariant& targetVariant,
QObject* insightWindowObj,
InsightWorker& insightWorker,
QWidget* parent
);
};
}

View File

@@ -1,3 +1,5 @@
#include "QuadFlatPackageWidget.hpp"
#include <QPainter>
#include <QVBoxLayout>
#include <QHBoxLayout>
@@ -6,7 +8,6 @@
#include <QFile>
#include "../../../InsightWindow.hpp"
#include "QuadFlatPackageWidget.hpp"
#include "src/Helpers/Paths.hpp"
#include "PinWidget.hpp"
#include "BodyWidget.hpp"
@@ -17,8 +18,9 @@ using namespace Bloom::Targets;
QuadFlatPackageWidget::QuadFlatPackageWidget(
const TargetVariant& targetVariant,
QObject* insightWindowObj,
InsightWorker& insightWorker,
QWidget* parent
): TargetPackageWidget(targetVariant, insightWindowObj, parent) {
): TargetPackageWidget(targetVariant, insightWorker, parent) {
assert((targetVariant.pinDescriptorsByNumber.size() % 4) == 0);
auto stylesheetFile = QFile(QString::fromStdString(

View File

@@ -32,6 +32,11 @@ namespace Bloom::Widgets::InsightTargetWidgets::Qfp
void drawWidget(QPainter& painter);
public:
QuadFlatPackageWidget(const Targets::TargetVariant& targetVariant, QObject* insightWindowObj, QWidget* parent);
QuadFlatPackageWidget(
const Targets::TargetVariant& targetVariant,
QObject* insightWindowObj,
InsightWorker& insightWorker,
QWidget* parent
);
};
}

View File

@@ -0,0 +1,87 @@
#include "TargetPackageWidget.hpp"
#include <QEvent>
#include "src/Insight/InsightWorker/Tasks/RefreshTargetPinStates.hpp"
using namespace Bloom;
using namespace Bloom::Widgets::InsightTargetWidgets;
using Bloom::Targets::TargetState;
TargetPackageWidget::TargetPackageWidget(
Targets::TargetVariant targetVariant,
InsightWorker& insightWorker,
QWidget* parent
): QWidget(parent), targetVariant(std::move(targetVariant)), insightWorker(insightWorker) {
this->connect(
&(this->insightWorker),
&InsightWorker::targetStateUpdated,
this,
&TargetPackageWidget::onTargetStateChanged
);
this->connect(
&(this->insightWorker),
&InsightWorker::targetPinStatesUpdated,
this,
[this] (int variantId, const Targets::TargetPinStateMappingType& pinStatesByNumber) {
if (variantId == this->targetVariant.id) {
this->updatePinStates(pinStatesByNumber);
if (this->targetState == TargetState::STOPPED && !this->isEnabled()) {
this->setDisabled(false);
}
}
}
);
this->setDisabled(true);
}
void TargetPackageWidget::refreshPinStates(std::optional<std::function<void(void)>> callback) {
auto refreshTask = new RefreshTargetPinStates(this->targetVariant.id);
this->connect(
refreshTask,
&RefreshTargetPinStates::targetPinStatesRetrieved,
this,
&TargetPackageWidget::updatePinStates
);
if (callback.has_value()) {
this->connect(
refreshTask,
&InsightWorkerTask::completed,
this,
callback.value()
);
}
this->insightWorker.queueTask(refreshTask);
}
void TargetPackageWidget::updatePinStates(const Targets::TargetPinStateMappingType& pinStatesByNumber) {
for (auto& pinWidget : this->pinWidgets) {
auto pinNumber = pinWidget->getPinNumber();
if (pinStatesByNumber.contains(pinNumber)) {
pinWidget->updatePinState(pinStatesByNumber.at(pinNumber));
}
}
this->repaint();
}
void TargetPackageWidget::onTargetStateChanged(TargetState newState) {
this->targetState = newState;
if (newState == TargetState::RUNNING) {
this->setDisabled(true);
} else if (newState == TargetState::STOPPED) {
this->refreshPinStates([this] {
if (this->targetState == TargetState::STOPPED) {
this->setDisabled(false);
}
});
}
}

View File

@@ -5,6 +5,8 @@
#include <vector>
#include <map>
#include "src/Insight/InsightWorker/InsightWorker.hpp"
#include "TargetPinWidget.hpp"
#include "src/Targets/TargetVariant.hpp"
#include "src/Targets/TargetDescriptor.hpp"
@@ -19,19 +21,17 @@ namespace Bloom::Widgets::InsightTargetWidgets
Q_OBJECT
protected:
Targets::TargetVariant targetVariant;
InsightWorker& insightWorker;
std::vector<TargetPinWidget*> pinWidgets;
public:
TargetPackageWidget(Targets::TargetVariant targetVariant, QObject* insightWindowObj, QWidget* parent):
QWidget(parent), targetVariant(std::move(targetVariant)) {};
Targets::TargetState targetState = Targets::TargetState::UNKNOWN;
virtual void updatePinStates(std::map<int, Targets::TargetPinState> pinStatesByNumber) {
for (auto& pinWidget : this->pinWidgets) {
auto pinNumber = pinWidget->getPinNumber();
if (pinStatesByNumber.contains(pinNumber)) {
pinWidget->updatePinState(pinStatesByNumber.at(pinNumber));
}
}
}
protected slots:
virtual void updatePinStates(const Targets::TargetPinStateMappingType& pinStatesByNumber);
void onTargetStateChanged(Targets::TargetState newState);
public:
TargetPackageWidget(Targets::TargetVariant targetVariant, InsightWorker& insightWorker, QWidget* parent);
virtual void refreshPinStates(std::optional<std::function<void(void)>> callback = std::nullopt);
};
}

View File

@@ -116,6 +116,13 @@ void TargetControllerConsole::requestPinStates(int variantId) {
this->eventManager.triggerEvent(requestEvent);
}
Targets::TargetPinStateMappingType TargetControllerConsole::getPinStates(int variantId) {
auto requestEvent = std::make_shared<RetrieveTargetPinStates>();
requestEvent->variantId = variantId;
return this->triggerTargetControllerEventAndWaitForResponse(requestEvent)->pinSatesByNumber;
}
void TargetControllerConsole::setPinState(int variantId, TargetPinDescriptor pinDescriptor, TargetPinState pinState) {
auto updateEvent = std::make_shared<SetTargetPinState>();
updateEvent->variantId = variantId;

View File

@@ -182,6 +182,20 @@ namespace Bloom
*/
void removeBreakpoint(Targets::TargetBreakpoint breakpoint);
/**
* Requests a pin state refresh from the TargetController, for a specific target variant.
*
* @param variantId
*/
void requestPinStates(int variantId);
/**
* Retrieves the pin states for a particular target variant.
*
* @param variantId
*/
Targets::TargetPinStateMappingType getPinStates(int variantId);
/**
* Requests a pin state update on the target, for a specific pin.
*
@@ -190,12 +204,5 @@ namespace Bloom
* @param pinState
*/
void setPinState(int variantId, Targets::TargetPinDescriptor pinDescriptor, Targets::TargetPinState pinState);
/**
* Requests a pin state refresh from the TargetController, for a specific target variant.
*
* @param variantId
*/
void requestPinStates(int variantId);
};
}