Skip to content
Permalink
Browse files

allow creating geometryless (plain) DBF tables from the New Shapefile…

… dialog (fix #15654)
  • Loading branch information
alexbruy committed May 5, 2020
1 parent 0ada65b commit ef186ea1fd633e746b00fab54b9d2d853ad7fcc2
Showing with 29 additions and 4 deletions.
  1. +3 −2 src/core/providers/ogr/qgsogrprovider.cpp
  2. +26 −2 src/gui/qgsnewvectorlayerdialog.cpp
@@ -3492,9 +3492,10 @@ bool QgsOgrProviderUtils::createEmptyDataSource( const QString &uri,
if ( driverName == QLatin1String( "ESRI Shapefile" ) )
{
if ( !uri.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) &&
!uri.endsWith( QLatin1String( ".shz" ), Qt::CaseInsensitive ) )
!uri.endsWith( QLatin1String( ".shz" ), Qt::CaseInsensitive ) &&
!uri.endsWith( QLatin1String( ".dbf" ), Qt::CaseInsensitive ) )
{
errorMessage = QObject::tr( "URI %1 doesn't end with .shp" ).arg( uri );
errorMessage = QObject::tr( "URI %1 doesn't end with .shp or .dbf" ).arg( uri );
QgsDebugMsg( errorMessage );
return false;
}
@@ -56,6 +56,7 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
mWidth->setValidator( new QIntValidator( 1, 255, this ) );
mPrecision->setValidator( new QIntValidator( 0, 15, this ) );

mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No geometry" ), QgsWkbTypes::NoGeometry );
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), QgsWkbTypes::Point );
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "MultiPoint" ), QgsWkbTypes::MultiPoint );
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "Line" ), QgsWkbTypes::LineString );
@@ -99,7 +100,23 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << QStringLiteral( "id" ) << QStringLiteral( "Integer" ) << QStringLiteral( "10" ) << QString() ) );
connect( mNameEdit, &QLineEdit::textChanged, this, &QgsNewVectorLayerDialog::nameChanged );
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewVectorLayerDialog::selectionChanged );
connect( mGeometryTypeBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewVectorLayerDialog::checkOk );
connect( mGeometryTypeBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
{
QString fileName = mFileName->filePath();
if ( !fileName.isEmpty() )
{
if ( index == 0 )
{
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".dbf" ) );
}
else
{
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".shp" ) );
}
mFileName->setFilePath( fileName );
}
checkOk();
} );

mAddAttributeButton->setEnabled( false );
mRemoveAttributeButton->setEnabled( false );
@@ -290,8 +307,15 @@ QString QgsNewVectorLayerDialog::execAndCreateLayer( QString &errorMessage, QWid

QgsSettings settings;
QString fileName = geomDialog.filename();
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && ( geometrytype != QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) ) )
fileName += QLatin1String( ".shp" );
else if ( fileformat == QLatin1String( "ESRI Shapefile" ) && ( geometrytype == QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( ".dbf" ), Qt::CaseInsensitive ) ) )
{
if ( fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".dbf" ) );
else
fileName += QLatin1String( ".dbf" );
}

settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
settings.setValue( QStringLiteral( "UI/encoding" ), enc );

0 comments on commit ef186ea

Please sign in to comment.
You can’t perform that action at this time.