31 #include <QDesktopServices> 33 #include <QWebElement> 35 #include <QHBoxLayout> 43 mMapTipVisible =
false;
51 QPoint &pixelPosition,
61 if ( !pMapCanvas->
layers().contains( pLayer ) )
67 QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
68 backgroundColor, strokeColor;
71 mWidget =
new QWidget( pMapCanvas );
72 mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
77 mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
78 mWebView->setContextMenuPolicy( Qt::NoContextMenu );
79 connect( mWebView, &QWebView::linkClicked,
this, &QgsMapTip::onLinkClicked );
80 connect( mWebView, &QWebView::loadFinished,
this, [ = ](
bool ) { resizeContent(); } );
83 mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled,
true );
84 mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled,
true );
85 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
88 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
89 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
91 QHBoxLayout *layout =
new QHBoxLayout;
92 layout->setContentsMargins( 0, 0, 0, 0 );
93 layout->addWidget( mWebView );
95 mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
96 mWidget->setLayout( layout );
99 const int MAX_WIDTH = pMapCanvas->geometry().width() / 2;
100 const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2;
101 mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
105 mWidget->resize( 0, 0 );
107 backgroundColor = mWidget->palette().base().color().name();
108 strokeColor = mWidget->palette().shadow().color().name();
109 mWidget->setStyleSheet( QStringLiteral(
111 "border: 1px solid %1;" 112 "background-color: %2;}" ).arg(
113 strokeColor, backgroundColor ) );
115 tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
117 mMapTipVisible = !tipText.isEmpty();
118 if ( !mMapTipVisible )
124 if ( tipText == lastTipText )
129 bodyStyle = QStringLiteral(
130 "background-color: %1;" 132 "font: %2pt \"%3\";" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily );
134 containerStyle = QStringLiteral(
135 "display: inline-block;" 138 tipHtml = QStringLiteral(
141 "<div id='QgsWebViewContainer' style='%2'>%3</div>" 143 "</html>" ).arg( bodyStyle, containerStyle, tipText );
147 mWidget->move( pixelPosition.x(),
150 mWebView->setHtml( tipHtml );
151 lastTipText = tipText;
156 void QgsMapTip::resizeContent()
160 QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
161 QStringLiteral(
"#QgsWebViewContainer" ) );
162 int width = container.geometry().width() + MARGIN_VALUE * 2;
163 int height = container.geometry().height() + MARGIN_VALUE * 2;
164 mWidget->resize( width, height );
166 mWebView->adjustSize();
172 if ( !mMapTipVisible )
175 mWebView->setHtml( QString() );
179 mMapTipVisible =
false;
207 if ( mapTip.isEmpty() )
209 exp.prepare( &context );
218 if ( !mapTip.isEmpty() )
224 tipString = exp.evaluate( &context ).toString();
227 if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
239 QFont defaultFont = qApp->font();
240 mFontSize = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
241 mFontFamily = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
245 void QgsMapTip::onLinkClicked(
const QUrl &url )
247 QDesktopServices::openUrl( url );
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
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.
This class is a composition of two QSettings instances:
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
A class to represent a 2D point.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QList< QgsMapLayer * > layers() const
Returns the list of layers shown within the map canvas.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
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.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
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)...
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
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
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.
void applyFontSettings()
Apply font family and size to match user settings.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Query the layer for features specified in request.
QgsMapTip()
Default constructor.
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
void setXMinimum(double x)
Set the minimum x value.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
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...