34 #include <QDesktopServices>
36 #include <QWebElement>
38 #include <QHBoxLayout>
46 mMapTipVisible =
false;
54 QPoint &pixelPosition,
64 if ( !pMapCanvas->
layers().contains( pLayer ) )
70 QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
71 backgroundColor, strokeColor, textColor;
74 mWidget =
new QWidget( pMapCanvas );
75 mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
80 mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
81 mWebView->setContextMenuPolicy( Qt::NoContextMenu );
82 connect( mWebView, &QWebView::linkClicked,
this, &QgsMapTip::onLinkClicked );
83 connect( mWebView, &QWebView::loadFinished,
this, [ = ](
bool ) { resizeContent(); } );
86 mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled,
true );
87 mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled,
true );
88 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
91 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
92 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
94 QHBoxLayout *layout =
new QHBoxLayout;
95 layout->setContentsMargins( 0, 0, 0, 0 );
96 layout->addWidget( mWebView );
98 mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
99 mWidget->setLayout( layout );
102 const int MAX_WIDTH = pMapCanvas->geometry().width() / 2;
103 const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2;
104 mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
108 mWidget->resize( 0, 0 );
110 backgroundColor = mWidget->palette().base().color().name();
111 strokeColor = mWidget->palette().shadow().color().name();
112 textColor = mWidget->palette().text().color().name();
113 mWidget->setStyleSheet( QString(
115 "border: 1px solid %1;"
116 "background-color: %2;}" ).arg(
117 strokeColor, backgroundColor ) );
119 tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
121 mMapTipVisible = !tipText.isEmpty();
122 if ( !mMapTipVisible )
128 if ( tipText == lastTipText )
134 "background-color: %1;"
137 "color: %4;" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily ).arg( textColor );
139 containerStyle = QString(
140 "display: inline-block;"
146 "<div id='QgsWebViewContainer' style='%2'>%3</div>"
148 "</html>" ).arg( bodyStyle, containerStyle, tipText );
152 mWidget->move( pixelPosition.x(),
155 mWebView->setHtml( tipHtml );
156 lastTipText = tipText;
161 void QgsMapTip::resizeContent()
165 QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
166 QStringLiteral(
"#QgsWebViewContainer" ) );
167 int width = container.geometry().width() + MARGIN_VALUE * 2;
168 int height = container.geometry().height() + MARGIN_VALUE * 2;
169 mWidget->resize( width, height );
171 mWebView->adjustSize();
177 if ( !mMapTipVisible )
180 mWebView->setHtml( QString() );
184 mMapTipVisible =
false;
211 if ( mapTip.isEmpty() )
213 exp.prepare( &context );
221 std::unique_ptr< QgsFeatureRenderer > renderer;
225 renderer->startRender( renderCtx, vlayer->
fields() );
228 const QString filterExpression = renderer->filter( vlayer->
fields() );
229 if ( ! filterExpression.isEmpty() )
240 context.setFeature( feature );
243 if ( filter && renderer && !renderer->willRenderFeature( feature, renderCtx ) )
248 if ( !mapTip.isEmpty() )
254 tipString = exp.evaluate( &context ).toString();
257 if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
264 renderer->stopRender( renderCtx );
272 QFont defaultFont = qApp->font();
273 mFontSize = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
274 mFontFamily = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
278 void QgsMapTip::onLinkClicked(
const QUrl &url )
280 QDesktopServices::openUrl( url );