Skip to content
Permalink
Browse files

refresh list of layers when layers in TOC change

git-svn-id: http://svn.osgeo.org/qgis/trunk@15474 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
brushtyler
brushtyler committed Mar 14, 2011
1 parent bf58ae2 commit 167df200ef75a3535b0aacbb5af810754f3e6809
@@ -78,8 +78,9 @@ def initGui( self ):
+ QCoreApplication.translate( "GdalTools", "This version of Gdal Tools requires at least QGIS version 1.0.0\nPlugin will not be enabled." ) )
return None

from tools.GdalTools_utils import Version, GdalConfig
from tools.GdalTools_utils import Version, GdalConfig, LayerRegistry
self.GdalVersion = Version( GdalConfig.version() )
LayerRegistry.setIface( self.iface )

# find the Raster menu
rasterMenu = None
@@ -8,8 +8,10 @@

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from qgis.gui import *

from osgeo import gdal
from osgeo.gdalconst import *
from osgeo import ogr
@@ -90,34 +92,72 @@ def getVectorExtensions():
extensions << FileFilter.getFilterExtensions( f )
return extensions

def getRasterLayers():
# mantein the reference between combobox indexes and canvas layers
layers = dict()
count = 0
names = []
layerMap = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layerMap.iteritems():
# only raster layers, but not WMS ones
if layer.type() == layer.RasterLayer:
if layer.usesProvider() and layer.providerKey() != 'gdal':
continue
layers[count] = layer
names.append(layer.name())
count = count +1
return (layers, names)

def getVectorLayers():
# mantein the reference between combobox indexes and canvas layers
layers = dict()
count = 0
names = []
layerMap = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layerMap.iteritems():
if layer.type() == layer.VectorLayer:
layers[count] = layer
names.append(layer.name())
count = count +1
return (layers, names)
class LayerRegistry(QObject):

_instance = None
_iface = None

@staticmethod
def instance():
if LayerRegistry._instance == None:
LayerRegistry._instance = LayerRegistry()
return LayerRegistry._instance

@staticmethod
def setIface(iface):
LayerRegistry._iface = iface

layers = []

def __init__(self):
QObject.__init__(self)
if LayerRegistry._instance != None:
return

LayerRegistry.layers = self.getAllLayers()
LayerRegistry._instance = self
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("removedAll()"), self.removeAllLayers)
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerWasAdded(QgsMapLayer *)"), self.layerAdded)
self.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerWillBeRemoved(QString)"), self.removeLayer)

def getAllLayers(self):
if LayerRegistry._iface and hasattr(LayerRegistry._iface, 'legendInterface'):
return LayerRegistry._iface.legendInterface().layers()
return QgsMapLayerRegistry.instance().mapLayers().values()

def layerAdded(self, layer):
LayerRegistry.layers.append( layer )
self.emit( SIGNAL( "layersChanged" ) )

def removeLayer(self, layerId):
LayerRegistry.layers = filter( lambda x: x.getLayerID() != layerId, LayerRegistry.layers)
self.emit( SIGNAL( "layersChanged" ) )

def removeAllLayers(self):
LayerRegistry.layers = []
self.emit( SIGNAL( "layersChanged" ) )

def getRasterLayers(self):
layers = []
names = []

for layer in LayerRegistry.layers:
# only gdal raster layers
if layer.type() == layer.RasterLayer:
if layer.usesProvider() and layer.providerKey() != 'gdal':
continue
layers.append(layer)
names.append(layer.name())
return (layers, names)

def getVectorLayers(self):
layers = []
names = []
for layer in LayerRegistry.layers:
if layer.type() == layer.VectorLayer:
layers.append(layer)
names.append(layer.name())
return (layers, names)

def getRasterFiles(path, recursive=False):
rasters = QStringList()
@@ -17,6 +17,7 @@ class GdalToolsBaseDialog(QDialog, Ui_Dialog):

def __init__(self, parent, iface, pluginBase, pluginName, pluginCommand):
QDialog.__init__(self, parent)
self.setAttribute(Qt.WA_DeleteOnClose)
self.iface = iface

self.process = QProcess(self)
@@ -13,6 +13,7 @@ class GdalToolsAboutDialog(QDialog, Ui_Dialog):

def __init__(self, iface):
QDialog.__init__(self, iface.mainWindow())
self.setAttribute(Qt.WA_DeleteOnClose)
self.iface = iface
self.setupUi(self)

@@ -33,11 +33,12 @@ def __init__(self, iface):
self.connect(self.selectOutputDirButton, SIGNAL("clicked()"), self.fillOutputDirEdit)

# fill layers combo
self.connect(Utils.LayerRegistry.instance(), SIGNAL("layersChanged"), self.fillInputLayerCombo)
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFileEdit(self):
@@ -58,8 +58,6 @@ def __init__(self, iface):
self.connect(self.inputLayerCombo, SIGNAL("currentIndexChanged(int)"), self.fillFieldsCombo)
self.connect(self.extentGroup, SIGNAL("toggled(bool)"), self.onExtentCheckedChenged)

# fill layers combo
self.fillInputLayerCombo()

def onClosing(self):
self.extentSelector.stop()
@@ -68,9 +66,12 @@ def onClosing(self):
def onExtentCheckedChenged(self, enabled):
self.extentSelector.start() if enabled else self.extentSelector.stop()

def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo(self):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getVectorLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getVectorLayers()
self.inputLayerCombo.addItems( names )

def fillFieldsCombo(self):
@@ -40,8 +40,6 @@ def __init__( self, iface ):
self.copyAll = QAction( self.tr( "Copy all" ), self )
QObject.connect( self.copyAll, SIGNAL( "triggered()" ), self.doCopyAll )

