[Qgis-developer] Patch for Table Manager (ver.0.17) - python plugin

Volkan Kepoglu vkepoglu at gmail.com
Thu Jun 4 05:44:54 EDT 2009


Hi Borys,

That is an excellent plugin, but I found an error and fixed for xp. The
error occurs when insert a new field and click the save button in win xp.

i actually do not know how to prepare a python patch in pythonic way, but i
marked my changed code between the commented blocks like  "BEGIN SECTION"

you can find more explanation in the code.

could you look at my suggestion and if it is ok for you, could you let me
know when the plugin is revised in your repo?

i want to test your plugin.

regards,
volkan.

  def doSave(self): # Called when appropriate button was pressed
    # I believe the procedure below is as much safe as it's only possible...

    ##temporary checking
    for i in self.fields:
      for j in self.fields:
        if self.fields[i].name().toUpper() ==
self.fields[j].name().toUpper() and self.fields[i]!=self.fields[j]:
          QMessageBox.warning(self, 'Table Manager', 'The names of two
fields (%s and %s) are the same.\nQuantum GIS isn\'t fully case-sensitive
yet, so you have to type unique field names, without distinguishing lower
and upper case.' % (self.fields[i].name(), self.fields[j].name()))
          return
    ##end of temporary checking

    tmpDir = QDir.tempPath()
    srcPath = self.provider.dataSourceUri()

    # BEGIN SECTION: PATCH #1
    # without this one line code in win xp, srcName return something wrong,
see below.
    srcPath = srcPath.replace("\\","/")
    # END PATCH

    if srcPath.right(4).toUpper() == '.SHP': # if the path points to the shp
file, remove the extension...
      srcPath.chop(4)
      srcName = srcPath.right(srcPath.size() - srcPath.lastIndexOf('/') - 1)
    else: # ...but if it points only to a directory, try to determine the
name of the file inside!
      qPath = QDir(srcPath)
      qPath.setNameFilters(['*.shp', '*.SHP'])
      if len(qPath.entryList()) == 1: # there is exactly one shapefile
inside
        srcName = qPath.entryList()[0]
        srcName.chop(4)
        srcPath += '/' + srcName
      else:
        QMessageBox.warning(self, 'Table Manager', 'I cannot determine the
layer source file: ' + srcPath + " !\nThe layer won't be changed, please use
the Save As button.")
        return

    # BEGIN SECTION: PATCH #2
    # if one line code - srcPath = srcPath.replace("\\","/") - is NOT added,
the error with msgbox is - Error creating file. Check permissions. -,
because
    # srcPath returns: "C:\temp\noNumeric"
    # and srcName is "C:\temp\noNumeric" -- that is not OK for writeToShp
function

    # when this code added, now is ok, because then both variable returns:
    # srcPath: "C:/temp/noNumeric"
    # srcName: "noNumeric"

    # these suggestions are specific to win xp, but NOT tested in linux.
    # END PATCH

    # write the layer to the temporary directory
    if self.writeToShp(tmpDir+'/'+srcName+'.shp', self.provider.encoding())
!= 0:
      QMessageBox.warning(self, 'Table Manager', 'Failed saving the changes
to the temporary directory: ' + tmpDir + " !\nThe layer won't be changed,
please use the Save As button.")
      QgsVectorFileWriter.deleteShapeFile(tmpDir+'/'+srcName+'.shp')
      return
    # try to remove the old .dbf~ backup
    QFile(srcPath+'.dbf~').remove()
    # rename the oryginal .dbf file to .dbf~
    if not QFile(srcPath+'.dbf').rename(srcPath+'.dbf~'):
      QMessageBox.warning(self, 'Table Manager', 'Failed backuping the old
table to '+srcPath+".dbf~\nThe layer won't be changed, please use the Save
As button.")
      QgsVectorFileWriter.deleteShapeFile(tmpDir+'/'+srcName+'.shp')
      return

    # BEGIN SECTION: PATCH #3
    # QFile can not overwrite for copy and rename. srcPath+'.dbf' file is
already exist. instead of QFile, i used shutil.copy
    import shutil
    shutil.copy(tmpDir+'/'+srcName+'.dbf', srcPath+'.dbf')
    QFile(srcPath+'.dbf~').remove()
    """
    # copy the .dbf from the temp directory to the target location
    #if not QFile(tmpDir+'/'+srcName+'.dbf').copy(srcPath+'.dbf'):
      # something went wrong. Restore the dbf~ file
      if not QFile(srcPath+'.dbf~').rename(srcPath+'.dbf'):
        QMessageBox.warning(self, 'Table Manager', 'WARNING! I can neither
save the new '+srcPath+'.dbf file\nnor restore it from the '+srcPath+'.dbf~
backup.\nPlease check it manually!')
        return
      QMessageBox.warning(self, 'Table Manager', 'Failed saving the changes
to '+srcPath+".dbf\nThe layer will not be changed, please use the Save As
button.")
      QgsVectorFileWriter.deleteShapeFile(tmpDir+'/'+srcName+'.shp')
      return
    """
    # END PATCH

    # clean the temp directory
    QgsVectorFileWriter.deleteShapeFile(tmpDir+'/'+srcName+'.shp')
    # create the new layer
    layerName = self.layer.name()
    newLayer = QgsVectorLayer(srcPath+'.shp', layerName, 'ogr')
    if not newLayer.isValid():
      QMessageBox.warning(self, 'Table Manager', "WARNING! The changes seem
to be commited, but I can't reload the layer!\nPlease check it out!\nThe old
table is backuped as "+srcName+'.dbf~.')
      return
    # copy the symbology (it's possible only if the clasyfying field was not
moved nor changed!)
    if QMessageBox.question(self, 'Saving successful','Saving successful.
The old table has been backuped as '+srcName+".dbf~. Do you wish to keep the
old symbology? Note that if the clasyfying field was moved, all objects can
become invisible. In that case please adjust the symbology manually.",
QMessageBox.Yes, QMessageBox.No) == QMessageBox.Yes:
      newLayer.copySymbologySettings(self.layer)
    # reload the layer
    QgsMapLayerRegistry.instance().removeMapLayer(self.layer.getLayerID())
    QgsMapLayerRegistry.instance().addMapLayer(newLayer)
    # point the self.layer to the new one.
    self.layer = self.iface.activeLayer()
    self.provider = self.layer.dataProvider()
    self.fields = self.provider.fields()
    self.butSave.setEnabled(False)
    self.saveCfg() # save cfg only in case of any successfull operations,
not when user just play ;)
    self.isUnsaved = False
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20090604/36db87cb/attachment-0001.html


More information about the Qgis-developer mailing list