Skip to content
Permalink
Browse files

Remove usage of legendInterface() from plugins (followup e5f62e4)

  • Loading branch information
wonder-sk committed Nov 22, 2016
1 parent 0c58555 commit d7166404030292b9536e649795e9efec49225dfd
@@ -37,7 +37,7 @@
from qgis.PyQt.QtCore import QObject, QSettings, QFileInfo, QDir, QCoreApplication, pyqtSignal
from qgis.PyQt.QtWidgets import QFileDialog

from qgis.core import QgsApplication, QgsMapLayerRegistry, QgsRectangle, QgsProviderRegistry, QgsLogger
from qgis.core import QgsApplication, QgsMapLayerRegistry, QgsRectangle, QgsProviderRegistry, QgsLogger, QgsProject
from qgis.gui import QgsEncodingFileDialog

from osgeo import gdal, ogr, osr
@@ -187,9 +187,7 @@ def __init__(self):
QgsMapLayerRegistry.instance().layerWillBeRemoved.connect(self.removeLayer)

def getAllLayers(self):
if LayerRegistry._iface and hasattr(LayerRegistry._iface, 'legendInterface'):
return LayerRegistry._iface.legendInterface().layers()
return list(QgsMapLayerRegistry.instance().mapLayers().values())
return list(node.layer() for node in QgsProject.instance().layerTreeRoot().findLayers())

def layerAdded(self, layer):
LayerRegistry.layers.append(layer)
@@ -56,7 +56,7 @@ def initGui(self):
self.iface.mainWindow())
self.layerAction.setObjectName("dbManagerUpdateSqlLayer")
self.layerAction.triggered.connect(self.onUpdateSqlLayer)
self.iface.legendInterface().addLegendLayerAction(self.layerAction, "", "dbManagerUpdateSqlLayer", QgsMapLayer.VectorLayer, False)
self.iface.addCustomActionForLayerType(self.layerAction, "", QgsMapLayer.VectorLayer, False)
for l in list(QgsMapLayerRegistry.instance().mapLayers().values()):
self.onLayerWasAdded(l)
QgsMapLayerRegistry.instance().layerWasAdded.connect(self.onLayerWasAdded)
@@ -72,7 +72,7 @@ def unload(self):
else:
self.iface.removeToolBarIcon(self.action)

self.iface.legendInterface().removeLegendLayerAction(self.layerAction)
self.iface.removeCustomActionForLayerType(self.layerAction)
QgsMapLayerRegistry.instance().layerWasAdded.disconnect(self.onLayerWasAdded)

if self.dlg is not None:
@@ -82,15 +82,15 @@ def onLayerWasAdded(self, aMapLayer):
if hasattr(aMapLayer, 'dataProvider') and aMapLayer.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
uri = QgsDataSourceUri(aMapLayer.source())
if re.search('^\(SELECT .+ FROM .+\)$', uri.table(), re.S):
self.iface.legendInterface().addLegendLayerActionForLayer(self.layerAction, aMapLayer)
self.addCustomActionForLayer(self.layerAction, aMapLayer)
# virtual has QUrl source
# url = QUrl(QUrl.fromPercentEncoding(l.source()))
# url.queryItemValue('query')
# url.queryItemValue('uid')
# url.queryItemValue('geometry')

def onUpdateSqlLayer(self):
l = self.iface.legendInterface().currentLayer()
l = self.iface.activeLayer()
if l.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
uri = QgsDataSourceUri(l.source())
if re.search('^\(SELECT .+ FROM .+\)$', uri.table(), re.S):
@@ -97,34 +97,27 @@ def run(item, action, mainwindow):
# load layers into the current project
toponame = item.schema().name
template_dir = os.path.join(current_path, 'templates')
registry = QgsMapLayerRegistry.instance()
legend = iface.legendInterface()

# do not refresh the canvas until all the layers are added
prevRenderFlagState = iface.mapCanvas().renderFlag()
iface.mapCanvas().setRenderFlag(False)
try:
supergroup = legend.addGroup(u'Topology "%s"' % toponame, False)
provider = db.dbplugin().providerName()
uri = db.uri()

# Force use of estimated metadata (topologies can be big)
uri.setUseEstimatedMetadata(True)

# FACES
group = legend.addGroup(u'Faces', False, supergroup)

