Skip to content

Commit

Permalink
refresh list of layers when layers in TOC change
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15474 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
brushtyler committed Mar 14, 2011
1 parent b4e10e4 commit fa92409
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 58 deletions.
3 changes: 2 additions & 1 deletion python/plugins/GdalTools/GdalTools.py
Expand Up @@ -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
Expand Down
96 changes: 68 additions & 28 deletions python/plugins/GdalTools/tools/GdalTools_utils.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions python/plugins/GdalTools/tools/dialogBase.py
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions python/plugins/GdalTools/tools/doAbout.py
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion python/plugins/GdalTools/tools/doContour.py
Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/GdalTools/tools/doGrid.py
Expand Up @@ -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()
Expand All @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/GdalTools/tools/doInfo.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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 ):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/GdalTools/tools/doNearBlack.py
Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/GdalTools/tools/doOverview.py
Expand Up @@ -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 ):
Expand All @@ -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 ):
Expand Down
6 changes: 4 additions & 2 deletions python/plugins/GdalTools/tools/doPctRgb.py
Expand Up @@ -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 ):
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/GdalTools/tools/doPolygonize.py
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/GdalTools/tools/doProximity.py
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/GdalTools/tools/doRasterize.py
Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions python/plugins/GdalTools/tools/doRgbPct.py
Expand Up @@ -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 ):
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions python/plugins/GdalTools/tools/doSettings.py
Expand Up @@ -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 )

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/GdalTools/tools/doSieve.py
Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/GdalTools/tools/doTranslate.py
Expand Up @@ -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() )
Expand Down Expand Up @@ -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 ):
Expand Down
6 changes: 4 additions & 2 deletions python/plugins/GdalTools/tools/doWarp.py
Expand Up @@ -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 ):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fa92409

Please sign in to comment.