Skip to content

Commit

Permalink
[DBManager] remove layers from QgsMapLayerRegistry once refreshing a …
Browse files Browse the repository at this point in the history
…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 eea81e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions python/plugins/db_manager/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
34 changes: 18 additions & 16 deletions python/plugins/db_manager/layer_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, parent=None):

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

# reuse settings from QGIS
settings = QSettings()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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.