Skip to content

Commit

Permalink
More endless hourglass protection
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jul 30, 2017
1 parent 4b6b843 commit 8473df5
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 425 deletions.
15 changes: 7 additions & 8 deletions python/plugins/db_manager/dlg_add_geometry_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog, QMessageBox, QApplication
from qgis.utils import OverrideCursor

from .db_plugins.plugin import DbError
from .dlg_db_error import DlgDbError
Expand Down Expand Up @@ -59,13 +60,11 @@ def createGeomColumn(self):
createSpatialIndex = False

# now create the geometry column
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
self.table.addGeometryColumn(name, geom_type, srid, dim, createSpatialIndex)
except DbError as e:
DlgDbError.showError(e, self)
return
finally:
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
try:
self.table.addGeometryColumn(name, geom_type, srid, dim, createSpatialIndex)
except DbError as e:
DlgDbError.showError(e, self)
return

self.accept()
15 changes: 7 additions & 8 deletions python/plugins/db_manager/dlg_create_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog, QApplication
from qgis.utils import OverrideCursor

from .db_plugins.plugin import DbError
from .dlg_db_error import DlgDbError
Expand Down Expand Up @@ -52,14 +53,12 @@ def createConstraint(self):
constr = self.getConstraint()

# now create the constraint
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
self.table.addConstraint(constr)
except DbError as e:
DlgDbError.showError(e, self)
return
finally:
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
try:
self.table.addConstraint(constr)
except DbError as e:
DlgDbError.showError(e, self)
return

self.accept()

Expand Down
15 changes: 7 additions & 8 deletions python/plugins/db_manager/dlg_create_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog, QMessageBox, QApplication
from qgis.utils import OverrideCursor

from .db_plugins.plugin import DbError
from .dlg_db_error import DlgDbError
Expand Down Expand Up @@ -60,14 +61,12 @@ def createIndex(self):
return

# now create the index
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
self.table.addIndex(idx)
except DbError as e:
DlgDbError.showError(e, self)
return
finally:
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
try:
self.table.addIndex(idx)
except DbError as e:
DlgDbError.showError(e, self)
return

self.accept()

Expand Down
88 changes: 43 additions & 45 deletions python/plugins/db_manager/dlg_export_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
QgsCoordinateReferenceSystem,
QgsVectorLayerExporter,
QgsSettings)
from qgis.utils import OverrideCursor

from .ui.ui_DlgExportVector import Ui_DbManagerDlgExportVector as Ui_Dialog

Expand Down Expand Up @@ -145,51 +146,48 @@ def accept(self):
self.tr("Invalid target srid: must be an integer"))
return

# override cursor
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
# store current input layer crs, so I can restore it later
prevInCrs = self.inLayer.crs()
try:
uri = self.editOutputFile.text()
providerName = "ogr"

options = {}

# set the OGR driver will be used
driverName = self.cboFileFormat.currentData()
options['driverName'] = driverName

# set the output file encoding
if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
enc = self.cboEncoding.currentText()
options['fileEncoding'] = enc

if self.chkDropTable.isChecked():
options['overwrite'] = True

outCrs = QgsCoordinateReferenceSystem()
if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
targetSrid = int(self.editTargetSrid.text())
outCrs = QgsCoordinateReferenceSystem(targetSrid)

# update input layer crs
if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
sourceSrid = int(self.editSourceSrid.text())
inCrs = QgsCoordinateReferenceSystem(sourceSrid)
self.inLayer.setCrs(inCrs)

# do the export!
ret, errMsg = QgsVectorLayerExporter.exportLayer(self.inLayer, uri, providerName, outCrs,
False, options)
except Exception as e:
ret = -1
errMsg = str(e)

finally:
# restore input layer crs and encoding
self.inLayer.setCrs(prevInCrs)
# restore cursor
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
# store current input layer crs, so I can restore it later
prevInCrs = self.inLayer.crs()
try:
uri = self.editOutputFile.text()
providerName = "ogr"

options = {}

# set the OGR driver will be used
driverName = self.cboFileFormat.currentData()
options['driverName'] = driverName

# set the output file encoding
if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
enc = self.cboEncoding.currentText()
options['fileEncoding'] = enc

if self.chkDropTable.isChecked():
options['overwrite'] = True

outCrs = QgsCoordinateReferenceSystem()
if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
targetSrid = int(self.editTargetSrid.text())
outCrs = QgsCoordinateReferenceSystem(targetSrid)

# update input layer crs
if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
sourceSrid = int(self.editSourceSrid.text())
inCrs = QgsCoordinateReferenceSystem(sourceSrid)
self.inLayer.setCrs(inCrs)

# do the export!
ret, errMsg = QgsVectorLayerExporter.exportLayer(self.inLayer, uri, providerName, outCrs,
False, options)
except Exception as e:
ret = -1
errMsg = str(e)

finally:
# restore input layer crs and encoding
self.inLayer.setCrs(prevInCrs)

if ret != 0:
QMessageBox.warning(self, self.tr("Export to file"), self.tr("Error {0}\n{1}").format(ret, errMsg))
Expand Down
156 changes: 77 additions & 79 deletions python/plugins/db_manager/dlg_import_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
QgsProject,
QgsSettings)
from qgis.gui import QgsMessageViewer
from qgis.utils import OverrideCursor

from .ui.ui_DlgImportVector import Ui_DbManagerDlgImportVector as Ui_Dialog

