History

Check script file for external changes when editor regains focus

Prompt user whether to reload.

David Rowe committed 12 years ago

Commit c454c791 parent 3df43dc4 files · +56 −4Browse files

Showing the changes to interface/src/ui/ScriptEditorWidget.cpp (1 of 4 files). Show all

Unified · Split

interface/src/ui/ScriptEditorWidget.cpp+35−4
@@ -29,8 +29,11 @@ · 28 unmodified lines
2929 ScriptEditorWidget::ScriptEditorWidget() :
3030 _scriptEditorWidgetUI(new Ui::ScriptEditorWidget),
3131 _scriptEngine(NULL),
32− _isRestarting(false)
32+ _isRestarting(false),
33+ _isReloading(false)
3334 {
35+ setAttribute(Qt::WA_DeleteOnClose);
36+
3437 _scriptEditorWidgetUI->setupUi(this);
3538
3639 connect(_scriptEditorWidgetUI->scriptEdit->document(), &QTextDocument::modificationChanged, this,
@@ -52,7 +55,7 @@ ScriptEditorWidget::~ScriptEditorWidget() { · 15 unmodified lines
5255 }
5356
5457 void ScriptEditorWidget::onScriptModified() {
55− if(_scriptEditorWidgetUI->onTheFlyCheckBox->isChecked() && isModified() && isRunning()) {
58+ if(_scriptEditorWidgetUI->onTheFlyCheckBox->isChecked() && isModified() && isRunning() && !_isReloading) {
5659 _isRestarting = true;
5760 setRunning(false);
5861 // Script is restarted once current script instance finishes.
@@ -76,7 +79,7 @@ bool ScriptEditorWidget::isRunning() { · 17 unmodified lines
7679 }
7780
7881 bool ScriptEditorWidget::setRunning(bool run) {
79− if (run && !save()) {
82+ if (run && isModified() && !save()) {
8083 return false;
8184 }
8285
@@ -114,13 +117,14 @@ bool ScriptEditorWidget::saveFile(const QString &scriptPath) { · 31 unmodified lines
114117
115118 QTextStream out(&file);
116119 out << _scriptEditorWidgetUI->scriptEdit->toPlainText();
120+ file.close();
117121
118122 setScriptFile(scriptPath);
119123 return true;
120124 }
121125
122126 void ScriptEditorWidget::loadFile(const QString& scriptPath) {
123− QUrl url(scriptPath);
127+ QUrl url(scriptPath);
124128
125129 // if the scheme length is one or lower, maybe they typed in a file, let's try
126130 const int WINDOWS_DRIVE_LETTER_SIZE = 1;
@@ -133,6 +137,7 @@ void ScriptEditorWidget::loadFile(const QString& scriptPath) { · 6 unmodified lines
133137 }
134138 QTextStream in(&file);
135139 _scriptEditorWidgetUI->scriptEdit->setPlainText(in.readAll());
140+ file.close();
136141 setScriptFile(scriptPath);
137142
138143 if (_scriptEngine != NULL) {
@@ -184,6 +189,7 @@ bool ScriptEditorWidget::saveAs() { · 45 unmodified lines
184189
185190 void ScriptEditorWidget::setScriptFile(const QString& scriptPath) {
186191 _currentScript = scriptPath;
192+ _currentScriptModified = QFileInfo(_currentScript).lastModified();
187193 _scriptEditorWidgetUI->scriptEdit->document()->setModified(false);
188194 setWindowModified(false);
189195
@@ -207,3 +213,28 @@ void ScriptEditorWidget::onScriptError(const QString& message) { · 17 unmodified lines
207213 void ScriptEditorWidget::onScriptPrint(const QString& message) {
208214 _scriptEditorWidgetUI->debugText->appendPlainText("> " + message);
209215 }
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+}