Skip to content

Commit

Permalink
Merge pull request #147 from Goldensunboy/gsb
Browse files Browse the repository at this point in the history
Fix cancelling save prompt closes editor, and check for unsaved changes on loading new ROM
  • Loading branch information
shinespeciall authored Feb 7, 2019
2 parents 57dd4c0 + 3e99ca5 commit 5371698
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ui_*
*.autosave
WL4Editor
TEST/LoadROMFileTEST.cpp
WL4Editor.pro.user*

# Visual Studio user files
Backup/
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ beta-7:
- Fix moving Entities via direction keys cannot save
- Support swapping Layers/Entity lists
- Support clearing Layers/Entity lists
- Fix pressing "cancel" on editor close save prompt still closes the editor
- Added a save prompt when loading a new ROM file

beta-6:
- Known issue: Editor instability after saving ROM (always close and re-open the editor after saving, for now!)
Expand Down
71 changes: 46 additions & 25 deletions WL4EditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ void WL4EditorWindow::LoadRoomUIUpdate()
/// </remarks>
void WL4EditorWindow::OpenROM()
{
// Check for unsaved operations
if(!UnsavedChangesPrompt(tr("There are unsaved changes. Discard changes and load ROM anyway?"))) return;

// Select a ROM file to open
QString qFilePath = QFileDialog::getOpenFileName(
this,
Expand Down Expand Up @@ -156,6 +159,7 @@ void WL4EditorWindow::OpenROM()
);
selectedRoom = 0;
int tmpTilesetID = CurrentLevel->GetRooms()[selectedRoom]->GetTilesetID();
UnsavedChanges = false;

// Only modify UI on the first time a ROM is loaded
if(!firstROMLoaded)
Expand Down Expand Up @@ -602,6 +606,7 @@ void WL4EditorWindow::closeEvent (QCloseEvent *event)
{
// If cancel is clicked, or X is clicked on the save prompt, then do nothing
event->ignore();
return;
}
}

Expand All @@ -618,31 +623,7 @@ void WL4EditorWindow::closeEvent (QCloseEvent *event)
void WL4EditorWindow::on_loadLevelButton_clicked()
{
// Check for unsaved operations
if(UnsavedChanges)
{
// Show save prompt
QMessageBox savePrompt;
savePrompt.setWindowTitle(tr("Unsaved changes"));
savePrompt.setText(tr("There are unsaved changes. Discard changes and load level anyway?"));
QPushButton *discardButton = savePrompt.addButton(tr("Discard"), QMessageBox::DestructiveRole);
QPushButton *cancelButton = savePrompt.addButton(tr("Cancel"), QMessageBox::NoRole);
QPushButton *saveButton = savePrompt.addButton(tr("Save"), QMessageBox::ApplyRole);
savePrompt.setDefaultButton(cancelButton);
savePrompt.exec();

if(savePrompt.clickedButton() == saveButton)
{
// Do not load level if there was an issue saving the file
if(!SaveCurrentFile())
{
return;
}
}
else if(savePrompt.clickedButton() != discardButton)
{
return;
}
}
if(!UnsavedChangesPrompt(tr("There are unsaved changes. Discard changes and load level anyway?"))) return;

// Deselect Door and Entity
ui->graphicsView->DeselectDoorAndEntity();
Expand Down Expand Up @@ -670,6 +651,46 @@ void WL4EditorWindow::on_loadLevelButton_clicked()
}
}

/// <summary>
/// Provide the user with a choice whether or not to save the ROM if there are unsaved changes.
/// </summary>
/// <param name="str">
/// The message to display in the save prompt.
/// </param>
/// <returns>
/// True if the user chose to continue with the save prompt.
/// </returns>
bool WL4EditorWindow::UnsavedChangesPrompt(QString str)
{
if(UnsavedChanges)
{
// Show save prompt
QMessageBox savePrompt;
savePrompt.setWindowTitle(tr("Unsaved changes"));
savePrompt.setText(str);
QPushButton *discardButton = savePrompt.addButton(tr("Discard"), QMessageBox::DestructiveRole);
QPushButton *cancelButton = savePrompt.addButton(tr("Cancel"), QMessageBox::NoRole);
QPushButton *saveButton = savePrompt.addButton(tr("Save"), QMessageBox::ApplyRole);
savePrompt.setDefaultButton(cancelButton);
savePrompt.exec();

if(savePrompt.clickedButton() == saveButton)
{
// Do not load level if there was an issue saving the file
if(!SaveCurrentFile())
{
return false;
}
}
else if(savePrompt.clickedButton() == discardButton)
{
return true;
}
else return false;
}
else return true;
}

/// <summary>
/// Decrease the index of the currently loaded room.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions WL4EditorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class WL4EditorWindow : public QMainWindow
void closeEvent (QCloseEvent *event);
static bool SaveCurrentFile() { return ROMUtils::SaveFile(ROMUtils::ROMFilePath); }
bool SaveCurrentFileAs();
bool UnsavedChangesPrompt(QString str);

protected:
void resizeEvent(QResizeEvent *event);
Expand Down

0 comments on commit 5371698

Please sign in to comment.