38 #include <QDesktopServices>
40 #include <QWebElement>
42 #include <QHBoxLayout>
50 mMapTipVisible =
false;
55 mDelayedClearTimer.setSingleShot(
true );
56 connect( &mDelayedClearTimer, &QTimer::timeout,
this, [ = ]() {this->
clear();} );
61 QPoint &pixelPosition,
71 if ( !pMapCanvas->
layers(
true ).contains( pLayer ) )
77 if ( mWidget && mWidget->underMouse() )
81 QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
82 backgroundColor, strokeColor, textColor;
85 mWidget =
new QWidget( pMapCanvas );
86 mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
91 mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
92 mWebView->setContextMenuPolicy( Qt::NoContextMenu );
93 connect( mWebView, &QWebView::linkClicked,
this, &QgsMapTip::onLinkClicked );
94 connect( mWebView, &QWebView::loadFinished,
this, [ = ](
bool ) { resizeContent(); } );
97 mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled,
true );
98 mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled,
true );
99 mWebView->page()->settings()->setAttribute( QWebSettings::LocalStorageEnabled,
true );
102 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
103 mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
105 QHBoxLayout *layout =
new QHBoxLayout;
106 layout->setContentsMargins( 0, 0, 0, 0 );
107 layout->addWidget( mWebView );
109 mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
110 mWidget->setLayout( layout );
113 const int MAX_WIDTH = pMapCanvas->geometry().width() / 2;
114 const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2;
115 mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
119 mWidget->resize( 0, 0 );
121 backgroundColor = mWidget->palette().base().color().name();
122 strokeColor = mWidget->palette().shadow().color().name();
123 textColor = mWidget->palette().text().color().name();
124 mWidget->setStyleSheet( QString(
126 "border: 1px solid %1;"
127 "background-color: %2;}" ).arg(
128 strokeColor, backgroundColor ) );
130 tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
132 mMapTipVisible = !tipText.isEmpty();
133 if ( !mMapTipVisible )
139 if ( tipText == lastTipText )
145 "background-color: %1;"
148 "color: %4;" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily, textColor );
150 containerStyle = QString(
151 "display: inline-block;"
157 "<div id='QgsWebViewContainer' style='%2'>%3</div>"
159 "</html>" ).arg( bodyStyle, containerStyle, tipText );
163 int cursorOffset = 0;
171 cursorOffset =
static_cast< int >( std::ceil( scale * 32 ) );
174 mWidget->move( pixelPosition.x() + cursorOffset, pixelPosition.y() );
176 mWebView->setHtml( tipHtml );
177 lastTipText = tipText;
182 void QgsMapTip::resizeContent()
186 const QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
187 QStringLiteral(
"#QgsWebViewContainer" ) );
188 const int width = container.geometry().width() + MARGIN_VALUE * 2;
189 const int height = container.geometry().height() + MARGIN_VALUE * 2;
190 mWidget->resize( width, height );
192 mWebView->adjustSize();
198 if ( !mMapTipVisible )
202 if ( mDelayedClearTimer.isActive() || mWidget->underMouse() )
207 mDelayedClearTimer.start( msDelay );
210 mWebView->setHtml( QString() );
214 mMapTipVisible =
false;
242 if ( canvasFilter == QLatin1String(
"FALSE" ) )
253 if ( !canvasFilter.isEmpty() )
256 if ( mapTip.isEmpty() )
258 exp.prepare( &context );
267 std::unique_ptr< QgsFeatureRenderer > renderer;
271 renderer->startRender( renderCtx, vlayer->
fields() );
274 const QString filterExpression = renderer->filter( vlayer->
fields() );
275 if ( ! filterExpression.isEmpty() )
287 context.setFeature( feature );
290 if ( filter && renderer && !renderer->willRenderFeature( feature, renderCtx ) )
295 if ( !mapTip.isEmpty() )
301 tipString = exp.evaluate( &context ).toString();
304 if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
311 renderer->stopRender( renderCtx );
319 const QFont defaultFont = qApp->font();
320 mFontSize = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
321 mFontFamily = settings.
value( QStringLiteral(
"/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
325 void QgsMapTip::onLinkClicked(
const QUrl &url )
327 QDesktopServices::openUrl( url );