Prompt user whether to reload.
Showing the changes to interface/src/ui/ScriptEditorWidget.cpp (1 of 4 files). Show all
Unified · Split
| @@ -29,8 +29,11 @@ · 28 unmodified lines | ||
| 29 | 29 | ScriptEditorWidget::ScriptEditorWidget() : |
| 30 | 30 | _scriptEditorWidgetUI(new Ui::ScriptEditorWidget), |
| 31 | 31 | _scriptEngine(NULL), |
| 32 | − _isRestarting(false) | |
| 32 | + _isRestarting(false), | |
| 33 | + _isReloading(false) | |
| 33 | 34 | { |
| 35 | + setAttribute(Qt::WA_DeleteOnClose); | |
| 36 | + | |
| 34 | 37 | _scriptEditorWidgetUI->setupUi(this); |
| 35 | 38 | |
| 36 | 39 | connect(_scriptEditorWidgetUI->scriptEdit->document(), &QTextDocument::modificationChanged, this, |
| @@ -52,7 +55,7 @@ ScriptEditorWidget::~ScriptEditorWidget() { · 15 unmodified lines | ||
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | void ScriptEditorWidget::onScriptModified() { |
| 55 | − if(_scriptEditorWidgetUI->onTheFlyCheckBox->isChecked() && isModified() && isRunning()) { | |
| 58 | + if(_scriptEditorWidgetUI->onTheFlyCheckBox->isChecked() && isModified() && isRunning() && !_isReloading) { | |
| 56 | 59 | _isRestarting = true; |
| 57 | 60 | setRunning(false); |
| 58 | 61 | // Script is restarted once current script instance finishes. |
| @@ -76,7 +79,7 @@ bool ScriptEditorWidget::isRunning() { · 17 unmodified lines | ||
| 76 | 79 | } |
| 77 | 80 | |
| 78 | 81 | bool ScriptEditorWidget::setRunning(bool run) { |
| 79 | − if (run && !save()) { | |
| 82 | + if (run && isModified() && !save()) { | |
| 80 | 83 | return false; |
| 81 | 84 | } |
| 82 | 85 | |
| @@ -114,13 +117,14 @@ bool ScriptEditorWidget::saveFile(const QString &scriptPath) { · 31 unmodified lines | ||
| 114 | 117 | |
| 115 | 118 | QTextStream out(&file); |
| 116 | 119 | out << _scriptEditorWidgetUI->scriptEdit->toPlainText(); |
| 120 | + file.close(); | |
| 117 | 121 | |
| 118 | 122 | setScriptFile(scriptPath); |
| 119 | 123 | return true; |
| 120 | 124 | } |
| 121 | 125 | |
| 122 | 126 | void ScriptEditorWidget::loadFile(const QString& scriptPath) { |
| 123 | − QUrl url(scriptPath); | |
| 127 | + QUrl url(scriptPath); | |
| 124 | 128 | |
| 125 | 129 | // if the scheme length is one or lower, maybe they typed in a file, let's try |
| 126 | 130 | const int WINDOWS_DRIVE_LETTER_SIZE = 1; |
| @@ -133,6 +137,7 @@ void ScriptEditorWidget::loadFile(const QString& scriptPath) { · 6 unmodified lines | ||
| 133 | 137 | } |
| 134 | 138 | QTextStream in(&file); |
| 135 | 139 | _scriptEditorWidgetUI->scriptEdit->setPlainText(in.readAll()); |
| 140 | + file.close(); | |
| 136 | 141 | setScriptFile(scriptPath); |
| 137 | 142 | |
| 138 | 143 | if (_scriptEngine != NULL) { |
| @@ -184,6 +189,7 @@ bool ScriptEditorWidget::saveAs() { · 45 unmodified lines | ||
| 184 | 189 | |
| 185 | 190 | void ScriptEditorWidget::setScriptFile(const QString& scriptPath) { |
| 186 | 191 | _currentScript = scriptPath; |
| 192 | + _currentScriptModified = QFileInfo(_currentScript).lastModified(); | |
| 187 | 193 | _scriptEditorWidgetUI->scriptEdit->document()->setModified(false); |
| 188 | 194 | setWindowModified(false); |
| 189 | 195 | |
| @@ -207,3 +213,28 @@ void ScriptEditorWidget::onScriptError(const QString& message) { · 17 unmodified lines | ||
| 207 | 213 | void ScriptEditorWidget::onScriptPrint(const QString& message) { |
| 208 | 214 | _scriptEditorWidgetUI->debugText->appendPlainText("> " + message); |
| 209 | 215 | } |
| 216 | + | |
| 217 | +void ScriptEditorWidget::onWindowActivated() { | |
| 218 | + if (!_isReloading) { | |
| 219 | + _isReloading = true; | |
| 220 | + | |
| 221 | + if (QFileInfo(_currentScript).lastModified() > _currentScriptModified) { | |
| 222 | + if (QMessageBox::warning(this, _currentScript, | |
| 223 | + tr("This file has been modified outside of the Interface editor.") + "\n\n" | |
| 224 | + +(isModified() | |
| 225 | + ? tr("Do you want to reload it and lose the changes you've made in the Interface editor?") | |
| 226 | + : tr("Do you want to reload it?")), | |
| 227 | + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { | |
| 228 | + loadFile(_currentScript); | |
| 229 | + if (_scriptEditorWidgetUI->onTheFlyCheckBox->isChecked() && isRunning()) { | |
| 230 | + _isRestarting = true; | |
| 231 | + setRunning(false); | |
| 232 | + // Script is restarted once current script instance finishes. | |
| 233 | + } | |
| 234 | + | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + _isReloading = false; | |
| 239 | + } | |
| 240 | +} | |