Expand Down Expand Up @@ -292,85 +293,82 @@ def accept(self):
self.tr("Invalid target srid: must be an integer"))
return

# override cursor
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
# store current input layer crs and encoding, so I can restore it
prevInCrs = self.inLayer.crs()
prevInEncoding = self.inLayer.dataProvider().encoding()

try:
schema = self.outUri.schema() if not self.cboSchema.isEnabled() else self.cboSchema.currentText()
table = self.cboTable.currentText()

# get pk and geom field names from the source layer or use the
# ones defined by the user
srcUri = QgsDataSourceUri(self.inLayer.source())

pk = srcUri.keyColumn() if not self.chkPrimaryKey.isChecked() else self.editPrimaryKey.text()
if not pk:
pk = self.default_pk

if self.inLayer.isSpatial() and self.chkGeomColumn.isEnabled():
geom = srcUri.geometryColumn() if not self.chkGeomColumn.isChecked() else self.editGeomColumn.text()
if not geom:
geom = self.default_geom
else:
geom = None

options = {}
if self.chkLowercaseFieldNames.isEnabled() and self.chkLowercaseFieldNames.isChecked():
pk = pk.lower()
if geom:
geom = geom.lower()
options['lowercaseFieldNames'] = True

# get output params, update output URI
self.outUri.setDataSource(schema, table, geom, "", pk)
typeName = self.db.dbplugin().typeName()
providerName = self.db.dbplugin().providerName()
if typeName == 'gpkg':
uri = self.outUri.database()
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = table
else:
uri = self.outUri.uri(False)

if self.chkDropTable.isChecked():
options['overwrite'] = True

if self.chkSinglePart.isEnabled() and self.chkSinglePart.isChecked():
options['forceSinglePartGeometryType'] = True

outCrs = QgsCoordinateReferenceSystem()
if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
targetSrid = int(self.editTargetSrid.text())
outCrs = QgsCoordinateReferenceSystem(targetSrid)

# update input layer crs and encoding
if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
sourceSrid = int(self.editSourceSrid.text())
inCrs = QgsCoordinateReferenceSystem(sourceSrid)
self.inLayer.setCrs(inCrs)

if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
enc = self.cboEncoding.currentText()
self.inLayer.setProviderEncoding(enc)

onlySelected = self.chkSelectedFeatures.isChecked()

# do the import!
ret, errMsg = QgsVectorLayerExporter.exportLayer(self.inLayer, uri, providerName, outCrs, onlySelected, options)
except Exception as e:
ret = -1
errMsg = str(e)

finally:
# restore input layer crs and encoding
self.inLayer.setCrs(prevInCrs)
self.inLayer.setProviderEncoding(prevInEncoding)
# restore cursor
QApplication.restoreOverrideCursor()
with OverrideCursor(Qt.WaitCursor):
# store current input layer crs and encoding, so I can restore it
prevInCrs = self.inLayer.crs()
prevInEncoding = self.inLayer.dataProvider().encoding()

try:
schema = self.outUri.schema() if not self.cboSchema.isEnabled() else self.cboSchema.currentText()
table = self.cboTable.currentText()

# get pk and geom field names from the source layer or use the
# ones defined by the user
srcUri = QgsDataSourceUri(self.inLayer.source())

pk = srcUri.keyColumn() if not self.chkPrimaryKey.isChecked() else self.editPrimaryKey.text()
if not pk:
pk = self.default_pk

if self.inLayer.isSpatial() and self.chkGeomColumn.isEnabled():
geom = srcUri.geometryColumn() if not self.chkGeomColumn.isChecked() else self.editGeomColumn.text()
if not geom:
geom = self.default_geom
else:
geom = None

options = {}
if self.chkLowercaseFieldNames.isEnabled() and self.chkLowercaseFieldNames.isChecked():
pk = pk.lower()
if geom:
geom = geom.lower()
options['lowercaseFieldNames'] = True

# get output params, update output URI
self.outUri.setDataSource(schema, table, geom, "", pk)
typeName = self.db.dbplugin().typeName()
providerName = self.db.dbplugin().providerName()
if typeName == 'gpkg':
uri = self.outUri.database()
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = table
else:
uri = self.outUri.uri(False)

if self.chkDropTable.isChecked():
options['overwrite'] = True

if self.chkSinglePart.isEnabled() and self.chkSinglePart.isChecked():
options['forceSinglePartGeometryType'] = True

outCrs = QgsCoordinateReferenceSystem()
if self.chkTargetSrid.isEnabled() and self.chkTargetSrid.isChecked():
targetSrid = int(self.editTargetSrid.text())
outCrs = QgsCoordinateReferenceSystem(targetSrid)

# update input layer crs and encoding
if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
sourceSrid = int(self.editSourceSrid.text())
inCrs = QgsCoordinateReferenceSystem(sourceSrid)
self.inLayer.setCrs(inCrs)

if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
enc = self.cboEncoding.currentText()
self.inLayer.setProviderEncoding(enc)

onlySelected = self.chkSelectedFeatures.isChecked()

# do the import!
ret, errMsg = QgsVectorLayerExporter.exportLayer(self.inLayer, uri, providerName, outCrs, onlySelected, options)
except Exception as e:
ret = -1
errMsg = str(e)

finally:
# restore input layer crs and encoding
self.inLayer.setCrs(prevInCrs)
self.inLayer.setProviderEncoding(prevInEncoding)

if ret != 0:
output = QgsMessageViewer()
Expand Down
Loading

0 comments on commit 8473df5

Please sign in to comment.