Skip to content

Commit

Permalink
[BACKPORT] fTools: update layers lists after adding new layer to TOC (f…
Browse files Browse the repository at this point in the history
…ix #4318)
  • Loading branch information
alexbruy committed Nov 12, 2011
1 parent 26528de commit d3fe194
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 33 deletions.
6 changes: 4 additions & 2 deletions python/plugins/fTools/tools/doGeometry.py
Expand Up @@ -161,7 +161,9 @@ def manageGui(self):
self.cmbField.setVisible( False )
self.field_label.setVisible( False )
self.resize( 381, 100 )
myList = []
self.populateLayers()

def populateLayers( self ):
self.inShape.clear()
if self.myFunction == 3 or self.myFunction == 6:
myList = ftools_utils.getLayerNames( [ QGis.Polygon, QGis.Line ] )
Expand All @@ -176,7 +178,6 @@ def manageGui(self):
else:
myList = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] )
self.inShape.addItems( myList )
return

#1: Singleparts to multipart
#2: Multipart to singleparts
Expand Down Expand Up @@ -241,6 +242,7 @@ def runFinishedFromThread( self, success ):
if addToTOC == QMessageBox.Yes:
if not ftools_utils.addShapeToCanvas( unicode( self.shapefileName ) ):
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.shapefileName ) ))
self.populateLayers()
else:
QMessageBox.warning( self, self.tr("Geometry"), self.tr( "Error writing output shapefile." ) )

Expand Down
9 changes: 6 additions & 3 deletions python/plugins/fTools/tools/doGeoprocessing.py
Expand Up @@ -182,6 +182,9 @@ def manageGui( self ):
self.label_2.setText( self.tr( "Union layer" ) )
self.setWindowTitle( self.tr( "Union" ) )
self.resize(381, 100)
self.populateLayers()

def populateLayers( self ):
myListA = []
myListB = []
self.inShapeA.clear()
Expand All @@ -195,7 +198,6 @@ def manageGui( self ):
myListB = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] )
self.inShapeA.addItems( myListA )
self.inShapeB.addItems( myListB )
return

#1: Buffer
#2: Convex Hull
Expand Down Expand Up @@ -257,6 +259,7 @@ def runFinishedFromThread( self, results ):
if addToTOC == QMessageBox.Yes:
if not ftools_utils.addShapeToCanvas( unicode( self.shapefileName ) ):
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.shapefileName ) ))
self.populateLayers()

def runStatusFromThread( self, status ):
self.progressBar.setValue( status )
Expand Down Expand Up @@ -1103,7 +1106,7 @@ def union( self ):
writer.addFeature( outFeat )
except:
FEATURE_EXCEPT = False
# this really shouldn't happen, as we
# this really shouldn't happen, as we
# haven't edited the input geom at all
# continue
else:
Expand Down Expand Up @@ -1144,7 +1147,7 @@ def union( self ):
# print str(err)
FEATURE_EXCEPT = False
# else:
# # this only happends if the bounding box
# # this only happends if the bounding box
# # intersects, but the geometry doesn't
# try:
# outFeat.setGeometry( geom )
Expand Down
8 changes: 6 additions & 2 deletions python/plugins/fTools/tools/doIntersectLines.py
Expand Up @@ -46,10 +46,13 @@ def __init__(self, iface):
QObject.connect(self.inLine2, SIGNAL("currentIndexChanged(QString)"), self.update2)
self.setWindowTitle( self.tr("Line intersections") )
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
self.populateLayers()

def populateLayers( self ):
layers = ftools_utils.getLayerNames([QGis.Line])
self.inLine1.clear()
self.inLine2.clear()
self.inLine1.addItems(layers)
self.inLine2.addItems(layers)

Expand Down Expand Up @@ -92,6 +95,7 @@ def accept(self):
if addToTOC == QMessageBox.Yes:
if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( outPath ) ))
self.populateLayers()
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

Expand Down
10 changes: 7 additions & 3 deletions python/plugins/fTools/tools/doMeanCoords.py
Expand Up @@ -45,12 +45,15 @@ def __init__(self, iface, function):
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )

# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
self.populateLayers()

def populateLayers( self ):
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
QObject.disconnect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)
self.inShape.clear()
self.inShape.addItems(layers)
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)

