Skip to content

Commit 0db8a58

Browse files
authoredJul 31, 2017
Merge pull request #4944 from m-kuhn/PyOverrideCursor
Safer cursor overriding for python
2 parents cf753e9 + 1da9c60 commit 0db8a58

22 files changed

+571
-671
lines changed
 

‎python/console/console_editor.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from qgis.PyQt.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs, QsciStyle
2828
from qgis.core import QgsApplication, QgsSettings
2929
from qgis.gui import QgsMessageBar
30+
from qgis.utils import OverrideCursor
3031
import sys
3132
import os
3233
import subprocess
@@ -726,10 +727,9 @@ def focusInEvent(self, e):
726727
file = open(pathfile, "r")
727728
fileLines = file.readlines()
728729
file.close()
729-
QApplication.setOverrideCursor(Qt.WaitCursor)
730-
for line in reversed(fileLines):
731-
self.insert(line)
732-
QApplication.restoreOverrideCursor()
730+
with OverrideCursor(Qt.WaitCursor):
731+
for line in reversed(fileLines):
732+
self.insert(line)
733733
self.setModified(False)
734734
self.endUndoAction()
735735

@@ -785,11 +785,10 @@ def loadFile(self, filename, modified):
785785
fn = codecs.open(filename, "rb", encoding='utf-8')
786786
txt = fn.read()
787787
fn.close()
788-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
789-
self.newEditor.setText(txt)
790-
if self.readOnly:
791-
self.newEditor.setReadOnly(self.readOnly)
792-
QApplication.restoreOverrideCursor()
788+
with OverrideCursor(Qt.WaitCursor):
789+
self.newEditor.setText(txt)
790+
if self.readOnly:
791+
self.newEditor.setReadOnly(self.readOnly)
793792
self.newEditor.setModified(modified)
794793
self.newEditor.recolor()
795794

@@ -1259,9 +1258,8 @@ def refreshSettingsEditor(self):
12591258
if objInspectorEnabled:
12601259
cW = self.currentWidget()
12611260
if cW and not self.parent.listClassMethod.isVisible():
1262-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
1263-
self.listObject(cW)
1264-
QApplication.restoreOverrideCursor()
1261+
with OverrideCursor(Qt.WaitCursor):
1262+
self.listObject(cW)
12651263

12661264
def changeLastDirPath(self, tab):
12671265
tabWidget = self.widget(tab)

‎python/plugins/MetaSearch/dialogs/maindialog.py

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
QgsCoordinateTransform, QgsGeometry, QgsPointXY,
4747
QgsProviderRegistry, QgsSettings)
4848
from qgis.gui import QgsRubberBand
49+
from qgis.utils import OverrideCursor
4950

5051
from owslib.csw import CatalogueServiceWeb # spellok
5152
from owslib.fes import BBox, PropertyIsLike
@@ -281,8 +282,6 @@ def connection_info(self):
281282
if not self._get_csw():
282283
return
283284

