Skip to content
Permalink
Browse files

Don't silently close QGIS with unsaved changes in the console script

editors

Instead prompt the user what to do with these changes

Fixes #38529
  • Loading branch information
nyalldawson committed Oct 6, 2020
1 parent df6c8d7 commit 9093bdfd1e46ba65af78c5fc01bcb4df1ed6a0e9
Showing with 48 additions and 2 deletions.
  1. +48 −2 python/console/console.py
@@ -23,14 +23,23 @@
from qgis.PyQt.QtCore import Qt, QTimer, QCoreApplication, QSize, QByteArray, QFileInfo, QUrl, QDir
from qgis.PyQt.QtWidgets import QToolBar, QToolButton, QWidget, QSplitter, QTreeWidget, QAction, QFileDialog, QCheckBox, QSizePolicy, QMenu, QGridLayout, QApplication, QShortcut
from qgis.PyQt.QtGui import QDesktopServices, QKeySequence
from qgis.PyQt.QtWidgets import QVBoxLayout
from qgis.PyQt.QtWidgets import (
QVBoxLayout,
QMessageBox
)
from qgis.utils import iface
from .console_sci import ShellScintilla
from .console_output import ShellOutputScintilla
from .console_editor import EditorTabWidget
from .console_settings import ConsoleOptionsFactory
from qgis.core import Qgis, QgsApplication, QgsSettings
from qgis.gui import QgsFilterLineEdit, QgsHelp, QgsDockWidget, QgsGui
from qgis.gui import (
QgsFilterLineEdit,
QgsHelp,
QgsDockWidget,
QgsGui,
QgsApplicationExitBlockerInterface
)
from functools import partial

import sys
@@ -80,6 +89,16 @@ def init_options_widget():
iface.registerOptionsWidgetFactory(_options_factory)


class ConsoleExitBlocker(QgsApplicationExitBlockerInterface):

def __init__(self, console):
super().__init__()
self.console = console

def allowExit(self):
return self.console.allowExit()


class PythonConsole(QgsDockWidget):

def __init__(self, parent=None):
@@ -543,6 +562,33 @@ def __init__(self, parent=None):
self.findScut.setContext(Qt.WidgetWithChildrenShortcut)
self.findScut.activated.connect(self._closeFind)

self.exit_blocker = ConsoleExitBlocker(self)
iface.registerApplicationExitBlocker(self.exit_blocker)

def allowExit(self):
tab_count = self.tabEditorWidget.count()
for i in range(tab_count):
# iterate backwards through tabs, as we may be closing some as we go
tab_index = tab_count - i - 1
tab_widget = self.tabEditorWidget.widget(tab_index)
if tab_widget.newEditor.isModified():
ret = QMessageBox.question(self, self.tr("Save {}").format(self.tabEditorWidget.tabText(tab_index)),
self.tr("There are unsaved changes in this script. Do you want to keep those?"),
QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Discard, QMessageBox.Cancel)
if ret == QMessageBox.Save:
tab_widget.save()
if tab_widget.newEditor.isModified():
# save failed, treat as cancel
return False
elif ret == QMessageBox.Discard:
pass
else:
return False

self.tabEditorWidget.removeTab(tab_index)

return True

def _toggleFind(self):
self.tabEditorWidget.currentWidget().newEditor.toggleFindWidget()

0 comments on commit 9093bdf

Please sign in to comment.
You can’t perform that action at this time.