38#include <QDesktopServices>
50 mMapTipVisible =
false;
55 mDelayedClearTimer.setSingleShot(
true );
56 connect( &mDelayedClearTimer, &QTimer::timeout,
this, [ = ]() {this->
clear();} );
61 const QPoint &pixelPosition,
71 if ( !pMapCanvas->
layers(
true ).contains( pLayer ) )
77 if ( mWidget && mWidget->underMouse() )
81 QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
82 backgroundColor, strokeColor, textColor;
86 mWidget =
new QWidget( pMapCanvas );
87 mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
92 mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
93 mWebView->setContextMenuPolicy( Qt::NoContextMenu );
94 connect( mWebView, &QWebView::linkClicked,
this, &QgsMapTip::onLinkClicked );
95 connect( mWebView, &QWebView::loadFinished,
this, [ = ](
bool ) { resizeContent(); } );
98 mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled,
true );
99 mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled,
true );
100 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
103 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
104 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
106 QHBoxLayout *layout =
new QHBoxLayout;
107 layout->setContentsMargins( 0, 0, 0, 0 );
108 layout->addWidget( mWebView );
110 mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
111 mWidget->setLayout( layout );
114 const int MAX_WIDTH = pMapCanvas->geometry().width() / 2;
115 const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2;
116 mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
120 mWidget->resize( 0, 0 );
122 backgroundColor = mWidget->palette().base().color().name();
123 strokeColor = mWidget->palette().shadow().color().name();
124 textColor = mWidget->palette().text().color().name();
125 mWidget->setStyleSheet( QString(
127 "border: 1px solid %1;"
128 "background-color: %2;}" ).arg(
129 strokeColor, backgroundColor ) );
133 switch ( pLayer->
type() )
135 case Qgis::LayerType::Vector:
136 tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
138 case Qgis::LayerType::Raster:
139 tipText = fetchRaster( pLayer, mapPosition, pMapCanvas );
145 mMapTipVisible = !tipText.isEmpty();
146 if ( !mMapTipVisible )
152 if ( tipText == lastTipText )
158 "background-color: %1;"
161 "color: %4;" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily, textColor );
163 containerStyle = QString(
164 "display: inline-block;"
170 "<div id='QgsWebViewContainer' style='%2'>%3</div>"
172 "</html>" ).arg( bodyStyle, containerStyle, tipText );
176 int cursorOffset = 0;
184 cursorOffset =
static_cast< int >( std::ceil( scale * 32 ) );
187 mWidget->move( pixelPosition.x() + cursorOffset, pixelPosition.y() );
188 mWebView->setHtml( tipHtml );
189 lastTipText = tipText;
195void QgsMapTip::resizeContent()
199 const QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
200 QStringLiteral(
"#QgsWebViewContainer" ) );
201 const int width = container.geometry().width() + MARGIN_VALUE * 2;
202 const int height = container.geometry().height() + MARGIN_VALUE * 2;
203 mWidget->resize( width, height );
205 mWebView->adjustSize();
211 if ( !mMapTipVisible )
215 if ( mDelayedClearTimer.isActive() || mWidget->underMouse() )
220 mDelayedClearTimer.start( msDelay );
223 mWebView->setHtml( QString() );
227 mMapTipVisible =
false;
257 if ( canvasFilter == QLatin1String(
"FALSE" ) )
268 if ( !canvasFilter.isEmpty() )
271 if ( mapTip.isEmpty() )
273 exp.prepare( &context );
282 std::unique_ptr< QgsFeatureRenderer > renderer;
286 renderer->startRender( renderCtx, vlayer->
fields() );
289 const QString filterExpression = renderer->filter( vlayer->
fields() );
290 if ( ! filterExpression.isEmpty() )
302 context.setFeature( feature );
305 if ( filter && renderer && !renderer->willRenderFeature( feature, renderCtx ) )
310 if ( !mapTip.isEmpty() )
316 tipString = exp.evaluate( &context ).toString();
319 if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
326 renderer->stopRender( renderCtx );
352 if ( ! tipText.isEmpty() )
366 const QFont defaultFont = qApp->font();
367 mFontSize = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
368 mFontFamily = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
372void QgsMapTip::onLinkClicked(
const QUrl &url )
374 QDesktopServices::openUrl( url );
static const double UI_SCALE_FACTOR
UI scaling factor.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
static QgsExpressionContextScope * mapLayerPositionScope(const QgsPointXY &position)
Sets the expression context variables which are available for expressions triggered by moving the mou...
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & combineFilterExpression(const QString &expression)
Modifies the existing filter expression to add an additional expression filter.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
static QString filterForLayer(QgsMapCanvas *canvas, QgsVectorLayer *layer)
Constructs a filter to use for selecting features from the given layer, in order to apply filters whi...
Map canvas is a class for displaying all GIS data types on a canvas.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
const QgsDateTimeRange & temporalRange() const
Returns map canvas datetime range.
QList< QgsMapLayer * > layers(bool expandGroupLayers=false) const
Returns the list of layers shown within the map canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
virtual bool isVisibleInTemporalRange(const QgsDateTimeRange &range) const
Returns true if the layer should be visible and rendered for the specified time range.
Base class for all map layer types.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
virtual QgsRectangle extent() const
Returns the extent of the layer.
virtual QgsMapLayerTemporalProperties * temporalProperties()
Returns the layer's temporal properties.
double scale() const
Returns the calculated map scale.
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
void showMapTip(QgsMapLayer *thepLayer, QgsPointXY &mapPosition, const QPoint &pixelPosition, QgsMapCanvas *mpMapCanvas)
Show a maptip at a given point on the map canvas.
QgsMapTip()
Default constructor.
void applyFontSettings()
Apply font family and size to match user settings.
void clear(QgsMapCanvas *mpMapCanvas=nullptr, int msDelay=0)
Clear the current maptip if it exists.
A class to represent a 2D point.
Represents a raster layer.
A rectangle specified with double values.
void setYMinimum(double y) SIP_HOLDGIL
Set the minimum y value.
void setXMaximum(double x) SIP_HOLDGIL
Set the maximum x value.
void setXMinimum(double x) SIP_HOLDGIL
Set the minimum x value.
void setYMaximum(double y) SIP_HOLDGIL
Set the maximum y value.
bool contains(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle contains other rectangle.
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
Contains information about the context of a rendering operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool isTemporal() const
Returns true if the object's temporal range is enabled, and the object will be filtered when renderin...
Represents a vector layer which manages a vector based data sets.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
QString displayExpression
The QgsWebView class is a collection of stubs to mimic the API of QWebView on systems where the real ...