33 #include <QDesktopServices> 35 #include <QWebElement> 37 #include <QHBoxLayout> 45 mMapTipVisible =
false;
53 QPoint &pixelPosition,
63 if ( !pMapCanvas->
layers().contains( pLayer ) )
69 QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
70 backgroundColor, strokeColor;
73 mWidget =
new QWidget( pMapCanvas );
74 mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
79 mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
80 mWebView->setContextMenuPolicy( Qt::NoContextMenu );
81 connect( mWebView, &QWebView::linkClicked,
this, &QgsMapTip::onLinkClicked );
82 connect( mWebView, &QWebView::loadFinished,
this, [ = ](
bool ) { resizeContent(); } );
85 mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled,
true );
86 mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled,
true );
87 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
90 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
91 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
93 QHBoxLayout *layout =
new QHBoxLayout;
94 layout->setContentsMargins( 0, 0, 0, 0 );
95 layout->addWidget( mWebView );
97 mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
98 mWidget->setLayout( layout );
101 const int MAX_WIDTH = pMapCanvas->geometry().width() / 2;
102 const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2;
103 mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
107 mWidget->resize( 0, 0 );
109 backgroundColor = mWidget->palette().base().color().name();
110 strokeColor = mWidget->palette().shadow().color().name();
111 mWidget->setStyleSheet( QString(
113 "border: 1px solid %1;" 114 "background-color: %2;}" ).arg(
115 strokeColor, backgroundColor ) );
117 tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
119 mMapTipVisible = !tipText.isEmpty();
120 if ( !mMapTipVisible )
126 if ( tipText == lastTipText )
132 "background-color: %1;" 134 "white-space: nowrap;" 135 "font: %2pt \"%3\";" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily );
137 containerStyle = QString(
138 "display: inline-block;" 144 "<div id='QgsWebViewContainer' style='%2'>%3</div>" 146 "</html>" ).arg( bodyStyle, containerStyle, tipText );
150 mWidget->move( pixelPosition.x(),
153 mWebView->setHtml( tipHtml );
154 lastTipText = tipText;
159 void QgsMapTip::resizeContent()
163 QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
164 QStringLiteral(
"#QgsWebViewContainer" ) );
165 int width = container.geometry().width() + MARGIN_VALUE * 2;
166 int height = container.geometry().height() + MARGIN_VALUE * 2;
167 mWidget->resize( width, height );
169 mWebView->adjustSize();
175 if ( !mMapTipVisible )
178 mWebView->setHtml( QString() );
182 mMapTipVisible =
false;
210 if ( mapTip.isEmpty() )
212 exp.prepare( &context );
221 if ( !mapTip.isEmpty() )
227 tipString = exp.evaluate( &context ).toString();
230 if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
242 QFont defaultFont = qApp->font();
243 mFontSize = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
244 mFontFamily = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
248 void QgsMapTip::onLinkClicked(
const QUrl &url )
250 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.
QList< QgsMapLayer * > layers() const
Returns the list of layers shown within 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:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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.
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
const QgsMapSettings & mapSettings() const
Gets 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.
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)
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.
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...