Skip to content

Commit

Permalink
[browser] Expose action to set field comments for compatible
Browse files Browse the repository at this point in the history
database fields
  • Loading branch information
nyalldawson committed Apr 18, 2023
1 parent e9cc5fb commit a98fbed
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -1373,6 +1373,7 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
const QString fieldName = fieldItem->field().name();
const QString domainName = fieldItem->field().constraints().domainName();
const QString alias = fieldItem->field().alias();
const QString comment = fieldItem->field().comment();

// Check if it is supported
QgsProviderMetadata *md { QgsProviderRegistry::instance()->providerMetadata( providerKey ) };
Expand Down Expand Up @@ -1507,6 +1508,34 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
menu->addAction( setAliasAction );
}

if ( conn && conn->capabilities2().testFlag( Qgis::DatabaseProviderConnectionCapability2::SetFieldComment ) )
{
QAction *setCommentAction = new QAction( tr( "Set Comment…" ), menu );
const QString itemName { item->name() };

connect( setCommentAction, &QAction::triggered, fieldsItem, [ md, fieldsItem, itemName, comment, context ]
{
bool ok = false;

const QString newComment = QInputDialog::getText( QgisApp::instance(), tr( "Set Comment For %1" ).arg( itemName ), tr( "Comment" ), QLineEdit::Normal, comment, &ok );
if ( ok && comment != newComment )
{
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
try
{
conn2->setFieldComment( itemName, fieldsItem->schema(), fieldsItem->tableName(), newComment );
fieldsItem->refresh();
}
catch ( const QgsProviderConnectionException &ex )
{
notify( tr( "Set Field Comment" ), tr( "Failed to set comment for field '%1': %2" ).arg( itemName, ex.what() ), context, Qgis::MessageLevel::Critical );
}
}
} );

menu->addAction( setCommentAction );
}

if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::DeleteField ) )
{
QAction *deleteFieldAction = new QAction( tr( "Delete Field…" ), menu );
Expand Down

0 comments on commit a98fbed

Please sign in to comment.