Avoid endless wait cursors

This commit is contained in:
Matthias Kuhn 2017-07-30 21:51:22 +02:00
parent cf753e9ff4
commit 973d3515d1
No known key found for this signature in database
GPG Key ID: A0E766808764D73F
5 changed files with 85 additions and 93 deletions

View File

@ -46,6 +46,7 @@ from qgis.core import (QgsApplication, QgsCoordinateReferenceSystem,
QgsCoordinateTransform, QgsGeometry, QgsPointXY,
QgsProviderRegistry, QgsSettings)
from qgis.gui import QgsRubberBand
from qgis.utils import OverrideCursor
from owslib.csw import CatalogueServiceWeb # spellok
from owslib.fes import BBox, PropertyIsLike
@ -281,8 +282,6 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
if not self._get_csw():
return
QApplication.restoreOverrideCursor()
if self.catalog: # display service metadata
self.btnCapabilities.setEnabled(True)
metadata = render_template('en', self.context,
@ -490,25 +489,22 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
# TODO: allow users to select resources types
# to find ('service', 'dataset', etc.)
try:
self.catalog.getrecords2(constraints=self.constraints,
maxrecords=self.maxrecords, esn='full')
with OverrideCursor(Qt.WaitCursor):
self.catalog.getrecords2(constraints=self.constraints,
maxrecords=self.maxrecords, esn='full')
except ExceptionReport as err:
QApplication.restoreOverrideCursor()
QMessageBox.warning(self, self.tr('Search error'),
self.tr('Search error: {0}').format(err))
return
except Exception as err:
QApplication.restoreOverrideCursor()
QMessageBox.warning(self, self.tr('Connection error'),
self.tr('Connection error: {0}').format(err))
return
if self.catalog.results['matches'] == 0:
QApplication.restoreOverrideCursor()
self.lblResults.setText(self.tr('0 results'))
return
QApplication.restoreOverrideCursor()
self.display_results()
def display_results(self):
@ -675,25 +671,20 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
else:
return
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
try:
self.catalog.getrecords2(constraints=self.constraints,
maxrecords=self.maxrecords,
startposition=self.startfrom, esn='full')
with OverrideCursor(Qt.WaitCursor):
self.catalog.getrecords2(constraints=self.constraints,
maxrecords=self.maxrecords,
startposition=self.startfrom, esn='full')
except ExceptionReport as err:
QApplication.restoreOverrideCursor()
QMessageBox.warning(self, self.tr('Search error'),
self.tr('Search error: {0}').format(err))
return
except Exception as err:
QApplication.restoreOverrideCursor()
QMessageBox.warning(self, self.tr('Connection error'),
self.tr('Connection error: {0}').format(err))
return
QApplication.restoreOverrideCursor()
self.display_results()
def add_to_ows(self):
@ -727,8 +718,6 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
stype = ['ESRI:ArcGIS:FeatureServer', 'afs', 'arcgisfeatureserver']
data_url = item_data['afs'].split('FeatureServer')[0] + 'FeatureServer'
QApplication.restoreOverrideCursor()
sname = '%s from MetaSearch' % stype[1]
# store connection
@ -820,14 +809,13 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
identifier = get_item_data(item, 'identifier')
try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout, # spellok
username=self.catalog_username,
password=self.catalog_password)
cat.getrecordbyid(
[self.catalog.records[identifier].identifier])
with OverrideCursor(Qt.WaitCursor):
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout, # spellok
username=self.catalog_username,
password=self.catalog_password)
cat.getrecordbyid(
[self.catalog.records[identifier].identifier])
except ExceptionReport as err:
QApplication.restoreOverrideCursor()
QMessageBox.warning(self, self.tr('GetRecords error'),
self.tr('Error getting response: {0}').format(err))
return
@ -835,11 +823,8 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
QMessageBox.warning(self,
self.tr('Record parsing error'),
self.tr('Unable to locate record identifier'))
QApplication.restoreOverrideCursor()
return
QApplication.restoreOverrideCursor()
record = cat.records[identifier]
record.xml_url = cat.request
@ -902,21 +887,20 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
"""convenience function to init owslib.csw.CatalogueServiceWeb""" # spellok
# connect to the server
try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
self.catalog = CatalogueServiceWeb(self.catalog_url, # spellok
timeout=self.timeout,
username=self.catalog_username,
password=self.catalog_password)
return True
except ExceptionReport as err:
msg = self.tr('Error connecting to service: {0}').format(err)
except ValueError as err:
msg = self.tr('Value Error: {0}').format(err)
except Exception as err:
msg = self.tr('Unknown Error: {0}').format(err)
with OverrideCursor(Qt.WaitCursor):
try:
self.catalog = CatalogueServiceWeb(self.catalog_url, # spellok
timeout=self.timeout,
username=self.catalog_username,
password=self.catalog_password)
return True
except ExceptionReport as err:
msg = self.tr('Error connecting to service: {0}').format(err)
except ValueError as err:
msg = self.tr('Value Error: {0}').format(err)
except Exception as err:
msg = self.tr('Unknown Error: {0}').format(err)
QApplication.restoreOverrideCursor()
QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
return False

