Skip to content

Commit

Permalink
[FEATURE] Show "Open Document..." action when right clicking certain
Browse files Browse the repository at this point in the history
files in the browser, allowing them to be opened with the default
application for that file type

E.g. PDFs will open with the default PDF viewer.

Works with PDF, ODS, XLS(X), CSV, TXT, PNG, JPEG, TIFF, SVG
(other types will likely need more work, since they aren't
currently shown in the browser)
  • Loading branch information
nyalldawson committed Dec 4, 2019
1 parent 66456a3 commit 2774df2
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -36,6 +36,7 @@
#include "qgsapplication.h"
#include "processing/qgsprojectstylealgorithms.h"
#include "qgsstylemanagerdialog.h"
#include "qgsproviderregistry.h"

#include <QFileInfo>
#include <QMenu>
Expand Down Expand Up @@ -355,6 +356,63 @@ 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…" ).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 2774df2

Please sign in to comment.