Skip to content

Commit

Permalink
More improvements to browser menu right click menu structure
Browse files Browse the repository at this point in the history
Move Delete Layer actions into a "Manage" submenu, so that there's
a bit more safety around these. And make sure the Manage submenu
always appears in the same place, regardless of datasource.
  • Loading branch information
nyalldawson committed Jul 28, 2021
1 parent 75b7bbe commit 870a4aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
46 changes: 32 additions & 14 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -663,18 +663,6 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
} );
}

if ( !menu->isEmpty() )
menu->addSeparator();

const QString addText = selectedItems.count() == 1 ? tr( "Add Layer to Project" )
: tr( "Add Selected Layers to Project" );
QAction *addAction = new QAction( addText, menu );
connect( addAction, &QAction::triggered, this, [ = ]
{
addLayersFromItems( selectedItems );
} );
menu->addAction( addAction );

if ( item->capabilities2() & Qgis::BrowserItemCapability::Delete )
{
QStringList selectedDeletableItemPaths;
Expand All @@ -684,16 +672,46 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
selectedDeletableItemPaths.append( qobject_cast<QgsLayerItem *>( selectedItem )->uri() );
}

const QString deleteText = selectedDeletableItemPaths.count() == 1 ? tr( "Delete Layer" )
const QString deleteText = selectedDeletableItemPaths.count() == 1 ? tr( "Delete Layer “%1”…" ).arg( layerItem->name() )
: tr( "Delete Selected Layers…" );
QAction *deleteAction = new QAction( deleteText, menu );
connect( deleteAction, &QAction::triggered, this, [ = ]
{
deleteLayers( selectedDeletableItemPaths, context );
} );
menu->addAction( deleteAction );

// this action should sit in the Manage menu. If one does not exist, create it now
bool foundExistingManageMenu = false;
QList<QAction *> actions = menu->actions();
for ( QAction *action : std::as_const( actions ) )
{
if ( action->text() == tr( "Manage" ) )
{
action->menu()->addAction( deleteAction );
foundExistingManageMenu = true;
break;
}
}
if ( !foundExistingManageMenu )
{
QMenu *manageLayerMenu = new QMenu( tr( "Manage" ), menu );
manageLayerMenu->addAction( deleteAction );
menu->addMenu( manageLayerMenu );
}
}

if ( !menu->isEmpty() )
menu->addSeparator();

const QString addText = selectedItems.count() == 1 ? tr( "Add Layer to Project" )
: tr( "Add Selected Layers to Project" );
QAction *addAction = new QAction( addText, menu );
connect( addAction, &QAction::triggered, this, [ = ]
{
addLayersFromItems( selectedItems );
} );
menu->addAction( addAction );

QAction *propertiesAction = new QAction( tr( "Layer Properties…" ), menu );
connect( propertiesAction, &QAction::triggered, this, [ = ]
{
Expand Down
9 changes: 6 additions & 3 deletions src/gui/providers/ogr/qgsgeopackageitemguiprovider.cpp
Expand Up @@ -50,8 +50,9 @@ void QgsGeoPackageItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu
// Check capabilities
if ( layerItem->capabilities2() & Qgis::BrowserItemCapability::Rename )
{
QAction *actionRenameLayer = new QAction( tr( "Rename Layer '%1'…" ).arg( layerItem->name() ), menu );
QVariantMap data;
QMenu *manageLayerMenu = new QMenu( tr( "Manage" ), menu );

QAction *actionRenameLayer = new QAction( tr( "Rename Layer “%1”…" ).arg( layerItem->name() ), menu );
const QString uri = layerItem->uri();
const QString providerKey = layerItem->providerKey();
const QStringList tableNames = layerItem->tableNames();
Expand All @@ -60,7 +61,9 @@ void QgsGeoPackageItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu
{
renameVectorLayer( uri, providerKey, tableNames, itemPointer, context );
} );
menu->addAction( actionRenameLayer );
manageLayerMenu->addAction( actionRenameLayer );

menu->addMenu( manageLayerMenu );
}
}

Expand Down

0 comments on commit 870a4aa

Please sign in to comment.