Skip to content

Commit

Permalink
Nicer arrangement of actions in browser menu
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 28, 2021
1 parent 2563dd8 commit 4fe2e9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -614,7 +614,23 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
layerItem->mapLayerType() == QgsMapLayerType::RasterLayer ) )
{
QMenu *exportMenu = new QMenu( tr( "Export Layer" ), menu );
menu->addMenu( exportMenu );

// if there's a "Manage" menu, we want this to come just before it
QAction *beforeAction = nullptr;
QList<QAction *> actions = menu->actions();
for ( QAction *action : std::as_const( actions ) )
{
if ( action->text() == tr( "Manage" ) )
{
beforeAction = action;
break;
}
}
if ( beforeAction )
menu->insertMenu( beforeAction, exportMenu );
else
menu->addMenu( exportMenu );

QAction *toFileAction = new QAction( tr( "To File…" ), exportMenu );
exportMenu->addAction( toFileAction );
connect( toFileAction, &QAction::triggered, layerItem, [ layerItem ]
Expand Down Expand Up @@ -652,6 +668,9 @@ 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 );
Expand Down Expand Up @@ -688,6 +707,12 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
menu->addAction( propertiesAction );
}

int QgsLayerItemGuiProvider::precedenceWhenPopulatingMenus() const
{
// we want this provider to be called second last (last place is reserved for QgsAppFileItemGuiProvider)
return std::numeric_limits< int >::max() - 1;
}

bool QgsLayerItemGuiProvider::handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext )
{
if ( !item || item->type() != Qgis::BrowserItemType::Layer )
Expand Down
1 change: 1 addition & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.h
Expand Up @@ -114,6 +114,7 @@ class QgsLayerItemGuiProvider : public QObject, public QgsDataItemGuiProvider

void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override;
int precedenceWhenPopulatingMenus() const override;
bool handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext context ) override;

private:
Expand Down
6 changes: 5 additions & 1 deletion src/gui/providers/ogr/qgsogritemguiprovider.cpp
Expand Up @@ -45,6 +45,8 @@ void QgsOgrItemGuiProvider::populateContextMenu(
{
// item is a layer which sits inside a collection.

QMenu *manageLayerMenu = new QMenu( tr( "Manage" ), menu );

// test if GDAL supports deleting this layer
const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layerItem->providerKey(), layerItem->uri() );
const QString path = parts.value( QStringLiteral( "path" ) ).toString();
Expand All @@ -64,7 +66,9 @@ void QgsOgrItemGuiProvider::populateContextMenu(
actionDeleteLayer->setData( data );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ] { onDeleteLayer( context ); } );
actionDeleteLayer->setEnabled( canDeleteLayers );
menu->addAction( actionDeleteLayer );
manageLayerMenu->addAction( actionDeleteLayer );

menu->addMenu( manageLayerMenu );
}
}
}
Expand Down

0 comments on commit 4fe2e9d

Please sign in to comment.