[maptips] fix additional wrongly sized widget scenarios

This commit is contained in:
nirvn 2018-07-18 11:58:56 +07:00 committed by Mathieu Pellerin
parent 95e62036ae
commit 3e6db63ca4
3 changed files with 25 additions and 32 deletions

View File

@ -62,6 +62,7 @@ Clear the current maptip if it exists
:param mpMapCanvas: the canvas from which the tip should be cleared. :param mpMapCanvas: the canvas from which the tip should be cleared.
%End %End
}; };
/************************************************************************ /************************************************************************
* This file has been generated automatically from * * This file has been generated automatically from *

View File

@ -37,9 +37,8 @@
#include "qgsmaptip.h" #include "qgsmaptip.h"
QgsMapTip::QgsMapTip() QgsMapTip::QgsMapTip()
{ {
// init the visible flag // Init the visible flag
mMapTipVisible = false; mMapTipVisible = false;
} }
@ -60,6 +59,7 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
delete mWidget; delete mWidget;
mWidget = new QWidget( pMapCanvas ); mWidget = new QWidget( pMapCanvas );
mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
mWebView = new QgsWebView( mWidget ); mWebView = new QgsWebView( mWidget );
@ -70,24 +70,27 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
connect( mWebView, &QWebView::loadFinished, this, [ = ]( bool ) { resizeContent(); } ); connect( mWebView, &QWebView::loadFinished, this, [ = ]( bool ) { resizeContent(); } );
#endif #endif
mWebView->page()->settings()->setAttribute( mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
QWebSettings::DeveloperExtrasEnabled, true ); mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
mWebView->page()->settings()->setAttribute(
QWebSettings::JavascriptEnabled, true ); // Disable scrollbars, avoid random resizing issues
mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
mWebView->page()->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
QHBoxLayout *layout = new QHBoxLayout; QHBoxLayout *layout = new QHBoxLayout;
layout->setContentsMargins( 0, 0, 0, 0 );
layout->addWidget( mWebView ); layout->addWidget( mWebView );
mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
mWidget->setLayout( layout ); mWidget->setLayout( layout );
//assure the map tip is never larger than half the map canvas // Assure the map tip is never larger than half the map canvas
const int MAX_WIDTH = pMapCanvas->geometry().width() / 2; const int MAX_WIDTH = pMapCanvas->geometry().width() / 2;
const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2; const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2;
mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT ); mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );
// start with 0 size, // Start with 0 size,
// the content will automatically make it grow up to MaximumSize // The content will automatically make it grow up to MaximumSize
mWidget->resize( 0, 0 ); mWidget->resize( 0, 0 );
backgroundColor = mWidget->palette().base().color().name(); backgroundColor = mWidget->palette().base().color().name();
@ -114,7 +117,8 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
bodyStyle = QString( bodyStyle = QString(
"background-color: %1;" "background-color: %1;"
"margin: 0;" ).arg( backgroundColor ); "margin: 0;"
"white-space: nowrap;" ).arg( backgroundColor );
containerStyle = QString( containerStyle = QString(
"display: inline-block;" "display: inline-block;"
@ -134,10 +138,6 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
lastTipText = tipText; lastTipText = tipText;
mWidget->show(); mWidget->show();
#if WITH_QTWEBKIT
resizeContent();
#endif
} }
void QgsMapTip::resizeContent() void QgsMapTip::resizeContent()
@ -145,19 +145,8 @@ void QgsMapTip::resizeContent()
// Get the content size // Get the content size
QWebElement container = mWebView->page()->mainFrame()->findFirstElement( QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
QStringLiteral( "#QgsWebViewContainer" ) ); QStringLiteral( "#QgsWebViewContainer" ) );
int width = container.geometry().width(); int width = container.geometry().width() + MARGIN_VALUE * 2;
int height = container.geometry().height(); int height = container.geometry().height() + MARGIN_VALUE * 2;
int scrollbarWidth = mWebView->page()->mainFrame()->scrollBarGeometry(
Qt::Vertical ).width();
int scrollbarHeight = mWebView->page()->mainFrame()->scrollBarGeometry(
Qt::Horizontal ).height();
if ( scrollbarWidth > 0 || scrollbarHeight > 0 )
{
width += 5 + scrollbarWidth;
height += 5 + scrollbarHeight;
}
mWidget->resize( width, height ); mWidget->resize( width, height );
} }
@ -169,7 +158,7 @@ void QgsMapTip::clear( QgsMapCanvas * )
mWebView->setHtml( QString() ); mWebView->setHtml( QString() );
mWidget->hide(); mWidget->hide();
// reset the visible flag // Reset the visible flag
mMapTipVisible = false; mMapTipVisible = false;
} }
@ -220,7 +209,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, Qg
return QString(); return QString();
} }
//This slot handles all clicks // This slot handles all clicks
void QgsMapTip::onLinkClicked( const QUrl &url ) void QgsMapTip::onLinkClicked( const QUrl &url )
{ {
QDesktopServices::openUrl( url ); QDesktopServices::openUrl( url );

View File

@ -78,6 +78,11 @@ class GUI_EXPORT QgsMapTip : public QWidget
* \param mpMapCanvas the canvas from which the tip should be cleared. * \param mpMapCanvas the canvas from which the tip should be cleared.
*/ */
void clear( QgsMapCanvas *mpMapCanvas = nullptr ); void clear( QgsMapCanvas *mpMapCanvas = nullptr );
private slots:
void onLinkClicked( const QUrl &url );
void resizeContent();
private: private:
// Fetch the feature to use for the maptip text. // Fetch the feature to use for the maptip text.
// Only the first feature in the search radius is used // Only the first feature in the search radius is used
@ -94,8 +99,6 @@ class GUI_EXPORT QgsMapTip : public QWidget
QWidget *mWidget = nullptr; QWidget *mWidget = nullptr;
QgsWebView *mWebView = nullptr; QgsWebView *mWebView = nullptr;
private slots: const int MARGIN_VALUE = 5;
void onLinkClicked( const QUrl &url );
void resizeContent();
}; };
#endif // QGSMAPTIP_H #endif // QGSMAPTIP_H