17#include "moc_qgsdbimportvectorlayerdialog.cpp"
27#include <QItemSelectionModel>
31 , mConnection( connection )
34 setObjectName(
"QgsDbImportVectorLayerDialog" );
40 mExtentGroupBox->setOutputCrs(
crs );
43 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
44 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsDbImportVectorLayerDialog::doImport );
46 Q_ASSERT( connection );
48 mFieldsView->setDestinationEditable(
true );
51 mFieldsView->setNativeTypes( connection->
nativeTypes() );
55 QgsDebugError( QStringLiteral(
"Could not retrieve connection native types: %1" ).arg( e.
what() ) );
57 connect( mResetButton, &QPushButton::clicked,
this, &QgsDbImportVectorLayerDialog::loadFieldsFromLayer );
58 connect( mAddButton, &QPushButton::clicked,
this, &QgsDbImportVectorLayerDialog::addField );
64 if ( supportsSchemas )
66 std::unique_ptr<QgsAbstractDatabaseProviderConnection> schemeComboConn;
69 mLayoutSchemeCombo->addWidget( mSchemaCombo );
74 mLabelSchemas =
nullptr;
75 delete mLayoutSchemeCombo;
76 mLayoutSchemeCombo =
nullptr;
80 if ( !supportsPrimaryKeyName )
82 delete mLabelPrimaryKey;
83 mLabelPrimaryKey =
nullptr;
84 delete mEditPrimaryKey;
85 mEditPrimaryKey =
nullptr;
89 if ( !supportsGeomColumnName )
91 delete mLabelGeometryColumn;
92 mLabelGeometryColumn =
nullptr;
93 delete mEditGeometryColumnName;
94 mEditGeometryColumnName =
nullptr;
98 if ( !supportsTableComments )
100 delete mLabelComment;
101 mLabelComment =
nullptr;
103 mEditComment =
nullptr;
106 mExtentGroupBox->setTitleBase( tr(
"Filter by Extent" ) );
107 mExtentGroupBox->setCheckable(
true );
108 mExtentGroupBox->setChecked(
false );
109 mExtentGroupBox->setCollapsed(
true );
111 mFilterExpressionWidget->registerExpressionContextGenerator(
this );
114 sourceLayerComboChanged();
121 delete mSourceLayerComboBox;
122 mSourceLayerComboBox =
nullptr;
123 delete mFilterExpressionWidget;
124 mFilterExpressionWidget =
nullptr;
126 mFieldsView =
nullptr;
137 mOwnedSource.reset();
138 mSourceLayer =
nullptr;
145 mOwnedSource.reset( vl );
146 mBlockSourceLayerChanges++;
147 mSourceLayerComboBox->setAdditionalLayers( { vl } );
148 mSourceLayerComboBox->setLayer( vl );
149 mBlockSourceLayerChanges--;
150 setSourceLayer( mOwnedSource.get() );
154 mBlockSourceLayerChanges++;
155 mSourceLayerComboBox->setLayer( vl );
156 mBlockSourceLayerChanges--;
157 setSourceLayer( vl );
161void QgsDbImportVectorLayerDialog::setSourceLayer(
QgsVectorLayer *layer )
163 mSourceLayer = layer;
164 if ( !mSourceLayer || !mSourceLayer->dataProvider() )
167 mEditTable->setText( layer->
name() );
169 const bool isSpatial = mSourceLayer->isSpatial();
170 if ( mEditGeometryColumnName )
171 mEditGeometryColumnName->setEnabled( isSpatial );
173 mCrsSelector->setEnabled( isSpatial );
175 mExtentGroupBox->setEnabled( isSpatial );
177 mExtentGroupBox->setChecked(
false );
179 const bool extentFilterEnabled = mExtentGroupBox->isChecked();
180 mExtentGroupBox->setOriginalExtent( mSourceLayer->extent(), mSourceLayer->crs() );
181 mExtentGroupBox->setOutputExtentFromOriginal();
182 mExtentGroupBox->setChecked( extentFilterEnabled );
183 mExtentGroupBox->setCollapsed( !extentFilterEnabled );
185 mFilterExpressionWidget->setLayer( mSourceLayer );
187 if ( mEditPrimaryKey )
191 const QgsAttributeList pkAttributes = mSourceLayer->dataProvider()->pkAttributeIndexes();
192 QString primaryKey = !pkAttributes.isEmpty() ? mSourceLayer->dataProvider()->fields().at( pkAttributes.at( 0 ) ).name() : QString();
193 if ( primaryKey.isEmpty() )
196 primaryKey = dsUri.keyColumn();
198 if ( primaryKey.isEmpty() )
200 primaryKey = mConnection->defaultPrimaryKeyColumnName();
203 mEditPrimaryKey->setText( primaryKey );
206 if ( mEditGeometryColumnName )
210 QString geomColumn = mSourceLayer->dataProvider()->geometryColumnName();
211 if ( geomColumn.isEmpty() )
214 geomColumn = dsUri.geometryColumn();
216 if ( geomColumn.isEmpty() )
218 geomColumn = mConnection->defaultGeometryColumnName();
221 mEditGeometryColumnName->setText( geomColumn );
226 mCrsSelector->setCrs( mSourceLayer->crs() );
231 mEditComment->setPlainText( mSourceLayer->metadata().abstract() );
234 mFieldsView->setSourceLayer( mSourceLayer );
235 mFieldsView->setSourceFields( mSourceLayer->fields() );
236 mFieldsView->setDestinationFields( mSourceLayer->fields() );
238 const bool selectedFeatures = mSourceLayer->selectedFeatureCount() > 0;
239 mSourceLayerOnlySelected->setEnabled( selectedFeatures );
242void QgsDbImportVectorLayerDialog::loadFieldsFromLayer()
246 mFieldsView->setSourceFields( mSourceLayer->fields() );
247 mFieldsView->setDestinationFields( mSourceLayer->fields() );
251void QgsDbImportVectorLayerDialog::addField()
253 const int rowCount = mFieldsView->model()->rowCount();
254 mFieldsView->appendField(
QgsField( QStringLiteral(
"new_field" ) ), QStringLiteral(
"NULL" ) );
255 const QModelIndex index = mFieldsView->model()->index( rowCount, 0 );
256 mFieldsView->selectionModel()->select(
258 QItemSelectionModel::SelectionFlags(
259 QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Current | QItemSelectionModel::Rows
262 mFieldsView->scrollTo( index );
267 return mSchemaCombo ? mSchemaCombo->
currentSchema() : QString();
272 return mEditTable->text();
277 return mEditComment ? mEditComment->toPlainText() : QString();
285 mExtentGroupBox->setMapCanvas( canvas,
false );
289void QgsDbImportVectorLayerDialog::doImport()
298 if ( !mSourceLayer || !mSourceLayer->dataProvider() )
301 QString destinationUri;
302 QVariantMap providerOptions;
305 exporterOptions.
layerName = mEditTable->text();
308 exporterOptions.
wkbType = mSourceLayer->wkbType();
309 if ( mEditPrimaryKey && !mEditPrimaryKey->text().trimmed().isEmpty() )
311 if ( mEditGeometryColumnName )
316 destinationUri = mConnection->createVectorLayerExporterDestinationUri( exporterOptions, providerOptions );
324 QVariantMap allProviderOptions = extraProviderOptions;
325 for (
auto it = providerOptions.constBegin(); it != providerOptions.constEnd(); ++it )
327 allProviderOptions.insert( it.key(), it.value() );
331 if ( mChkDropTable->isChecked() )
333 allProviderOptions.insert( QStringLiteral(
"overwrite" ), true );
339 allProviderOptions.insert( QStringLiteral(
"skipConvertFields" ), true );
347 if ( !mFilterExpressionWidget->expression().isEmpty() )
353 if ( mExtentGroupBox->isEnabled() && mExtentGroupBox->isChecked() )
358 if ( mSourceLayerOnlySelected->isEnabled() && mSourceLayerOnlySelected->isChecked() )
363 const QList<QgsFieldMappingModel::Field> fieldMapping = mFieldsView->mapping();
364 QList<QgsVectorLayerExporter::OutputField> outputFields;
365 outputFields.reserve( fieldMapping.size() );
372 return std::make_unique<QgsVectorLayerExporterTask>( mSourceLayer->clone(), destinationUri, mConnection->providerKey(), exportOptions, allProviderOptions,
true );
382void QgsDbImportVectorLayerDialog::sourceLayerComboChanged()
384 if ( mBlockSourceLayerChanges )
387 if ( mSourceLayerComboBox->currentLayer() == mSourceLayer )
390 setSourceLayer( qobject_cast< QgsVectorLayer * >( mSourceLayerComboBox->currentLayer() ) );
@ SetPrimaryKeyName
Can set the name of the primary key column.
@ SetGeometryColumnName
Can set the name of the geometry column.
@ SetTableComment
Can set comments for tables via setTableComment()
Provides common functionality for database based connections.
@ Schemas
Can list schemas (if not set, the connection does not support schemas)
virtual QList< QgsVectorDataProvider::NativeType > nativeTypes() const =0
Returns a list of native types supported by the connection.
Represents a coordinate reference system (CRS).
Stores the component parts of a data source URI (e.g.
A combo box which displays the list of schemas for a specific database connection.
void setSchema(const QString &schema)
Sets the current schema selected in the combo box.
QString currentSchema() const
Returns the name of the current schema selected in the combo box.
void setDestinationSchema(const QString &schema)
Sets the destination schema for the new table.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
std::unique_ptr< QgsVectorLayerExporterTask > createExporterTask(const QVariantMap &extraProviderOptions=QVariantMap())
Creates a new exporter task to match the settings defined in the dialog.
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas to associate with the dialog.
~QgsDbImportVectorLayerDialog() override
void setSourceUri(const QgsMimeDataUtils::Uri &uri)
Sets the source table uri.
QString schema() const
Returns the destination schema.
QString tableName() const
Returns the destination table name.
QString tableComment() const
Returns the optional comment to use for the new table.
QgsDbImportVectorLayerDialog(QgsAbstractDatabaseProviderConnection *connection, QWidget *parent=nullptr)
Constructor for QgsDbImportVectorLayerDialog.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Encapsulate a field in an attribute table or 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...
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
Custom exception class for provider connection related exceptions.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
A QgsRectangle with associated coordinate reference system.
Encapsulates options for use with QgsVectorLayerExporter.
void setExtent(const QgsReferencedRectangle &extent)
Sets an extent filter for the features to export.
void setOutputFields(const QList< QgsVectorLayerExporter::OutputField > &fields)
Sets the output field definitions for the destination table.
void setSelectedOnly(bool selected)
Sets whether the export should only include selected features.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system to use for exported features.
void setFilterExpression(const QString &expression)
Set the filter expression for the features to export.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context to use when a filterExpression() is set.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context to use when transforming exported features.
Represents a vector layer which manages a vector based dataset.
QList< int > QgsAttributeList
#define QgsDebugError(str)
const QgsCoordinateReferenceSystem & crs
Stores all information required to create a QgsVectorLayerExporter for the backend.
QStringList primaryKeyColumns
List of primary key column names. Note that some providers may ignore this if not supported.
QString schema
Optional schema for the new layer. May not be supported by all providers.
QString geometryColumn
Preferred name for the geometry column, if required. Note that some providers may ignore this if a sp...
QString layerName
Name for the new layer.
Qgis::WkbType wkbType
WKB type for destination layer geometry.
The Field struct holds information about a mapped field.
QgsVectorLayer * vectorLayer(bool &owner, QString &error) const
Gets vector layer from uri if possible, otherwise returns nullptr and error is set.
Encapsulates output field definition.