Skip to content

Commit

Permalink
Open Externally action only applies to file items
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 28, 2021
1 parent 4fe2e9d commit 75b7bbe
Showing 1 changed file with 52 additions and 57 deletions.
109 changes: 52 additions & 57 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -431,6 +431,58 @@ void QgsAppFileItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *m
if ( !( item->capabilities2() & Qgis::BrowserItemCapability::ItemRepresentsFile ) )
return;

// Check for certain file items
const QString filename = item->path();
if ( !filename.isEmpty() )
{
QFileInfo fi( filename );

const static QList< std::pair< QString, QString > > sStandardFileTypes =
{
{ QStringLiteral( "pdf" ), QObject::tr( "Document" )},
{ QStringLiteral( "xls" ), QObject::tr( "Spreadsheet" )},
{ QStringLiteral( "xlsx" ), QObject::tr( "Spreadsheet" )},
{ QStringLiteral( "ods" ), QObject::tr( "Spreadsheet" )},
{ QStringLiteral( "csv" ), QObject::tr( "CSV File" )},
{ QStringLiteral( "txt" ), QObject::tr( "Text File" )},
{ QStringLiteral( "png" ), QObject::tr( "PNG Image" )},
{ QStringLiteral( "jpg" ), QObject::tr( "JPEG Image" )},
{ QStringLiteral( "jpeg" ), QObject::tr( "JPEG Image" )},
{ QStringLiteral( "tif" ), QObject::tr( "TIFF Image" )},
{ QStringLiteral( "tiff" ), QObject::tr( "TIFF Image" )},
{ QStringLiteral( "svg" ), QObject::tr( "SVG File" )}
};
for ( const auto &it : sStandardFileTypes )
{
const QString ext = it.first;
const QString name = it.second;
if ( fi.suffix().compare( ext, Qt::CaseInsensitive ) == 0 )
{
// pdf file
QAction *viewAction = new QAction( tr( "Open %1 Externally…" ).arg( name ), menu );
connect( viewAction, &QAction::triggered, this, [ = ]
{
QDesktopServices::openUrl( QUrl::fromLocalFile( filename ) );
} );

// we want this action to be at the top
QAction *beforeAction = menu->actions().value( 0 );
if ( beforeAction )
{
menu->insertAction( beforeAction, viewAction );
menu->insertSeparator( beforeAction );
}
else
{
menu->addAction( viewAction );
menu->addSeparator();
}
// will only find one!
break;
}
}
}

if ( qobject_cast< QgsDataCollectionItem * >( item ) )
{
QAction *actionRefresh = new QAction( QObject::tr( "Refresh" ), menu );
Expand Down Expand Up @@ -553,63 +605,6 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
return;

QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( item );

if ( layerItem )
{
// Check for certain file items
QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layerItem->providerKey(), layerItem->uri() );
const QString filename = parts.value( QStringLiteral( "path" ) ).toString();
if ( !filename.isEmpty() )
{
QFileInfo fi( filename );

const static QList< std::pair< QString, QString > > sStandardFileTypes =
{
{ QStringLiteral( "pdf" ), QObject::tr( "Document" )},
{ QStringLiteral( "xls" ), QObject::tr( "Spreadsheet" )},
{ QStringLiteral( "xlsx" ), QObject::tr( "Spreadsheet" )},
{ QStringLiteral( "ods" ), QObject::tr( "Spreadsheet" )},
{ QStringLiteral( "csv" ), QObject::tr( "CSV File" )},
{ QStringLiteral( "txt" ), QObject::tr( "Text File" )},
{ QStringLiteral( "png" ), QObject::tr( "PNG Image" )},
{ QStringLiteral( "jpg" ), QObject::tr( "JPEG Image" )},
{ QStringLiteral( "jpeg" ), QObject::tr( "JPEG Image" )},
{ QStringLiteral( "tif" ), QObject::tr( "TIFF Image" )},
{ QStringLiteral( "tiff" ), QObject::tr( "TIFF Image" )},
{ QStringLiteral( "svg" ), QObject::tr( "SVG File" )}
};
for ( const auto &it : sStandardFileTypes )
{
const QString ext = it.first;
const QString name = it.second;
if ( fi.suffix().compare( ext, Qt::CaseInsensitive ) == 0 )
{
// pdf file
QAction *viewAction = new QAction( tr( "Open %1 Externally…" ).arg( name ), menu );
connect( viewAction, &QAction::triggered, this, [ = ]
{
QDesktopServices::openUrl( QUrl::fromLocalFile( filename ) );
} );

// we want this action to be at the top
QAction *beforeAction = menu->actions().value( 0 );
if ( beforeAction )
{
menu->insertAction( beforeAction, viewAction );
menu->insertSeparator( beforeAction );
}
else
{
menu->addAction( viewAction );
menu->addSeparator();
}
// will only find one!
break;
}
}
}
}

if ( layerItem && ( layerItem->mapLayerType() == QgsMapLayerType::VectorLayer ||
layerItem->mapLayerType() == QgsMapLayerType::RasterLayer ) )
{
Expand Down

0 comments on commit 75b7bbe

Please sign in to comment.