Skip to content

Commit

Permalink
allow creating geometryless (plain) DBF tables from the New Shapefile…
Browse files Browse the repository at this point in the history
… dialog (fix #15654)
  • Loading branch information
alexbruy committed May 5, 2020
1 parent 0ada65b commit ef186ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -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;
}
Expand Down
28 changes: 26 additions & 2 deletions src/gui/qgsnewvectorlayerdialog.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit ef186ea

Please sign in to comment.