Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b5c99af

Browse files
rouaultnyalldawson
authored andcommittedSep 24, 2021
New shapefile layer dialog: avoid accidental overwrite of a file whose extension is not specified
and avoid closing the dialog when the user doesn't confirm overwriting Fixes #44299
1 parent 2ed7b3e commit b5c99af

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed
 

‎python/gui/auto_generated/qgsnewvectorlayerdialog.sip.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ Sets the ``crs`` value for the new layer in the dialog.
107107
.. versionadded:: 3.0
108108
%End
109109

110+
public slots:
111+
virtual void accept();
112+
113+
110114
};
111115

112116
/************************************************************************

‎src/gui/qgsnewvectorlayerdialog.cpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,9 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
100100
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << QStringLiteral( "id" ) << QStringLiteral( "Integer" ) << QStringLiteral( "10" ) << QString() ) );
101101
connect( mNameEdit, &QLineEdit::textChanged, this, &QgsNewVectorLayerDialog::nameChanged );
102102
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewVectorLayerDialog::selectionChanged );
103-
connect( mGeometryTypeBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
103+
connect( mGeometryTypeBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
104104
{
105-
QString fileName = mFileName->filePath();
106-
if ( !fileName.isEmpty() )
107-
{
108-
if ( index == 0 )
109-
{
110-
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".dbf" ) );
111-
}
112-
else
113-
{
114-
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".shp" ) );
115-
}
116-
mFileName->setFilePath( fileName );
117-
}
105+
updateExtension();
118106
checkOk();
119107
} );
120108

@@ -284,6 +272,41 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pE
284272
return res;
285273
}
286274

275+
void QgsNewVectorLayerDialog::updateExtension()
276+
{
277+
QString fileName = filename();
278+
const QString fileformat = selectedFileFormat();
279+
const QgsWkbTypes::Type geometrytype = selectedType();
280+
if ( fileformat == QLatin1String( "ESRI Shapefile" ) )
281+
{
282+
if ( geometrytype != QgsWkbTypes::NoGeometry )
283+
{
284+
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".shp" ) );
285+
if ( !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
286+
fileName += QLatin1String( ".shp" );
287+
}
288+
else
289+
{
290+
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".dbf" ) );
291+
if ( !fileName.endsWith( QLatin1String( ".dbf" ), Qt::CaseInsensitive ) )
292+
fileName += QLatin1String( ".dbf" );
293+
}
294+
}
295+
setFilename( fileName );
296+
297+
}
298+
299+
void QgsNewVectorLayerDialog::accept()
300+
{
301+
updateExtension();
302+
303+
if ( QFile::exists( filename() ) && QMessageBox::warning( this, tr( "New ShapeFile Layer" ), tr( "The layer already exists. Are you sure you want to overwrite the existing file?" ),
304+
QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
305+
return;
306+
307+
QDialog::accept();
308+
}
309+
287310
QString QgsNewVectorLayerDialog::execAndCreateLayer( QString &errorMessage, QWidget *parent, const QString &initialPath, QString *encoding, const QgsCoordinateReferenceSystem &crs )
288311
{
289312
errorMessage.clear();
@@ -296,30 +319,17 @@ QString QgsNewVectorLayerDialog::execAndCreateLayer( QString &errorMessage, QWid
296319
return QString();
297320
}
298321

299-
if ( QFile::exists( geomDialog.filename() ) && QMessageBox::warning( parent, tr( "New ShapeFile Layer" ), tr( "The layer already exists. Are you sure you want to overwrite the existing file?" ),
300-
QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
301-
return QString();
322+
const QString fileformat = geomDialog.selectedFileFormat();
323+
const QgsWkbTypes::Type geometrytype = geomDialog.selectedType();
324+
QString fileName = geomDialog.filename();
302325

303-
QgsWkbTypes::Type geometrytype = geomDialog.selectedType();
304-
QString fileformat = geomDialog.selectedFileFormat();
305-
QString enc = geomDialog.selectedFileEncoding();
326+
const QString enc = geomDialog.selectedFileEncoding();
306327
QgsDebugMsg( QStringLiteral( "New file format will be: %1" ).arg( fileformat ) );
307328

308329
QList< QPair<QString, QString> > attributes;
309330
geomDialog.attributes( attributes );
310331

311332
QgsSettings settings;
312-
QString fileName = geomDialog.filename();
313-
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && ( geometrytype != QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) ) )
314-
fileName += QLatin1String( ".shp" );
315-
else if ( fileformat == QLatin1String( "ESRI Shapefile" ) && ( geometrytype == QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( ".dbf" ), Qt::CaseInsensitive ) ) )
316-
{
317-
if ( fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
318-
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".dbf" ) );
319-
else
320-
fileName += QLatin1String( ".dbf" );
321-
}
322-
323333
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
324334
settings.setValue( QStringLiteral( "UI/encoding" ), enc );
325335

‎src/gui/qgsnewvectorlayerdialog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
112112
*/
113113
void setCrs( const QgsCoordinateReferenceSystem &crs );
114114

115+
public slots:
116+
void accept() override;
117+
115118
private slots:
116119
void mAddAttributeButton_clicked();
117120
void mRemoveAttributeButton_clicked();
@@ -126,6 +129,8 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
126129

127130
private:
128131
QPushButton *mOkButton = nullptr;
132+
133+
void updateExtension();
129134
};
130135

131136
#endif //qgsnewvectorlayerdialog_H

0 commit comments

Comments
 (0)
Please sign in to comment.