Skip to content
Permalink
Browse files

Merge pull request #36232 from alexbruy/close-prompt

prompt before closing SQL editor tabs with unsaved changes (fix #14636)
  • Loading branch information
alexbruy committed May 6, 2020
2 parents 1ddcde9 + bba874c commit 8e2a9d579788639ed5138fa62031bb21691df674
@@ -362,8 +362,13 @@ def unregisterAllActions(self):
def close_tab(self, index):
widget = self.tabs.widget(index)
if widget not in [self.info, self.table, self.preview]:
self.tabs.removeTab(index)
widget.deleteLater()
if hasattr(widget, "close"):
if widget.close():
self.tabs.removeTab(index)
widget.deleteLater()
else:
self.tabs.removeTab(index)
widget.deleteLater()

def toolBarOrientation(self):
button_style = Qt.ToolButtonIconOnly
@@ -27,8 +27,20 @@
from hashlib import md5

from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtWidgets import QDialog, QWidget, QAction, QApplication, QStyledItemDelegate
from qgis.PyQt.QtGui import QKeySequence, QCursor, QClipboard, QIcon, QStandardItemModel, QStandardItem
from qgis.PyQt.QtWidgets import (QDialog,
QWidget,
QAction,
QApplication,
QStyledItemDelegate,
QMessageBox
)
from qgis.PyQt.QtGui import (QKeySequence,
QCursor,
QClipboard,
QIcon,
QStandardItemModel,
QStandardItem
)
from qgis.PyQt.Qsci import QsciAPIs
from qgis.PyQt.QtXml import QDomDocument

@@ -61,6 +73,7 @@

class DlgSqlLayerWindow(QWidget, Ui_Dialog):
nameChanged = pyqtSignal(str)
hasChanged = False

def __init__(self, iface, layer, parent=None):
QWidget.__init__(self, parent)
@@ -104,6 +117,7 @@ def __init__(self, iface, layer, parent=None):
self.editSql.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.editSql.setMarginVisible(True)
self.initCompleter()
self.editSql.textChanged.connect(lambda: self.setHasChanged(True))

# allow copying results
copyAction = QAction("copy", self)
@@ -542,3 +556,23 @@ def setFilter(self):
if dlg.exec_():
self.filter = dlg.sql()
layer.deleteLater()

def setHasChanged(self, hasChanged):
self.hasChanged = hasChanged

def close(self):
if self.hasChanged:
ret = QMessageBox.question(
self, self.tr('Unsaved Changes?'),
self.tr('There are unsaved changes. Do you want to keep them?'),
QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Discard, QMessageBox.Cancel)

if ret == QMessageBox.Save:
self.saveAsFilePreset()
return True
elif ret == QMessageBox.Discard:
return True
else:
return False
else:
return True
@@ -29,8 +29,23 @@
import os

from qgis.PyQt.QtCore import Qt, pyqtSignal, QDir
from qgis.PyQt.QtWidgets import QDialog, QWidget, QAction, QApplication, QInputDialog, QStyledItemDelegate, QTableWidgetItem, QFileDialog
from qgis.PyQt.QtGui import QKeySequence, QCursor, QClipboard, QIcon, QStandardItemModel, QStandardItem
from qgis.PyQt.QtWidgets import (QDialog,
QWidget,
QAction,
QApplication,
QInputDialog,
QStyledItemDelegate,
QTableWidgetItem,
QFileDialog,
QMessageBox
)
from qgis.PyQt.QtGui import (QKeySequence,
QCursor,
QClipboard,
QIcon,
QStandardItemModel,
QStandardItem
)
from qgis.PyQt.Qsci import QsciAPIs

from qgis.core import (
@@ -94,6 +109,7 @@ def check_comments_in_sql(raw_sql_input):
class DlgSqlWindow(QWidget, Ui_Dialog):
nameChanged = pyqtSignal(str)
QUERY_HISTORY_LIMIT = 20
hasChanged = False

def __init__(self, iface, db, parent=None):
QWidget.__init__(self, parent)
@@ -121,6 +137,7 @@ def __init__(self, iface, db, parent=None):
self.editSql.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.editSql.setMarginVisible(True)
self.initCompleter()
self.editSql.textChanged.connect(lambda: self.setHasChanged(True))

settings = QgsSettings()
self.history = settings.value('DB_Manager/queryHistory/' + self.dbType, {self.connectionName: []})
@@ -325,6 +342,7 @@ def clearSql(self):
self.editSql.clear()
self.editSql.setFocus()
self.filter = ""
self.setHasChanged(True)

def updateUiWhileSqlExecution(self, status):
if status:
@@ -387,7 +405,6 @@ def executeSqlCompleted(self):
self.geomCombo.clear()

def executeSql(self):

sql = self._getExecutableSqlQuery()
if sql == "":
return
@@ -664,3 +681,23 @@ def setFilter(self):
if dlg.exec_():
self.filter = dlg.sql()
layer.deleteLater()

def setHasChanged(self, hasChanged):
self.hasChanged = hasChanged

def close(self):
if self.hasChanged:
ret = QMessageBox.question(
self, self.tr('Unsaved Changes?'),
self.tr('There are unsaved changes. Do you want to keep them?'),
QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Discard, QMessageBox.Cancel)

if ret == QMessageBox.Save:
self.saveAsFilePreset()
return True
elif ret == QMessageBox.Discard:
return True
else:
return False
else:
return True

0 comments on commit 8e2a9d5

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