# face mbr
uri.setDataSource(toponame, 'face', 'mbr', '', 'face_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.Polygon)
layer = QgsVectorLayer(uri.uri(False), u'%s.face_mbr' % toponame, provider)
layer.loadNamedStyle(os.path.join(template_dir, 'face_mbr.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
face_extent = layer.extent()
layerFaceMbr = QgsVectorLayer(uri.uri(False), u'%s.face_mbr' % toponame, provider)
layerFaceMbr.loadNamedStyle(os.path.join(template_dir, 'face_mbr.qml'))

face_extent = layerFaceMbr.extent()

# face geometry
sql = u'SELECT face_id, topology.ST_GetFaceGeometry(%s,' \
@@ -134,13 +127,9 @@ def run(item, action, mainwindow):
uri.setDataSource('', u'(%s\n)' % sql, 'geom', '', 'face_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.Polygon)
layer = QgsVectorLayer(uri.uri(False), u'%s.face' % toponame, provider)
layer.setExtent(face_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'face.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerFaceGeom = QgsVectorLayer(uri.uri(False), u'%s.face' % toponame, provider)
layerFaceGeom.setExtent(face_extent)
layerFaceGeom.loadNamedStyle(os.path.join(template_dir, 'face.qml'))

# face_seed
sql = u'SELECT face_id, ST_PointOnSurface(' \
@@ -151,128 +140,117 @@ def run(item, action, mainwindow):
uri.setDataSource('', u'(%s)' % sql, 'geom', '', 'face_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.Point)
layer = QgsVectorLayer(uri.uri(False), u'%s.face_seed' % toponame, provider)
layer.setExtent(face_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'face_seed.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerFaceSeed = QgsVectorLayer(uri.uri(False), u'%s.face_seed' % toponame, provider)
layerFaceSeed.setExtent(face_extent)
layerFaceSeed.loadNamedStyle(os.path.join(template_dir, 'face_seed.qml'))

# TODO: add polygon0, polygon1 and polygon2 ?

# NODES
group = legend.addGroup(u'Nodes', False, supergroup)

# node
uri.setDataSource(toponame, 'node', 'geom', '', 'node_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.Point)
layer = QgsVectorLayer(uri.uri(False), u'%s.node' % toponame, provider)
layer.loadNamedStyle(os.path.join(template_dir, 'node.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
node_extent = layer.extent()
layerNode = QgsVectorLayer(uri.uri(False), u'%s.node' % toponame, provider)
layerNode.loadNamedStyle(os.path.join(template_dir, 'node.qml'))
node_extent = layerNode.extent()

# node labels
uri.setDataSource(toponame, 'node', 'geom', '', 'node_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.Point)
layer = QgsVectorLayer(uri.uri(False), u'%s.node_id' % toponame, provider)
layer.setExtent(node_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'node_label.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerNodeLabel = QgsVectorLayer(uri.uri(False), u'%s.node_id' % toponame, provider)
layerNodeLabel.setExtent(node_extent)
layerNodeLabel.loadNamedStyle(os.path.join(template_dir, 'node_label.qml'))

# EDGES
group = legend.addGroup(u'Edges', False, supergroup)

# edge
uri.setDataSource(toponame, 'edge_data', 'geom', '', 'edge_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.LineString)
layer = QgsVectorLayer(uri.uri(False), u'%s.edge' % toponame, provider)
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
edge_extent = layer.extent()
layerEdge = QgsVectorLayer(uri.uri(False), u'%s.edge' % toponame, provider)
edge_extent = layerEdge.extent()

# directed edge
uri.setDataSource(toponame, 'edge_data', 'geom', '', 'edge_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.LineString)
layer = QgsVectorLayer(uri.uri(False), u'%s.directed_edge' % toponame, provider)
layer.setExtent(edge_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'edge.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerDirectedEdge = QgsVectorLayer(uri.uri(False), u'%s.directed_edge' % toponame, provider)
layerDirectedEdge.setExtent(edge_extent)
layerDirectedEdge.loadNamedStyle(os.path.join(template_dir, 'edge.qml'))

# edge labels
uri.setDataSource(toponame, 'edge_data', 'geom', '', 'edge_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.LineString)
layer = QgsVectorLayer(uri.uri(False), u'%s.edge_id' % toponame, provider)
layer.setExtent(edge_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'edge_label.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerEdgeLabel = QgsVectorLayer(uri.uri(False), u'%s.edge_id' % toponame, provider)
layerEdgeLabel.setExtent(edge_extent)
layerEdgeLabel.loadNamedStyle(os.path.join(template_dir, 'edge_label.qml'))

# face_left
uri.setDataSource(toponame, 'edge_data', 'geom', '', 'edge_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.LineString)
layer = QgsVectorLayer(uri.uri(False), u'%s.face_left' % toponame, provider)
layer.setExtent(edge_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'face_left.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerFaceLeft = QgsVectorLayer(uri.uri(False), u'%s.face_left' % toponame, provider)
layerFaceLeft.setExtent(edge_extent)
layerFaceLeft.loadNamedStyle(os.path.join(template_dir, 'face_left.qml'))

# face_right
uri.setDataSource(toponame, 'edge_data', 'geom', '', 'edge_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.LineString)
layer = QgsVectorLayer(uri.uri(False), u'%s.face_right' % toponame, provider)
layer.setExtent(edge_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'face_right.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerFaceRight = QgsVectorLayer(uri.uri(False), u'%s.face_right' % toponame, provider)
layerFaceRight.setExtent(edge_extent)
layerFaceRight.loadNamedStyle(os.path.join(template_dir, 'face_right.qml'))

# next_left
uri.setDataSource(toponame, 'edge_data', 'geom', '', 'edge_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.LineString)
layer = QgsVectorLayer(uri.uri(False), u'%s.next_left' % toponame, provider)
layer.setExtent(edge_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'next_left.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerNextLeft = QgsVectorLayer(uri.uri(False), u'%s.next_left' % toponame, provider)
layerNextLeft.setExtent(edge_extent)
layerNextLeft.loadNamedStyle(os.path.join(template_dir, 'next_left.qml'))

# next_right
uri.setDataSource(toponame, 'edge_data', 'geom', '', 'edge_id')
uri.setSrid(toposrid)
uri.setWkbType(QgsWkbTypes.LineString)
layer = QgsVectorLayer(uri.uri(False), u'%s.next_right' % toponame, provider)
layer.setExtent(edge_extent)
layer.loadNamedStyle(os.path.join(template_dir, 'next_right.qml'))
registry.addMapLayers([layer])
legend.moveLayer(layer, group)
legend.setLayerVisible(layer, False)
legend.setLayerExpanded(layer, False)
layerNextRight = QgsVectorLayer(uri.uri(False), u'%s.next_right' % toponame, provider)
layerNextRight.setExtent(edge_extent)
layerNextRight.loadNamedStyle(os.path.join(template_dir, 'next_right.qml'))

# Add layers to the layer tree

faceLayers = [layerFaceMbr, layerFaceGeom, layerFaceSeed]
nodeLayers = [layerNode, layerNodeLabel]
edgeLayers = [layerEdge, layerDirectedEdge, layerEdgeLabel, layerEdgeFaceLeft, layerEdgeFaceRight, layerEdgeNextLeft, layerEdgeNextRight]

QgsMapLayerRegistry.instance().addMapLayers(faceLayers, False)

groupFaces = QgsLayerTreeGroup(u'Faces')
for layer in faceLayers:
nodeLayer = groupFaces.addLayer(layer)
nodeLayer.setVisible(Qt.Unchecked)
nodeLayer.setExpanded(False)

groupNodes = QgsLayerTreeGroup(u'Nodes')
for layer in faceLayers:
nodeLayer = groupNodes.addLayer(layer)
nodeLayer.setVisible(Qt.Unchecked)
nodeLayer.setExpanded(False)

groupEdges = QgsLayerTreeGroup(u'Edges')
for layer in faceLayers:
nodeLayer = groupEdges.addLayer(layer)
nodeLayer.setVisible(Qt.Unchecked)
nodeLayer.setExpanded(False)

supergroup = QgsLayerTreeGroup(u'Topology "%s"' % toponame)
supergroup.insertChildNodes(-1, [groupFaces, groupNodes, groupEdges])

QgsProject.instance().layerTreeRoot().addChildNode(supergroup)

finally:

@@ -28,7 +28,7 @@
from qgis.PyQt.QtWidgets import QDialog, QFileDialog, QMessageBox, QApplication
from qgis.PyQt.QtGui import QCursor

from qgis.core import QgsDataSourceUri, QgsVectorLayer, QgsRasterLayer, QgsMimeDataUtils, QgsMapLayer, QgsProviderRegistry, QgsCoordinateReferenceSystem, QgsVectorLayerImport
from qgis.core import QgsDataSourceUri, QgsVectorLayer, QgsRasterLayer, QgsMimeDataUtils, QgsMapLayer, QgsProviderRegistry, QgsCoordinateReferenceSystem, QgsVectorLayerImport, QgsProject, QgsMapLayerRegistry
from qgis.gui import QgsMessageViewer
from qgis.utils import iface

@@ -118,10 +118,11 @@ def checkSupports(self):

def populateLayers(self):
self.cboInputLayer.clear()
for index, layer in enumerate(iface.legendInterface().layers()):
for nodeLayer in QgsProject.instance().layerTreeRoot().findLayers():
layer = nodeLayer.layer()
# TODO: add import raster support!
if layer.type() == QgsMapLayer.VectorLayer:
self.cboInputLayer.addItem(layer.name(), index)
self.cboInputLayer.addItem(layer.name(), layer.id())

def deleteInputLayer(self):
""" unset the input layer, then destroy it but only if it was created from this dialog """
@@ -181,8 +182,8 @@ def reloadInputLayer(self):
self.inLayerMustBeDestroyed = True

else:
legendIndex = self.cboInputLayer.itemData(index)
self.inLayer = iface.legendInterface().layers()[legendIndex]
layerId = self.cboInputLayer.itemData(index)
self.inLayer = QgsMapLayerRegistry.instance().mapLayer(layerId)
self.inLayerMustBeDestroyed = False

self.checkSupports()

1 comment on commit d716640

@nirvn

This comment has been minimized.

Copy link
Contributor

@nirvn nirvn commented on d716640 Nov 22, 2016

Thanks

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