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;
210 if ( mapTip.isEmpty() )
212 exp.prepare( &context );
220 context.setFeature( feature );
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 );