3737#include " qgsmaptip.h"
3838
3939QgsMapTip::QgsMapTip ()
40-
4140{
42- // init the visible flag
41+ // Init the visible flag
4342 mMapTipVisible = false ;
4443}
4544
@@ -60,6 +59,7 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
6059
6160 delete mWidget ;
6261 mWidget = new QWidget ( pMapCanvas );
62+ mWidget ->setContentsMargins ( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
6363 mWebView = new QgsWebView ( mWidget );
6464
6565
@@ -70,24 +70,27 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
7070 connect ( mWebView , &QWebView::loadFinished, this , [ = ]( bool ) { resizeContent (); } );
7171#endif
7272
73- mWebView ->page ()->settings ()->setAttribute (
74- QWebSettings::DeveloperExtrasEnabled, true );
75- mWebView ->page ()->settings ()->setAttribute (
76- QWebSettings::JavascriptEnabled, true );
73+ mWebView ->page ()->settings ()->setAttribute ( QWebSettings::DeveloperExtrasEnabled, true );
74+ mWebView ->page ()->settings ()->setAttribute ( QWebSettings::JavascriptEnabled, true );
75+
76+ // Disable scrollbars, avoid random resizing issues
77+ mWebView ->page ()->mainFrame ()->setScrollBarPolicy ( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
78+ mWebView ->page ()->mainFrame ()->setScrollBarPolicy ( Qt::Vertical, Qt::ScrollBarAlwaysOff );
7779
7880 QHBoxLayout *layout = new QHBoxLayout;
81+ layout->setContentsMargins ( 0 , 0 , 0 , 0 );
7982 layout->addWidget ( mWebView );
8083
8184 mWidget ->setSizePolicy ( QSizePolicy::Expanding, QSizePolicy::Expanding );
8285 mWidget ->setLayout ( layout );
8386
84- // assure the map tip is never larger than half the map canvas
87+ // Assure the map tip is never larger than half the map canvas
8588 const int MAX_WIDTH = pMapCanvas->geometry ().width () / 2 ;
8689 const int MAX_HEIGHT = pMapCanvas->geometry ().height () / 2 ;
8790 mWidget ->setMaximumSize ( MAX_WIDTH, MAX_HEIGHT );
8891
89- // start with 0 size,
90- // the content will automatically make it grow up to MaximumSize
92+ // Start with 0 size,
93+ // The content will automatically make it grow up to MaximumSize
9194 mWidget ->resize ( 0 , 0 );
9295
9396 backgroundColor = mWidget ->palette ().base ().color ().name ();
@@ -114,7 +117,8 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
114117
115118 bodyStyle = QString (
116119 " background-color: %1;"
117- " margin: 0;" ).arg ( backgroundColor );
120+ " margin: 0;"
121+ " white-space: nowrap;" ).arg ( backgroundColor );
118122
119123 containerStyle = QString (
120124 " display: inline-block;"
@@ -134,30 +138,15 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
134138 lastTipText = tipText;
135139
136140 mWidget ->show ();
137-
138- #if WITH_QTWEBKIT
139- resizeContent ();
140- #endif
141141}
142142
143143void QgsMapTip::resizeContent ()
144144{
145145 // Get the content size
146146 QWebElement container = mWebView ->page ()->mainFrame ()->findFirstElement (
147147 QStringLiteral ( " #QgsWebViewContainer" ) );
148- int width = container.geometry ().width ();
149- int height = container.geometry ().height ();
150- int scrollbarWidth = mWebView ->page ()->mainFrame ()->scrollBarGeometry (
151- Qt::Vertical ).width ();
152- int scrollbarHeight = mWebView ->page ()->mainFrame ()->scrollBarGeometry (
153- Qt::Horizontal ).height ();
154-
155- if ( scrollbarWidth > 0 || scrollbarHeight > 0 )
156- {
157- width += 5 + scrollbarWidth;
158- height += 5 + scrollbarHeight;
159- }
160-
148+ int width = container.geometry ().width () + MARGIN_VALUE * 2 ;
149+ int height = container.geometry ().height () + MARGIN_VALUE * 2 ;
161150 mWidget ->resize ( width, height );
162151}
163152
@@ -169,7 +158,7 @@ void QgsMapTip::clear( QgsMapCanvas * )
169158 mWebView ->setHtml ( QString () );
170159 mWidget ->hide ();
171160
172- // reset the visible flag
161+ // Reset the visible flag
173162 mMapTipVisible = false ;
174163}
175164
@@ -220,7 +209,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, Qg
220209 return QString ();
221210}
222211
223- // This slot handles all clicks
212+ // This slot handles all clicks
224213void QgsMapTip::onLinkClicked ( const QUrl &url )
225214{
226215 QDesktopServices::openUrl ( url );
0 commit comments