Skip to content
Permalink
Browse files

Avoid endless wait cursors

  • Loading branch information
m-kuhn committed Jul 30, 2017
1 parent cf753e9 commit 973d3515d12a9a2f61460827d7839cf40ca92796
@@ -46,6 +46,7 @@
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 @@ def connection_info(self):
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 @@ def search(self):
# 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 @@ def navigate(self):
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 @@ def add_to_ows(self):
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,26 +809,22 @@ def show_metadata(self):
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
except KeyError as err:
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 @@ def _get_csw(self):
"""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

@@ -32,6 +32,8 @@

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 @@ def closeEvent(self, e):
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 @@ def reloadButtons(self):
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 @@ def invokeCallback(self, callback, *params):
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'):
@@ -40,6 +40,7 @@
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 @@ def getParamValues(self):

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 @@ def accept(self):
not keepOpen)
if not keepOpen:
QDialog.reject(self)
finally:
QApplication.restoreOverrideCursor()

def reject(self):
self.executed = False
@@ -51,6 +51,7 @@
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 @@ def accept(self):
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()

@@ -625,6 +625,28 @@ def spatialite_connect(*args, **kwargs):
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

0 comments on commit 973d351

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