Skip to content
Permalink
Browse files

Capture copies in lambda

  • Loading branch information
elpaso committed Jul 12, 2020
1 parent 82f36c0 commit 93d9700708b3e1abfc0d24699ba46ef01ca8dfdd
@@ -235,6 +235,7 @@ This information is calculated from the geometry columns types.
SpatialIndexExists,
DeleteSpatialIndex,
DeleteField,
DeleteFieldCascade,
AddField,
};

@@ -374,6 +375,8 @@ Raises a QgsProviderConnectionException if any errors are encountered.
it is responsibility of the caller to handle open layers and registry entries.

:raises :: py:class:`QgsProviderConnectionException`

.. versionadded:: 3.16
%End

virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const throw( QgsProviderConnectionException );
@@ -390,6 +393,8 @@ Raises a QgsProviderConnectionException if any errors are encountered.
it is responsibility of the caller to handle open layers and registry entries.

:raises :: py:class:`QgsProviderConnectionException`

.. versionadded:: 3.16
%End


@@ -715,36 +715,42 @@ void QgsFieldsItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *me
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::AddField ) )
{
QAction *addColumnAction = new QAction( tr( "Add New Field…" ), menu );
connect( addColumnAction, &QAction::triggered, this, [ = ]
const QString itemName { item->name() };
const QString tableName { fieldsItem->name() };
const QString schema { fieldsItem->schema() };
QgsVectorLayer *itemLayer { fieldsItem->layer( ) };
const QString connectionUri { fieldsItem->connectionUri() };

connect( addColumnAction, &QAction::triggered, this, [ md, connectionUri, itemLayer, schema, tableName, context, menu ]
{
std::unique_ptr<QgsVectorLayer> layer { fieldsItem->layer( ) };
std::unique_ptr<QgsVectorLayer> layer { itemLayer };
if ( layer )
{
QgsAddAttrDialog dialog( layer.get(), menu );
if ( dialog.exec() == QDialog::Accepted )
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) };
try
{
conn2->addField( dialog.field(), fieldsItem->schema(), fieldsItem->tableName() );
item->refresh();
conn2->addField( dialog.field(), schema, tableName );
// FIXME!!! item->refresh();
}
catch ( const QgsProviderConnectionException &ex )
{
if ( context.messageBar() )
{
context.messageBar()->pushCritical( tr( "New Field" ), tr( "Failed to add the new field to '%1': %2" ).arg( fieldsItem->tableName(), ex.what() ) );
context.messageBar()->pushCritical( tr( "New Field" ), tr( "Failed to add the new field to '%1': %2" ).arg( tableName, ex.what() ) );
}
else
{
QMessageBox::critical( menu, tr( "New Field" ), tr( "Failed to a add new field to '%1': %2" ).arg( fieldsItem->tableName(), ex.what() ) );
QMessageBox::critical( menu, tr( "New Field" ), tr( "Failed to a add new field to '%1': %2" ).arg( tableName, ex.what() ) );
}
}
}
}
else
{
const QString message { tr( "Failed to create layer '%1'. Check application logs and user permissions." ).arg( fieldsItem->tableName() ) };
const QString message { tr( "Failed to create layer '%1'. Check application logs and user permissions." ).arg( tableName ) };
if ( context.messageBar() )
{
context.messageBar()->pushCritical( tr( "Add Field" ), message );
@@ -787,25 +793,42 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::DeleteField ) )
{
QAction *deleteFieldAction = new QAction( tr( "Delete Field…" ), menu );
connect( deleteFieldAction, &QAction::triggered, this, [ = ]
const bool supportsCascade { conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::DeleteFieldCascade ) };
const QString itemName { item->name() };
const QString tableName { fieldsItem->name() };
const QString schema { fieldsItem->schema() };
const QString connectionUri { fieldsItem->connectionUri() };

connect( deleteFieldAction, &QAction::triggered, this, [ md, itemName, connectionUri, tableName, schema, context, supportsCascade, menu ]
{
if ( QMessageBox::warning( menu, tr( "Delete Field" ), tr( "Delete '%1' permanently (with CASCADE)?" ).arg( item->name() ), QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Ok )
// Confirmation dialog
QMessageBox msgbox{QMessageBox::Icon::Question, tr( "Delete Field" ), tr( "Delete '%1' permanently?" ).arg( itemName ), QMessageBox::Ok | QMessageBox::Cancel };
QCheckBox *cb = new QCheckBox( tr( "Delete all related objects (CASCADE)?" ) );
msgbox.setCheckBox( cb );
msgbox.setDefaultButton( QMessageBox::Cancel );

if ( ! supportsCascade )
{
cb->hide();
}

if ( msgbox.exec() == QMessageBox::Ok )
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) };
try
{
conn2->deleteField( item->name(), fieldsItem->schema(), fieldsItem->tableName(), true );
fieldsItem->refresh();
conn2->deleteField( itemName, schema, tableName, supportsCascade && cb->isChecked() );
// FIXME!!! fieldsItem->refresh();
}
catch ( const QgsProviderConnectionException &ex )
{
if ( context.messageBar() )
{
context.messageBar()->pushCritical( tr( "Delete Field" ), tr( "Failed to delete field '%1': %2" ).arg( item->name(), ex.what() ) );
context.messageBar()->pushCritical( tr( "Delete Field" ), tr( "Failed to delete field '%1': %2" ).arg( itemName, ex.what() ) );
}
else
{
QMessageBox::critical( menu, tr( "Delete Field" ), tr( "Failed to delete field '%1': %2" ).arg( item->name(), ex.what() ) );
QMessageBox::critical( menu, tr( "Delete Field" ), tr( "Failed to delete field '%1': %2" ).arg( itemName, ex.what() ) );
}
}
}
@@ -57,7 +57,7 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem *parent,
mIsSubLayer = isSubLayer;
mToolTip = uri;
const bool isIndex { QRegularExpression( R"(=idx_[^_]+_[^_]+.*$)" ).match( uri ).hasMatch() };
setState( ( driverName == QStringLiteral( "SQLite" ) && ! isIndex ) ? NotPopulated : Populated ); // children are not expected except for sqlite
setState( ( driverName == QStringLiteral( "SQLite" ) && ! isIndex ) ? NotPopulated : Populated ); // children are accepted except for sqlite
}


@@ -294,7 +294,8 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
SpatialIndexExists = 1 << 17, //!< The connection can determine if a spatial index exists
DeleteSpatialIndex = 1 << 18, //!< The connection can delete spatial indices for tables
DeleteField = 1 << 19, //!< Can delete an existing field/column
AddField = 1 << 20, //!< Can add a new field/column
DeleteFieldCascade = 1 << 20, //!< Can delete an existing field/column with cascade
AddField = 1 << 21, //!< Can add a new field/column
};

