diff --git a/src/gui/scriptpanel.cpp b/src/gui/scriptpanel.cpp index 0ffa362f..a6b36f30 100644 --- a/src/gui/scriptpanel.cpp +++ b/src/gui/scriptpanel.cpp @@ -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"); diff --git a/src/gui/scriptpanel.h b/src/gui/scriptpanel.h index 72e9c60f..99442164 100644 --- a/src/gui/scriptpanel.h +++ b/src/gui/scriptpanel.h @@ -45,6 +45,7 @@ class ScriptPanel : public QPlainTextEdit void openScript(); void newScript(); void newScriptDialog(); + bool checkNeedToSaveScript(); void saveScript(); void editDialog(); void checkEditDialogButton();