@@ -100,21 +100,9 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
100
100
mAttributeView ->addTopLevelItem ( new QTreeWidgetItem ( QStringList () << QStringLiteral ( " id" ) << QStringLiteral ( " Integer" ) << QStringLiteral ( " 10" ) << QString () ) );
101
101
connect ( mNameEdit , &QLineEdit::textChanged, this , &QgsNewVectorLayerDialog::nameChanged );
102
102
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 )
104
104
{
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 ();
118
106
checkOk ();
119
107
} );
120
108
@@ -284,6 +272,41 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pE
284
272
return res;
285
273
}
286
274
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
+
287
310
QString QgsNewVectorLayerDialog::execAndCreateLayer ( QString &errorMessage, QWidget *parent, const QString &initialPath, QString *encoding, const QgsCoordinateReferenceSystem &crs )
288
311
{
289
312
errorMessage.clear ();
@@ -296,30 +319,17 @@ QString QgsNewVectorLayerDialog::execAndCreateLayer( QString &errorMessage, QWid
296
319
return QString ();
297
320
}
298
321
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 ();
302
325
303
- QgsWkbTypes::Type geometrytype = geomDialog.selectedType ();
304
- QString fileformat = geomDialog.selectedFileFormat ();
305
- QString enc = geomDialog.selectedFileEncoding ();
326
+ const QString enc = geomDialog.selectedFileEncoding ();
306
327
QgsDebugMsg ( QStringLiteral ( " New file format will be: %1" ).arg ( fileformat ) );
307
328
308
329
QList< QPair<QString, QString> > attributes;
309
330
geomDialog.attributes ( attributes );
310
331
311
332
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
-
323
333
settings.setValue ( QStringLiteral ( " UI/lastVectorFileFilterDir" ), QFileInfo ( fileName ).absolutePath () );
324
334
settings.setValue ( QStringLiteral ( " UI/encoding" ), enc );
325
335
0 commit comments