Skip to content

Commit 0e9428b

Browse files
authoredApr 15, 2019
Merge pull request #9787 from elpaso/request-21787-identify-links
Clickable links in identify results from GetFeatureInfo
2 parents 04409ac + 2895a64 commit 0e9428b

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed
 

‎src/app/qgsidentifyresultsdialog.cpp

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -806,13 +806,13 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
806806
// Add all supported formats, best first. HTML is considered the best because
807807
// it usually holds most information.
808808
int capabilities = layer->dataProvider()->capabilities();
809-
QList<QgsRaster::IdentifyFormat> formats;
810-
formats << QgsRaster::IdentifyFormatHtml
811-
<< QgsRaster::IdentifyFormatFeature
812-
<< QgsRaster::IdentifyFormatText
813-
<< QgsRaster::IdentifyFormatValue;
814-
const auto constFormats = formats;
815-
for ( QgsRaster::IdentifyFormat f : constFormats )
809+
static const QList<QgsRaster::IdentifyFormat> formats
810+
{
811+
QgsRaster::IdentifyFormatHtml,
812+
QgsRaster::IdentifyFormatFeature,
813+
QgsRaster::IdentifyFormatText,
814+
QgsRaster::IdentifyFormatValue };
815+
for ( const auto &f : formats )
816816
{
817817
if ( !( QgsRasterDataProvider::identifyFormatToCapability( f ) & capabilities ) )
818818
continue;
@@ -829,8 +829,8 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
829829
QTreeWidgetItem *formatItem = new QTreeWidgetItem( QStringList() << ' ' + tr( "Format" ) );
830830
layItem->addChild( formatItem );
831831
lstResults->setItemWidget( formatItem, 1, formatCombo );
832-
connect( formatCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
833-
this, static_cast<void ( QgsIdentifyResultsDialog::* )( int )>( &QgsIdentifyResultsDialog::formatChanged ) );
832+
connect( formatCombo, qgis::overload<int>::of( &QComboBox::currentIndexChanged ),
833+
this, qgis::overload<int>::of( &QgsIdentifyResultsDialog::formatChanged ) );
834834
}
835835
else
836836
{
@@ -856,23 +856,46 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
856856
if ( i >= fields.count() )
857857
continue;
858858

859-
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << attrs.at( i ).toString() );
860-
859+
const auto value { attrs.at( i ).toString() };
860+
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << value );
861861
attrItem->setData( 0, Qt::DisplayRole, fields.at( i ).name() );
862-
863-
QVariant value = attrs.at( i );
864-
attrItem->setData( 1, Qt::DisplayRole, value );
862+
attrItem->setData( 1, Qt::DisplayRole, attrs.at( i ) );
865863
featItem->addChild( attrItem );
864+
bool foundLinks = false;
865+
const auto links { QgsStringUtils::insertLinks( value, &foundLinks ) };
866+
if ( foundLinks )
867+
{
868+
auto valueLabel { qgis::make_unique<QLabel>( links ) };
869+
attrItem->setText( 1, QString( ) );
870+
valueLabel->setOpenExternalLinks( true );
871+
lstResults->setItemWidget( attrItem, 1, valueLabel.release() );
872+
}
866873
}
867874
}
868875

869876
if ( currentFormat == QgsRaster::IdentifyFormatHtml || currentFormat == QgsRaster::IdentifyFormatText )
870877
{
871878
QgsIdentifyResultsWebViewItem *attrItem = new QgsIdentifyResultsWebViewItem( lstResults );
879+
attrItem->webView()->page()->setLinkDelegationPolicy( QWebPage::DelegateExternalLinks );
880+
const int horizontalDpi = qApp->desktop()->screen()->logicalDpiX();
881+
// Adjust zoom: text is ok, but HTML seems rather big at least on Linux/KDE
882+
if ( horizontalDpi > 96 )
883+
{
884+
attrItem->webView()->setZoomFactor( attrItem->webView()->zoomFactor() * ( currentFormat == QgsRaster::IdentifyFormatHtml ? 0.7 : 0.9 ) );
885+
}
886+
connect( attrItem->webView(), &QWebView::linkClicked, [ ]( const QUrl & url )
887+
{
888+
QDesktopServices::openUrl( url );
889+
} );
872890
featItem->addChild( attrItem ); // before setHtml()!
873891
if ( !attributes.isEmpty() )
874892
{
875-
attrItem->setContent( attributes.begin().value().toUtf8(), currentFormat == QgsRaster::IdentifyFormatHtml ? "text/html" : "text/plain; charset=utf-8" );
893+
auto value { QgsStringUtils::insertLinks( attributes.begin().value() ) };
894+
if ( currentFormat == QgsRaster::IdentifyFormatText )
895+
{
896+
value.prepend( QStringLiteral( "<pre style=\"font-family: monospace;\">" ) ).append( QStringLiteral( "</pre>" ) );
897+
}
898+
attrItem->setHtml( value );
876899
}
877900
else
878901
{
@@ -914,7 +937,20 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
914937
tblResults->setItem( j, 0, item );
915938
tblResults->setItem( j, 1, new QTableWidgetItem( QString::number( i + 1 ) ) );
916939
tblResults->setItem( j, 2, new QTableWidgetItem( it.key() ) );
917-
tblResults->setItem( j, 3, new QTableWidgetItem( it.value() ) );
940+
941+
bool foundLinks = false;
942+
QString links = QgsStringUtils::insertLinks( it.value(), &foundLinks );
943+
if ( foundLinks )
944+
{
945+
auto valueLabel { qgis::make_unique<QLabel>( links ) };
946+
valueLabel->setOpenExternalLinks( true );
947+
tblResults->setItem( j, 3, item );
948+
tblResults->setCellWidget( j, 3, valueLabel.release() );
949+
}
950+
else
951+
{
952+
tblResults->setItem( j, 3, new QTableWidgetItem( it.value() ) );
953+
}
918954

919955
tblResults->resizeRowToContents( j );
920956

‎src/core/qgswebview.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class CORE_EXPORT QgsWebView : public QWebView
4141
{
4242
QDesktopWidget desktop;
4343
// Apply zoom factor for HiDPI screens
44-
if ( desktop.physicalDpiX() > 96 )
44+
if ( desktop.logicalDpiX() > 96 )
4545
{
46-
setZoomFactor( desktop.physicalDpiX() / 96 );
46+
setZoomFactor( desktop.logicalDpiX() / 96 );
4747
}
4848
}
4949
};

0 commit comments

Comments
 (0)
Please sign in to comment.