QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsmaptip.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptips.cpp - Query a layer and show a maptip on the canvas
3 ---------------------
4 begin : October 2007
5 copyright : (C) 2007 by Gary Sherman
6 email : sherman @ mrcc dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15// QGIS includes
16#include "qgsapplication.h"
17#include "qgsexpression.h"
19#include "qgsfeatureiterator.h"
20#include "qgslogger.h"
21#include "qgsmapcanvas.h"
22#include "qgsmapcanvasutils.h"
24#include "qgsmaptool.h"
25#include "qgsrasterlayer.h"
26#include "qgsrendercontext.h"
27#include "qgsrenderer.h"
28#include "qgssettings.h"
29#include "qgsvectorlayer.h"
30#include "qgswebframe.h"
31#include "qgswebview.h"
32
33#include <QString>
34
35using namespace Qt::StringLiterals;
36
37// Qt includes
38#include <QPoint>
39#include <QToolTip>
40#include <QSettings>
41#include <QLabel>
42#include <QDesktopServices>
43#include <QHBoxLayout>
44
45#include "qgsmaptip.h"
46#include "moc_qgsmaptip.cpp"
47
48
49const QString QgsMapTip::sMapTipTemplate = "<html>\n"
50 " <head>\n"
51 " <style>\n"
52 " body {\n"
53 " margin: 0;\n"
54 " font: %1pt \"%2\";\n"
55 " color: %3;\n"
56 " width: %4px;\n"
57 " }\n"
58 " #QgsWebViewContainer {\n"
59 " background-color: %5;\n"
60 " border: 1px solid %6;\n"
61 " display: inline-block;\n"
62 " margin: 0\n"
63 " }\n"
64 " #QgsWebViewContainerInner {\n"
65 " margin: 5px\n"
66 " }\n"
67 " </style>\n"
68 " </head>\n"
69 " <body>\n"
70 " <div id='QgsWebViewContainer'>\n"
71 " <div id='QgsWebViewContainerInner'>\n"
72 " %7\n"
73 " </div>\n"
74 " </div>\n"
75 " </body>\n"
76 "</html>\n";
77
78
80{
81 // Init the visible flag
82 mMapTipVisible = false;
83
84 mDelayedClearTimer.setSingleShot( true );
85 connect( &mDelayedClearTimer, &QTimer::timeout, this, [this]() { this->clear(); } );
86}
87
88void QgsMapTip::showMapTip( QgsMapLayer *pLayer, QgsPointXY &mapPosition, const QPoint &pixelPosition, QgsMapCanvas *pMapCanvas )
89{
90 // Do the search using the active layer and the preferred label field for the
91 // layer. The label field must be defined in the layer configuration
92 // file/database. The code required to do this is similar to identify, except
93 // we only want the first qualifying feature and we will only display the
94 // field defined as the label field in the layer configuration file/database
95
96 // Do not render map tips if the layer is not visible
97 if ( !pMapCanvas->layers( true ).contains( pLayer ) )
98 {
99 return;
100 }
101
102 // Do not render a new map tip when the mouse hovers an existing one
103 if ( mWebView && mWebView->underMouse() )
104 {
105 return;
106 }
107
108 // Show the maptip on the canvas
109 QString tipText, lastTipText, tipHtml;
110
111 if ( !mWebView )
112 {
113 mWebView = new QgsWebView( pMapCanvas );
114 // Make the webwiew transparent
115
116 // Setting the background color to 'transparent' does not play nice
117 // with webkit scrollbars, that are rendered as black rectangles (#54683)
118 QColor transparentColor = mWebView->palette().color( QPalette::Window );
119 transparentColor.setAlpha( 0 );
120 mWebView->setStyleSheet( QString( "background:%1;" ).arg( transparentColor.name( QColor::HexArgb ) ) );
121
122 mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
123 mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
124 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled, true );
125
126 // Disable scrollbars, avoid random resizing issues
127 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
128 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
129 }
130
131 // Only supported layer types here:
132 switch ( pLayer->type() )
133 {
135 tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
136 break;
138 tipText = fetchRaster( pLayer, mapPosition, pMapCanvas );
139 break;
140 default:
141 break;
142 }
143
144 mMapTipVisible = !tipText.isEmpty();
145 if ( !mMapTipVisible )
146 {
147 clear();
148 return;
149 }
150
151 if ( tipText == lastTipText )
152 {
153 return;
154 }
155
156 // Compute offset from the cursor position
157 int cursorOffset = 0;
159 {
160 // The following calculations are taken
161 // from QgsApplication::getThemeCursor, and are used to calculate the correct cursor size
162 // for both hi-dpi and non-hi-dpi screens.
163 double scale = Qgis::UI_SCALE_FACTOR * QgsApplication::instance()->fontMetrics().height() / 32.0;
164 cursorOffset = static_cast<int>( std::ceil( scale * 32 ) );
165 }
166
167 // Ensures the map tip is never larger than the available space
168 const int MAX_WIDTH = std::max( pixelPosition.x(), pMapCanvas->width() - pixelPosition.x() ) - cursorOffset - 5;
169 const int MAX_HEIGHT = std::max( pixelPosition.y(), pMapCanvas->height() - pixelPosition.y() ) - 5;
170
171 mWebView->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
172
173 tipHtml = QgsMapTip::htmlText( tipText, MAX_WIDTH );
174
175 QgsDebugMsgLevel( tipHtml, 2 );
176
177 mPosition = pixelPosition;
178 mMapCanvas = pMapCanvas;
179 mWebView->setHtml( tipHtml );
180 lastTipText = tipText;
181
182 resizeAndMoveToolTip();
183}
184
185void QgsMapTip::resizeAndMoveToolTip()
186{
187 mWebView->adjustSize();
188
189 int cursorOffset = 0;
190 // attempt to shift the tip away from the cursor.
192 {
193 // The following calculations are taken
194 // from QgsApplication::getThemeCursor, and are used to calculate the correct cursor size
195 // for both hi-dpi and non-hi-dpi screens.
196 double scale = Qgis::UI_SCALE_FACTOR * QgsApplication::instance()->fontMetrics().height() / 32.0;
197 cursorOffset = static_cast<int>( std::ceil( scale * 32 ) );
198 }
199
200 if ( !mMapCanvas )
201 {
202 mWebView->move( mPosition );
203 mWebView->show();
204 return;
205 }
206
207 // Check if there is enough space to the right of the cursor
208 int availableWidthRight = mMapCanvas->width() - mPosition.x() - cursorOffset;
209 int availableWidthLeft = mPosition.x() - cursorOffset;
210 int availableHeightBottom = mMapCanvas->height() - mPosition.y();
211 int availableHeightTop = mPosition.y();
212 int x, y;
213 // If there is enough space on the right, or more space on the right than on the left, move the map tip to the right of the cursor
214 if ( mWebView->width() < availableWidthRight || availableWidthRight > availableWidthLeft )
215 {
216 x = mPosition.x() + cursorOffset;
217 }
218 // Otherwise, move the map tip to the left of the cursor
219 else
220 {
221 x = mPosition.x() - mWebView->width() - cursorOffset;
222 }
223
224 // If there is enough space on the bottom, or more space on the bottom than on the top, move the map tip to the bottom of the cursor
225 if ( mWebView->height() < availableHeightBottom || availableHeightBottom > availableHeightTop )
226 {
227 y = mPosition.y();
228 }
229 // Otherwise, move the map tip to the top of the cursor
230 else
231 {
232 y = mPosition.y() - mWebView->height();
233 }
234 mWebView->move( x, y );
235 mWebView->show();
236}
237
238void QgsMapTip::clear( QgsMapCanvas *, int msDelay )
239{
240 if ( !mMapTipVisible )
241 {
242 return;
243 }
244
245 // Skip clearing the map tip if the user interacts with it or the timer still runs
246 if ( mDelayedClearTimer.isActive() || mWebView->underMouse() )
247 {
248 return;
249 }
250
251 if ( msDelay > 0 )
252 {
253 mDelayedClearTimer.start( msDelay );
254 return;
255 }
256 mWebView->setHtml( QString() );
257 mWebView->hide();
258
259 // Reset the visible flag
260 mMapTipVisible = false;
261}
262
263QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, QgsMapCanvas *mapCanvas )
264{
265 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
266 if ( !vlayer || !vlayer->isSpatial() || !vlayer->mapTipsEnabled() )
267 {
268 return QString();
269 }
270
271 if ( !layer->isInScaleRange( mapCanvas->mapSettings().scale() ) || ( mapCanvas->mapSettings().isTemporal() && layer->temporalProperties() && !layer->temporalProperties()->isVisibleInTemporalRange( mapCanvas->temporalRange() ) ) )
272 {
273 return QString();
274 }
275
276 const double searchRadius = QgsMapTool::searchRadiusMU( mapCanvas );
277
278 QgsRectangle r;
279 r.setXMinimum( mapPosition.x() - searchRadius );
280 r.setYMinimum( mapPosition.y() - searchRadius );
281 r.setXMaximum( mapPosition.x() + searchRadius );
282 r.setYMaximum( mapPosition.y() + searchRadius );
283
284 r = mapCanvas->mapSettings().mapToLayerCoordinates( layer, r );
285
286 QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vlayer ) );
287 context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );
288 context.appendScope( QgsExpressionContextUtils::mapLayerPositionScope( r.center() ) );
289
290 const QString canvasFilter = QgsMapCanvasUtils::filterForLayer( mapCanvas, vlayer );
291 if ( canvasFilter == "FALSE"_L1 )
292 {
293 return QString();
294 }
295
296 const QString mapTip = vlayer->mapTipTemplate();
297 QString tipString;
298 QgsExpression exp( vlayer->displayExpression() );
299 QgsFeature feature;
300
301 QgsFeatureRequest request;
302 request.setFilterRect( r );
304 if ( !canvasFilter.isEmpty() )
305 {
306 request.setFilterExpression( canvasFilter );
307 }
308
309 if ( mapTip.isEmpty() )
310 {
311 exp.prepare( &context );
312 request.setSubsetOfAttributes( exp.referencedColumns(), vlayer->fields() );
313 }
314
315 QgsRenderContext renderCtx = QgsRenderContext::fromMapSettings( mapCanvas->mapSettings() );
316 renderCtx.setExpressionContext( mapCanvas->createExpressionContext() );
318
319 bool filter = false;
320 std::unique_ptr<QgsFeatureRenderer> renderer;
321 if ( vlayer->renderer() )
322 {
323 renderer.reset( vlayer->renderer()->clone() );
324 renderer->startRender( renderCtx, vlayer->fields() );
325 filter = renderer->capabilities() & QgsFeatureRenderer::Filter;
326
327 const QString filterExpression = renderer->filter( vlayer->fields() );
328 if ( !filterExpression.isEmpty() )
329 {
330 request.combineFilterExpression( filterExpression );
331 }
332 }
333 request.setExpressionContext( renderCtx.expressionContext() );
334
335 QgsFeatureIterator it = vlayer->getFeatures( request );
336 QElapsedTimer timer;
337 timer.start();
338 while ( it.nextFeature( feature ) )
339 {
340 context.setFeature( feature );
341
342 renderCtx.expressionContext().setFeature( feature );
343 if ( filter && renderer && !renderer->willRenderFeature( feature, renderCtx ) )
344 {
345 continue;
346 }
347
348 if ( !mapTip.isEmpty() )
349 {
350 tipString = QgsExpression::replaceExpressionText( mapTip, &context );
351 }
352 else
353 {
354 tipString = exp.evaluate( &context ).toString();
355 }
356
357 if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
358 {
359 break;
360 }
361 }
362
363 if ( renderer )
364 {
365 renderer->stopRender( renderCtx );
366 }
367
368 return tipString;
369}
370
371QString QgsMapTip::fetchRaster( QgsMapLayer *layer, QgsPointXY &mapPosition, QgsMapCanvas *mapCanvas )
372{
373 QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
374 if ( !rlayer || !rlayer->mapTipsEnabled() )
375 {
376 return QString();
377 }
378
379 if ( !layer->isInScaleRange( mapCanvas->mapSettings().scale() ) || ( mapCanvas->mapSettings().isTemporal() && !layer->temporalProperties()->isVisibleInTemporalRange( mapCanvas->temporalRange() ) ) )
380 {
381 return QString();
382 }
383
384 if ( rlayer->mapTipTemplate().isEmpty() )
385 {
386 return QString();
387 }
388
389 const QgsPointXY mappedPosition { mapCanvas->mapSettings().mapToLayerCoordinates( layer, mapPosition ) };
390
391 if ( !layer->extent().contains( mappedPosition ) )
392 {
393 return QString();
394 }
395
396 QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
397 context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );
398 context.appendScope( QgsExpressionContextUtils::mapLayerPositionScope( mappedPosition ) );
399 return QgsExpression::replaceExpressionText( rlayer->mapTipTemplate(), &context );
400}
401
402QString QgsMapTip::htmlText( const QString &text, int maxWidth )
403{
404 const QgsSettings settings;
405 const QFont defaultFont = qApp->font();
406 const int fontSize = defaultFont.pointSize();
407 const QString fontFamily = defaultFont.family();
408 const QString backgroundColor = QgsApplication::palette().base().color().name();
409 const QString strokeColor = QgsApplication::palette().shadow().color().name();
410 const QString textColor = QgsApplication::palette().toolTipText().color().name();
411 return sMapTipTemplate.arg( fontSize ).arg( fontFamily ).arg( textColor ).arg( maxWidth == -1 ? "" : QString::number( maxWidth ) ).arg( backgroundColor ).arg( strokeColor ).arg( text );
412}
413
414// This slot handles all clicks
415void QgsMapTip::onLinkClicked( const QUrl &url )
416{
417 QDesktopServices::openUrl( url );
418}
419
420
421QString QgsMapTip::vectorMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate, const QString &displayExpression )
422{
423 // Only spatial layers can have map tips
424 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
425 if ( !mapCanvas || !vlayer || !vlayer->isSpatial() )
426 return QString();
427
428 // If no map tip template or display expression is set, return an empty string
429 if ( mapTemplate.isEmpty() && displayExpression.isEmpty() )
430 return QString();
431
432 // Create an expression context
435
436 // Get the first feature if any, and add it to the expression context
437 QgsFeature previewFeature;
438 if ( vlayer->featureCount() > 0 )
439 {
440 QgsFeatureIterator it = vlayer->getFeatures( QgsFeatureRequest().setLimit( 1 ) );
441 it.nextFeature( previewFeature );
442 }
443 else
444 {
445 previewFeature = QgsFeature( vlayer->fields() );
446 }
447 context.setFeature( previewFeature );
448
449 // Generate the map tip text from the context and the mapTipTemplate/displayExpression
450 QString tipText;
451 if ( mapTemplate.isEmpty() )
452 {
453 QgsExpression exp( displayExpression );
454 exp.prepare( &context );
455 tipText = exp.evaluate( &context ).toString();
456 }
457 else
458 {
459 tipText = QgsExpression::replaceExpressionText( mapTemplate, &context );
460 }
461
462 // Insert the map tip text into the html template
463 return QgsMapTip::htmlText( tipText, mapCanvas->width() / 2 );
464}
465
466QString QgsMapTip::rasterMapTipPreviewText( QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate )
467{
468 QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
469 if ( !mapCanvas || !rlayer || mapTemplate.isEmpty() )
470 {
471 return QString();
472 }
473
474 // Create an expression context
477
478 // Get the position of the center of the layer, and add it to the expression context
479 const QgsPointXY mappedPosition { layer->extent().center() };
481
482 // Generate the map tip text from the context and the mapTipTemplate
483 const QString tipText = QgsExpression::replaceExpressionText( mapTemplate, &context );
484
485 // Insert the map tip text into the html template
486 return QgsMapTip::htmlText( tipText, mapCanvas->width() / 2 );
487}
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
Definition qgis.h:2256
@ Vector
Vector layer.
Definition qgis.h:194
@ Raster
Raster layer.
Definition qgis.h:195
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:6499
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 appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Handles parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
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...
QVariant evaluate()
Evaluate the feature and return the result.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
@ 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.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & combineFilterExpression(const QString &expression)
Modifies the existing filter expression to add an additional expression filter.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
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...
Definition qgsfeature.h:60
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.
Definition qgsmaplayer.h:83
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.
Qgis::LayerType type
Definition qgsmaplayer.h:93
virtual QgsMapLayerTemporalProperties * temporalProperties()
Returns the layer's temporal properties.
bool mapTipsEnabled
Definition qgsmaplayer.h:97
QString mapTipTemplate
Definition qgsmaplayer.h:96
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.
Definition qgsmaptip.cpp:88
static QString rasterMapTipPreviewText(QgsMapLayer *layer, QgsMapCanvas *mapCanvas, const QString &mapTemplate)
Returns the html that would be displayed in a maptip for a given layer.
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.
QgsMapTip()
Default constructor.
Definition qgsmaptip.cpp:79
void clear(QgsMapCanvas *mpMapCanvas=nullptr, int msDelay=0)
Clear the current maptip if it exists.
static double searchRadiusMU(const QgsRenderContext &context)
Gets search radius in map units for given context.
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Represents a raster layer.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
QgsPointXY center
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.
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 dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
bool isSpatial() const final
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
QString displayExpression
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
A collection of stubs to mimic the API of QWebView on systems where the real library is not available...
Definition qgswebview.h:39
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63