284-
QApplication.restoreOverrideCursor()
285-
286285
if self.catalog: # display service metadata
287286
self.btnCapabilities.setEnabled(True)
288287
metadata = render_template('en', self.context,
@@ -490,25 +489,22 @@ def search(self):
490489
# TODO: allow users to select resources types
491490
# to find ('service', 'dataset', etc.)
492491
try:
493-
self.catalog.getrecords2(constraints=self.constraints,
494-
maxrecords=self.maxrecords, esn='full')
492+
with OverrideCursor(Qt.WaitCursor):
493+
self.catalog.getrecords2(constraints=self.constraints,
494+
maxrecords=self.maxrecords, esn='full')
495495
except ExceptionReport as err:
496-
QApplication.restoreOverrideCursor()
497496
QMessageBox.warning(self, self.tr('Search error'),
498497
self.tr('Search error: {0}').format(err))
499498
return
500499
except Exception as err:
501-
QApplication.restoreOverrideCursor()
502500
QMessageBox.warning(self, self.tr('Connection error'),
503501
self.tr('Connection error: {0}').format(err))
504502
return
505503

506504
if self.catalog.results['matches'] == 0:
507-
QApplication.restoreOverrideCursor()
508505
self.lblResults.setText(self.tr('0 results'))
509506
return
510507

511-
QApplication.restoreOverrideCursor()
512508
self.display_results()
513509

514510
def display_results(self):
@@ -675,25 +671,20 @@ def navigate(self):
675671
else:
676672
return
677673

678-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
679-
680674
try:
681-
self.catalog.getrecords2(constraints=self.constraints,
682-
maxrecords=self.maxrecords,
683-
startposition=self.startfrom, esn='full')
675+
with OverrideCursor(Qt.WaitCursor):
676+
self.catalog.getrecords2(constraints=self.constraints,
677+
maxrecords=self.maxrecords,
678+
startposition=self.startfrom, esn='full')
684679
except ExceptionReport as err:
685-
QApplication.restoreOverrideCursor()
686680
QMessageBox.warning(self, self.tr('Search error'),
687681
self.tr('Search error: {0}').format(err))
688682
return
689683
except Exception as err:
690-
QApplication.restoreOverrideCursor()
691684
QMessageBox.warning(self, self.tr('Connection error'),
692685
self.tr('Connection error: {0}').format(err))
693686
return
694687

695-
QApplication.restoreOverrideCursor()
696-
697688
self.display_results()
698689

699690
def add_to_ows(self):
@@ -727,8 +718,6 @@ def add_to_ows(self):
727718
stype = ['ESRI:ArcGIS:FeatureServer', 'afs', 'arcgisfeatureserver']
728719
data_url = item_data['afs'].split('FeatureServer')[0] + 'FeatureServer'
729720

730-
QApplication.restoreOverrideCursor()
731-
732721
sname = '%s from MetaSearch' % stype[1]
733722

734723
# store connection
@@ -820,26 +809,22 @@ def show_metadata(self):
820809
identifier = get_item_data(item, 'identifier')
821810

822811
try:
823-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
824-
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout, # spellok
825-
username=self.catalog_username,
826-
password=self.catalog_password)
827-
cat.getrecordbyid(
828-
[self.catalog.records[identifier].identifier])
812+
with OverrideCursor(Qt.WaitCursor):
813+
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout, # spellok
814+
username=self.catalog_username,
815+
password=self.catalog_password)
816+
cat.getrecordbyid(
817+
[self.catalog.records[identifier].identifier])
829818
except ExceptionReport as err:
830-
QApplication.restoreOverrideCursor()
831819
QMessageBox.warning(self, self.tr('GetRecords error'),
832820
self.tr('Error getting response: {0}').format(err))
833821
return
834822
except KeyError as err:
835823
QMessageBox.warning(self,
836824
self.tr('Record parsing error'),
837825
self.tr('Unable to locate record identifier'))
838-
QApplication.restoreOverrideCursor()
839826
return
840827

841-
QApplication.restoreOverrideCursor()
842-
843828
record = cat.records[identifier]
844829
record.xml_url = cat.request
845830

@@ -902,21 +887,20 @@ def _get_csw(self):
902887
"""convenience function to init owslib.csw.CatalogueServiceWeb""" # spellok
903888

904889
# connect to the server
905-
try:
906-
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
907-
self.catalog = CatalogueServiceWeb(self.catalog_url, # spellok
908-
timeout=self.timeout,
909-
username=self.catalog_username,
910-
password=self.catalog_password)
911-
return True
912-
except ExceptionReport as err:
913-
msg = self.tr('Error connecting to service: {0}').format(err)
914-
except ValueError as err:
915-
msg = self.tr('Value Error: {0}').format(err)
916-
except Exception as err:
917-
msg = self.tr('Unknown Error: {0}').format(err)
890+
with OverrideCursor(Qt.WaitCursor):
891+
try:
892+
self.catalog = CatalogueServiceWeb(self.catalog_url, # spellok
893+
timeout=self.timeout,
894+
username=self.catalog_username,
895+
password=self.catalog_password)
896+
return True
897+
except ExceptionReport as err:
898+
msg = self.tr('Error connecting to service: {0}').format(err)
899+
except ValueError as err:
900+
msg = self.tr('Value Error: {0}').format(err)
901+
except Exception as err:
902+
msg = self.tr('Unknown Error: {0}').format(err)
918903

919-
QApplication.restoreOverrideCursor()
920904
QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
921905
return False
922906

‎python/plugins/MetaSearch/ui/newconnectiondialog.ui

