/
qgsmaptip.cpp
238 lines (197 loc) · 7.72 KB
/
qgsmaptip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***************************************************************************
qgsmaptips.cpp - Query a layer and show a maptip on the canvas
---------------------
begin : October 2007
copyright : (C) 2007 by Gary Sherman
email : sherman @ mrcc dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// QGIS includes
#include "qgsfeatureiterator.h"
#include "qgsmapcanvas.h"
#include "qgsmaptool.h"
#include "qgsvectorlayer.h"
#include "qgsexpression.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgswebview.h"
#include "qgswebframe.h"
// Qt includes
#include <QPoint>
#include <QToolTip>
#include <QSettings>
#include <QLabel>
#include <QDesktopServices>
#if WITH_QTWEBKIT
#include <QWebElement>
#endif
#include <QHBoxLayout>
#include "qgsmaptip.h"
QgsMapTip::QgsMapTip()
{
// Init the visible flag
mMapTipVisible = false;
// Init font-related values
applyFontSettings();
}
void QgsMapTip::showMapTip( QgsMapLayer *pLayer,
QgsPointXY &mapPosition,
QPoint &pixelPosition,
QgsMapCanvas *pMapCanvas )
{
// Do the search using the active layer and the preferred label field for the
// layer. The label field must be defined in the layer configuration
// file/database. The code required to do this is similar to identify, except
// we only want the first qualifying feature and we will only display the
// field defined as the label field in the layer configuration file/database
// Show the maptip on the canvas
QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
backgroundColor, strokeColor;
delete mWidget;
mWidget = new QWidget( pMapCanvas );
mWidget->setContentsMargins( MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE, MARGIN_VALUE );
mWebView = new QgsWebView( mWidget );
#if WITH_QTWEBKIT
mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );//Handle link clicks by yourself
mWebView->setContextMenuPolicy( Qt::NoContextMenu ); //No context menu is allowed if you don't need it
connect( mWebView, &QWebView::linkClicked, this, &QgsMapTip::onLinkClicked );
connect( mWebView, &QWebView::loadFinished, this, [ = ]( bool ) { resizeContent(); } );
#endif
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
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
mWidget->resize( 0, 0 );
backgroundColor = mWidget->palette().base().color().name();
strokeColor = mWidget->palette().shadow().color().name();
mWidget->setStyleSheet( QString(
".QWidget{"
"border: 1px solid %1;"
"background-color: %2;}" ).arg(
strokeColor, backgroundColor ) );
tipText = fetchFeature( pLayer, mapPosition, pMapCanvas );
mMapTipVisible = !tipText.isEmpty();
if ( !mMapTipVisible )
{
clear();
return;
}
if ( tipText == lastTipText )
{
return;
}
bodyStyle = QString(
"background-color: %1;"
"margin: 0;"
"white-space: nowrap;"
"font: %2pt \"%3\";" ).arg( backgroundColor ).arg( mFontSize ).arg( mFontFamily );
containerStyle = QString(
"display: inline-block;"
"margin: 0px" );
tipHtml = QString(
"<html>"
"<body style='%1'>"
"<div id='QgsWebViewContainer' style='%2'>%3</div>"
"</body>"
"</html>" ).arg( bodyStyle, containerStyle, tipText );
QgsDebugMsg( tipHtml );
mWidget->move( pixelPosition.x(),
pixelPosition.y() );
mWebView->setHtml( tipHtml );
lastTipText = tipText;
mWidget->show();
}
void QgsMapTip::resizeContent()
{
// Get the content size
QWebElement container = mWebView->page()->mainFrame()->findFirstElement(
QStringLiteral( "#QgsWebViewContainer" ) );
int width = container.geometry().width() + MARGIN_VALUE * 2;
int height = container.geometry().height() + MARGIN_VALUE * 2;
mWidget->resize( width, height );
}
void QgsMapTip::clear( QgsMapCanvas * )
{
if ( !mMapTipVisible )
return;
mWebView->setHtml( QString() );
mWidget->hide();
// Reset the visible flag
mMapTipVisible = false;
}
QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, QgsMapCanvas *mapCanvas )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
return QString();
double searchRadius = QgsMapTool::searchRadiusMU( mapCanvas );
QgsRectangle r;
r.setXMinimum( mapPosition.x() - searchRadius );
r.setYMinimum( mapPosition.y() - searchRadius );
r.setXMaximum( mapPosition.x() + searchRadius );
r.setYMaximum( mapPosition.y() + searchRadius );
r = mapCanvas->mapSettings().mapToLayerCoordinates( layer, r );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vlayer ) );
if ( mapCanvas )
context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );
QString mapTip = vlayer->mapTipTemplate();
QString tipString;
QgsExpression exp( vlayer->displayExpression() );
QgsFeature feature;
QgsFeatureRequest request = QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect );
if ( mapTip.isEmpty() )
{
exp.prepare( &context );
request.setSubsetOfAttributes( exp.referencedColumns(), vlayer->fields() );
}
QgsFeatureIterator it = vlayer->getFeatures( request );
QTime timer;
timer.start();
while ( it.nextFeature( feature ) )
{
context.setFeature( feature );
if ( !mapTip.isEmpty() )
{
tipString = QgsExpression::replaceExpressionText( mapTip, &context );
}
else
{
tipString = exp.evaluate( &context ).toString();
}
if ( !tipString.isEmpty() || timer.elapsed() >= 1000 )
{
break;
}
}
return tipString;
}
void QgsMapTip::applyFontSettings()
{
QgsSettings settings;
QFont defaultFont = qApp->font();
mFontSize = settings.value( QStringLiteral( "/qgis/stylesheet/fontPointSize" ), defaultFont.pointSize() ).toInt();
mFontFamily = settings.value( QStringLiteral( "/qgis/stylesheet/fontFamily" ), defaultFont.family() ).toString();
}
// This slot handles all clicks
void QgsMapTip::onLinkClicked( const QUrl &url )
{
QDesktopServices::openUrl( url );
}