Q_ENUM( Capability )
@@ -403,6 +404,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
* \param force if TRUE, a DROP CASCADE will drop all related objects
* \note it is responsibility of the caller to handle open layers and registry entries.
* \throws QgsProviderConnectionException
* \since QGIS 3.16
*/
virtual void deleteField( const QString &fieldName, const QString &schema, const QString &tableName, bool force = false ) const SIP_THROW( QgsProviderConnectionException );

@@ -414,6 +416,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
* \param tableName name of the table
* \note it is responsibility of the caller to handle open layers and registry entries.
* \throws QgsProviderConnectionException
* \since QGIS 3.16
*/
virtual void addField( const QgsField &field, const QString &schema, const QString &tableName ) const SIP_THROW( QgsProviderConnectionException );

@@ -85,6 +85,7 @@ void QgsMssqlProviderConnection::setDefaultCapabilities()
Capability::Spatial,
Capability::TableExists,
Capability::DeleteField,
Capability::DeleteFieldCascade,
Capability::AddField
};
}
@@ -69,6 +69,7 @@ void QgsPostgresProviderConnection::setDefaultCapabilities()
Capability::SpatialIndexExists,
Capability::DeleteSpatialIndex,
Capability::DeleteField,
Capability::DeleteFieldCascade,
Capability::AddField
};
}

0 comments on commit 93d9700

Please sign in to comment.
You can’t perform that action at this time.