# fill layers combo
self.fillInputLayerCombo()

def doCopyLine( self ):
output = QString()
@@ -71,9 +69,12 @@ def keyPressEvent( self, e ):
else:
QWidget.keyPressEvent( self, e )

def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def finished( self ):
@@ -33,12 +33,12 @@ def __init__(self, iface):
self.connect(self.selectInputFileButton, SIGNAL("clicked()"), self.fillInputFileEdit)
self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit)

# fill layers combo
def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFileEdit(self):
@@ -44,8 +44,6 @@ def __init__( self, iface ):
self.connect( self.selectInputFileButton, SIGNAL( "clicked()" ), self.fillInputFile )
self.connect( self.batchCheck, SIGNAL( "stateChanged( int )" ), self.switchToolMode )

# fill layers combo
self.fillInputLayerCombo()

# switch to batch or normal mode
def switchToolMode( self ):
@@ -71,9 +69,12 @@ def switchToolMode( self ):
QObject.disconnect( self.selectInputFileButton, SIGNAL( "clicked()" ), self.fillInputDir )
QObject.connect( self.selectInputFileButton, SIGNAL( "clicked()" ), self.fillInputFile )

def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFile( self ):
@@ -39,7 +39,6 @@ def __init__(self, iface):
self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit)
self.connect( self.batchCheck, SIGNAL( "stateChanged( int )" ), self.switchToolMode )

self.fillInputLayerCombo()

# switch to batch or normal mode
def switchToolMode( self ):
@@ -77,9 +76,12 @@ def switchToolMode( self ):
QObject.connect( self.selectInputFileButton, SIGNAL( "clicked()" ), self.fillInputFile )
QObject.connect( self.selectOutputFileButton, SIGNAL( "clicked()" ), self.fillOutputFileEdit )

def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo(self):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFile(self):
@@ -31,12 +31,12 @@ def __init__(self, iface):
self.connect(self.selectInputFileButton, SIGNAL("clicked()"), self.fillInputFileEdit)
self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit)

# fill layers combo
def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFileEdit(self):
@@ -37,12 +37,12 @@ def __init__(self, iface):
self.connect(self.selectInputFileButton, SIGNAL("clicked()"), self.fillInputFileEdit)
self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit)

# fill layers combo
def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFileEdit(self):
@@ -37,12 +37,12 @@ def __init__(self, iface):
self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit)
self.connect(self.inputLayerCombo, SIGNAL("currentIndexChanged(int)"), self.fillFieldsCombo)

# fill layers combo
def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getVectorLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getVectorLayers()
self.inputLayerCombo.addItems( names )

def fillFieldsCombo(self):
@@ -37,7 +37,6 @@ def __init__(self, iface):
self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit)
self.connect( self.batchCheck, SIGNAL( "stateChanged( int )" ), self.switchToolMode )

self.fillInputLayerCombo()

# switch to batch or normal mode
def switchToolMode( self ):
@@ -75,9 +74,12 @@ def switchToolMode( self ):
QObject.connect( self.selectInputFileButton, SIGNAL( "clicked()" ), self.fillInputFile )
QObject.connect( self.selectOutputFileButton, SIGNAL( "clicked()" ), self.fillOutputFileEdit )

def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo(self):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFile(self):
@@ -11,6 +11,7 @@
class GdalToolsSettingsDialog( QDialog, Ui_Dialog ):
def __init__( self, iface ):
QDialog.__init__( self, iface.mainWindow() )
self.setAttribute(Qt.WA_DeleteOnClose)
self.iface = iface
self.setupUi( self )

@@ -32,12 +32,12 @@ def __init__(self, iface):
self.connect(self.selectInputFileButton, SIGNAL("clicked()"), self.fillInputFileEdit)
self.connect(self.selectOutputFileButton, SIGNAL("clicked()"), self.fillOutputFileEdit)

# fill layers combo
def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo( self ):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFileEdit(self):
@@ -61,8 +61,6 @@ def __init__(self, iface):
# add raster filters to combo
self.formatCombo.addItems( Utils.FileFilter.allRastersFilter().split( ";;" ) )

# add layers to combo
self.fillInputLayerCombo()

def switchToolMode( self ):
self.setCommandViewerEnabled( not self.batchCheck.isChecked() )
@@ -103,9 +101,12 @@ def switchToolMode( self ):
QObject.connect( self.selectInputFileButton, SIGNAL( "clicked()" ), self.fillInputFile )
QObject.connect( self.selectOutputFileButton, SIGNAL( "clicked()" ), self.fillOutputFileEdit )

def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo(self):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFile( self ):
@@ -51,7 +51,6 @@ def __init__(self, iface):
self.connect(self.selectTargetSRSButton, SIGNAL("clicked()"), self.fillTargetSRSEdit)
self.connect( self.batchCheck, SIGNAL( "stateChanged( int )" ), self.switchToolMode )

self.fillInputLayerCombo()

# switch to batch or normal mode
def switchToolMode( self ):
@@ -89,9 +88,12 @@ def switchToolMode( self ):
QObject.connect( self.selectInputFileButton, SIGNAL( "clicked()" ), self.fillInputFile )
QObject.connect( self.selectOutputFileButton, SIGNAL( "clicked()" ), self.fillOutputFileEdit )

def onLayersChanged(self):
self.fillInputLayerCombo()

def fillInputLayerCombo(self):
self.inputLayerCombo.clear()
( self.layers, names ) = Utils.getRasterLayers()
( self.layers, names ) = Utils.LayerRegistry.instance().getRasterLayers()
self.inputLayerCombo.addItems( names )

def fillInputFile(self):

0 comments on commit 167df20

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