mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-18 00:03:05 -04:00
[FEATURE] Make links in feature identify results clickable
This commit is contained in:
parent
f34c79c04b
commit
0075a4bb16
@ -53,6 +53,7 @@
|
||||
#include <QDesktopServices>
|
||||
#include <QMessageBox>
|
||||
#include <QComboBox>
|
||||
#include <QTextDocument>
|
||||
|
||||
//graph
|
||||
#include <qwt_plot.h>
|
||||
@ -61,6 +62,27 @@
|
||||
#include <qwt_legend.h>
|
||||
#include "qgsvectorcolorrampv2.h" // for random colors
|
||||
|
||||
static QString& insertLinkAnchors( QString& value )
|
||||
{
|
||||
// http://alanstorm.com/url_regex_explained
|
||||
static QRegExp urlRegEx( "(\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^!\"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~\\s]|/))))" );
|
||||
static QRegExp protoRegEx( "^(?:f|ht)tps?://" );
|
||||
|
||||
int offset = 0;
|
||||
while ( urlRegEx.indexIn( value, offset ) != -1 )
|
||||
{
|
||||
QString url = urlRegEx.cap( 1 );
|
||||
QString protoUrl = url;
|
||||
if ( protoRegEx.indexIn( protoUrl ) == -1 )
|
||||
{
|
||||
protoUrl.prepend( "http://" );
|
||||
}
|
||||
QString anchor = QString( "<a href=\"%1\">%2</a>" ).arg( Qt::escape( protoUrl ) ).arg( Qt::escape( url ) );
|
||||
value.replace( urlRegEx.pos( 1 ), url.length(), anchor );
|
||||
offset = urlRegEx.pos( 1 ) + anchor.length();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWebView( parent )
|
||||
{
|
||||
@ -471,12 +493,16 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
|
||||
if ( i >= fields.count() )
|
||||
continue;
|
||||
|
||||
if ( vlayer->editorWidgetV2( i ) == "Hidden" )
|
||||
continue;
|
||||
|
||||
QString defVal;
|
||||
if ( fields.fieldOrigin( i ) == QgsFields::OriginProvider && vlayer->dataProvider() )
|
||||
defVal = vlayer->dataProvider()->defaultValue( fields.fieldOriginIndex( i ) ).toString();
|
||||
|
||||
QString value = defVal == attrs.at( i ) ? defVal : fields.at( i ).displayString( attrs.at( i ) );
|
||||
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << value );
|
||||
featItem->addChild( attrItem );
|
||||
|
||||
attrItem->setData( 0, Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
|
||||
attrItem->setData( 0, Qt::UserRole, fields[i].name() );
|
||||
@ -490,9 +516,12 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
|
||||
continue;
|
||||
}
|
||||
|
||||
value = representValue( vlayer, fields[i].name(), attrs.at( i ) );
|
||||
value = representValue( vlayer, fields[i].name(), attrs[i] );
|
||||
QLabel* valueLabel = new QLabel( insertLinkAnchors( value ) );
|
||||
valueLabel->setOpenExternalLinks( true );
|
||||
attrItem->treeWidget()->setItemWidget( attrItem, 1, valueLabel );
|
||||
|
||||
attrItem->setData( 1, Qt::DisplayRole, value );
|
||||
attrItem->setData( 1, Qt::DisplayRole, QString() );
|
||||
|
||||
if ( fields[i].name() == vlayer->displayField() )
|
||||
{
|
||||
@ -500,8 +529,6 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
|
||||
featItem->setText( 1, attrItem->text( 1 ) );
|
||||
featureLabeled = true;
|
||||
}
|
||||
|
||||
featItem->addChild( attrItem );
|
||||
}
|
||||
|
||||
if ( !featureLabeled )
|
||||
@ -1480,7 +1507,9 @@ void QgsIdentifyResultsDialog::attributeValueChanged( QgsFeatureId fid, int idx,
|
||||
if ( item->data( 0, Qt::UserRole + 1 ).toInt() == idx )
|
||||
{
|
||||
value = representValue( vlayer, fld.name(), val );
|
||||
item->setData( 1, Qt::DisplayRole, value );
|
||||
QLabel* valueLabel = new QLabel( insertLinkAnchors( value ) );
|
||||
valueLabel->setOpenExternalLinks( true );
|
||||
item->treeWidget()->setItemWidget( item, 1, valueLabel );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user