I did some experiments with the new SIP API with the gdaltools core plugin.
It's completely untested and incomplete, but if somebody intends to update the gdal plugin, he should have a look at this diff before giving it a go.
commit 84030362bc5c598d7a4fed34ccb7dd3ef792c5b5 Author: Matthias Kuhn <matthias.k...@gmx.ch> Date: Tue May 7 08:21:21 2013 +0200 Update to gdaltools for SIP V2 diff --git a/python/plugins/GdalTools/GdalTools.py b/python/plugins/GdalTools/GdalTools.py index c2b305b..505ea27 100644 --- a/python/plugins/GdalTools/GdalTools.py +++ b/python/plugins/GdalTools/GdalTools.py @@ -71,11 +71,11 @@ class GdalTools: userPluginPath = QFileInfo( QgsApplication.qgisUserDbFilePath() ).path() + "/python/plugins/GdalTools" systemPluginPath = QgsApplication.prefixPath() + "/python/plugins/GdalTools" - overrideLocale = QSettings().value( "locale/overrideFlag", QVariant( False ) ).toBool() + overrideLocale = QSettings().value( "locale/overrideFlag", False, type=bool ) if not overrideLocale: localeFullName = QLocale.system().name() else: - localeFullName = QSettings().value( "locale/userLocale", QVariant( "" ) ).toString() + localeFullName = QSettings().value( "locale/userLocale", "" ) if QFileInfo( userPluginPath ).exists(): translationPath = userPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" diff --git a/python/plugins/GdalTools/tools/GdalTools_utils.py b/python/plugins/GdalTools/tools/GdalTools_utils.py index bc83fb0..5d2900d 100644 --- a/python/plugins/GdalTools/tools/GdalTools_utils.py +++ b/python/plugins/GdalTools/tools/GdalTools_utils.py @@ -44,20 +44,20 @@ import platform # Escapes arguments and return them joined in a string def escapeAndJoin(strList): - joined = QString() + joined = '' for s in strList: - if s.contains(" "): + if s.find(" ") is not -1: escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"' else: escaped = s joined += escaped + " " - return joined.trimmed() + return joined.strip() # Retrieves last used dir from persistent settings def getLastUsedDir(): settings = QSettings() - lastProjectDir = settings.value( "/UI/lastProjectDir", QVariant(".") ).toString() - return settings.value( "/GdalTools/lastUsedDir", QVariant(lastProjectDir) ).toString() + lastProjectDir = settings.value( "/UI/lastProjectDir", u"." ) + return settings.value( "/GdalTools/lastUsedDir", QVariant(lastProjectDir) ) # Stores last used dir in persistent settings def setLastUsedDir(filePath): @@ -72,7 +72,7 @@ def setLastUsedDir(filePath): # Retrieves GDAL binaries location def getGdalBinPath(): settings = QSettings() - return settings.value( "/GdalTools/gdalPath", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/gdalPath", u"" ) # Stores GDAL binaries location def setGdalBinPath( path ): @@ -82,7 +82,7 @@ def setGdalBinPath( path ): # Retrieves GDAL python modules location def getGdalPymodPath(): settings = QSettings() - return settings.value( "/GdalTools/gdalPymodPath", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/gdalPymodPath", u"" ) # Stores GDAL python modules location def setGdalPymodPath( path ): @@ -92,7 +92,7 @@ def setGdalPymodPath( path ): # Retrieves GDAL help files location def getHelpPath(): settings = QSettings() - return settings.value( "/GdalTools/helpPath", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/helpPath", u"" ) # Stores GDAL help files location def setHelpPath( path ): @@ -102,7 +102,7 @@ def setHelpPath( path ): # Retrieves last used encoding from persistent settings def getLastUsedEncoding(): settings = QSettings() - return settings.value( "/UI/encoding", QVariant("System") ).toString() + return settings.value( "/UI/encoding", u"System" ) # Stores last used encoding in persistent settings def setLastUsedEncoding(encoding): @@ -219,7 +219,7 @@ def getRasterFiles(path, recursive=False): return rasters def fillRasterOutputFormat(aFilter = None, filename = None): - shortName = QString() + shortName = '' if aFilter != None: supportedRasters = GdalConfig.getSupportedRasters() @@ -238,7 +238,7 @@ def fillRasterOutputFormat(aFilter = None, filename = None): return shortName def fillVectorOutputFormat(aFilter = None, filename = None): - shortName = QString() + shortName = '' if aFilter != None: supportedVectors = GdalConfig.getSupportedVectors() @@ -291,11 +291,11 @@ def getRasterSRS( parent, fileName ): processSRS.close() if arr.isEmpty(): - return QString() + return '' info = QString( arr ).split( "\n" ).filter( "AUTHORITY" ) if info.count() == 0: - return QString() + return '' srs = info[ info.count() - 1 ] srs = srs.simplified().remove( "AUTHORITY[" ) @@ -337,7 +337,7 @@ def getRasterExtent(parent, fileName): # so sometimes the dialog excedes the screen width class FileDialog: @classmethod - def getDialog(self, parent = None, caption = QString(), acceptMode = QFileDialog.AcceptOpen, fileMode = QFileDialog.ExistingFile, filter = QString(), selectedFilter = None, useEncoding = False): + def getDialog(self, parent = None, caption = '', acceptMode = QFileDialog.AcceptOpen, fileMode = QFileDialog.ExistingFile, filter = '', selectedFilter = None, useEncoding = False): if useEncoding: dialog = QgsEncodingFileDialog(parent, caption, getLastUsedDir(), filter, getLastUsedEncoding()) else: @@ -350,8 +350,8 @@ class FileDialog: if not dialog.exec_(): if useEncoding: - return (QString(), None) - return QString() + return ('', None) + return '' # change the selected filter value if selectedFilter != None: @@ -389,26 +389,26 @@ class FileDialog: return files @classmethod - def getOpenFileNames(self, parent = None, caption = QString(), filter = QString(), selectedFilter = None, useEncoding = False): + def getOpenFileNames(self, parent = None, caption = '', filter = '', selectedFilter = None, useEncoding = False): return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.ExistingFiles, filter, selectedFilter, useEncoding) @classmethod - def getOpenFileName(self, parent = None, caption = QString(), filter = QString(), selectedFilter = None, useEncoding = False): + def getOpenFileName(self, parent = None, caption = '', filter = '', selectedFilter = None, useEncoding = False): return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.ExistingFile, filter, selectedFilter, useEncoding) @classmethod - def getSaveFileName(self, parent = None, caption = QString(), filter = QString(), selectedFilter = None, useEncoding = False): + def getSaveFileName(self, parent = None, caption = '', filter = '', selectedFilter = None, useEncoding = False): return self.getDialog(parent, caption, QFileDialog.AcceptSave, QFileDialog.AnyFile, filter, selectedFilter, useEncoding) @classmethod - def getExistingDirectory(self, parent = None, caption = QString(), useEncoding = False): - return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.DirectoryOnly, QString(), None, useEncoding) + def getExistingDirectory(self, parent = None, caption = '', useEncoding = False): + return self.getDialog(parent, caption, QFileDialog.AcceptOpen, QFileDialog.DirectoryOnly, '', None, useEncoding) class FileFilter: @classmethod def getFilter(self, typeName): settings = QSettings() - return settings.value( "/GdalTools/" + typeName + "FileFilter", QVariant( "" ) ).toString() + return settings.value( "/GdalTools/" + typeName + "FileFilter", u"" ) @classmethod def setFilter(self, typeName, aFilter): @@ -416,7 +416,7 @@ class FileFilter: settings.setValue( "/GdalTools/" + typeName + "FileFilter", QVariant( aFilter ) ) # stores the supported raster file filter - rastersFilter = QString() + rastersFilter = '' # Retrieves the filter for supported raster files @classmethod @@ -428,7 +428,7 @@ class FileFilter: # separates multiple extensions that joined by a slash if QGis.QGIS_VERSION[0:3] < "1.5": formats = self.rastersFilter.split( ";;" ) - self.rastersFilter = QString() + self.rastersFilter = '' for f in formats: oldExts = QString(f).remove( QRegExp('^.*\(') ).remove( QRegExp('\).*$') ) newExts = QString(oldExts).replace( '/', ' *.' ) @@ -447,7 +447,7 @@ class FileFilter: self.setFilter("lastRaster", aFilter) # stores the supported vectors file filter - vectorsFilter = QString() + vectorsFilter = '' # Retrieves the filter for supported vector files @classmethod @@ -532,7 +532,7 @@ class GdalConfig: longName = QString(driver.LongName).remove( QRegExp( '\(.*$' ) ).trimmed() shortName = QString(driver.ShortName).remove( QRegExp( '\(.*$' ) ).trimmed() - extensions = QString() + extensions = '' description = QString(driver.GetDescription()) glob = QStringList() @@ -596,7 +596,7 @@ class GdalConfig: continue driverName = QString(driver.GetName()) - longName = QString() + longName = '' glob = QStringList() if driverName.startsWith( "AVCBin" ): @@ -710,7 +710,7 @@ class GdalConfig: @classmethod def long2ShortName(self, longName): if longName.isEmpty(): - return QString() + return '' if self.dict_long2shortName.has_key(longName): return self.dict_long2shortName[longName] @@ -719,7 +719,7 @@ class GdalConfig: if gdal.GetDriverCount() == 0: gdal.AllRegister() - shortName = QString() + shortName = '' # for each loaded GDAL driver for i in range(gdal.GetDriverCount()): @@ -739,9 +739,9 @@ class GdalConfig: @classmethod def filename2ShortName(self, fileName): if fileName.isEmpty(): - return QString() + return '' - shortName = QString() + shortName = '' # for each raster format search for the file extension formats = FileFilter.allRastersFilter().split( ";;" ) diff --git a/python/plugins/GdalTools/tools/dialogSRS.py b/python/plugins/GdalTools/tools/dialogSRS.py index 7da043e..330ec13 100644 --- a/python/plugins/GdalTools/tools/dialogSRS.py +++ b/python/plugins/GdalTools/tools/dialogSRS.py @@ -57,4 +57,4 @@ class GdalToolsSRSDialog(QDialog): if not self.selector.selectedProj4String().isEmpty(): return self.proj4string() - return QString() + return '' diff --git a/python/plugins/GdalTools/tools/doContour.py b/python/plugins/GdalTools/tools/doContour.py index a030495..dca174b 100644 --- a/python/plugins/GdalTools/tools/doContour.py +++ b/python/plugins/GdalTools/tools/doContour.py @@ -98,7 +98,7 @@ class GdalToolsDialog(QWidget, Ui_Widget, BasePluginWidget): arguments << self.attributeEdit.text() if True: # XXX in this moment the -i argument is not optional arguments << "-i" - arguments << QString(str(self.intervalDSpinBox.value())) + arguments << unicode(self.intervalDSpinBox.value()) arguments << self.getInputFileName() arguments << self.outSelector.filename() return arguments diff --git a/python/plugins/GdalTools/tools/doGrid.py b/python/plugins/GdalTools/tools/doGrid.py index 0e7712d..60501c5 100644 --- a/python/plugins/GdalTools/tools/doGrid.py +++ b/python/plugins/GdalTools/tools/doGrid.py @@ -198,7 +198,7 @@ class GdalToolsDialog(QWidget, Ui_Widget, BasePluginWidget): arguments.append("nodata=" + str(self.datametricsNoDataSpin.value())) return arguments.join(":") - def loadFields(self, vectorFile = QString()): + def loadFields(self, vectorFile = ''): self.zfieldCombo.clear() if vectorFile.isEmpty(): diff --git a/python/plugins/GdalTools/tools/doInfo.py b/python/plugins/GdalTools/tools/doInfo.py index 91cbb37..48b9946 100644 --- a/python/plugins/GdalTools/tools/doInfo.py +++ b/python/plugins/GdalTools/tools/doInfo.py @@ -66,7 +66,7 @@ class GdalToolsDialog( QWidget, Ui_Widget, BasePluginWidget ): def doCopyLine( self ): - output = QString() + output = '' items = self.rasterInfoList.selectedItems() for r in items: output.append( r.text() + "\n" ) @@ -75,7 +75,7 @@ class GdalToolsDialog( QWidget, Ui_Widget, BasePluginWidget ): clipboard.setText( output ) def doCopyAll( self ): - output = QString() + output = '' for r in range( self.rasterInfoList.count() ): output.append( self.rasterInfoList.item( r ).text() + "\n" ) if not output.isEmpty(): @@ -84,7 +84,7 @@ class GdalToolsDialog( QWidget, Ui_Widget, BasePluginWidget ): def keyPressEvent( self, e ): if ( e.modifiers() == Qt.ControlModifier or e.modifiers() == Qt.MetaModifier ) and e.key() == Qt.Key_C: - items = QString() + items = '' for r in range( self.rasterInfoList.count() ): items.append( self.rasterInfoList.item( r ).text() + "\n" ) if not items.isEmpty(): diff --git a/python/plugins/GdalTools/tools/doProjection.py b/python/plugins/GdalTools/tools/doProjection.py index fbe1d84..0604884 100644 --- a/python/plugins/GdalTools/tools/doProjection.py +++ b/python/plugins/GdalTools/tools/doProjection.py @@ -122,7 +122,7 @@ class GdalToolsDialog( QWidget, Ui_Widget, BaseBatchWidget ): inputFn = self.getInputFileName() arguments << inputFn - self.tempFile = QString( inputFn ) + self.tempFile = inputFn self.needOverwrite = False if not self.tempFile.isEmpty(): if self.tempFile.toLower().contains( QRegExp( "\.tif{1,2}" ) ): @@ -157,7 +157,7 @@ class GdalToolsDialog( QWidget, Ui_Widget, BaseBatchWidget ): def getBatchOutputFileName(self, fn): # get GeoTiff - fn = QString( fn ).replace( QRegExp( "\.[a-zA-Z]{2,4}$" ), ".tif" ) + fn = re.sub( r'\.[a-zA-Z]{2,4}$', r'.tif', fn ) return BaseBatchWidget.getBatchOutputFileName( self, fn ) def addLayerIntoCanvas(self, fileInfo): diff --git a/python/plugins/GdalTools/tools/doTranslate.py b/python/plugins/GdalTools/tools/doTranslate.py index e871d91..bec30e9 100644 --- a/python/plugins/GdalTools/tools/doTranslate.py +++ b/python/plugins/GdalTools/tools/doTranslate.py @@ -145,7 +145,7 @@ class GdalToolsDialog(QWidget, Ui_Widget, BaseBatchWidget): workDir.setNameFilters( filter ) # search for a valid SRS, then use it as default target SRS - srs = QString() + srs = '' for fname in workDir.entryList(): fl = inputDir + "/" + fname srs = Utils.getRasterSRS( self, fl ) diff --git a/python/plugins/GdalTools/tools/inOutSelector.py b/python/plugins/GdalTools/tools/inOutSelector.py index b094365..fd4689c 100644 --- a/python/plugins/GdalTools/tools/inOutSelector.py +++ b/python/plugins/GdalTools/tools/inOutSelector.py @@ -132,8 +132,8 @@ class GdalToolsInOutSelector(QWidget, Ui_GdalToolsInOutSelector): if isinstance(fn, QgsMapLayer): fn = fn.source() - elif isinstance(fn, str) or isinstance(fn, unicode) or isinstance(fn, QString): - fn = QString( fn ) + elif isinstance(fn, str) or isinstance(fn, unicode): + fn = unicode( fn ) elif hasattr(fn, '__iter__') or isinstance(fn, QStringList): if len( fn ) > 0: @@ -142,10 +142,10 @@ class GdalToolsInOutSelector(QWidget, Ui_GdalToolsInOutSelector): self.filenames = fn fn = fn.join( "," ) else: - fn = QString() + fn = '' else: - fn = QString() + fn = '' if not (self.getType() & self.LAYER): self.fileEdit.setText( fn ) @@ -215,7 +215,7 @@ class GdalToolsInOutSelector(QWidget, Ui_GdalToolsInOutSelector): def saveComboState(self): index = self.combo.currentIndex() text = self.combo.currentText() - layerID = self.combo.itemData(index).toString() if index >= 0 else "" + layerID = self.combo.itemData(index) if index >= 0 else "" self.prevState = ( index, text, layerID ) def restoreComboState(self): @@ -241,7 +241,7 @@ class GdalToolsInOutSelector(QWidget, Ui_GdalToolsInOutSelector): def layer(self): if self.getType() != self.FILE and self.combo.currentIndex() >= 0: - layerID = self.combo.itemData(self.combo.currentIndex()).toString() + layerID = self.combo.itemData(self.combo.currentIndex()) return QgsMapLayerRegistry.instance().mapLayer( layerID ) return None @@ -259,5 +259,5 @@ class GdalToolsInOutSelector(QWidget, Ui_GdalToolsInOutSelector): if layer != None: return layer.source() - return QString() + return '' diff --git a/python/plugins/GdalTools/tools/widgetBatchBase.py b/python/plugins/GdalTools/tools/widgetBatchBase.py index 0372f83..2b048e8 100644 --- a/python/plugins/GdalTools/tools/widgetBatchBase.py +++ b/python/plugins/GdalTools/tools/widgetBatchBase.py @@ -37,9 +37,9 @@ class GdalToolsBaseBatchWidget(BasePluginWidget): BasePluginWidget.__init__(self, iface, commandName) def getBatchArguments(self, inFile, outFile = None): - arguments = QStringList() - arguments << self.getArguments() - arguments << inFile + arguments = [] + arguments.append( self.getArguments() ) + arguments.append( inFile ) if outFile != None: arguments << outFile return arguments @@ -62,9 +62,9 @@ class GdalToolsBaseBatchWidget(BasePluginWidget): # if overwrites existent files if outDir == None or outDir == inDir: - return QString( fn ).append( ".tmp" ) + return fn + ".tmp" - return QString( fn ).mid( len(inDir) ).prepend( outDir ) + return outDir + fn[len(inDir):] def onRun( self ): if not self.isBatchEnabled(): @@ -85,7 +85,7 @@ class GdalToolsBaseBatchWidget(BasePluginWidget): for f in self.inFiles: self.outFiles.append( self.getBatchOutputFileName( f ) ) - self.errors = QStringList() + self.errors = [] self.batchIndex = 0 self.batchTotal = len( self.inFiles ) self.setProgressRange( self.batchTotal ) @@ -112,7 +112,7 @@ class GdalToolsBaseBatchWidget(BasePluginWidget): BasePluginWidget.onFinished(self, exitCode, status) return - msg = QString.fromLocal8Bit( self.base.process.readAllStandardError() ) + msg = bytes.decode( bytes( self.base.process.readAllStandardError() ) ) if not msg.isEmpty(): self.errors.append( ">> " + self.inFiles[self.batchIndex] + "<br>" + msg.replace( "\n", "<br>" ) ) @@ -134,7 +134,7 @@ class GdalToolsBaseBatchWidget(BasePluginWidget): self.base.stop() if not self.errors.isEmpty(): - msg = QString( "Processing of the following files ended with error: <br><br>" ).append( self.errors.join( "<br><br>" ) ) + msg = u"Processing of the following files ended with error: <br><br>" + "<br><br>".join( self.errors ) QErrorMessage( self ).showMessage( msg ) inDir = self.getInputFileName() @@ -146,17 +146,17 @@ class GdalToolsBaseBatchWidget(BasePluginWidget): canvas = self.iface.mapCanvas() previousRenderFlag = canvas.renderFlag() canvas.setRenderFlag( False ) - notCreatedList = QStringList() + notCreatedList = [] for item in self.outFiles: fileInfo = QFileInfo( item ) if fileInfo.exists(): if self.base.loadCheckBox.isChecked(): self.addLayerIntoCanvas( fileInfo ) else: - notCreatedList << item + notCreatedList.append( item ) canvas.setRenderFlag( previousRenderFlag ) - if notCreatedList.isEmpty(): + if len( notCreatedList ) == 0: QMessageBox.information( self, self.tr( "Finished" ), self.tr( "Operation completed." ) ) else: QMessageBox.warning( self, self.tr( "Warning" ), self.tr( "The following files were not created: \n%1" ).arg( notCreatedList.join( ", " ) ) ) diff --git a/python/plugins/GdalTools/tools/widgetPluginBase.py b/python/plugins/GdalTools/tools/widgetPluginBase.py index f7dea74..d46aad3 100644 --- a/python/plugins/GdalTools/tools/widgetPluginBase.py +++ b/python/plugins/GdalTools/tools/widgetPluginBase.py @@ -106,8 +106,7 @@ class GdalToolsBasePluginWidget: if outFn == None: return - outFn = QString(outFn) - if outFn.isEmpty(): + if outFn == '': QMessageBox.warning(self, self.tr( "Warning" ), self.tr( "No output file created." ) ) return
_______________________________________________ Qgis-developer mailing list Qgis-developer@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer