20 #include "qgsexpression.h" 30 #include <QDesktopServices> 32 #include <QWebElement> 34 #include <QHBoxLayout> 43 mMapTipVisible =
false;
48 QPoint &pixelPosition,
58 QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
59 backgroundColor, strokeColor;
62 mWidget =
new QWidget( pMapCanvas );
67 mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
68 mWebView->setContextMenuPolicy( Qt::NoContextMenu );
69 connect( mWebView, &QWebView::linkClicked,
this, &QgsMapTip::onLinkClicked );
72 mWebView->page()->settings()->setAttribute(
73 QWebSettings::DeveloperExtrasEnabled,
true );
74 mWebView->page()->settings()->setAttribute(
75 QWebSettings::JavascriptEnabled,
true );
77 QHBoxLayout *layout =
new QHBoxLayout;
78 layout->addWidget( mWebView );
80 mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
81 mWidget->setLayout( layout );
84 const int MAX_WIDTH = pMapCanvas->geometry().width() / 2;
85 const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2;
86 mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
90 mWidget->resize( 0, 0 );
92 backgroundColor = mWidget->palette().base().color().name();
93 strokeColor = mWidget->palette().shadow().color().name();
94 mWidget->setStyleSheet( QString(
96 "border: 1px solid %1;" 97 "background-color: %2;}" ).arg(
98 strokeColor, backgroundColor ) );
100 tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
102 mMapTipVisible = !tipText.isEmpty();
103 if ( !mMapTipVisible )
109 if ( tipText == lastTipText )
115 "background-color: %1;" 116 "margin: 0;" ).arg( backgroundColor );
118 containerStyle = QString(
119 "display: inline-block;" 125 "<div id='QgsWebViewContainer' style='%2'>%3</div>" 127 "</html>" ).arg( bodyStyle, containerStyle, tipText );
129 mWidget->move( pixelPosition.x(),
132 mWebView->setHtml( tipHtml );
133 lastTipText = tipText;
138 int scrollbarWidth = mWebView->page()->mainFrame()->scrollBarGeometry(
139 Qt::Vertical ).width();
140 int scrollbarHeight = mWebView->page()->mainFrame()->scrollBarGeometry(
141 Qt::Horizontal ).height();
143 if ( scrollbarWidth > 0 || scrollbarHeight > 0 )
146 QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
147 QStringLiteral(
"#QgsWebViewContainer" ) );
148 int width = container.geometry().width() + 5 + scrollbarWidth;
149 int height = container.geometry().height() + 5 + scrollbarHeight;
151 mWidget->resize( width, height );
158 if ( !mMapTipVisible )
161 mWebView->setHtml( QString() );
165 mMapTipVisible =
false;
196 if ( !mapTip.isEmpty() )
198 return QgsExpression::replaceExpressionText( mapTip, &context );
203 return exp.evaluate( &context ).toString();
208 void QgsMapTip::onLinkClicked(
const QUrl &url )
210 QDesktopServices::openUrl( url );
A rectangle specified with double values.
Base class for all map layer types.
void showMapTip(QgsMapLayer *thepLayer, QgsPointXY &mapPosition, QPoint &pixelPosition, QgsMapCanvas *mpMapCanvas)
Show a maptip at a given point on the map canvas.
void setXMaximum(double x)
Set the maximum x value.
Use exact geometry intersection (slower) instead of bounding boxes.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
A class to represent a 2D point.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Map canvas is a class for displaying all GIS data types on a canvas.
void setYMinimum(double y)
Set the minimum y value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void clear(QgsMapCanvas *mpMapCanvas=nullptr)
Clear the current maptip if it exists.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
The QgsWebView class is a collection of stubs to mimic the API of QWebView on systems where the real ...
QString displayExpression
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object...
void setYMaximum(double y)
Set the maximum y value.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
QgsMapTip()
Default constructor.
bool nextFeature(QgsFeature &f)
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
Represents a vector layer which manages a vector based data sets.
void setXMinimum(double x)
Set the minimum x value.