Lines changed: 52 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,74 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>494</width>
10-
<height>224</height>
9+
<width>585</width>
10+
<height>327</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Create a new Catalog connection</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout">
17-
<item row="2" column="2">
18-
<widget class="QLineEdit" name="leURL"/>
19-
</item>
20-
<item row="2" column="0">
21-
<widget class="QLabel" name="label_2">
17+
<item row="0" column="0">
18+
<widget class="QLabel" name="label">
2219
<property name="text">
23-
<string>URL</string>
20+
<string>Name</string>
2421
</property>
2522
</widget>
2623
</item>
24+
<item row="0" column="1">
25+
<widget class="QLineEdit" name="leName"/>
26+
</item>
2727
<item row="1" column="0">
28-
<widget class="QLabel" name="label">
28+
<widget class="QLabel" name="label_2">
2929
<property name="text">
30-
<string>Name</string>
30+
<string>URL</string>
3131
</property>
3232
</widget>
3333
</item>
34-
<item row="1" column="2">
35-
<widget class="QLineEdit" name="leName"/>
34+
<item row="1" column="1">
35+
<widget class="QLineEdit" name="leURL"/>
36+
</item>
37+
<item row="2" column="0" colspan="2">
38+
<widget class="QGroupBox" name="authenticationGroupBox">
39+
<property name="title">
40+
<string>Authentication</string>
41+
</property>
42+
<layout class="QFormLayout" name="formLayout_2">
43+
<item row="0" column="0" colspan="2">
44+
<widget class="QLabel" name="label_3">
45+
<property name="text">
46+
<string>If the service requires basic authentication, enter a user name and optional password</string>
47+
</property>
48+
<property name="wordWrap">
49+
<bool>true</bool>
50+
</property>
51+
</widget>
52+
</item>
53+
<item row="1" column="0">
54+
<widget class="QLabel" name="label_4">
55+
<property name="text">
56+
<string>User name</string>
57+
</property>
58+
</widget>
59+
</item>
60+
<item row="1" column="1">
61+
<widget class="QLineEdit" name="leUsername"/>
62+
</item>
63+
<item row="2" column="0">
64+
<widget class="QLabel" name="label_5">
65+
<property name="text">
66+
<string>Password</string>
67+
</property>
68+
</widget>
69+
</item>
70+
<item row="2" column="1">
71+
<widget class="QLineEdit" name="lePassword"/>
72+
</item>
73+
</layout>
74+
</widget>
3675
</item>
37-
<item row="5" column="2">
76+
<item row="3" column="1">
3877
<widget class="QDialogButtonBox" name="buttonBox">
3978
<property name="orientation">
4079
<enum>Qt::Horizontal</enum>
@@ -44,85 +83,11 @@
4483
</property>
4584
</widget>
4685
</item>
47-
<item row="3" column="2">
48-
<widget class="QTabWidget" name="tabNewConnectionOptions">
49-
<property name="currentIndex">
50-
<number>0</number>
51-
</property>
52-
<widget class="QWidget" name="tab">
53-
<attribute name="title">
54-
<string>Authentication</string>
55-
</attribute>
56-
<widget class="QLabel" name="label_3">
57-
<property name="geometry">
58-
<rect>
59-
<x>10</x>
60-
<y>10</y>
61-
<width>421</width>
62-
<height>16</height>
63-
</rect>
64-
</property>
65-
<property name="text">
66-
<string>If the service requires basic authentication, enter a user name and optional password</string>
67-
</property>
68-
</widget>
69-
<widget class="QLabel" name="label_4">
70-
<property name="geometry">
71-
<rect>
72-
<x>10</x>
73-
<y>30</y>
74-
<width>51</width>
75-
<height>16</height>
76-
</rect>
77-
</property>
78-
<property name="text">
79-
<string>User name</string>
80-
</property>
81-
</widget>
82-
<widget class="QLabel" name="label_5">
83-
<property name="geometry">
84-
<rect>
85-
<x>10</x>
86-
<y>60</y>
87-
<width>46</width>
88-
<height>13</height>
89-
</rect>
90-
</property>
91-
<property name="text">
92-
<string>Password</string>
93-
</property>
94-
</widget>
95-
<widget class="QLineEdit" name="leUsername">
96-
<property name="geometry">
97-
<rect>
98-
<x>80</x>
99-
<y>30</y>
100-
<width>341</width>
101-
<height>20</height>
102-
</rect>
103-
</property>
104-
</widget>
105-
<widget class="QLineEdit" name="lePassword">
106-
<property name="geometry">
107-
<rect>
108-
<x>80</x>
109-
<y>60</y>
110-
<width>341</width>
111-
<height>20</height>
112-
</rect>
113-
</property>
114-
</widget>
115-
</widget>
116-
</widget>
117-
</item>
11886
</layout>
11987
</widget>
12088
<tabstops>
12189
<tabstop>leName</tabstop>
12290
<tabstop>leURL</tabstop>
123-
<tabstop>leUsername</tabstop>
124-
<tabstop>lePassword</tabstop>
125-
<tabstop>tabNewConnectionOptions</tabstop>
12691
<tabstop>buttonBox</tabstop>
12792
</tabstops>
12893
<resources/>

0 commit comments

Comments
 (0)
Please sign in to comment.