File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -343,6 +343,16 @@ void QgsLegendLayerFile::saveAsShapefileGeneral(bool saveOnlySelection)
343
343
shapefileName += " .shp" ;
344
344
}
345
345
346
+ // overwrite the file - user will already have been prompted
347
+ // to verify they want to overwrite by the file dialog above
348
+ if (QFile::exists (shapefileName))
349
+ {
350
+ if (!QgsVectorFileWriter::deleteShapeFile (shapefileName))
351
+ {
352
+ return ;
353
+ }
354
+ }
355
+ // ok if the file existed it should be deleted now so we can continue...
346
356
QApplication::setOverrideCursor (Qt::waitCursor);
347
357
348
358
QgsVectorFileWriter::WriterError error;
Original file line number Diff line number Diff line change 26
26
#include " qgsvectordataprovider.h"
27
27
28
28
#include < QFile>
29
+ #include < QFileInfo>
29
30
#include < QTextCodec>
30
31
31
32
#include < cassert>
@@ -329,3 +330,49 @@ QgsVectorFileWriter::WriterError
329
330
330
331
return NoError;
331
332
}
333
+
334
+
335
+ bool QgsVectorFileWriter::deleteShapeFile (QString theFileName)
336
+ {
337
+ //
338
+ // Remove old copies that may be lying around
339
+ //
340
+ QFileInfo myInfo (theFileName);
341
+ QString myFileBase = theFileName.replace (" .shp" ," " );
342
+ if (myInfo.exists ())
343
+ {
344
+ if (!QFile::remove (myFileBase + " .shp" ))
345
+ {
346
+ qDebug (" Removing file failed : " + myFileBase.toLocal8Bit () + " .shp" );
347
+ return false ;
348
+ }
349
+ }
350
+ myInfo.setFile (myFileBase + " .shx" );
351
+ if (myInfo.exists ())
352
+ {
353
+ if (!QFile::remove (myFileBase + " .shx" ))
354
+ {
355
+ qDebug (" Removing file failed : " + myFileBase.toLocal8Bit () + " .shx" );
356
+ return false ;
357
+ }
358
+ }
359
+ myInfo.setFile (myFileBase + " .dbf" );
360
+ if (myInfo.exists ())
361
+ {
362
+ if (!QFile::remove (myFileBase + " .dbf" ))
363
+ {
364
+ qDebug (" Removing file failed : " + myFileBase.toLocal8Bit () + " .dbf" );
365
+ return false ;
366
+ }
367
+ }
368
+ myInfo.setFile (myFileBase + " .prj" );
369
+ if (myInfo.exists ())
370
+ {
371
+ if (!QFile::remove (myFileBase + " .prj" ))
372
+ {
373
+ qDebug (" Removing file failed : " + myFileBase.toLocal8Bit () + " .prj" );
374
+ return false ;
375
+ }
376
+ }
377
+ return true ;
378
+ }
Original file line number Diff line number Diff line change @@ -73,6 +73,11 @@ class CORE_EXPORT QgsVectorFileWriter
73
73
/* * close opened shapefile for writing */
74
74
~QgsVectorFileWriter ();
75
75
76
+ /* * Delete a shapefile (and its accompanying shx / dbf / prf)
77
+ * @param QString theFileName - /path/to/file.shp
78
+ * @return bool true if the file was deleted successfully
79
+ */
80
+ static bool deleteShapeFile (QString theFileName);
76
81
protected:
77
82
78
83
OGRGeometry* createEmptyGeometry (QGis::WKBTYPE wkbType);
You can’t perform that action at this time.
0 commit comments