diff --git a/src/ui/BlueprintPanel.cpp b/src/ui/BlueprintPanel.cpp index 1b5f2bd..8b57cea 100644 --- a/src/ui/BlueprintPanel.cpp +++ b/src/ui/BlueprintPanel.cpp @@ -53,23 +53,15 @@ BlueprintPanel::BlueprintPanel(Simulation* sim, const GameConfig* config, QWidge connect(m_createBtn, &QPushButton::clicked, this, &BlueprintPanel::onCreateClicked); - QHBoxLayout* ioLayout = new QHBoxLayout(); - m_saveBtn = new QPushButton(tr("Save"), this); - m_loadBtn = new QPushButton(tr("Load"), this); - m_saveBtn->setFixedHeight(36); - m_loadBtn->setFixedHeight(36); - ioLayout->addWidget(m_saveBtn); - ioLayout->addWidget(m_loadBtn); - layout->addLayout(ioLayout); - - connect(m_saveBtn, &QPushButton::clicked, this, &BlueprintPanel::onSaveClicked); - connect(m_loadBtn, &QPushButton::clicked, this, &BlueprintPanel::onLoadClicked); + loadFromDisk(); + rebuildButtons(); registerForEvents(); } BlueprintPanel::~BlueprintPanel() { + saveToDisk(); unregisterForEvents(); } @@ -281,65 +273,35 @@ void BlueprintPanel::rebuildButtons() refreshButtonStates(); } -void BlueprintPanel::onSaveClicked() +void BlueprintPanel::saveToDisk() const { + // Persist on shutdown; write errors are silently ignored (REQ-UI-BLUEPRINT-SAVE). const QString path = QCoreApplication::applicationDirPath() + "/blueprints.toml"; + QFile file(path); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return; } try { const std::string content = BlueprintSerializer::serialize(m_blueprints); - QFile file(path); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) - { - QMessageBox::critical(this, tr("Save Failed"), - tr("Could not open file for writing:\n%1").arg(path)); - return; - } file.write(QByteArray::fromStdString(content)); } - catch (const std::exception& e) - { - QMessageBox::critical(this, tr("Save Failed"), - tr("Failed to save blueprints:\n%1").arg(e.what())); - } + catch (...) {} } -void BlueprintPanel::onLoadClicked() +void BlueprintPanel::loadFromDisk() { - QMessageBox box(this); - box.setWindowTitle(tr("Load Blueprints")); - box.setText(tr("Load blueprints? This will replace all current blueprints.")); - QPushButton* confirmBtn = box.addButton(tr("Confirm"), QMessageBox::AcceptRole); - box.addButton(tr("Cancel"), QMessageBox::RejectRole); - box.exec(); - if (box.clickedButton() != confirmBtn) { return; } - + // Load at startup (REQ-UI-BLUEPRINT-LOAD). Missing file: start empty, no error. const QString path = QCoreApplication::applicationDirPath() + "/blueprints.toml"; + QFile file(path); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } try { - QFile file(path); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) - { - QMessageBox::critical(this, tr("Load Failed"), - tr("Could not open file:\n%1").arg(path)); - return; - } - const std::string content = file.readAll().toStdString(); - std::vector loaded = BlueprintSerializer::deserialize(content); - - if (m_activeIndex >= 0) - { - EventManager::getInstance()->sendEventImmediately( - std::make_shared()); - m_activeIndex = -1; - } - - m_blueprints = std::move(loaded); - rebuildButtons(); + m_blueprints = BlueprintSerializer::deserialize(file.readAll().toStdString()); } catch (const std::exception& e) { QMessageBox::critical(this, tr("Load Failed"), tr("Failed to load blueprints:\n%1").arg(e.what())); + m_blueprints.clear(); } } diff --git a/src/ui/BlueprintPanel.h b/src/ui/BlueprintPanel.h index 3552c11..bf16846 100644 --- a/src/ui/BlueprintPanel.h +++ b/src/ui/BlueprintPanel.h @@ -38,8 +38,6 @@ private slots: void onCreateClicked(); void onDeleteBlueprintClicked(int index); void onBlueprintButtonClicked(int index); - void onSaveClicked(); - void onLoadClicked(); private: void onSelectionChanged(const std::vector& ids); @@ -48,6 +46,8 @@ private: int computeBlueprintCost(const Blueprint& bp) const; void rebuildButtons(); void refreshButtonStates(); + void loadFromDisk(); + void saveToDisk() const; Simulation* m_sim; const GameConfig* m_config; @@ -57,8 +57,6 @@ private: std::vector m_blueprints; std::vector m_blueprintButtons; QPushButton* m_createBtn; - QPushButton* m_saveBtn; - QPushButton* m_loadBtn; QWidget* m_buttonsContainer; QVBoxLayout* m_buttonsLayout; };