Files
BloomPatched/src/ProjectSettings.cpp

361 lines
14 KiB
C++
Raw Normal View History

#include "ProjectSettings.hpp"
#include <QJsonArray>
#include "src/Helpers/EnumToStringMappings.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp"
namespace Bloom
{
ProjectSettings::ProjectSettings(const QJsonObject& jsonObject) {
2023-05-10 19:53:39 +01:00
#ifndef EXCLUDE_INSIGHT
if (jsonObject.contains("insight")) {
this->insightSettings = InsightProjectSettings(jsonObject.find("insight")->toObject());
}
2023-05-10 19:53:39 +01:00
#endif
}
QJsonObject ProjectSettings::toJson() const {
auto projectSettingsObj = QJsonObject();
2023-05-10 19:53:39 +01:00
#ifndef EXCLUDE_INSIGHT
projectSettingsObj.insert("insight", this->insightSettings.toJson());
2023-05-10 19:53:39 +01:00
#endif
return projectSettingsObj;
}
2023-05-10 19:53:39 +01:00
#ifndef EXCLUDE_INSIGHT
InsightProjectSettings::InsightProjectSettings(const QJsonObject& jsonObject) {
if (jsonObject.contains("mainWindowSize")) {
const auto mainWindowSizeObj = jsonObject.find("mainWindowSize")->toObject();
if (mainWindowSizeObj.contains("width") && mainWindowSizeObj.contains("height")) {
this->mainWindowSize = QSize(
mainWindowSizeObj.find("width")->toInt(),
mainWindowSizeObj.find("height")->toInt()
);
}
}
if (jsonObject.contains("leftPanelState")) {
this->leftPanelState = this->panelStateFromJson(
jsonObject.find("leftPanelState")->toObject()
2022-02-06 20:28:46 +00:00
);
}
if (jsonObject.contains("bottomPanelState")) {
this->bottomPanelState = this->panelStateFromJson(
jsonObject.find("bottomPanelState")->toObject()
2022-02-06 20:28:46 +00:00
);
}
if (jsonObject.contains("registersPaneState")) {
this->registersPaneState = this->paneStateFromJson(
jsonObject.find("registersPaneState")->toObject()
2022-02-06 20:28:46 +00:00
);
}
if (jsonObject.contains("ramInspectionPaneState")) {
this->ramInspectionPaneState = this->paneStateFromJson(
jsonObject.find("ramInspectionPaneState")->toObject()
2022-02-06 20:28:46 +00:00
);
}
if (jsonObject.contains("eepromInspectionPaneState")) {
this->eepromInspectionPaneState = this->paneStateFromJson(
jsonObject.find("eepromInspectionPaneState")->toObject()
2022-02-06 20:28:46 +00:00
);
}
if (jsonObject.contains("flashInspectionPaneState")) {
this->flashInspectionPaneState = this->paneStateFromJson(
jsonObject.find("flashInspectionPaneState")->toObject()
);
}
if (jsonObject.contains("memoryInspectionPaneSettings")) {
const auto settingsMappingObj = jsonObject.find("memoryInspectionPaneSettings")->toObject();
for (auto settingsIt = settingsMappingObj.begin(); settingsIt != settingsMappingObj.end(); settingsIt++) {
const auto settingsObj = settingsIt.value().toObject();
const auto memoryTypeName = settingsIt.key();
if (!EnumToStringMappings::targetMemoryTypes.contains(memoryTypeName)) {
continue;
}
2022-02-06 20:28:46 +00:00
this->memoryInspectionPaneSettingsByMemoryType.insert(std::pair(
EnumToStringMappings::targetMemoryTypes.at(memoryTypeName),
2022-02-06 20:28:46 +00:00
this->memoryInspectionPaneSettingsFromJson(settingsObj)
));
}
}
}
QJsonObject InsightProjectSettings::toJson() const {
auto insightObj = QJsonObject();
if (this->mainWindowSize.has_value()) {
insightObj.insert("mainWindowSize", QJsonObject({
{"width", this->mainWindowSize->width()},
{"height", this->mainWindowSize->height()},
}));
}
auto memoryInspectionPaneSettingsObj = QJsonObject();
for (const auto& [memoryType, inspectionPaneSettings] : this->memoryInspectionPaneSettingsByMemoryType) {
if (!EnumToStringMappings::targetMemoryTypes.contains(memoryType)) {
// This is just a precaution - all known memory types should be in the mapping.
continue;
}
2022-02-06 20:28:46 +00:00
memoryInspectionPaneSettingsObj.insert(
EnumToStringMappings::targetMemoryTypes.at(memoryType),
2022-02-06 20:28:46 +00:00
this->memoryInspectionPaneSettingsToJson(inspectionPaneSettings)
);
}
2022-02-06 20:28:46 +00:00
insightObj.insert("memoryInspectionPaneSettings", memoryInspectionPaneSettingsObj);
if (this->leftPanelState.has_value()) {
2022-02-06 20:28:46 +00:00
insightObj.insert(
"leftPanelState",
this->panelStateToJson(this->leftPanelState.value())
2022-02-06 20:28:46 +00:00
);
}
if (this->bottomPanelState.has_value()) {
2022-02-06 20:28:46 +00:00
insightObj.insert(
"bottomPanelState",
this->panelStateToJson(this->bottomPanelState.value())
2022-02-06 20:28:46 +00:00
);
}
if (this->registersPaneState.has_value()) {
2022-02-06 20:28:46 +00:00
insightObj.insert(
"registersPaneState",
this->paneStateToJson(this->registersPaneState.value())
2022-02-06 20:28:46 +00:00
);
}
if (this->ramInspectionPaneState.has_value()) {
2022-02-06 20:28:46 +00:00
insightObj.insert(
"ramInspectionPaneState",
this->paneStateToJson(this->ramInspectionPaneState.value())
2022-02-06 20:28:46 +00:00
);
}
if (this->eepromInspectionPaneState.has_value()) {
2022-02-06 20:28:46 +00:00
insightObj.insert(
"eepromInspectionPaneState",
this->paneStateToJson(this->eepromInspectionPaneState.value())
2022-02-06 20:28:46 +00:00
);
}
if (this->flashInspectionPaneState.has_value()) {
insightObj.insert(
"flashInspectionPaneState",
this->paneStateToJson(this->flashInspectionPaneState.value())
);
}
2022-02-06 20:28:46 +00:00
return insightObj;
}
Widgets::TargetMemoryInspectionPaneSettings InsightProjectSettings::memoryInspectionPaneSettingsFromJson(
const QJsonObject& jsonObject
) const {
using Exceptions::Exception;
2022-02-06 20:28:46 +00:00
auto inspectionPaneSettings = Widgets::TargetMemoryInspectionPaneSettings();
if (jsonObject.contains("refreshOnTargetStop")) {
inspectionPaneSettings.refreshOnTargetStop = jsonObject.value("refreshOnTargetStop").toBool();
}
if (jsonObject.contains("refreshOnActivation")) {
inspectionPaneSettings.refreshOnActivation = jsonObject.value("refreshOnActivation").toBool();
}
2022-02-06 20:28:46 +00:00
if (jsonObject.contains("hexViewerSettings")) {
auto& hexViewerSettings = inspectionPaneSettings.hexViewerWidgetSettings;
const auto hexViewerSettingsObj = jsonObject.find("hexViewerSettings")->toObject();
if (hexViewerSettingsObj.contains("groupStackMemory")) {
hexViewerSettings.groupStackMemory =
hexViewerSettingsObj.value("groupStackMemory").toBool();
2022-02-06 20:28:46 +00:00
}
if (hexViewerSettingsObj.contains("highlightFocusedMemory")) {
hexViewerSettings.highlightFocusedMemory =
hexViewerSettingsObj.value("highlightFocusedMemory").toBool();
}
if (hexViewerSettingsObj.contains("highlightHoveredRowAndCol")) {
hexViewerSettings.highlightHoveredRowAndCol =
hexViewerSettingsObj.value("highlightHoveredRowAndCol").toBool();
}
if (hexViewerSettingsObj.contains("displayAsciiValues")) {
hexViewerSettings.displayAsciiValues =
hexViewerSettingsObj.value("displayAsciiValues").toBool();
}
2022-02-06 20:28:46 +00:00
if (hexViewerSettingsObj.contains("displayAnnotations")) {
hexViewerSettings.displayAnnotations =
hexViewerSettingsObj.value("displayAnnotations").toBool();
}
if (hexViewerSettingsObj.contains("addressLabelType")) {
hexViewerSettings.addressLabelType = InsightProjectSettings::addressTypesByName.valueAt(
hexViewerSettingsObj.value("addressLabelType").toString()
).value_or(hexViewerSettings.addressLabelType);
}
2022-02-06 20:28:46 +00:00
}
if (jsonObject.contains("focusedRegions")) {
for (const auto& regionValue : jsonObject.find("focusedRegions")->toArray()) {
try {
2023-03-27 21:33:28 +01:00
inspectionPaneSettings.focusedMemoryRegions.emplace_back(regionValue.toObject());
} catch (Exception exception) {
Logger::warning(
"Failed to parse focused memory region from project settings file - "
+ exception.getMessage() + " - region will be ignored."
);
2022-02-06 20:28:46 +00:00
}
}
2022-02-06 20:28:46 +00:00
}
if (jsonObject.contains("excludedRegions")) {
for (const auto& regionValue : jsonObject.find("excludedRegions")->toArray()) {
try {
2023-03-27 21:33:28 +01:00
inspectionPaneSettings.excludedMemoryRegions.emplace_back(regionValue.toObject());
} catch (Exception exception) {
Logger::warning(
"Failed to parse excluded memory region from project settings file - "
+ exception.getMessage() + " - region will be ignored."
);
}
}
2022-02-06 20:28:46 +00:00
}
2022-02-06 20:28:46 +00:00
return inspectionPaneSettings;
}
Widgets::PanelState InsightProjectSettings::panelStateFromJson(const QJsonObject& jsonObject) const {
return Widgets::PanelState(
(jsonObject.contains("size") ? static_cast<int>(jsonObject.value("size").toInteger()) : 0),
(jsonObject.contains("open") ? jsonObject.value("open").toBool() : false)
);
}
Widgets::PaneState InsightProjectSettings::paneStateFromJson(const QJsonObject& jsonObject) const {
auto detachedWindowState = std::optional<Widgets::DetachedWindowState>(std::nullopt);
if (jsonObject.contains("detachedWindowState")) {
detachedWindowState = Widgets::DetachedWindowState();
const auto detachedWindowStateObject = jsonObject.value("detachedWindowState").toObject();
if (detachedWindowStateObject.contains("size")) {
const auto sizeObject = detachedWindowStateObject.value("size").toObject();
detachedWindowState->size = QSize(
sizeObject.value("width").toInt(0),
sizeObject.value("height").toInt(0)
);
}
if (detachedWindowStateObject.contains("position")) {
const auto positionObject = detachedWindowStateObject.value("position").toObject();
detachedWindowState->position = QPoint(
positionObject.value("x").toInt(0),
positionObject.value("y").toInt(0)
);
}
}
2022-02-06 20:28:46 +00:00
return Widgets::PaneState(
(jsonObject.contains("activated") ? jsonObject.value("activated").toBool() : false),
(jsonObject.contains("attached") ? jsonObject.value("attached").toBool() : true),
detachedWindowState
2022-02-06 20:28:46 +00:00
);
}
QJsonObject InsightProjectSettings::memoryInspectionPaneSettingsToJson(
const Widgets::TargetMemoryInspectionPaneSettings& inspectionPaneSettings
) const {
const auto& addressTypesByName = InsightProjectSettings::addressTypesByName;
auto settingsObj = QJsonObject({
{"refreshOnTargetStop", inspectionPaneSettings.refreshOnTargetStop},
{"refreshOnActivation", inspectionPaneSettings.refreshOnActivation},
});
2022-02-06 20:28:46 +00:00
const auto& hexViewerSettings = inspectionPaneSettings.hexViewerWidgetSettings;
settingsObj.insert("hexViewerSettings", QJsonObject({
{"groupStackMemory", hexViewerSettings.groupStackMemory},
2022-02-06 20:28:46 +00:00
{"highlightFocusedMemory", hexViewerSettings.highlightFocusedMemory},
{"highlightHoveredRowAndCol", hexViewerSettings.highlightHoveredRowAndCol},
{"displayAsciiValues", hexViewerSettings.displayAsciiValues},
{"displayAnnotations", hexViewerSettings.displayAnnotations},
{"addressLabelType", addressTypesByName.valueAt(hexViewerSettings.addressLabelType).value()},
2022-02-06 20:28:46 +00:00
}));
auto focusedRegions = QJsonArray();
for (const auto& focusedRegion : inspectionPaneSettings.focusedMemoryRegions) {
focusedRegions.push_back(focusedRegion.toJson());
}
2022-02-06 20:28:46 +00:00
auto excludedRegions = QJsonArray();
for (const auto& excludedRegion : inspectionPaneSettings.excludedMemoryRegions) {
excludedRegions.push_back(excludedRegion.toJson());
2022-02-06 20:28:46 +00:00
}
settingsObj.insert("focusedRegions", focusedRegions);
settingsObj.insert("excludedRegions", excludedRegions);
return settingsObj;
}
QJsonObject InsightProjectSettings::panelStateToJson(const Widgets::PanelState& panelState) const {
return QJsonObject({
{"size", panelState.size},
{"open", panelState.open},
});
}
QJsonObject InsightProjectSettings::paneStateToJson(const Widgets::PaneState& paneState) const {
auto json = QJsonObject({
2022-02-06 20:28:46 +00:00
{"activated", paneState.activated},
{"attached", paneState.attached},
2022-02-06 20:28:46 +00:00
});
if (paneState.detachedWindowState.has_value()) {
json.insert("detachedWindowState", QJsonObject({
{
"size",
QJsonObject({
{"width", paneState.detachedWindowState->size.width()},
{"height", paneState.detachedWindowState->size.height()},
})
},
{
"position",
QJsonObject({
{"x", paneState.detachedWindowState->position.x()},
{"y", paneState.detachedWindowState->position.y()},
})
}
}));
}
return json;
}
2023-05-10 19:53:39 +01:00
#endif
}