Skip to content

Commit a98fbed

Browse files
committedApr 18, 2023
[browser] Expose action to set field comments for compatible
database fields
1 parent e9cc5fb commit a98fbed

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎src/app/browser/qgsinbuiltdataitemproviders.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
13731373
const QString fieldName = fieldItem->field().name();
13741374
const QString domainName = fieldItem->field().constraints().domainName();
13751375
const QString alias = fieldItem->field().alias();
1376+
const QString comment = fieldItem->field().comment();
13761377

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

1511+
if ( conn && conn->capabilities2().testFlag( Qgis::DatabaseProviderConnectionCapability2::SetFieldComment ) )
1512+
{
1513+
QAction *setCommentAction = new QAction( tr( "Set Comment…" ), menu );
1514+
const QString itemName { item->name() };
1515+
1516+
connect( setCommentAction, &QAction::triggered, fieldsItem, [ md, fieldsItem, itemName, comment, context ]
1517+
{
1518+
bool ok = false;
1519+
1520+
const QString newComment = QInputDialog::getText( QgisApp::instance(), tr( "Set Comment For %1" ).arg( itemName ), tr( "Comment" ), QLineEdit::Normal, comment, &ok );
1521+
if ( ok && comment != newComment )
1522+
{
1523+
std::unique_ptr<QgsAbstractDatabaseProviderConnection> conn2 { static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( fieldsItem->connectionUri(), {} ) ) };
1524+
try
1525+
{
1526+
conn2->setFieldComment( itemName, fieldsItem->schema(), fieldsItem->tableName(), newComment );
1527+
fieldsItem->refresh();
1528+
}
1529+
catch ( const QgsProviderConnectionException &ex )
1530+
{
1531+
notify( tr( "Set Field Comment" ), tr( "Failed to set comment for field '%1': %2" ).arg( itemName, ex.what() ), context, Qgis::MessageLevel::Critical );
1532+
}
1533+
}
1534+
} );
1535+
1536+
menu->addAction( setCommentAction );
1537+
}
1538+
15101539
if ( conn && conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::DeleteField ) )
15111540
{
15121541
QAction *deleteFieldAction = new QAction( tr( "Delete Field…" ), menu );

0 commit comments

Comments
 (0)
Please sign in to comment.