QGIS API Documentation  3.6.0-Noosa (5873452)
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 #include <QLibrary>
40 
41 #include <ogr_api.h>
42 #include <ogr_srs_api.h>
43 #include <gdal_version.h>
44 #include <cpl_error.h>
45 #include <cpl_string.h>
46 
47 #define DEFAULT_OGR_FID_COLUMN_TITLE "fid" // default value from OGR
48 
49 QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::WindowFlags fl )
50  : QDialog( parent, fl )
51 {
52  setupUi( this );
54 
55  connect( mAddAttributeButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked );
56  connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked );
57  connect( mFieldTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged );
58  connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged );
59  connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged );
60  connect( mTableNameEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited );
61  connect( mLayerIdentifierEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited );
62  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsNewGeoPackageLayerDialog::buttonBox_accepted );
63  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsNewGeoPackageLayerDialog::buttonBox_rejected );
64  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewGeoPackageLayerDialog::showHelp );
65 
66  mAddAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionNewAttribute.svg" ) ) );
67  mRemoveAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteAttribute.svg" ) ) );
68 
69  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No geometry" ), wkbNone );
70  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), wkbPoint );
71  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "Line" ), wkbLineString );
72  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "Polygon" ), wkbPolygon );
73  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "MultiPoint" ), wkbMultiPoint );
74  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "MultiLine" ), wkbMultiLineString );
75  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "MultiPolygon" ), wkbMultiPolygon );
76 
77 #if 0
78  // QGIS always create CompoundCurve and there's no real interest of having just CircularString. CompoundCurve are more useful
79  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "CircularString" ), wkbCircularString );
80 #endif
81  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "CompoundCurve" ), wkbCompoundCurve );
82  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "CurvePolygon" ), wkbCurvePolygon );
83  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "MultiCurve" ), wkbMultiCurve );
84  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "MultiSurface" ), wkbMultiSurface );
85 
86  mGeometryWithZCheckBox->setEnabled( false );
87  mGeometryWithMCheckBox->setEnabled( false );
88  mGeometryColumnEdit->setEnabled( false );
89  mGeometryColumnEdit->setText( QStringLiteral( "geometry" ) );
90  mFeatureIdColumnEdit->setPlaceholderText( QStringLiteral( DEFAULT_OGR_FID_COLUMN_TITLE ) );
91  mCheckBoxCreateSpatialIndex->setEnabled( false );
92  mCrsSelector->setEnabled( false );
93 
94  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldText.svg" ) ), tr( "Text data" ), "text" );
95  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldInteger.svg" ) ), tr( "Whole number (integer)" ), "integer" );
96  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldInteger.svg" ) ), tr( "Whole number (integer 64 bit)" ), "integer64" );
97  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldFloat.svg" ) ), tr( "Decimal number (real)" ), "real" );
98  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDate.svg" ) ), tr( "Date" ), "date" );
99  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDateTime.svg" ) ), tr( "Date & time" ), "datetime" );
100  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldBool.svg" ) ), tr( "Boolean" ), "bool" );
101  mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldBinary.svg" ) ), tr( "Binary (BLOB)" ), "binary" );
102 
103  mOkButton = buttonBox->button( QDialogButtonBox::Ok );
104  mOkButton->setEnabled( false );
105 
106  connect( mFieldNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::fieldNameChanged );
107  connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewGeoPackageLayerDialog::selectionChanged );
108  connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::checkOk );
109 
110  mAddAttributeButton->setEnabled( false );
111  mRemoveAttributeButton->setEnabled( false );
112 
113  mCheckBoxCreateSpatialIndex->setChecked( true );
114 
115  QgsSettings settings;
116  mDatabase->setStorageMode( QgsFileWidget::SaveFile );
117  mDatabase->setFilter( tr( "GeoPackage" ) + " (*.gpkg)" );
118  mDatabase->setDialogTitle( tr( "Select Existing or Create a New GeoPackage Database Fileā€¦" ) );
119  mDatabase->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
120  mDatabase->setConfirmOverwrite( false );
121  connect( mDatabase, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
122  {
123  QgsSettings settings;
124  QFileInfo tmplFileInfo( filePath );
125  settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
126  if ( !filePath.isEmpty() && !mTableNameEdited )
127  {
128  QFileInfo fileInfo( filePath );
129  mTableNameEdit->setText( fileInfo.baseName() );
130  }
131  checkOk();
132  } );
133 }
134 
136 {
137  mCrsSelector->setCrs( crs );
138 }
139 
141 {
142  mDatabase->setReadOnly( true );
143 }
144 
145 void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged( int )
146 {
147  QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
148  mFieldLengthEdit->setEnabled( myType == QLatin1String( "text" ) );
149  if ( myType != QLatin1String( "text" ) )
150  mFieldLengthEdit->clear();
151 }
152 
153 
154 void QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged( int )
155 {
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 );
164 }
165 
166 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged( const QString &text )
167 {
168  mTableNameEdited = !text.isEmpty();
169  if ( !text.isEmpty() && !mLayerIdentifierEdited )
170  {
171  mLayerIdentifierEdit->setText( text );
172  }
173 }
174 
175 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited( const QString &text )
176 {
177  // Remember if the user explicitly defined a name
178  mTableNameEdited = !text.isEmpty();
179 }
180 
181 void QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited( const QString &text )
182 {
183  // Remember if the user explicitly defined a name
184  mLayerIdentifierEdited = !text.isEmpty();
185 }
186 
187 void QgsNewGeoPackageLayerDialog::checkOk()
188 {
189  bool ok = !mDatabase->filePath().isEmpty() &&
190  !mTableNameEdit->text().isEmpty();
191  mOkButton->setEnabled( ok );
192 }
193 
194 void QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked()
195 {
196  if ( !mFieldNameEdit->text().isEmpty() )
197  {
198  QString myName = mFieldNameEdit->text();
199  const QString featureId = mFeatureIdColumnEdit->text().isEmpty() ? QStringLiteral( DEFAULT_OGR_FID_COLUMN_TITLE ) : mFeatureIdColumnEdit->text();
200  if ( myName.compare( featureId, Qt::CaseInsensitive ) == 0 )
201  {
202  QMessageBox::critical( this, tr( "Add Field" ), tr( "The field cannot have the same name as the feature identifier." ) );
203  return;
204  }
205 
206  //use userrole to avoid translated type string
207  QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
208  QString length = mFieldLengthEdit->text();
209  mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType << length ) );
210 
211  checkOk();
212 
213  mFieldNameEdit->clear();
214  }
215 }
216 
217 void QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked()
218 {
219  delete mAttributeView->currentItem();
220 
221  checkOk();
222 }
223 
224 void QgsNewGeoPackageLayerDialog::fieldNameChanged( const QString &name )
225 {
226  mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
227 }
228 
229 void QgsNewGeoPackageLayerDialog::selectionChanged()
230 {
231  mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
232 }
233 
234 void QgsNewGeoPackageLayerDialog::buttonBox_accepted()
235 {
236  if ( apply() )
237  accept();
238 }
239 
240 void QgsNewGeoPackageLayerDialog::buttonBox_rejected()
241 {
242  reject();
243 }
244 
245 bool QgsNewGeoPackageLayerDialog::apply()
246 {
247  QString fileName( mDatabase->filePath() );
248  if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
249  fileName += QLatin1String( ".gpkg" );
250 
251  bool createNewDb = false;
252 
253  if ( QFile( fileName ).exists( fileName ) )
254  {
255  bool overwrite = false;
256 
257  switch ( mBehavior )
258  {
259  case Prompt:
260  {
261  QMessageBox msgBox;
262  msgBox.setIcon( QMessageBox::Question );
263  msgBox.setWindowTitle( tr( "New GeoPackage Layer" ) );
264  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?" ) );
265  QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
266  QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add New Layer" ), QMessageBox::ActionRole );
267  msgBox.setStandardButtons( QMessageBox::Cancel );
268  msgBox.setDefaultButton( addNewLayerButton );
269  bool cancel = false;
270  if ( property( "hideDialogs" ).toBool() )
271  {
272  overwrite = property( "question_existing_db_answer_overwrite" ).toBool();
273  if ( !overwrite )
274  cancel = !property( "question_existing_db_answer_add_new_layer" ).toBool();
275  }
276  else
277  {
278  int ret = msgBox.exec();
279  if ( ret == QMessageBox::Cancel )
280  cancel = true;
281  if ( msgBox.clickedButton() == overwriteButton )
282  overwrite = true;
283  }
284  if ( cancel )
285  {
286  return false;
287  }
288  break;
289  }
290 
291  case Overwrite:
292  overwrite = true;
293  break;
294 
295  case AddNewLayer:
296  overwrite = false;
297  break;
298  }
299 
300  if ( overwrite )
301  {
302  QFile( fileName ).remove();
303  createNewDb = true;
304  }
305  }
306  else
307  {
308  createNewDb = true;
309  }
310 
311  OGRSFDriverH hGpkgDriver = OGRGetDriverByName( "GPKG" );
312  if ( !hGpkgDriver )
313  {
314  if ( !property( "hideDialogs" ).toBool() )
315  QMessageBox::critical( this, tr( "New GeoPackage Layer" ),
316  tr( "Layer creation failed. GeoPackage driver not found." ) );
317  return false;
318  }
319 
321  if ( createNewDb )
322  {
323  hDS.reset( OGR_Dr_CreateDataSource( hGpkgDriver, fileName.toUtf8().constData(), nullptr ) );
324  if ( !hDS )
325  {
326  QString msg( tr( "Creation of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
327  if ( !property( "hideDialogs" ).toBool() )
328  QMessageBox::critical( this, tr( "New GeoPackage Layer" ), msg );
329  return false;
330  }
331  }
332  else
333  {
334  OGRSFDriverH hDriver = nullptr;
335  hDS.reset( OGROpen( fileName.toUtf8().constData(), true, &hDriver ) );
336  if ( !hDS )
337  {
338  QString msg( tr( "Opening of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
339  if ( !property( "hideDialogs" ).toBool() )
340  QMessageBox::critical( this, tr( "New GeoPackage Layer" ), msg );
341  return false;
342  }
343  if ( hDriver != hGpkgDriver )
344  {
345  QString msg( tr( "Opening of file succeeded, but this is not a GeoPackage database." ) );
346  if ( !property( "hideDialogs" ).toBool() )
347  QMessageBox::critical( this, tr( "New GeoPackage Layer" ), msg );
348  return false;
349  }
350  }
351 
352  QString tableName( mTableNameEdit->text() );
353 
354  bool overwriteTable = false;
355  if ( OGR_DS_GetLayerByName( hDS.get(), tableName.toUtf8().constData() ) )
356  {
357  if ( property( "hideDialogs" ).toBool() )
358  {
359  overwriteTable = property( "question_existing_layer_answer_overwrite" ).toBool();
360  }
361  else if ( QMessageBox::question( this, tr( "New GeoPackage Layer" ),
362  tr( "A table with the same name already exists. Do you want to overwrite it?" ),
363  QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
364  {
365  overwriteTable = true;
366  }
367 
368  if ( !overwriteTable )
369  {
370  return false;
371  }
372  }
373 
374  QString layerIdentifier( mLayerIdentifierEdit->text() );
375  QString layerDescription( mLayerDescriptionEdit->text() );
376 
377  OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType>
378  ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
379 
380  // z-coordinate & m-value.
381  if ( mGeometryWithZCheckBox->isChecked() )
382  wkbType = OGR_GT_SetZ( wkbType );
383 
384  if ( mGeometryWithMCheckBox->isChecked() )
385  wkbType = OGR_GT_SetM( wkbType );
386 
387  OGRSpatialReferenceH hSRS = nullptr;
388  // consider spatial reference system of the layer
389  QgsCoordinateReferenceSystem srs = mCrsSelector->crs();
390  if ( wkbType != wkbNone && srs.isValid() )
391  {
392  QString srsWkt = srs.toWkt();
393  hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
394  }
395 
396  // Set options
397  char **options = nullptr;
398 
399  if ( overwriteTable )
400  options = CSLSetNameValue( options, "OVERWRITE", "YES" );
401  if ( !layerIdentifier.isEmpty() )
402  options = CSLSetNameValue( options, "IDENTIFIER", layerIdentifier.toUtf8().constData() );
403  if ( !layerDescription.isEmpty() )
404  options = CSLSetNameValue( options, "DESCRIPTION", layerDescription.toUtf8().constData() );
405 
406  QString featureId( mFeatureIdColumnEdit->text() );
407  if ( !featureId.isEmpty() )
408  options = CSLSetNameValue( options, "FID", featureId.toUtf8().constData() );
409 
410  QString geometryColumn( mGeometryColumnEdit->text() );
411  if ( wkbType != wkbNone && !geometryColumn.isEmpty() )
412  options = CSLSetNameValue( options, "GEOMETRY_COLUMN", geometryColumn.toUtf8().constData() );
413 
414  if ( wkbType != wkbNone )
415  options = CSLSetNameValue( options, "SPATIAL_INDEX", mCheckBoxCreateSpatialIndex->isChecked() ? "YES" : "NO" );
416 
417  OGRLayerH hLayer = OGR_DS_CreateLayer( hDS.get(), tableName.toUtf8().constData(), hSRS, wkbType, options );
418  CSLDestroy( options );
419  if ( hSRS )
420  OSRRelease( hSRS );
421  if ( !hLayer )
422  {
423  QString msg( tr( "Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
424  if ( !property( "hideDialogs" ).toBool() )
425  QMessageBox::critical( this, tr( "New GeoPackage Layer" ), msg );
426  return false;
427  }
428 
429  QTreeWidgetItemIterator it( mAttributeView );
430  while ( *it )
431  {
432  QString fieldName( ( *it )->text( 0 ) );
433  QString fieldType( ( *it )->text( 1 ) );
434  QString fieldWidth( ( *it )->text( 2 ) );
435 
436  bool isBool = false;
437  OGRFieldType ogrType( OFTString );
438  if ( fieldType == QLatin1String( "text" ) )
439  ogrType = OFTString;
440  else if ( fieldType == QLatin1String( "integer" ) )
441  ogrType = OFTInteger;
442  else if ( fieldType == QLatin1String( "integer64" ) )
443  ogrType = OFTInteger64;
444  else if ( fieldType == QLatin1String( "real" ) )
445  ogrType = OFTReal;
446  else if ( fieldType == QLatin1String( "date" ) )
447  ogrType = OFTDate;
448  else if ( fieldType == QLatin1String( "datetime" ) )
449  ogrType = OFTDateTime;
450  else if ( fieldType == QLatin1String( "bool" ) )
451  {
452  ogrType = OFTInteger;
453  isBool = true;
454  }
455  else if ( fieldType == QLatin1String( "binary" ) )
456  ogrType = OFTBinary;
457 
458  int ogrWidth = fieldWidth.toInt();
459 
460  gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( fieldName.toUtf8().constData(), ogrType ) );
461  if ( ogrType != OFTBinary )
462  OGR_Fld_SetWidth( fld.get(), ogrWidth );
463  if ( isBool )
464  OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
465 
466  if ( OGR_L_CreateField( hLayer, fld.get(), true ) != OGRERR_NONE )
467  {
468  if ( !property( "hideDialogs" ).toBool() )
469  {
470  QMessageBox::critical( this, tr( "New GeoPackage Layer" ),
471  tr( "Creation of field %1 failed (OGR error: %2)" )
472  .arg( fieldName, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
473  }
474  return false;
475  }
476 
477  ++it;
478  }
479 
480  // In GDAL >= 2.0, the driver implements a deferred creation strategy, so
481  // issue a command that will force table creation
482  CPLErrorReset();
483  OGR_L_ResetReading( hLayer );
484  if ( CPLGetLastErrorType() != CE_None )
485  {
486  QString msg( tr( "Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
487  if ( !property( "hideDialogs" ).toBool() )
488  QMessageBox::critical( this, tr( "New GeoPackage Layer" ), msg );
489  return false;
490  }
491  hDS.reset();
492 
493  QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
494  QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
495  std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
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.
void fileChanged(const QString &)
emitted as soon as the current file or directory is changed
#define DEFAULT_OGR_FID_COLUMN_TITLE
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
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 setAddToProject(bool addToProject)
Sets whether a newly created layer should automatically be added to the current project.
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:430
This class represents a coordinate reference system (CRS).
QString toWkt() const
Returns a WKT representation of this CRS.
std::unique_ptr< std::remove_pointer< OGRFieldDefnH >::type, OGRFldDeleter > ogr_field_def_unique_ptr
Scoped OGR field definition.
Definition: qgsogrutils.h:124
Select multiple files.
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:104
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
void * OGRSpatialReferenceH
bool isValid() const
Returns whether this CRS is correctly initialized and usable.