diff --git a/src/core/providers/ogr/qgsogrproviderutils.cpp b/src/core/providers/ogr/qgsogrproviderutils.cpp index 93a80b402b57..51a9cdedf60a 100644 --- a/src/core/providers/ogr/qgsogrproviderutils.cpp +++ b/src/core/providers/ogr/qgsogrproviderutils.cpp @@ -917,6 +917,11 @@ bool QgsOgrProviderUtils::createEmptyDataSource( const QString &uri, { field = OGR_Fld_Create( codec->fromUnicode( it->first ).constData(), OFTDateTime ); } + else if ( fields[0] == QLatin1String( "bool" ) ) + { + field = OGR_Fld_Create( codec->fromUnicode( it->first ).constData(), OFTInteger ); + OGR_Fld_SetSubType( field, OFSTBoolean ); + } else { QgsMessageLog::logMessage( QObject::tr( "field %1 with unsupported type %2 skipped" ).arg( it->first, fields[0] ), QObject::tr( "OGR" ) ); diff --git a/src/gui/qgsnewvectorlayerdialog.cpp b/src/gui/qgsnewvectorlayerdialog.cpp index 181e5a9059ed..fb5ffd03f8f1 100644 --- a/src/gui/qgsnewvectorlayerdialog.cpp +++ b/src/gui/qgsnewvectorlayerdialog.cpp @@ -31,6 +31,8 @@ #include "qgsvariantutils.h" #include "qgsogrproviderutils.h" +#include + #include #include #include @@ -57,6 +59,9 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla mTypeBox->addItem( QgsFields::iconForFieldType( QMetaType::Type::Int ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::Int ), "Integer" ); mTypeBox->addItem( QgsFields::iconForFieldType( QMetaType::Type::Double ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ), "Real" ); mTypeBox->addItem( QgsFields::iconForFieldType( QMetaType::Type::QDate ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ), "Date" ); +#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION( 3, 9, 0 ) + mTypeBox->addItem( QgsFields::iconForFieldType( QMetaType::Type::Bool ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::Bool ), "bool" ); +#endif mWidth->setValidator( new QIntValidator( 1, 255, this ) ); mPrecision->setValidator( new QIntValidator( 0, 15, this ) ); @@ -154,6 +159,7 @@ void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged( int index ) if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 ) mWidth->setText( QStringLiteral( "80" ) ); mPrecision->setEnabled( false ); + mWidth->setEnabled( true ); mWidth->setValidator( new QIntValidator( 1, 255, this ) ); break; @@ -161,6 +167,7 @@ void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged( int index ) if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 ) mWidth->setText( QStringLiteral( "10" ) ); mPrecision->setEnabled( false ); + mWidth->setEnabled( true ); mWidth->setValidator( new QIntValidator( 1, 10, this ) ); break; @@ -171,11 +178,15 @@ void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged( int index ) mPrecision->setText( QStringLiteral( "6" ) ); mPrecision->setEnabled( true ); + mWidth->setEnabled( true ); mWidth->setValidator( new QIntValidator( 1, 20, this ) ); break; default: - QgsDebugError( QStringLiteral( "unexpected index" ) ); + mPrecision->setEnabled( false ); + mWidth->setEnabled( false ); + mWidth->clear(); + mPrecision->clear(); break; } } diff --git a/tests/src/core/testqgsogrutils.cpp b/tests/src/core/testqgsogrutils.cpp index 872c329baea2..0ced946249e7 100644 --- a/tests/src/core/testqgsogrutils.cpp +++ b/tests/src/core/testqgsogrutils.cpp @@ -74,6 +74,7 @@ class TestQgsOgrUtils : public QObject void testOgrStringToVariant_data(); void testOgrStringToVariant(); void testOgrUtilsStoredStyle(); + void testOgrUtilsCreateFields(); #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION( 3, 3, 0 ) void testConvertFieldDomain(); @@ -1086,6 +1087,41 @@ void TestQgsOgrUtils::testOgrUtilsStoredStyle() QCOMPARE( QSet( names.constBegin(), names.constEnd() ), QSet() << QStringLiteral( "style1" ) << QStringLiteral( "style2" ) << QStringLiteral( "style3" ) ); } +/** + * Test for issue GH #60324 + */ +void TestQgsOgrUtils::testOgrUtilsCreateFields() +{ + // Create a test GPKG file with layer in a temporary directory + QTemporaryDir tempDir; + QVERIFY( tempDir.isValid() ); + QString tempDirPath = tempDir.path(); + QString testFile = tempDirPath + "/test.gpkg"; + // Create datasource + QString error; + QList> fields; + fields << QPair( QStringLiteral( "boolfield" ), QStringLiteral( "bool" ) ); + fields << QPair( QStringLiteral( "intfield" ), QStringLiteral( "Integer" ) ); + fields << QPair( QStringLiteral( "realfield" ), QStringLiteral( "Real" ) ); + fields << QPair( QStringLiteral( "stringfield" ), QStringLiteral( "String" ) ); + fields << QPair( QStringLiteral( "datefield" ), QStringLiteral( "Date" ) ); + fields << QPair( QStringLiteral( "datetimefield" ), QStringLiteral( "DateTime" ) ); + QVERIFY( QgsOgrProviderUtils::createEmptyDataSource( testFile, QStringLiteral( "GPKG" ), QStringLiteral( "UTF-8" ), Qgis::WkbType::Point, fields, QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), error ) ); + + // Open the datasource + QgsVectorLayer vl = QgsVectorLayer( testFile, QStringLiteral( "test" ), QStringLiteral( "ogr" ) ); + QVERIFY( vl.isValid() ); + + const QgsFields gotFields = vl.fields(); + // Field 0 is the FID + QCOMPARE( gotFields[1].type(), QMetaType::Type::Bool ); + QCOMPARE( gotFields[2].type(), QMetaType::Type::Int ); + QCOMPARE( gotFields[3].type(), QMetaType::Type::Double ); + QCOMPARE( gotFields[4].type(), QMetaType::Type::QString ); + QCOMPARE( gotFields[5].type(), QMetaType::Type::QDate ); + QCOMPARE( gotFields[6].type(), QMetaType::Type::QDateTime ); +} + #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION( 3, 3, 0 ) void TestQgsOgrUtils::testConvertFieldDomain() {