Skip to content

Commit

Permalink
Move html template to its own method
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ committed Mar 28, 2023
1 parent 16fab1e commit cfb236e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
70 changes: 38 additions & 32 deletions src/gui/qgsmaptip.cpp
Expand Up @@ -119,9 +119,8 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
// The content will automatically make it grow up to MaximumSize
mWidget->resize( 0, 0 );

backgroundColor = mWidget->palette().base().color().name();
strokeColor = mWidget->palette().shadow().color().name();
textColor = mWidget->palette().text().color().name();
backgroundColor = QgsApplication::palette().base().color().name();
strokeColor = QgsApplication::palette().shadow().color().name();
mWidget->setStyleSheet( QString(
".QWidget{"
"border: 1px solid %1;"
Expand Down Expand Up @@ -154,22 +153,7 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
return;
}

bodyStyle = QString(
"background-color: %1;"
"margin: 0;"
"font: %2pt \"%3\";"
"color: %4;" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily, textColor );

containerStyle = QString(
"display: inline-block;"
"margin: 0px" );

tipHtml = QString(
"<html>"
"<body style='%1'>"
"<div id='QgsWebViewContainer' style='%2'>%3</div>"
"</body>"
"</html>" ).arg( bodyStyle, containerStyle, tipText );
tipHtml = QgsMapTip::htmlText( tipText );

QgsDebugMsg( tipHtml );

Expand Down Expand Up @@ -336,28 +320,50 @@ QString QgsMapTip::fetchRaster( QgsMapLayer *layer, QgsPointXY &mapPosition, Qgs

if ( !layer->isInScaleRange( mapCanvas->mapSettings().scale() ) ||
( mapCanvas->mapSettings().isTemporal() && !layer->temporalProperties()->isVisibleInTemporalRange( mapCanvas->temporalRange() ) ) )
{
return QString();
}

if ( rlayer->mapTipTemplate().isEmpty() )
return QString();

const QgsPointXY mappedPosition { mapCanvas->mapSettings().mapToLayerCoordinates( layer, mapPosition ) };

if ( ! layer->extent().contains( mappedPosition ) )
{
return QString( );
}

QString tipText { rlayer->mapTipTemplate() };
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );
context.appendScope( QgsExpressionContextUtils::mapLayerPositionScope( mappedPosition ) );
return QgsExpression::replaceExpressionText( rlayer->mapTipTemplate(), &context );
}

QString QgsMapTip::htmlText( const QString &text )
{
const QgsSettings settings;
const QFont defaultFont = qApp->font();
const int fontSize = settings.value( QStringLiteral( "/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
const QString fontFamily = settings.value( QStringLiteral( "/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
QString bodyStyle, containerStyle, backgroundColor, strokeColor, textColor;

backgroundColor = QgsApplication::palette().base().color().name();
strokeColor = QgsApplication::palette().shadow().color().name();
textColor = QgsApplication::palette().windowText().color().name();

if ( ! tipText.isEmpty() )
{
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );
context.appendScope( QgsExpressionContextUtils::mapLayerPositionScope( mappedPosition ) );
tipText = QgsExpression::replaceExpressionText( tipText, &context );
}
bodyStyle = QString(
"background-color: %1;"
"margin: 0;"
"font: %2pt \"%3\";"
"color: %4;" ).arg( backgroundColor ).arg( fontSize ).arg( fontFamily, textColor );

containerStyle = QString(
"display: inline-block;"
"margin: 0px" );

return tipText;
return QString(
"<html>"
"<body style='%1'>"
"<div id='QgsWebViewContainer' style='%2'>%3</div>"
"</body>"
"</html>" ).arg( bodyStyle, containerStyle, text );
}

void QgsMapTip::applyFontSettings()
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsmaptip.h
Expand Up @@ -102,8 +102,8 @@ class GUI_EXPORT QgsMapTip : public QWidget
QgsPointXY &mapPosition,
QgsMapCanvas *mapCanvas );

QString replaceText(
QString displayText, QgsVectorLayer *layer, QgsFeature &feat );
// Insert the raw map tip text into an HTML template and return the result
static QString htmlText( const QString &text );

// Flag to indicate if a maptip is currently being displayed
bool mMapTipVisible;
Expand Down

0 comments on commit cfb236e

Please sign in to comment.