Skip to content

Commit

Permalink
Display current filename in progressbar when merging and add option to
Browse files Browse the repository at this point in the history
select encoding for input files
  • Loading branch information
alexbruy committed May 11, 2011
1 parent 89fe228 commit 8eac180
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions python/plugins/fTools/tools/doMergeShapes.py
Expand Up @@ -19,13 +19,15 @@ def __init__( self, iface ):
self.mergeThread = None
self.inputFiles = None
self.outFileName = None
self.inEncoding = None

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

QObject.connect( self.btnSelectDir, SIGNAL( "clicked()" ), self.inputDir )
QObject.connect( self.btnSelectFile, SIGNAL( "clicked()" ), self.outFile )
QObject.connect( self.chkListMode, SIGNAL( "stateChanged( int )" ), self.changeMode )
QObject.connect( self.leOutShape, SIGNAL( "editingFinished()" ), self.updateOutFile )

def inputDir( self ):
inDir = QFileDialog.getExistingDirectory( self,
Expand Down Expand Up @@ -83,6 +85,11 @@ def changeMode( self ):
self.lblGeometry.setEnabled( True )
self.cmbGeometry.setEnabled( True )

def updateOutFile( self ):
self.outFileName = self.leOutShape.text()
settings = QSettings()
self.outEncoding = settings.value( "/UI/encoding" ).toString()

def reject( self ):
QDialog.reject( self )

Expand Down Expand Up @@ -110,22 +117,27 @@ def accept( self ):
else:
baseDir = self.leInputDir.text()

# look for shapes with specified geometry type
self.inputFiles = ftools_utils.getShapesByGeometryType( baseDir, self.inputFiles, self.cmbGeometry.currentIndex() )

self.progressFiles.setRange( 0, self.inputFiles.count() )

outFile = QFile( self.outFileName )
if outFile.exists():
if not QgsVectorFileWriter.deleteShapeFile( self.outFileName ):
QMessageBox.warning( self, self.tr( "Delete error" ), self.tr( "Can't delete file %1" ).arg( outFileName ) )
return

# look for shapes with specified geometry type
self.inputFiles = ftools_utils.getShapesByGeometryType( baseDir, self.inputFiles, self.cmbGeometry.currentIndex() )
self.progressFiles.setRange( 0, self.inputFiles.count() )

if self.inEncoding == None:
self.inEncoding = "System"

QApplication.setOverrideCursor( QCursor( Qt.WaitCursor ) )
self.btnOk.setEnabled( False )

self.mergeThread = ShapeMergeThread( baseDir, self.inputFiles, self.outFileName, self.encoding )
self.mergeThread = ShapeMergeThread( baseDir, self.inputFiles, self.inEncoding, self.outFileName, self.encoding )
QObject.connect( self.mergeThread, SIGNAL( "rangeChanged( PyQt_PyObject )" ), self.setProgressRange )
QObject.connect( self.mergeThread, SIGNAL( "checkStarted()" ), self.setFeatureProgressFormat )
QObject.connect( self.mergeThread, SIGNAL( "checkFinished()" ), self.resetFeatureProgressFormat )
QObject.connect( self.mergeThread, SIGNAL( "fileNameChanged( PyQt_PyObject )" ), self.setShapeProgressFormat )
QObject.connect( self.mergeThread, SIGNAL( "featureProcessed()" ), self.featureProcessed )
QObject.connect( self.mergeThread, SIGNAL( "shapeProcessed()" ), self.shapeProcessed )
QObject.connect( self.mergeThread, SIGNAL( "processingFinished()" ), self.processingFinished )
Expand All @@ -141,9 +153,18 @@ def setProgressRange( self, max ):
self.progressFeatures.setRange( 0, max )
self.progressFeatures.setValue( 0 )

def setFeatureProgressFormat( self ):
self.progressFeatures.setFormat( "Checking files: %p% ")

def resetFeatureProgressFormat( self ):
self.progressFeatures.setFormat( "%p% ")

def featureProcessed( self ):
self.progressFeatures.setValue( self.progressFeatures.value() + 1 )

def setShapeProgressFormat( self, fileName ):
self.progressFiles.setFormat( "%p% " + fileName )

def shapeProcessed( self ):
self.progressFiles.setValue( self.progressFiles.value() + 1 )

Expand All @@ -152,7 +173,9 @@ def processingFinished( self ):

if self.chkAddToCanvas.isChecked():
if not ftools_utils.addShapeToCanvas( unicode( self.outFileName ) ):
QMessageBox.warning( self, self.tr( "Merging" ), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.outFileName ) ) )
QMessageBox.warning( self, self.tr( "Merging" ),
self.tr( "Error loading output shapefile:\n%1" )
.arg( unicode( self.outFileName ) ) )

self.restoreGui()

Expand All @@ -165,6 +188,7 @@ def stopProcessing( self ):
self.mergeThread = None

def restoreGui( self ):
self.progressFiles.setFormat( "%p%" )
self.progressFeatures.setRange( 0, 100 )
self.progressFeatures.setValue( 0 )
self.progressFiles.setValue( 0 )
Expand All @@ -174,10 +198,11 @@ def restoreGui( self ):
self.btnOk.setEnabled( True )

class ShapeMergeThread( QThread ):
def __init__( self, dir, shapes, outputFileName, outputEncoding ):
def __init__( self, dir, shapes, inputEncoding, outputFileName, outputEncoding ):
QThread.__init__( self, QThread.currentThread() )
self.baseDir = dir
self.shapes = shapes
self.inputEncoding = inputEncoding
self.outputFileName = outputFileName
self.outputEncoding = outputEncoding

Expand Down Expand Up @@ -208,10 +233,12 @@ def run( self ):
if not newLayer.isValid():
continue
vprovider = newLayer.dataProvider()
vprovider.setEncoding( self.inputEncoding )
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
nFeat = vprovider.featureCount()
self.emit( SIGNAL( "rangeChanged( PyQt_PyObject )" ), nFeat )
self.emit( SIGNAL( "fileNameChanged( PyQt_PyObject )" ), fileName )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand Down

0 comments on commit 8eac180

Please sign in to comment.