Skip to content
Permalink
Browse files

[maptips] fix additional wrongly sized widget scenarios

  • Loading branch information
nirvn committed Jul 18, 2018
1 parent 95e6203 commit 3e6db63ca452b2c2c354ed031b49d928ce434f9c
Showing with 25 additions and 32 deletions.
  1. +1 −0 python/gui/auto_generated/qgsmaptip.sip.in
  2. +18 −29 src/gui/qgsmaptip.cpp
  3. +6 −3 src/gui/qgsmaptip.h
@@ -62,6 +62,7 @@ Clear the current maptip if it exists

:param mpMapCanvas: the canvas from which the tip should be cleared.
%End

};
/************************************************************************
* This file has been generated automatically from *
@@ -37,9 +37,8 @@
#include "qgsmaptip.h"

QgsMapTip::QgsMapTip()

{
// init the visible flag
// Init the visible flag
mMapTipVisible = false;
}

@@ -60,6 +59,7 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,

delete mWidget;
mWidget = new QWidget( pMapCanvas );
mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
mWebView = new QgsWebView( mWidget );


@@ -70,24 +70,27 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
connect( mWebView, &QWebView::loadFinished, this, [ = ]( bool ) { resizeContent(); } );
#endif

mWebView->page()->settings()->setAttribute(
QWebSettings::DeveloperExtrasEnabled, true );
mWebView->page()->settings()->setAttribute(
QWebSettings::JavascriptEnabled, true );
mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, 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;
layout->setContentsMargins( 0, 0, 0, 0 );
layout->addWidget( mWebView );

mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
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_HEIGHT = pMapCanvas->geometry().height() / 2;
mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT );

// start with 0 size,
// the content will automatically make it grow up to MaximumSize
// Start with 0 size,
// The content will automatically make it grow up to MaximumSize
mWidget->resize( 0, 0 );

backgroundColor = mWidget->palette().base().color().name();
@@ -114,7 +117,8 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,

bodyStyle = QString(
"background-color: %1;"
"margin: 0;" ).arg( backgroundColor );
"margin: 0;"
"white-space: nowrap;" ).arg( backgroundColor );

containerStyle = QString(
"display: inline-block;"
@@ -134,30 +138,15 @@ void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
lastTipText = tipText;

mWidget->show();

#if WITH_QTWEBKIT
resizeContent();
#endif
}

void QgsMapTip::resizeContent()
{
// Get the content size
QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
QStringLiteral( "#QgsWebViewContainer" ) );
int width = container.geometry().width();
int height = container.geometry().height();
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;
}

int width = container.geometry().width() + MARGIN_VALUE * 2;
int height = container.geometry().height() + MARGIN_VALUE * 2;
mWidget->resize( width, height );
}

@@ -169,7 +158,7 @@ void QgsMapTip::clear( QgsMapCanvas * )
mWebView->setHtml( QString() );
mWidget->hide();

// reset the visible flag
// Reset the visible flag
mMapTipVisible = false;
}

@@ -220,7 +209,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, Qg
return QString();
}

//This slot handles all clicks
// This slot handles all clicks
void QgsMapTip::onLinkClicked( const QUrl &url )
{
QDesktopServices::openUrl( url );
@@ -78,6 +78,11 @@ class GUI_EXPORT QgsMapTip : public QWidget
* \param mpMapCanvas the canvas from which the tip should be cleared.
*/
void clear( QgsMapCanvas *mpMapCanvas = nullptr );

private slots:
void onLinkClicked( const QUrl &url );
void resizeContent();

private:
// Fetch the feature to use for the maptip text.
// Only the first feature in the search radius is used
@@ -94,8 +99,6 @@ class GUI_EXPORT QgsMapTip : public QWidget
QWidget *mWidget = nullptr;
QgsWebView *mWebView = nullptr;

private slots:
void onLinkClicked( const QUrl &url );
void resizeContent();
const int MARGIN_VALUE = 5;
};
#endif // QGSMAPTIP_H

0 comments on commit 3e6db63

Please sign in to comment.
You can’t perform that action at this time.