Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[maptips] fix additional wrongly sized widget scenarios
  • Loading branch information
nirvn committed Jul 19, 2018
1 parent 95e6203 commit 3e6db63
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
1 change: 1 addition & 0 deletions python/gui/auto_generated/qgsmaptip.sip.in
Expand Up @@ -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 *
Expand Down
47 changes: 18 additions & 29 deletions src/gui/qgsmaptip.cpp
Expand Up @@ -37,9 +37,8 @@
#include "qgsmaptip.h"

QgsMapTip::QgsMapTip()

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

Expand All @@ -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 );


Expand All @@ -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();
Expand All @@ -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;"
Expand All @@ -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 );
}

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

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

Expand Down Expand Up @@ -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 );
Expand Down
9 changes: 6 additions & 3 deletions src/gui/qgsmaptip.h
Expand Up @@ -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
Expand All @@ -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.