17#include "moc_qgsdbimportvectorlayerdialog.cpp"
25#include <QItemSelectionModel>
29 , mConnection( connection )
32 setObjectName(
"QgsDbImportVectorLayerDialog" );
38 mExtentGroupBox->setOutputCrs(
crs );
41 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
42 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsDbImportVectorLayerDialog::doImport );
44 Q_ASSERT( connection );
46 mEditSchema->setReadOnly(
true );
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 )
67 mLabelSchemas =
nullptr;
69 mEditSchema =
nullptr;
72 if ( !supportsPrimaryKeyName )
74 delete mLabelPrimaryKey;
75 mLabelPrimaryKey =
nullptr;
76 delete mEditPrimaryKey;
77 mEditPrimaryKey =
nullptr;
81 if ( !supportsGeomColumnName )
83 delete mLabelGeometryColumn;
84 mLabelGeometryColumn =
nullptr;
85 delete mEditGeometryColumnName;
86 mEditGeometryColumnName =
nullptr;
90 if ( !supportsTableComments )
93 mLabelComment =
nullptr;
95 mEditComment =
nullptr;
98 mExtentGroupBox->setTitleBase( tr(
"Filter by Extent" ) );
99 mExtentGroupBox->setCheckable(
true );
100 mExtentGroupBox->setChecked(
false );
101 mExtentGroupBox->setCollapsed(
true );
103 mFilterExpressionWidget->registerExpressionContextGenerator(
this );
111 mEditSchema->setText(
schema );
116 mOwnedSource.reset();
117 mSourceLayer =
nullptr;
124 mOwnedSource.reset( vl );
125 mBlockSourceLayerChanges++;
126 mSourceLayerComboBox->setAdditionalLayers( { vl } );
127 mSourceLayerComboBox->setLayer( vl );
128 mBlockSourceLayerChanges--;
129 setSourceLayer( mOwnedSource.get() );
133 mBlockSourceLayerChanges++;
134 mSourceLayerComboBox->setLayer( vl );
135 mBlockSourceLayerChanges--;
136 setSourceLayer( vl );
140void QgsDbImportVectorLayerDialog::setSourceLayer(
QgsVectorLayer *layer )
142 mSourceLayer = layer;
143 if ( !mSourceLayer || !mSourceLayer->dataProvider() )
146 mEditTable->setText( layer->
name() );
148 const bool isSpatial = mSourceLayer->isSpatial();
149 if ( mEditGeometryColumnName )
150 mEditGeometryColumnName->setEnabled( isSpatial );
152 mCrsSelector->setEnabled( isSpatial );
154 mExtentGroupBox->setEnabled( isSpatial );
156 mExtentGroupBox->setChecked(
false );
158 const bool extentFilterEnabled = mExtentGroupBox->isChecked();
159 mExtentGroupBox->setOriginalExtent( mSourceLayer->extent(), mSourceLayer->crs() );
160 mExtentGroupBox->setOutputExtentFromOriginal();
161 mExtentGroupBox->setChecked( extentFilterEnabled );
162 mExtentGroupBox->setCollapsed( !extentFilterEnabled );
164 mFilterExpressionWidget->setLayer( mSourceLayer );
166 if ( mEditPrimaryKey )
170 const QgsAttributeList pkAttributes = mSourceLayer->dataProvider()->pkAttributeIndexes();
171 QString primaryKey = !pkAttributes.isEmpty() ? mSourceLayer->dataProvider()->fields().at( pkAttributes.at( 0 ) ).name() : QString();
172 if ( primaryKey.isEmpty() )
175 primaryKey = dsUri.keyColumn();
177 if ( primaryKey.isEmpty() )
179 primaryKey = mConnection->defaultPrimaryKeyColumnName();
182 mEditPrimaryKey->setText( primaryKey );
185 if ( mEditGeometryColumnName )
189 QString geomColumn = mSourceLayer->dataProvider()->geometryColumnName();
190 if ( geomColumn.isEmpty() )
193 geomColumn = dsUri.geometryColumn();
195 if ( geomColumn.isEmpty() )
197 geomColumn = mConnection->defaultGeometryColumnName();
200 mEditGeometryColumnName->setText( geomColumn );
205 mCrsSelector->setCrs( mSourceLayer->crs() );
210 mEditComment->setPlainText( mSourceLayer->metadata().abstract() );
213 mFieldsView->setSourceLayer( mSourceLayer );
214 mFieldsView->setSourceFields( mSourceLayer->fields() );
215 mFieldsView->setDestinationFields( mSourceLayer->fields() );
218void QgsDbImportVectorLayerDialog::loadFieldsFromLayer()
222 mFieldsView->setSourceFields( mSourceLayer->fields() );
223 mFieldsView->setDestinationFields( mSourceLayer->fields() );
227void QgsDbImportVectorLayerDialog::addField()
229 const int rowCount = mFieldsView->model()->rowCount();
230 mFieldsView->appendField(
QgsField( QStringLiteral(
"new_field" ) ) );
231 const QModelIndex index = mFieldsView->model()->index( rowCount, 0 );
232 mFieldsView->selectionModel()->select(
234 QItemSelectionModel::SelectionFlags(
235 QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Current | QItemSelectionModel::Rows
238 mFieldsView->scrollTo( index );
243 return mEditSchema ? mEditSchema->text() : QString();
248 return mEditTable->text();
253 return mEditComment ? mEditComment->toPlainText() : QString();
261 mExtentGroupBox->setMapCanvas( canvas,
false );
265void QgsDbImportVectorLayerDialog::doImport()
274 if ( !mSourceLayer || !mSourceLayer->dataProvider() )
277 QString destinationUri;
278 QVariantMap providerOptions;
281 exporterOptions.
layerName = mEditTable->text();
283 exporterOptions.
schema = mEditSchema->text();
284 exporterOptions.
wkbType = mSourceLayer->wkbType();
285 if ( mEditPrimaryKey && !mEditPrimaryKey->text().trimmed().isEmpty() )
287 if ( mEditGeometryColumnName )
292 destinationUri = mConnection->createVectorLayerExporterDestinationUri( exporterOptions, providerOptions );
300 QVariantMap allProviderOptions = extraProviderOptions;
301 for (
auto it = providerOptions.constBegin(); it != providerOptions.constEnd(); ++it )
303 allProviderOptions.insert( it.key(), it.value() );
307 if ( mChkDropTable->isChecked() )
309 allProviderOptions.insert( QStringLiteral(
"overwrite" ), true );
318 if ( !mFilterExpressionWidget->expression().isEmpty() )
324 if ( mExtentGroupBox->isEnabled() && mExtentGroupBox->isChecked() )
329 const QList<QgsFieldMappingModel::Field> fieldMapping = mFieldsView->mapping();
330 QList<QgsVectorLayerExporter::OutputField> outputFields;
331 outputFields.reserve( fieldMapping.size() );
338 return std::make_unique<QgsVectorLayerExporterTask>( mSourceLayer->clone(), destinationUri, mConnection->providerKey(), exportOptions, allProviderOptions,
true );
348void QgsDbImportVectorLayerDialog::sourceLayerComboChanged()
350 if ( mBlockSourceLayerChanges )
353 if ( mSourceLayerComboBox->currentLayer() == mSourceLayer )
356 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()
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
@ 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.
This class represents a coordinate reference system (CRS).
Class for storing the component parts of a RDBMS data source URI (e.g.
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.
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 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 data sets.
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.