Skip to content
Permalink
Browse files

[FEATURE] Make links in feature identify results clickable

  • Loading branch information
manisandro authored and nyalldawson committed Jul 12, 2016
1 parent f34c79c commit 0075a4bb1664463aac67e92f5558ad804af0156c
Showing with 34 additions and 5 deletions.
  1. +34 −5 src/app/qgsidentifyresultsdialog.cpp
@@ -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,18 +516,19 @@ 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() )
{
featItem->setText( 0, attrItem->text( 0 ) );
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;
}
}

0 comments on commit 0075a4b

Please sign in to comment.
You can’t perform that action at this time.