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, textColor;
    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   textColor = mWidget->palette().text().color().name();
   112   mWidget->setStyleSheet( QString(
   114                             "border: 1px solid %1;"   115                             "background-color: %2;}" ).arg(
   116                             strokeColor, backgroundColor ) );
   118   tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
   120   mMapTipVisible = !tipText.isEmpty();
   121   if ( !mMapTipVisible )
   127   if ( tipText == lastTipText )
   133                 "background-color: %1;"   136                 "color: %4;" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily ).arg( textColor );
   138   containerStyle = QString(
   139                      "display: inline-block;"   145               "<div id='QgsWebViewContainer' style='%2'>%3</div>"   147               "</html>" ).arg( bodyStyle, containerStyle, tipText );
   151   mWidget->move( pixelPosition.x(),
   154   mWebView->setHtml( tipHtml );
   155   lastTipText = tipText;
   160 void QgsMapTip::resizeContent()
   164   QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
   165                             QStringLiteral( 
"#QgsWebViewContainer" ) );
   166   int width = container.geometry().width() + MARGIN_VALUE * 2;
   167   int height = container.geometry().height() + MARGIN_VALUE * 2;
   168   mWidget->resize( width, height );
   170   mWebView->adjustSize();
   176   if ( !mMapTipVisible )
   179   mWebView->setHtml( QString() );
   183   mMapTipVisible = 
false;
   211   if ( mapTip.isEmpty() )
   213     exp.prepare( &context );
   222     if ( !mapTip.isEmpty() )
   228       tipString = exp.evaluate( &context ).toString();
   231     if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
   243   QFont defaultFont = qApp->font();
   244   mFontSize = settings.
value( QStringLiteral( 
"/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
   245   mFontFamily = settings.
value( QStringLiteral( 
"/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
   249 void QgsMapTip::onLinkClicked( 
const QUrl &url )
   251   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
Queries 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...