def updateUi(self):
if self.function == 1:
Expand Down Expand Up @@ -96,6 +99,7 @@ def accept(self):
if addToTOC == QMessageBox.Yes:
vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
self.populateLayers()
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

Expand Down
12 changes: 8 additions & 4 deletions python/plugins/fTools/tools/doPointsInPolygon.py
Expand Up @@ -44,14 +44,18 @@ def __init__(self, iface):
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
self.setWindowTitle(self.tr("Count Points in Polygon"))
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
self.populateLayers()

def populateLayers( self ):
layers = ftools_utils.getLayerNames([QGis.Polygon])
self.inPolygon.clear()
self.inPolygon.addItems(layers)

self.inPoint.clear()
layers = ftools_utils.getLayerNames([QGis.Point])
self.inPoint.addItems(layers)

def accept(self):
self.buttonOk.setEnabled( False )
if self.inPolygon.currentText() == "":
Expand Down Expand Up @@ -79,6 +83,7 @@ def accept(self):
if addToTOC == QMessageBox.Yes:
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
self.populateLayers()
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

Expand Down Expand Up @@ -112,7 +117,6 @@ def compute(self, inPoly, inPts, inField, outPath, progressBar):
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
return
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList, polyProvider.geometryType(), sRs)
#writer = QgsVectorFileWriter(outPath, "UTF-8", fieldList, polyProvider.geometryType(), sRs)
inFeat = QgsFeature()
inFeatB = QgsFeature()
outFeat = QgsFeature()
Expand Down
10 changes: 8 additions & 2 deletions python/plugins/fTools/tools/doRandPoints.py
Expand Up @@ -46,9 +46,14 @@ def __init__(self, iface):
self.progressBar.setValue(0)
self.setWindowTitle(self.tr("Random Points"))
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
self.mapCanvas = self.iface.mapCanvas()
self.populateLayers()

def populateLayers( self ):
layers = ftools_utils.getLayerNames([QGis.Polygon, "Raster"])
QObject.disconnect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)
self.inShape.clear()
self.inShape.addItems(layers)
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)

# If input layer is changed, update field list
def update(self, inputLayer):
Expand Down Expand Up @@ -123,6 +128,7 @@ def accept(self):
if addToTOC == QMessageBox.Yes:
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
self.populateLayers()
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

