QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsnewgeopackagelayerdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnewgeopackagelayerdialog.cpp
3 
4  -------------------
5  begin : April 2016
6  copyright : (C) 2016 by Even Rouault
7  email : even.rouault at spatialys.com
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 
21 
22 #include "qgis.h"
23 #include "qgsapplication.h"
24 #include "qgsproviderregistry.h"
25 #include "qgsvectorlayer.h"
26 #include "qgsproject.h"
29 #include "qgslogger.h"
30 #include "qgssettings.h"
31 #include "qgshelp.h"
32 #include "qgsogrutils.h"
33 #include "qgsgui.h"
34 
35 #include <QPushButton>
36 #include <QLineEdit>
37 #include <QMessageBox>
38 #include <QFileDialog>
39 
40 #include <ogr_api.h>
41 #include <ogr_srs_api.h>
42 #include <gdal_version.h>
43 #include <cpl_error.h>
44 #include <cpl_string.h>
45 
46 #define DEFAULT_OGR_FID_COLUMN_TITLE "fid" // default value from OGR
47 
48 QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::WindowFlags fl )
49  : QDialog( parent, fl )
50 {
51  setupUi( this );
53 
54  connect( mAddAttributeButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked );
55  connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked );
56  connect( mFieldTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged );
57  connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged );
58  connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged );
59  connect( mTableNameEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited );
60  connect( mLayerIdentifierEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited );
61  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsNewGeoPackageLayerDialog::buttonBox_accepted );
62  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsNewGeoPackageLayerDialog::buttonBox_rejected );
63  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewGeoPackageLayerDialog::showHelp );
64 
65  mAddAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionNewAttribute.svg" ) ) );
66  mRemoveAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteAttribute.svg" ) ) );
67 
68  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No geometry" ), wkbNone );
69  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), wkbPoint );
70  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "Line" ), wkbLineString );
71  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "Polygon" ), wkbPolygon );
72  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "MultiPoint" ), wkbMultiPoint );
73  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "MultiLine" ), wkbMultiLineString );
74  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "MultiPolygon" ), wkbMultiPolygon );
75 
76 #if 0
77  // QGIS always create CompoundCurve and there's no real interest of having just CircularString. CompoundCurve are more useful
78  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "CircularString" ), wkbCircularString );
79 #endif
80  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "CompoundCurve" ), wkbCompoundCurve );
81  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "CurvePolygon" ), wkbCurvePolygon );
82  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "MultiCurve" ), wkbMultiCurve );
83  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "MultiSurface" ), wkbMultiSurface );
84 
85  mGeometryWithZCheckBox->setEnabled( false );
86  mGeometryWithMCheckBox->setEnabled( false );
87  mGeometryColumnEdit->setEnabled( false );
88  mGeometryColumnEdit->setText( QStringLiteral( "geometry" ) );
89  mFeatureIdColumnEdit->setPlaceholderText( QStringLiteral( DEFAULT_OGR_FID_COLUMN_TITLE ) );
90  mCheckBoxCreateSpatialIndex->setEnabled( false );
91  mCrsSelector->setEnabled( false );
92 
93  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldText.svg" ) ), tr( "Text data" ), "text" );
94  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldInteger.svg" ) ), tr( "Whole number (integer)" ), "integer" );
95  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldInteger.svg" ) ), tr( "Whole number (integer 64 bit)" ), "integer64" );
96  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldFloat.svg" ) ), tr( "Decimal number (real)" ), "real" );
97  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDate.svg" ) ), tr( "Date" ), "date" );
98  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDateTime.svg" ) ), tr( "Date & time" ), "datetime" );
99  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldBool.svg" ) ), tr( "Boolean" ), "bool" );
100  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldBinary.svg" ) ), tr( "Binary (BLOB)" ), "binary" );
101 
102  mOkButton = buttonBox->button( QDialogButtonBox::Ok );
103  mOkButton->setEnabled( false );
104 
105  connect( mFieldNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::fieldNameChanged );
106  connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewGeoPackageLayerDialog::selectionChanged );
107  connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::checkOk );
108 
109  mAddAttributeButton->setEnabled( false );
110  mRemoveAttributeButton->setEnabled( false );
111 
112  mCheckBoxCreateSpatialIndex->setChecked( true );
113 
114  QgsSettings settings;
115  mDatabase->setStorageMode( QgsFileWidget::SaveFile );
116  mDatabase->setFilter( tr( "GeoPackage" ) + " (*.gpkg)" );
117  mDatabase->setDialogTitle( tr( "Select Existing or Create a New GeoPackage Database File…" ) );
118  mDatabase->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
119  mDatabase->setConfirmOverwrite( false );
120  connect( mDatabase, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
121  {
122  QgsSettings settings;
123  QFileInfo tmplFileInfo( filePath );
124  settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
125  if ( !filePath.isEmpty() && !mTableNameEdited )
126  {
127  QFileInfo fileInfo( filePath );
128  mTableNameEdit->setText( fileInfo.baseName() );
129  }
130  checkOk();
131  } );
132 }
133 
135 {
136  mCrsSelector->setCrs( crs );
137 }
138 
140 {
141  mDatabase->setReadOnly( true );
142 }
143 
144 void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged( int )
145 {
146  QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
147  mFieldLengthEdit->setEnabled( myType == QLatin1String( "text" ) );
148  if ( myType != QLatin1String( "text" ) )
149  mFieldLengthEdit->clear();
150 }
151 
152 
153 void QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged( int )
154 {
155  OGRwkbGeometryType geomType = static_cast<OGRwkbGeometryType>
156  ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
157  bool isSpatial = geomType != wkbNone;
158  mGeometryWithZCheckBox->setEnabled( isSpatial );
159  mGeometryWithMCheckBox->setEnabled( isSpatial );
160  mGeometryColumnEdit->setEnabled( isSpatial );
161  mCheckBoxCreateSpatialIndex->setEnabled( isSpatial );
162  mCrsSelector->setEnabled( isSpatial );
163 }
164 
165 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged( const QString &text )
166 {
167  mTableNameEdited = !text.isEmpty();
168  if ( !text.isEmpty() && !mLayerIdentifierEdited )
169  {
170  mLayerIdentifierEdit->setText( text );
171  }
172 }
173 
174 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited( const QString &text )
175 {
176  // Remember if the user explicitly defined a name
177  mTableNameEdited = !text.isEmpty();
178 }
179 
180 void QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited( const QString &text )
181 {
182  // Remember if the user explicitly defined a name
183  mLayerIdentifierEdited = !text.isEmpty();
184 }
185 
186 void QgsNewGeoPackageLayerDialog::checkOk()
187 {
188  bool ok = !mDatabase->filePath().isEmpty() &&
189  !mTableNameEdit->text().isEmpty();
190  mOkButton->setEnabled( ok );
191 }
192 
193 void QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked()
194 {
195  if ( !mFieldNameEdit->text().isEmpty() )
196  {
197  QString myName = mFieldNameEdit->text();
198  const QString featureId = mFeatureIdColumnEdit->text().isEmpty() ? QStringLiteral( DEFAULT_OGR_FID_COLUMN_TITLE ) : mFeatureIdColumnEdit->text();
199  if ( myName.compare( featureId, Qt::CaseInsensitive ) == 0 )
200  {
201  QMessageBox::critical( this, tr( "Add Field" ), tr( "The field cannot have the same name as the feature identifier." ) );
202  return;
203  }
204 
205  //use userrole to avoid translated type string
206  QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
207  QString length = mFieldLengthEdit->text();
208  mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType << length ) );
209 
210  checkOk();
211 
212  mFieldNameEdit->clear();
213  }
214 }
215 
216 void QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked()
217 {
218  delete mAttributeView->currentItem();
219 
220  checkOk();
221 }
222 
223 void QgsNewGeoPackageLayerDialog::fieldNameChanged( const QString &name )
224 {
225  mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
226 }
227 
228 void QgsNewGeoPackageLayerDialog::selectionChanged()
229 {
230  mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
231 }
232 
233 void QgsNewGeoPackageLayerDialog::buttonBox_accepted()
234 {
235  if ( apply() )
236  accept();
237 }
238 
239 void QgsNewGeoPackageLayerDialog::buttonBox_rejected()
240 {
241  reject();
242 }
243 
244 bool QgsNewGeoPackageLayerDialog::apply()
245 {
246  QString fileName( mDatabase->filePath() );
247  if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
248  fileName += QLatin1String( ".gpkg" );
249 
250  bool createNewDb = false;
251 
252  if ( QFile( fileName ).exists( fileName ) )
253  {
254  bool overwrite = false;
255 
256  switch ( mBehavior )
257  {
258  case Prompt:
259  {
260  QMessageBox msgBox;
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 );
268  bool cancel = false;
269  if ( property( "hideDialogs" ).toBool() )
270  {
271  overwrite = property( "question_existing_db_answer_overwrite" ).toBool();
272  if ( !overwrite )
273  cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool();
274  }
275  else
276  {
277  int ret = msgBox.exec();
278  if ( ret == QMessageBox::Cancel )
279  cancel = true;
280  if ( msgBox.clickedButton() == overwriteButton )
281  overwrite = true;
282  }
283  if ( cancel )
284  {
285  return false;
286  }
287  break;
288  }
289 
290  case Overwrite:
291  overwrite = true;
292  break;
293 
294  case AddNewLayer:
295  overwrite = false;
296  break;
297  }
298 
299  if ( overwrite )
300  {
301  QFile( fileName ).remove();
302  createNewDb = true;
303  }
304  }
305  else
306  {
307  createNewDb = true;
308  }
309 
310  OGRSFDriverH hGpkgDriver = OGRGetDriverByName( "GPKG" );
311  if ( !hGpkgDriver )
312  {
313  if ( !property( "hideDialogs" ).toBool() )
314  QMessageBox::critical( this, tr( "New GeoPackage Layer" ),
315  tr( "Layer creation failed. GeoPackage driver not found." ) );
316  return false;
317  }
318 
320  if ( createNewDb )
321  {
322  hDS.reset( OGR_Dr_CreateDataSource( hGpkgDriver, fileName.toUtf8().constData(), nullptr ) );
323  if ( !hDS )
324  {
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 );
328  return false;
329  }
330  }
331  else
332  {
333  OGRSFDriverH hDriver = nullptr;
334  hDS.reset( OGROpen( fileName.toUtf8().constData(), true, &hDriver ) );
335  if ( !hDS )
336  {
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 );
340  return false;
341  }
342  if ( hDriver != hGpkgDriver )
343  {
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 );
347  return false;
348  }
349  }
350 
351  QString tableName( mTableNameEdit->text() );
352 
353  bool overwriteTable = false;
354  if ( OGR_DS_GetLayerByName( hDS.get(), tableName.toUtf8().constData() ) )
355  {
356  if ( property( "hideDialogs" ).toBool() )
357  {
358  overwriteTable = property( "question_existing_layer_answer_overwrite" ).toBool();
359  }
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 )
363  {
364  overwriteTable = true;
365  }
366 
367  if ( !overwriteTable )
368  {
369  return false;
370  }
371  }
372 
373  QString layerIdentifier( mLayerIdentifierEdit->text() );
374  QString layerDescription( mLayerDescriptionEdit->text() );
375 
376  OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType>
377  ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
378 
379  // z-coordinate & m-value.
380  if ( mGeometryWithZCheckBox->isChecked() )
381  wkbType = OGR_GT_SetZ( wkbType );
382 
383  if ( mGeometryWithMCheckBox->isChecked() )
384  wkbType = OGR_GT_SetM( wkbType );
385 
386  OGRSpatialReferenceH hSRS = nullptr;
387  // consider spatial reference system of the layer
388  QgsCoordinateReferenceSystem srs = mCrsSelector->crs();
389  if ( wkbType != wkbNone && srs.isValid() )
390  {
391  QString srsWkt = srs.toWkt();
392  hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
393  }
394 
395  // Set options
396  char **options = nullptr;
397 
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() );
404 
405  QString featureId( mFeatureIdColumnEdit->text() );
406  if ( !featureId.isEmpty() )
407  options = CSLSetNameValue( options, "FID", featureId.toUtf8().constData() );
408 
409  QString geometryColumn( mGeometryColumnEdit->text() );
410  if ( wkbType != wkbNone && !geometryColumn.isEmpty() )
411  options = CSLSetNameValue( options, "GEOMETRY_COLUMN", geometryColumn.toUtf8().constData() );
412 
413  if ( wkbType != wkbNone )
414  options = CSLSetNameValue( options, "SPATIAL_INDEX", mCheckBoxCreateSpatialIndex->isChecked() ? "YES" : "NO" );
415 
416  OGRLayerH hLayer = OGR_DS_CreateLayer( hDS.get(), tableName.toUtf8().constData(), hSRS, wkbType, options );
417  CSLDestroy( options );
418  if ( hSRS )
419  OSRRelease( hSRS );
420  if ( !hLayer )
421  {
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 );
425  return false;
426  }
427 
428  QTreeWidgetItemIterator it( mAttributeView );
429  while ( *it )
430  {
431  QString fieldName( ( *it )->text( 0 ) );
432  QString fieldType( ( *it )->text( 1 ) );
433  QString fieldWidth( ( *it )->text( 2 ) );
434 
435  bool isBool = false;
436  OGRFieldType ogrType( OFTString );
437  if ( fieldType == QLatin1String( "text" ) )
438  ogrType = OFTString;
439  else if ( fieldType == QLatin1String( "integer" ) )
440  ogrType = OFTInteger;
441  else if ( fieldType == QLatin1String( "integer64" ) )
442  ogrType = OFTInteger64;
443  else if ( fieldType == QLatin1String( "real" ) )
444  ogrType = OFTReal;
445  else if ( fieldType == QLatin1String( "date" ) )
446  ogrType = OFTDate;
447  else if ( fieldType == QLatin1String( "datetime" ) )
448  ogrType = OFTDateTime;
449  else if ( fieldType == QLatin1String( "bool" ) )
450  {
451  ogrType = OFTInteger;
452  isBool = true;
453  }
454  else if ( fieldType == QLatin1String( "binary" ) )
455  ogrType = OFTBinary;
456 
457  int ogrWidth = fieldWidth.toInt();
458 
459  gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( fieldName.toUtf8().constData(), ogrType ) );
460  if ( ogrType != OFTBinary )
461  OGR_Fld_SetWidth( fld.get(), ogrWidth );
462  if ( isBool )
463  OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
464 
465  if ( OGR_L_CreateField( hLayer, fld.get(), true ) != OGRERR_NONE )
466  {
467  if ( !property( "hideDialogs" ).toBool() )
468  {
469  QMessageBox::critical( this, tr( "New GeoPackage Layer" ),
470  tr( "Creation of field %1 failed (OGR error: %2)" )
471  .arg( fieldName, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
472  }
473  return false;
474  }
475 
476  ++it;
477  }
478 
479  // In GDAL >= 2.0, the driver implements a deferred creation strategy, so
480  // issue a command that will force table creation
481  CPLErrorReset();
482  OGR_L_ResetReading( hLayer );
483  if ( CPLGetLastErrorType() != CE_None )
484  {
485  QString msg( tr( "Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
486  if ( !property( "hideDialogs" ).toBool() )
487  QMessageBox::critical( this, tr( "New GeoPackage Layer" ), msg );
488  return false;
489  }
490  hDS.reset();
491 
492  QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
493  QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
495  std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( uri, userVisiblelayerName, QStringLiteral( "ogr" ), layerOptions );
496  if ( layer->isValid() )
497  {
498  if ( mAddToProject )
499  {
500  // register this layer with the central layers registry
501  QList<QgsMapLayer *> myList;
502  myList << layer.release();
503  //addMapLayers returns a list of all successfully added layers
504  //so we compare that to our original list.
505  if ( myList == QgsProject::instance()->addMapLayers( myList ) )
506  return true;
507  }
508  else
509  {
510  return true;
511  }
512  }
513  else
514  {
515  if ( !property( "hideDialogs" ).toBool() )
516  QMessageBox::critical( this, tr( "New GeoPackage Layer" ), tr( "%1 is an invalid layer and cannot be loaded." ).arg( tableName ) );
517  }
518 
519  return false;
520 }
521 
523 {
524  mBehavior = behavior;
525 }
526 
528 {
529  mAddToProject = addToProject;
530 }
531 
532 void QgsNewGeoPackageLayerDialog::showHelp()
533 {
534  QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );
535 }
void setOverwriteBehavior(OverwriteBehavior behavior)
Sets the behavior to use when a path to an existing geopackage file is used.
#define DEFAULT_OGR_FID_COLUMN_TITLE
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
Setting options for loading vector layers.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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.
const QgsCoordinateReferenceSystem & crs
OverwriteBehavior
Behavior to use when an existing geopackage already exists.
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
void setAddToProject(bool addToProject)
Sets whether a newly created layer should automatically be added to the current project.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:96
void lockDatabasePath()
Sets the database path widgets to a locked and read-only mode.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:442
This class represents a coordinate reference system (CRS).
std::unique_ptr< std::remove_pointer< OGRFieldDefnH >::type, OGRFldDeleter > ogr_field_def_unique_ptr
Scoped OGR field definition.
Definition: qgsogrutils.h:124
Select a single new or pre-existing file.
Definition: qgsfilewidget.h:68
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...
Definition: qgsgui.cpp:127
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs value for the new layer in the dialog.
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
Definition: qgsogrutils.h:114
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
void * OGRSpatialReferenceH
bool isValid() const
Returns whether this CRS is correctly initialized and usable.