Skip to content

Commit

Permalink
fix: Ask user to save modified script before new one
Browse files Browse the repository at this point in the history
This will avoid losing the changes made to the script when the user
tries to create or open a new script without saving the current one.
  • Loading branch information
narnaud committed Nov 29, 2024
1 parent 52ab04d commit 3f7f230
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/gui/scriptpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,34 @@ void ScriptPanel::setupNewFile(const QString &scriptText, int cursorLeftMove)

void ScriptPanel::newScript()
{
if (!checkNeedToSaveScript())
return;
setupNewFile(DefaultScript, 3);
m_editDialogButton->hide();
}

void ScriptPanel::newScriptDialog()
{
if (!checkNeedToSaveScript())
return;
setupNewFile(DefaultScriptDialog, 9);
m_editDialogButton->show();
}

bool ScriptPanel::checkNeedToSaveScript()
{
const auto text = toPlainText();
if (text.isEmpty() || text == DefaultScript || text == DefaultScriptDialog)
return true;

const auto ret = QMessageBox::question(this, tr("Save Script"), tr("Do you want to save the script?"),
QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);

if (ret == QMessageBox::Save)
saveScript();
return ret != QMessageBox::Cancel;
}

void ScriptPanel::saveScript()
{
const bool isQml = toPlainText().contains("import Knut");
Expand Down
1 change: 1 addition & 0 deletions src/gui/scriptpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ScriptPanel : public QPlainTextEdit
void openScript();
void newScript();
void newScriptDialog();
bool checkNeedToSaveScript();
void saveScript();
void editDialog();
void checkEditDialogButton();
Expand Down

0 comments on commit 3f7f230

Please sign in to comment.