Expand Down Expand Up @@ -208,7 +214,7 @@ def randomize(self, inLayer, outPath, minimum, design, value):
points = self.vectorRandom(int(value), inLayer,
ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
else: points = self.loopThruPolygons(inLayer, value, design)
crs = self.mapCanvas.mapRenderer().destinationSrs()
crs = self.iface.mapCanvas().mapRenderer().destinationSrs()
if not crs.isValid(): crs = None
fields = { 0 : QgsField("ID", QVariant.Int) }
check = QFile(self.shapefileName)
Expand Down
9 changes: 7 additions & 2 deletions python/plugins/fTools/tools/doRegPoints.py
Expand Up @@ -50,7 +50,11 @@ def __init__(self, iface):
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
self.progressBar.setValue(0)
self.mapCanvas = self.iface.mapCanvas()
self.populateLayers()

def populateLayers( self ):
layers = ftools_utils.getLayerNames("all")
self.inShape.clear()
self.inShape.addItems(layers)

def accept(self):
Expand Down Expand Up @@ -89,6 +93,7 @@ def accept(self):
if addToTOC == QMessageBox.Yes:
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
self.populateLayers()
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

Expand All @@ -98,7 +103,7 @@ def outFile(self):
if self.shapefileName is None or self.encoding is None:
return
self.outShape.setText( QString( self.shapefileName ) )

# Generate list of random points
def simpleRandom(self, n, bound, xmin, xmax, ymin, ymax):
seed()
Expand All @@ -109,7 +114,7 @@ def simpleRandom(self, n, bound, xmin, xmax, ymin, ymax):
if pGeom.intersects(bound):
points.append(pGeom)
i = i + 1
return points
return points

def regularize(self, bound, outPath, offset, value, gridType, inset, crs):
area = bound.width() * bound.height()
Expand Down
22 changes: 12 additions & 10 deletions python/plugins/fTools/tools/doSimplify.py
Expand Up @@ -47,26 +47,27 @@ def __init__( self, iface, function ):

self.workThread = None

if self.myFunction == 2:
self.setWindowTitle( self.tr( "Densify geometries" ) )
self.lblTolerance.setText( self.tr( "Vertices to add" ) )
self.spnTolerance.setDecimals( 0 )
self.spnTolerance.setMinimum( 1 )
self.spnTolerance.setSingleStep( 1 )
self.spnTolerance.setValue( 1.0 )

self.btnOk = self.buttonBox.button( QDialogButtonBox.Ok )
self.btnClose = self.buttonBox.button( QDialogButtonBox.Close )

QObject.connect( self.chkWriteShapefile, SIGNAL( "stateChanged( int )" ), self.updateGui )
QObject.connect( self.btnSelectOutputFile, SIGNAL( "clicked()" ), self.selectOutputFile )

self.manageGui()
self.populateLayers()

def manageGui( self ):
def populateLayers( self ):
layers = ftools_utils.getLayerNames( [ QGis.Polygon, QGis.Line ] )
self.cmbInputLayer.clear()
self.cmbInputLayer.addItems( layers )

if self.myFunction == 2:
self.setWindowTitle( self.tr( "Densify geometries" ) )
self.lblTolerance.setText( self.tr( "Vertices to add" ) )
self.spnTolerance.setDecimals( 0 )
self.spnTolerance.setMinimum( 1 )
self.spnTolerance.setSingleStep( 1 )
self.spnTolerance.setValue( 1.0 )

def updateGui( self ):
if self.chkWriteShapefile.isChecked():
self.edOutputFile.setEnabled( True )
Expand Down Expand Up @@ -140,6 +141,7 @@ def processFinished( self, pointsCount ):
QMessageBox.warning( self, self.tr( "Error" ),
self.tr( "Error loading output shapefile:\n%1" )
.arg( unicode( self.shapeFileName ) ) )
self.populateLayers()

QMessageBox.information( self, self.tr( "Finished" ), self.tr( "Processing completed." ) )
self.iface.mapCanvas().refresh()
Expand Down
10 changes: 7 additions & 3 deletions python/plugins/fTools/tools/doSumLines.py
Expand Up @@ -44,14 +44,17 @@ def __init__(self, iface):
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
self.setWindowTitle(self.tr("Sum line lengths"))
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
self.populateLayers()

def populateLayers( self ):
layers = ftools_utils.getLayerNames([QGis.Line])
self.inPoint.clear()
self.inPoint.addItems(layers)
layers = ftools_utils.getLayerNames([QGis.Polygon])
self.inPolygon.clear()
self.inPolygon.addItems(layers)

def accept(self):
self.buttonOk.setEnabled( False )
if self.inPolygon.currentText() == "":
Expand Down Expand Up @@ -79,6 +82,7 @@ def accept(self):
if addToTOC == QMessageBox.Yes:
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
self.populateLayers()
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

Expand Down
9 changes: 7 additions & 2 deletions python/plugins/fTools/tools/doVectorGrid.py
Expand Up @@ -50,6 +50,10 @@ def __init__(self, iface):
self.xMax.setValidator(QDoubleValidator(self.xMax))
self.yMin.setValidator(QDoubleValidator(self.yMin))
self.yMax.setValidator(QDoubleValidator(self.yMax))
self.populateLayers()

def populateLayers( self ):
self.inShape.clear()
layermap = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layermap.iteritems():
self.inShape.addItem( unicode( layer.name() ) )
Expand All @@ -64,12 +68,12 @@ def updateLayer( self ):
mLayer = ftools_utils.getMapLayerByName( unicode( mLayerName ) )
boundBox = mLayer.extent()
self.updateExtents( boundBox )

def updateCanvas( self ):
canvas = self.iface.mapCanvas()
boundBox = canvas.extent()
self.updateExtents( boundBox )

def updateExtents( self, boundBox ):
self.xMin.setText( unicode( boundBox.xMinimum() ) )
self.yMin.setText( unicode( boundBox.yMinimum() ) )
Expand Down Expand Up @@ -100,6 +104,7 @@ def accept(self):
addToTOC = QMessageBox.question(self, self.tr("Generate Vector Grid"), self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?").arg(unicode(self.shapefileName)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
if addToTOC == QMessageBox.Yes:
ftools_utils.addShapeToCanvas( self.shapefileName )
self.populateLayers()
self.progressBar.setValue( 0 )
self.buttonOk.setEnabled( True )

Expand Down

0 comments on commit d3fe194

Please sign in to comment.