View File

@ -32,6 +32,8 @@ from qgis.PyQt.QtGui import QIcon, QKeySequence
from qgis.gui import QgsMessageBar
from qgis.core import QgsSettings, QgsMapLayer
from qgis.utils import OverrideCursor
from .info_viewer import InfoViewer
from .table_viewer import TableViewer
from .layer_preview import LayerPreview
@ -72,29 +74,23 @@ class DBManager(QMainWindow):
QMainWindow.closeEvent(self, e)
def refreshItem(self, item=None):
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
if item is None:
item = self.tree.currentItem()
self.tree.refreshItem(item) # refresh item children in the db tree
except BaseError as e:
DlgDbError.showError(e, self)
return
finally:
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
try:
if item is None:
item = self.tree.currentItem()
self.tree.refreshItem(item) # refresh item children in the db tree
except BaseError as e:
DlgDbError.showError(e, self)
def itemChanged(self, item):
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
self.reloadButtons()
# clear preview, this will delete the layer in preview tab
self.preview.loadPreview(None)
self.refreshTabs()
except BaseError as e:
DlgDbError.showError(e, self)
return
finally:
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
try:
self.reloadButtons()
# clear preview, this will delete the layer in preview tab
self.preview.loadPreview(None)
self.refreshTabs()
except BaseError as e:
DlgDbError.showError(e, self)
def reloadButtons(self):
db = self.tree.currentDatabase()
@ -114,14 +110,11 @@ class DBManager(QMainWindow):
self._lastDb.registerAllActions(self)
def tabChanged(self, index):
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
self.refreshTabs()
except BaseError as e:
DlgDbError.showError(e, self)
return
finally:
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
try:
self.refreshTabs()
except BaseError as e:
DlgDbError.showError(e, self)
def refreshTabs(self):
index = self.tabs.currentIndex()
@ -300,17 +293,12 @@ class DBManager(QMainWindow):
This method takes care to override and restore the cursor,
but also catches exceptions and displays the error dialog.
"""
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
callback(self.tree.currentItem(), self.sender(), self, *params)
except BaseError as e:
# catch database errors and display the error dialog
DlgDbError.showError(e, self)
return
finally:
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
try:
callback(self.tree.currentItem(), self.sender(), self, *params)
except BaseError as e:
# catch database errors and display the error dialog
DlgDbError.showError(e, self)
def unregisterAction(self, action, menuName):
if not hasattr(self, '_registeredDbActions'):

View File

@ -40,6 +40,7 @@ from qgis.core import (QgsExpressionContextUtils,
QgsMapLayerProxyModel,
QgsMessageLog)
from qgis.gui import QgsEncodingFileDialog
from qgis.utils import OverrideCursor
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingLog import ProcessingLog
@ -220,10 +221,9 @@ class FieldsCalculatorDialog(BASE, WIDGET):
def accept(self):
keepOpen = ProcessingConfig.getSetting(ProcessingConfig.KEEP_DIALOG_OPEN)
try:
parameters = self.getParamValues()
if parameters:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
parameters = self.getParamValues()
if parameters:
with OverrideCusor(Qt.WaitCursor):
context = dataobjects.createContext()
ProcessingLog.addToLog(self.alg.asPythonCommand(parameters, context))
@ -235,8 +235,6 @@ class FieldsCalculatorDialog(BASE, WIDGET):
not keepOpen)
if not keepOpen:
QDialog.reject(self)
finally:
QApplication.restoreOverrideCursor()
def reject(self):
self.executed = False

View File

@ -51,6 +51,7 @@ from qgis.gui import (QgsDoubleSpinBox,
QgsSpinBox,
QgsOptionsPageWidget)
from qgis.core import NULL, QgsApplication, QgsSettings
from qgis.utils import OverrideCursor
from processing.core.ProcessingConfig import (ProcessingConfig,
settingsWatcher,
@ -293,10 +294,9 @@ class ConfigDialog(BASE, WIDGET):
return
setting.save(qsettings)
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
for p in QgsApplication.processingRegistry().providers():
p.refreshAlgorithms()
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
for p in QgsApplication.processingRegistry().providers():
p.refreshAlgorithms()
settingsWatcher.settingsChanged.emit()

View File

@ -625,6 +625,28 @@ or using the "mod_spatialite" extension (python3)"""
return dbapi2.connect(*args, **kwargs)
class OverrideCursor():
"""
Executes a code block with a different cursor set and makes sure the cursor
is restored even if exceptions are raised or an intermediate ``return``
statement is hit.
Example:
```
with OverrideCursor(Qt.WaitCursor):
do_a_slow(operation)
```
"""
def __init__(self, cursor):
self.cursor = cursor
def __enter__(self):
QApplication.setOverrideCursor(self.cursor)
def __exit__(self, exc_type, exc_val, exc_tb):
QApplication.restoreOverrideCursor()
#######################
# IMPORT wrapper