From 0fe0c667a187e73f9aca26a8a7701d6d66ac7bc1 Mon Sep 17 00:00:00 2001 From: parax0 Date: Mon, 1 Feb 2016 18:30:09 -0700 Subject: [PATCH] Check for unsaved changes before changing area/world --- src/Editor/CStartWindow.cpp | 59 ++++++++++++++----------- src/Editor/WorldEditor/CWorldEditor.cpp | 38 +++++++++------- src/Editor/WorldEditor/CWorldEditor.h | 1 + 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/Editor/CStartWindow.cpp b/src/Editor/CStartWindow.cpp index 1f9baae1..6c5c7305 100644 --- a/src/Editor/CStartWindow.cpp +++ b/src/Editor/CStartWindow.cpp @@ -33,11 +33,14 @@ void CStartWindow::on_actionOpen_MLVL_triggered() QString WorldFile = QFileDialog::getOpenFileName(this, "Open MLVL", "", "Metroid Prime World (*.MLVL)"); if (WorldFile.isEmpty()) return; - gResCache.SetFolder(TString(WorldFile.toStdString()).GetFileDirectory()); - mpWorld = gResCache.GetResource(WorldFile.toStdString()); - mpWorldEditor->close(); + if (mpWorldEditor->CheckUnsavedChanges()) + { + gResCache.SetFolder(TString(WorldFile.toStdString()).GetFileDirectory()); + mpWorld = gResCache.GetResource(WorldFile.toStdString()); + mpWorldEditor->close(); - FillWorldUI(); + FillWorldUI(); + } } void CStartWindow::FillWorldUI() @@ -156,31 +159,33 @@ void CStartWindow::on_AttachedAreasList_doubleClicked(const QModelIndex &index) void CStartWindow::on_LaunchWorldEditorButton_clicked() { - u64 AreaID = mpWorld->GetAreaResourceID(mSelectedAreaIndex); - TResPtr pArea = gResCache.GetResource(AreaID, "MREA"); - - if (!pArea) + if (mpWorldEditor->CheckUnsavedChanges()) { - QMessageBox::warning(this, "Error", "Couldn't load area!"); - mpWorldEditor->close(); + u64 AreaID = mpWorld->GetAreaResourceID(mSelectedAreaIndex); + TResPtr pArea = gResCache.GetResource(AreaID, "MREA"); + + if (!pArea) + { + QMessageBox::warning(this, "Error", "Couldn't load area!"); + } + + else + { + mpWorld->SetAreaLayerInfo(pArea, mSelectedAreaIndex); + mpWorldEditor->SetArea(mpWorld, pArea, mSelectedAreaIndex); + mpWorldEditor->setWindowModality(Qt::WindowModal); + mpWorldEditor->showMaximized(); + + // Display errors + CErrorLogDialog ErrorDialog(mpWorldEditor); + bool HasErrors = ErrorDialog.GatherErrors(); + + if (HasErrors) + ErrorDialog.exec(); + + gResCache.Clean(); + } } - - else - { - mpWorld->SetAreaLayerInfo(pArea, mSelectedAreaIndex); - mpWorldEditor->SetArea(mpWorld, pArea, mSelectedAreaIndex); - mpWorldEditor->setWindowModality(Qt::WindowModal); - mpWorldEditor->showMaximized(); - - // Display errors - CErrorLogDialog ErrorDialog(mpWorldEditor); - bool HasErrors = ErrorDialog.GatherErrors(); - - if (HasErrors) - ErrorDialog.exec(); - } - - gResCache.Clean(); } void CStartWindow::on_actionLaunch_model_viewer_triggered() diff --git a/src/Editor/WorldEditor/CWorldEditor.cpp b/src/Editor/WorldEditor/CWorldEditor.cpp index 211e1602..e4cc958a 100644 --- a/src/Editor/WorldEditor/CWorldEditor.cpp +++ b/src/Editor/WorldEditor/CWorldEditor.cpp @@ -89,21 +89,7 @@ CWorldEditor::~CWorldEditor() void CWorldEditor::closeEvent(QCloseEvent *pEvent) { - bool ShouldClose = true; - - if (isWindowModified()) - { - int Result = QMessageBox::warning(this, "Save", "You have unsaved changes. Save?", QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel); - - if (Result == QMessageBox::Yes) - ShouldClose = Save(); - - else if (Result == QMessageBox::No) - ShouldClose = true; - - else if (Result == QMessageBox::Cancel) - ShouldClose = false; - } + bool ShouldClose = CheckUnsavedChanges(); if (ShouldClose) { @@ -198,6 +184,28 @@ CGameArea* CWorldEditor::ActiveArea() return mpArea; } +bool CWorldEditor::CheckUnsavedChanges() +{ + // Check whether the user has unsaved changes, return whether it's okay to clear the scene + bool OkToClear = !isWindowModified(); + + if (!OkToClear) + { + int Result = QMessageBox::warning(this, "Save", "You have unsaved changes. Save?", QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel); + + if (Result == QMessageBox::Yes) + OkToClear = Save(); + + else if (Result == QMessageBox::No) + OkToClear = true; + + else if (Result == QMessageBox::Cancel) + OkToClear = false; + } + + return OkToClear; +} + // ************ PUBLIC SLOTS ************ bool CWorldEditor::Save() { diff --git a/src/Editor/WorldEditor/CWorldEditor.h b/src/Editor/WorldEditor/CWorldEditor.h index d5edf4a5..3445c739 100644 --- a/src/Editor/WorldEditor/CWorldEditor.h +++ b/src/Editor/WorldEditor/CWorldEditor.h @@ -44,6 +44,7 @@ public: bool eventFilter(QObject *pObj, QEvent *pEvent); void SetArea(CWorld *pWorld, CGameArea *pArea, u32 AreaIndex); CGameArea* ActiveArea(); + bool CheckUnsavedChanges(); public slots: bool Save();