35 #include <QPushButton> 37 #include <QMessageBox> 38 #include <QFileDialog> 42 #include <ogr_srs_api.h> 43 #include <gdal_version.h> 44 #include <cpl_error.h> 45 #include <cpl_string.h> 48 : QDialog( parent, fl )
53 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked );
54 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked );
55 connect( mFieldTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged );
56 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged );
57 connect( mTableNameEdit, &QLineEdit::textChanged,
this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged );
58 connect( mTableNameEdit, &QLineEdit::textEdited,
this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited );
59 connect( mLayerIdentifierEdit, &QLineEdit::textEdited,
this, &QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited );
60 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QgsNewGeoPackageLayerDialog::buttonBox_accepted );
61 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QgsNewGeoPackageLayerDialog::buttonBox_rejected );
62 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewGeoPackageLayerDialog::showHelp );
71 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPointLayer.svg" ) ), tr(
"MultiPoint" ), wkbMultiPoint );
72 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconLineLayer.svg" ) ), tr(
"MultiLine" ), wkbMultiLineString );
73 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPolygonLayer.svg" ) ), tr(
"MultiPolygon" ), wkbMultiPolygon );
77 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconLineLayer.svg" ) ), tr(
"CircularString" ), wkbCircularString );
79 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconLineLayer.svg" ) ), tr(
"CompoundCurve" ), wkbCompoundCurve );
80 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPolygonLayer.svg" ) ), tr(
"CurvePolygon" ), wkbCurvePolygon );
82 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPolygonLayer.svg" ) ), tr(
"MultiSurface" ), wkbMultiSurface );
84 mGeometryWithZCheckBox->setEnabled(
false );
85 mGeometryWithMCheckBox->setEnabled(
false );
86 mGeometryColumnEdit->setEnabled(
false );
87 mGeometryColumnEdit->setText(
"geometry" );
88 mFeatureIdColumnEdit->setText(
"fid" );
89 mCheckBoxCreateSpatialIndex->setEnabled(
false );
90 mCrsSelector->setEnabled(
false );
93 mFieldTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconFieldInteger.svg" ) ), tr(
"Whole number (integer)" ),
"integer" );
94 mFieldTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconFieldInteger.svg" ) ), tr(
"Whole number (integer 64 bit)" ),
"integer64" );
95 mFieldTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconFieldFloat.svg" ) ), tr(
"Decimal number (real)" ),
"real" );
99 mOkButton = buttonBox->button( QDialogButtonBox::Ok );
100 mOkButton->setEnabled(
false );
102 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewGeoPackageLayerDialog::fieldNameChanged );
103 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewGeoPackageLayerDialog::selectionChanged );
104 connect( mTableNameEdit, &QLineEdit::textChanged,
this, &QgsNewGeoPackageLayerDialog::checkOk );
106 mAddAttributeButton->setEnabled(
false );
107 mRemoveAttributeButton->setEnabled(
false );
109 mCheckBoxCreateSpatialIndex->setChecked(
true );
113 mDatabase->setFilter( tr(
"GeoPackage" ) +
" (*.gpkg)" );
114 mDatabase->setDialogTitle( tr(
"Select Existing or Create a New GeoPackage Database Fileā¦" ) );
115 mDatabase->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
116 mDatabase->setConfirmOverwrite(
false );
120 QFileInfo tmplFileInfo( filePath );
121 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
122 if ( !filePath.isEmpty() && !mTableNameEdited )
124 QFileInfo fileInfo( filePath );
125 mTableNameEdit->setText( fileInfo.baseName() );
137 mCrsSelector->setCrs( crs );
142 mDatabase->setReadOnly(
true );
145 void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged(
int )
147 QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
148 mFieldLengthEdit->setEnabled( myType == QLatin1String(
"text" ) );
149 if ( myType != QLatin1String(
"text" ) )
150 mFieldLengthEdit->clear();
154 void QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged(
int )
156 OGRwkbGeometryType geomType =
static_cast<OGRwkbGeometryType
> 157 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
158 bool isSpatial = geomType != wkbNone;
159 mGeometryWithZCheckBox->setEnabled( isSpatial );
160 mGeometryWithMCheckBox->setEnabled( isSpatial );
161 mGeometryColumnEdit->setEnabled( isSpatial );
162 mCheckBoxCreateSpatialIndex->setEnabled( isSpatial );
163 mCrsSelector->setEnabled( isSpatial );
166 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged(
const QString &text )
168 mTableNameEdited = !text.isEmpty();
169 if ( !text.isEmpty() && !mLayerIdentifierEdited )
171 mLayerIdentifierEdit->setText( text );
175 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited(
const QString &text )
178 mTableNameEdited = !text.isEmpty();
181 void QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited(
const QString &text )
184 mLayerIdentifierEdited = !text.isEmpty();
187 void QgsNewGeoPackageLayerDialog::checkOk()
189 bool ok = !mDatabase->filePath().isEmpty() &&
190 !mTableNameEdit->text().isEmpty();
191 mOkButton->setEnabled( ok );
194 void QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked()
196 if ( !mFieldNameEdit->text().isEmpty() )
198 QString myName = mFieldNameEdit->text();
199 if ( myName == mFeatureIdColumnEdit->text() )
201 QMessageBox::critical(
this, tr(
"Add Field" ), tr(
"The field cannot have the same name as the feature identifier." ) );
206 QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
207 QString length = mFieldLengthEdit->text();
208 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << length ) );
212 mFieldNameEdit->clear();
216 void QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked()
218 delete mAttributeView->currentItem();
223 void QgsNewGeoPackageLayerDialog::fieldNameChanged(
const QString &name )
225 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
228 void QgsNewGeoPackageLayerDialog::selectionChanged()
230 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
233 void QgsNewGeoPackageLayerDialog::buttonBox_accepted()
239 void QgsNewGeoPackageLayerDialog::buttonBox_rejected()
244 bool QgsNewGeoPackageLayerDialog::apply()
246 QString fileName( mDatabase->filePath() );
247 if ( !fileName.endsWith( QLatin1String(
".gpkg" ), Qt::CaseInsensitive ) )
248 fileName += QLatin1String(
".gpkg" );
250 bool createNewDb =
false;
252 if ( QFile( fileName ).exists( fileName ) )
254 bool overwrite =
false;
261 msgBox.setIcon( QMessageBox::Question );
262 msgBox.setWindowTitle( tr(
"New GeoPackage Layer" ) );
263 msgBox.setText( tr(
"The File already exists. Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
264 QPushButton *overwriteButton = msgBox.addButton( tr(
"Overwrite" ), QMessageBox::ActionRole );
265 QPushButton *addNewLayerButton = msgBox.addButton( tr(
"Add new layer" ), QMessageBox::ActionRole );
266 msgBox.setStandardButtons( QMessageBox::Cancel );
267 msgBox.setDefaultButton( addNewLayerButton );
269 if ( property(
"hideDialogs" ).toBool() )
271 overwrite = property(
"question_existing_db_answer_overwrite" ).toBool();
273 cancel = !property(
"question_existing_db_answer_add_new_layer" ).toBool();
277 int ret = msgBox.exec();
278 if ( ret == QMessageBox::Cancel )
280 if ( msgBox.clickedButton() == overwriteButton )
301 QFile( fileName ).remove();
310 OGRSFDriverH hGpkgDriver = OGRGetDriverByName(
"GPKG" );
313 if ( !property(
"hideDialogs" ).toBool() )
314 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ),
315 tr(
"Layer creation failed. GeoPackage driver not found." ) );
322 hDS.reset( OGR_Dr_CreateDataSource( hGpkgDriver, fileName.toUtf8().constData(), nullptr ) );
325 QString msg( tr(
"Creation of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
326 if ( !property(
"hideDialogs" ).toBool() )
327 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
333 OGRSFDriverH hDriver =
nullptr;
334 hDS.reset( OGROpen( fileName.toUtf8().constData(),
true, &hDriver ) );
337 QString msg( tr(
"Opening of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
338 if ( !property(
"hideDialogs" ).toBool() )
339 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
342 if ( hDriver != hGpkgDriver )
344 QString msg( tr(
"Opening of file succeeded, but this is not a GeoPackage database." ) );
345 if ( !property(
"hideDialogs" ).toBool() )
346 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
351 QString tableName( mTableNameEdit->text() );
353 bool overwriteTable =
false;
354 if ( OGR_DS_GetLayerByName( hDS.get(), tableName.toUtf8().constData() ) )
356 if ( property(
"hideDialogs" ).toBool() )
358 overwriteTable = property(
"question_existing_layer_answer_overwrite" ).toBool();
360 else if ( QMessageBox::question(
this, tr(
"New GeoPackage Layer" ),
361 tr(
"A table with the same name already exists. Do you want to overwrite it?" ),
362 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
364 overwriteTable =
true;
367 if ( !overwriteTable )
373 QString layerIdentifier( mLayerIdentifierEdit->text() );
374 QString layerDescription( mLayerDescriptionEdit->text() );
376 OGRwkbGeometryType wkbType =
static_cast<OGRwkbGeometryType
> 377 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
380 if ( mGeometryWithZCheckBox->isChecked() )
381 wkbType = OGR_GT_SetZ( wkbType );
383 if ( mGeometryWithMCheckBox->isChecked() )
384 wkbType = OGR_GT_SetM( wkbType );
389 if ( wkbType != wkbNone && srs.
isValid() )
391 QString srsWkt = srs.
toWkt();
392 hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
396 char **options =
nullptr;
398 if ( overwriteTable )
399 options = CSLSetNameValue( options,
"OVERWRITE",
"YES" );
400 if ( !layerIdentifier.isEmpty() )
401 options = CSLSetNameValue( options,
"IDENTIFIER", layerIdentifier.toUtf8().constData() );
402 if ( !layerDescription.isEmpty() )
403 options = CSLSetNameValue( options,
"DESCRIPTION", layerDescription.toUtf8().constData() );
405 QString featureId( mFeatureIdColumnEdit->text() );
406 if ( !featureId.isEmpty() )
407 options = CSLSetNameValue( options,
"FID", featureId.toUtf8().constData() );
409 QString geometryColumn( mGeometryColumnEdit->text() );
410 if ( wkbType != wkbNone && !geometryColumn.isEmpty() )
411 options = CSLSetNameValue( options,
"GEOMETRY_COLUMN", geometryColumn.toUtf8().constData() );
413 if ( wkbType != wkbNone )
414 options = CSLSetNameValue( options,
"SPATIAL_INDEX", mCheckBoxCreateSpatialIndex->isChecked() ?
"YES" :
"NO" );
416 OGRLayerH hLayer = OGR_DS_CreateLayer( hDS.get(), tableName.toUtf8().constData(), hSRS, wkbType, options );
417 CSLDestroy( options );
422 QString msg( tr(
"Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
423 if ( !property(
"hideDialogs" ).toBool() )
424 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
428 QTreeWidgetItemIterator it( mAttributeView );
431 QString fieldName( ( *it )->text( 0 ) );
432 QString fieldType( ( *it )->text( 1 ) );
433 QString fieldWidth( ( *it )->text( 2 ) );
435 OGRFieldType ogrType( OFTString );
436 if ( fieldType == QLatin1String(
"text" ) )
438 else if ( fieldType == QLatin1String(
"integer" ) )
439 ogrType = OFTInteger;
440 else if ( fieldType == QLatin1String(
"integer64" ) )
441 ogrType = OFTInteger64;
442 else if ( fieldType == QLatin1String(
"real" ) )
444 else if ( fieldType == QLatin1String(
"date" ) )
446 else if ( fieldType == QLatin1String(
"datetime" ) )
447 ogrType = OFTDateTime;
449 int ogrWidth = fieldWidth.toInt();
452 OGR_Fld_SetWidth( fld.get(), ogrWidth );
454 if ( OGR_L_CreateField( hLayer, fld.get(), true ) != OGRERR_NONE )
456 if ( !property(
"hideDialogs" ).toBool() )
458 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ),
459 tr(
"Creation of field %1 failed (OGR error: %2)" )
460 .arg( fieldName, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
471 OGR_L_ResetReading( hLayer );
472 if ( CPLGetLastErrorType() != CE_None )
474 QString msg( tr(
"Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
475 if ( !property(
"hideDialogs" ).toBool() )
476 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
481 QString uri( QStringLiteral(
"%1|layername=%2" ).arg( fileName, tableName ) );
482 QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
484 if ( layer->isValid() )
487 QList<QgsMapLayer *> myList;
496 if ( !property(
"hideDialogs" ).toBool() )
497 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), tr(
"%1 is an invalid layer and cannot be loaded." ).arg( tableName ) );
506 mBehavior = behavior;
509 void QgsNewGeoPackageLayerDialog::showHelp()
511 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );
QList< QgsMapLayer * > addMapLayers(const QList< QgsMapLayer *> &mapLayers, bool addToLegend=true, bool takeOwnership=true)
Add a list of layers to the map of loaded layers.
void setOverwriteBehavior(OverwriteBehavior behavior)
Sets the behavior to use when a path to an existing geopackage file is used.
This class is a composition of two QSettings instances:
QgsNewGeoPackageLayerDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Keep existing contents and add new layer.
static QgsGui * instance()
Returns a pointer to the singleton instance.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
OverwriteBehavior
Behavior to use when an existing geopackage already exists.
Overwrite whole geopackage.
std::unique_ptr< void, OGRFldDeleter > ogr_field_def_unique_ptr
Scoped OGR field definition.
void lockDatabasePath()
Sets the database path widgets to a locked and read-only mode.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
static QgsProject * instance()
Returns the QgsProject singleton instance.
This class represents a coordinate reference system (CRS).
QString toWkt() const
Returns a WKT representation of this CRS.
std::unique_ptr< void, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs value for the new layer in the dialog.
~QgsNewGeoPackageLayerDialog() override
Represents a vector layer which manages a vector based data sets.
void * OGRSpatialReferenceH
bool isValid() const
Returns whether this CRS is correctly initialized and usable.