Skip to content
Permalink
Browse files

[DBManager] remove layers from QgsMapLayerRegistry once refreshing a …

…table, previewing a different layer or closing the DBManager main window (fix #12938)
  • Loading branch information
brushtyler committed Sep 4, 2015
1 parent e6f4fa6 commit eea81e81c54dba518422f2ff2a6948b049b123b9
Showing with 22 additions and 16 deletions.
  1. +4 −0 python/plugins/db_manager/db_manager.py
  2. +18 −16 python/plugins/db_manager/layer_preview.py
@@ -58,6 +58,8 @@ def __init__(self, iface, parent=None):

def closeEvent(self, e):
self.unregisterAllActions()
# clear preview, this will delete the layer in preview tab
self.preview.loadPreview(None)

# save the window state
settings = QSettings()
@@ -82,6 +84,8 @@ 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)
@@ -38,6 +38,7 @@ def __init__(self, parent=None):

self.item = None
self.dirty = False
self.currentLayer = None

# reuse settings from QGIS
settings = QSettings()
@@ -46,8 +47,6 @@ def __init__(self, parent=None):
zoomFactor = settings.value("/qgis/zoom_factor", 2, type=float)
self.setWheelAction(QgsMapCanvas.WheelAction(action), zoomFactor)

self._clear()

def refresh(self):
self.setDirty(True)
self.loadPreview(self.item)
@@ -80,19 +79,18 @@ def _clear(self):
self.disconnect(self.item, SIGNAL('aboutToChange'), self.setDirty)
except RuntimeError:
pass

self.item = None
self.dirty = False

self.currentLayerId = None
self.setLayerSet([])
self._loadTablePreview(None)

def _loadTablePreview(self, table, limit=False):
""" if has geometry column load to map canvas """
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
self.setRenderFlag(False)
newLayerId = None
vl = None

if table.geomType:
if table and table.geomType:
# limit the query result if required
if limit and table.rowCount > 1000:
uniqueField = table.getValidQGisUniqueFields(True)
@@ -112,17 +110,21 @@ def _loadTablePreview(self, table, limit=False):
vl = table.toMapLayer()

if not vl.isValid():
self.setLayerSet([])
else:
newLayerId = vl.id() if hasattr(vl, 'id') else vl.id()
self.setLayerSet([QgsMapCanvasLayer(vl)])
QgsMapLayerRegistry.instance().addMapLayers([vl], False)
self.zoomToFullExtent()
vl.deleteLater()
vl = None

# remove old layer (if any) and set new
if self.currentLayerId:
QgsMapLayerRegistry.instance().removeMapLayers([self.currentLayerId], False)
self.currentLayerId = newLayerId
if self.currentLayer:
QgsMapLayerRegistry.instance().removeMapLayers([self.currentLayer.id()])

if vl:
self.setLayerSet([QgsMapCanvasLayer(vl)])
QgsMapLayerRegistry.instance().addMapLayers([vl], False)
self.zoomToFullExtent()
else:
self.setLayerSet([])

self.currentLayer = vl

self.setRenderFlag(True)
QApplication.restoreOverrideCursor()

0 comments on commit eea81e8

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