Skip to content

Commit

Permalink
[DB Manager] previewing layers in Virtual layers section remove them …
Browse files Browse the repository at this point in the history
…from the Layers panel: fixies #16476

plus more vlayer db_manager plugin fixes to reduce (but can't avoid) exceptions due to C++/SIP object removes
  • Loading branch information
luipir authored and slarosa committed May 27, 2017
1 parent f3407f8 commit dae921c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 10 additions & 0 deletions python/plugins/db_manager/db_plugins/vlayers/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def getLayer(self, l):
lid = self.layers.get(l)
if lid is None:
return lid
# the instance can refer to a layer in map previe and not in qgis general canvas
if lid not in QgsMapLayerRegistry.instance().mapLayers().keys():
self.layers.pop(l)
return None
return QgsMapLayerRegistry.instance().mapLayer(lid)


Expand Down Expand Up @@ -246,12 +250,16 @@ def getRasterTables(self, schema=None):
def getTableRowCount(self, table):
t = table[1]
l = VLayerRegistry.instance().getLayer(t)
if not l or not l.isValid():
return None
return l.featureCount()

def getTableFields(self, table):
""" return list of columns in table """
t = table[1]
l = VLayerRegistry.instance().getLayer(t)
if not l or not l.isValid():
return []
# id, name, type, nonnull, default, pk
n = l.dataProvider().fields().size()
f = [(i, f.name(), f.typeName(), False, None, False)
Expand All @@ -277,6 +285,8 @@ def getTableExtent(self, table, geom):
l = QgsMapLayerRegistry.instance().mapLayer(t)
else:
l = VLayerRegistry.instance().getLayer(t)
if not l or not l.isValid():
return None
e = l.extent()
r = (e.xMinimum(), e.yMinimum(), e.xMaximum(), e.yMaximum())
return r
Expand Down
11 changes: 7 additions & 4 deletions python/plugins/db_manager/layer_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from qgis.PyQt.QtWidgets import QApplication

from qgis.gui import QgsMapCanvas, QgsMapCanvasLayer, QgsMessageBar
from qgis.core import QgsVectorLayer, QgsMapLayerRegistry
from qgis.core import QgsVectorLayer, QgsMapLayerRegistry, QgsProject

from .db_plugins.plugin import Table

Expand Down Expand Up @@ -113,15 +113,18 @@ def _loadTablePreview(self, table, limit=False):
else:
vl = table.toMapLayer()

if not vl.isValid():
if vl and not vl.isValid():
vl.deleteLater()
vl = None

# remove old layer (if any) and set new
if self.currentLayer:
QgsMapLayerRegistry.instance().removeMapLayers([self.currentLayer.id()])
# but not remove it if in layer list panel
# fix https://issues.qgis.org/issues/16476
if not QgsProject.instance().layerTreeRoot().findLayer(self.currentLayer.id()):
QgsMapLayerRegistry.instance().removeMapLayers([self.currentLayer.id()])

if vl:
if vl and vl.isValid():
self.setLayerSet([QgsMapCanvasLayer(vl)])
QgsMapLayerRegistry.instance().addMapLayers([vl], False)
self.zoomToFullExtent()
Expand Down

0 comments on commit dae921c

Please sign in to comment.