Skip to content

Commit

Permalink
Add static methods to get a html preview for a given layer
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ committed Mar 28, 2023
1 parent cfb236e commit 66b64bc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/gui/auto_generated/qgsmaptip.sip.in
Expand Up @@ -67,6 +67,22 @@ Clear the current maptip if it exists
void applyFontSettings();
%Docstring
Apply font family and size to match user settings
%End

static QString vectorMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate, const QString &displayExpression );
%Docstring
Returns the html that would be displayed in a maptip for a given layer. If the layer has features, the first feature is used
to evaluate the expressions.

.. versionadded:: 3.32
%End

static QString rasterMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate );
%Docstring
Returns the html that would be displayed in a maptip for a given layer. The center pixel of the raster is used to
evaluate the expressions.

.. versionadded:: 3.32
%End

};
Expand Down
68 changes: 68 additions & 0 deletions src/gui/qgsmaptip.cpp
Expand Up @@ -379,3 +379,71 @@ void QgsMapTip::onLinkClicked( const QUrl &url )
{
QDesktopServices::openUrl( url );
}


QString QgsMapTip::vectorMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate, const QString &displayExpression )
{
// Only spatial layers can have map tips
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer || !vlayer->isSpatial() )
return QString();

// If no map tip template or display expression is set, return an empty string
if ( mapTemplate.isEmpty() && displayExpression.isEmpty() )
return QString();

// Create an expression context
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vlayer ) );
context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );

// Get the first feature if any, and add it to the expression context
QgsFeature previewFeature;
if ( vlayer->featureCount() > 0 )
{
QgsFeatureIterator it = vlayer->getFeatures( QgsFeatureRequest().setLimit( 1 ) );
it.nextFeature( previewFeature );
}
else
{
previewFeature = QgsFeature( vlayer->fields() );
}
context.setFeature( previewFeature );

// Generate the map tip text from the context and the mapTipTemplate/displayExpression
QString tipText;
if ( mapTemplate.isEmpty() )
{
QgsExpression exp( displayExpression );
exp.prepare( &context );
tipText = exp.evaluate( &context ).toString();
}
else
{
tipText = QgsExpression::replaceExpressionText( mapTemplate, &context );
}

// Insert the map tip text into the html template
return QgsMapTip::htmlText( tipText );

}

QString QgsMapTip::rasterMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate )
{
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( !rlayer || mapTemplate.isEmpty() )
return QString();

// Create an expression context
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );

// Get the position of the center of the layer, and add it to the expression context
const QgsPointXY mappedPosition { layer->extent().center() };
context.appendScope( QgsExpressionContextUtils::mapLayerPositionScope( mappedPosition ) );

// Generate the map tip text from the context and the mapTipTemplate
const QString tipText = QgsExpression::replaceExpressionText( mapTemplate, &context );

// Insert the map tip text into the html template
return QgsMapTip::htmlText( tipText );
}
14 changes: 14 additions & 0 deletions src/gui/qgsmaptip.h
Expand Up @@ -86,6 +86,20 @@ class GUI_EXPORT QgsMapTip : public QWidget
*/
void applyFontSettings();

/**
* Returns the html that would be displayed in a maptip for a given layer. If the layer has features, the first feature is used
* to evaluate the expressions.
* \since QGIS 3.32
*/
static QString vectorMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate, const QString &displayExpression );

/**
* Returns the html that would be displayed in a maptip for a given layer. The center pixel of the raster is used to
* evaluate the expressions.
* \since QGIS 3.32
*/
static QString rasterMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate );

private slots:
void onLinkClicked( const QUrl &url );
void resizeContent();
Expand Down

0 comments on commit 66b64bc

Please sign in to comment.