Skip to content
Permalink
Browse files

Update ui and add shortcut to cancel pending query

  • Loading branch information
pblottiere committed Mar 2, 2018
1 parent 86d66c7 commit fea66f99139c369aa46ab2d66a691642c6d3d15b
@@ -158,8 +158,10 @@ def __init__(self):
self.status = None
self.model = None
self.task = None
self.canceled = False

def cancel(self):
self.canceled = True
if self.task:
self.task.cancel()

This file was deleted.

@@ -37,7 +37,6 @@
from .db_plugins.postgis.plugin import PGDatabase
from .dlg_db_error import DlgDbError
from .dlg_query_builder import QueryBuilderDlg
from .dlg_cancel_task_query import DlgCancelTaskQuery

try:
from qgis.gui import QgsCodeEditorSQL # NOQA
@@ -57,12 +56,11 @@ class DlgSqlWindow(QWidget, Ui_Dialog):

def __init__(self, iface, db, parent=None):
QWidget.__init__(self, parent)
self.mainWindow = parent
self.iface = iface
self.db = db
self.filter = ""
self.modelAsync = None
self.dlg_cancel_task = DlgCancelTaskQuery(self)
self.dlg_cancel_task.canceled.connect(self.executeSqlCanceled)
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostgreSQL allows a primary key to span multiple columns, SpatiaLite doesn't
self.aliasSubQuery = isinstance(db, PGDatabase) # only PostgreSQL requires subqueries to be aliases
self.setupUi(self)
@@ -81,6 +79,16 @@ def __init__(self, iface, db, parent=None):
self.editSql.setMarginVisible(True)
self.initCompleter()

self.btnCancel.setText(self.tr("Cancel (ESC)"))
self.btnCancel.setEnabled(False)
self.btnCancel.clicked.connect(self.executeSqlCanceled)
self.btnCancel.setShortcut(QKeySequence.Cancel)
self.progressBar.setEnabled(False)
self.progressBar.setRange(0, 100)
self.progressBar.setValue(0)
self.progressBar.setFormat("")
self.progressBar.setAlignment(Qt.AlignCenter)

# allow copying results
copyAction = QAction("copy", self)
self.viewResult.addAction(copyAction)
@@ -181,11 +189,45 @@ def clearSql(self):
self.editSql.setFocus()
self.filter = ""

def updateUiWhileSqlExecution(self, status):
if status:
for i in range(0, self.mainWindow.tabs.count()):
if i != self.mainWindow.tabs.currentIndex():
self.mainWindow.tabs.setTabEnabled(i, False)

self.mainWindow.menuBar.setEnabled(False)
self.mainWindow.toolBar.setEnabled(False)
self.mainWindow.tree.setEnabled(False)

for w in self.findChildren(QWidget):
w.setEnabled(False)

self.btnCancel.setEnabled(True)
self.progressBar.setEnabled(True)
self.progressBar.setRange(0, 0)
else:
for i in range(0, self.mainWindow.tabs.count()):
if i != self.mainWindow.tabs.currentIndex():
self.mainWindow.tabs.setTabEnabled(i, True)

self.mainWindow.refreshTabs()
self.mainWindow.menuBar.setEnabled(True)
self.mainWindow.toolBar.setEnabled(True)
self.mainWindow.tree.setEnabled(True)

for w in self.findChildren(QWidget):
w.setEnabled(True)

self.btnCancel.setEnabled(False)
self.progressBar.setRange(0, 100)
self.progressBar.setEnabled(False)

def executeSqlCanceled(self):
self.btnCancel.setEnabled(False)
self.modelAsync.cancel()

def executeSqlCompleted(self):
self.dlg_cancel_task.hide()
self.updateUiWhileSqlExecution(False)

with OverrideCursor(Qt.WaitCursor):
if self.modelAsync.task.status() == QgsTask.Complete:
@@ -201,7 +243,7 @@ def executeSqlCompleted(self):

self.setColumnCombos(cols, quotedCols)
self.update()
elif not self.dlg_cancel_task.cancelStatus:
elif not self.modelAsync.canceled:
DlgDbError.showError(self.modelAsync.error, self)
self.uniqueModel.clear()
self.geomCombo.clear()
@@ -222,7 +264,7 @@ def executeSql(self):
try:
self.modelAsync = self.db.sqlResultModelAsync(sql, self)
self.modelAsync.done.connect(self.executeSqlCompleted)
self.dlg_cancel_task.show()
self.updateUiWhileSqlExecution(True)
QgsApplication.taskManager().addTask(self.modelAsync.task)
except Exception as e:
DlgDbError.showError(e, self)
@@ -393,6 +393,27 @@ unique values</string>
</widget>
</widget>
</item>
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnCancel">
<property name="text">
<string>Cancel (ESC)</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>

0 comments on commit fea66f9

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