Skip to content

Commit

Permalink
Support CTRL+Click for opening links in attribute table
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro committed Jul 30, 2021
1 parent 1bd8f5c commit 636042c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -698,17 +698,37 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
}

QVariant val = mFeat.attribute( fieldId );
bool isLink = false;
if ( val.type() == QVariant::String )
{
QString textVal = val.toString();
if ( QRegExp( "^(http|https|ftp|file)://\\S+$" ).indexIn( textVal ) != -1 )
{
isLink = true;
}
}

switch ( role )
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
return mFieldFormatters.at( index.column() )->representValue( mLayer,
fieldId,
mWidgetConfigs.at( index.column() ),
mAttributeWidgetCaches.at( index.column() ),
val );

case Qt::ToolTipRole:
{
QString tooltip = mFieldFormatters.at( index.column() )->representValue( mLayer,
fieldId,
mWidgetConfigs.at( index.column() ),
mAttributeWidgetCaches.at( index.column() ),
val );
if ( isLink )
{
tooltip = QString( "%1 (Ctrl+click to open)" ).arg( tooltip );
}
return tooltip;
}
case Qt::EditRole:
return val;

Expand Down Expand Up @@ -746,14 +766,27 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
if ( role == Qt::TextColorRole && style.validTextColor() )
#else
if ( role == Qt::ForegroundRole && style.validTextColor() )
if ( role == Qt::ForegroundRole )
#endif
return style.textColor();
if ( role == Qt::DecorationRole )
return style.icon();
if ( role == Qt::FontRole )
return style.font();
}
else if ( isLink )
{
if ( role == Qt::ForegroundRole )
{
return QColor( Qt::blue );
}
else if ( role == Qt::FontRole )
{
QFont font;
font.setUnderline( true );
return font;
}
}

return QVariant();
}
Expand Down
14 changes: 14 additions & 0 deletions src/gui/attributetable/qgsattributetableview.cpp
Expand Up @@ -13,6 +13,7 @@
* *
***************************************************************************/

#include <QDesktopServices>
#include <QKeyEvent>
#include <QHeaderView>
#include <QMenu>
Expand Down Expand Up @@ -304,6 +305,19 @@ void QgsAttributeTableView::mouseReleaseEvent( QMouseEvent *event )
setSelectionMode( QAbstractItemView::NoSelection );
QTableView::mouseReleaseEvent( event );
setSelectionMode( QAbstractItemView::ExtendedSelection );
if ( event->modifiers() == Qt::ControlModifier )
{
QModelIndex index = indexAt( event->pos() );
QVariant data = model()->data( index, Qt::DisplayRole );
if ( data.type() == QVariant::String )
{
QString textVal = data.toString();
if ( QRegExp( "^(http|https|ftp|file)://\\S+$" ).indexIn( textVal ) != -1 )
{
QDesktopServices::openUrl( QUrl( textVal ) );
}
}
}
}

void QgsAttributeTableView::mouseMoveEvent( QMouseEvent *event )
Expand Down

0 comments on commit 636042c

Please sign in to comment.