18
18
#include " qgsvectorlayer.h"
19
19
#include " qgsexpression.h"
20
20
#include " qgslogger.h"
21
+ #include " qgswebview.h"
22
+ #include " qgswebframe.h"
21
23
22
24
// Qt includes
23
25
#include < QPoint>
24
26
#include < QToolTip>
25
27
#include < QSettings>
28
+ #include < QLabel>
29
+ #include < QWebElement>
30
+ #include < QHBoxLayout>
31
+
26
32
27
33
#include " qgsmaptip.h"
28
34
29
35
QgsMapTip::QgsMapTip ()
36
+ : mWidget( nullptr ), mWebView( nullptr )
30
37
{
31
38
// init the visible flag
32
39
mMapTipVisible = false ;
@@ -37,38 +44,115 @@ QgsMapTip::~QgsMapTip()
37
44
38
45
}
39
46
40
- void QgsMapTip::showMapTip ( QgsMapLayer *thepLayer ,
41
- QgsPoint & theMapPosition ,
47
+ void QgsMapTip::showMapTip ( QgsMapLayer *pLayer ,
48
+ QgsPoint & mapPosition ,
42
49
QPoint & thePixelPosition,
43
- QgsMapCanvas *thepMapCanvas )
50
+ QgsMapCanvas *pMapCanvas )
44
51
{
45
- // Do the search using the active layer and the preferred label
46
- // field for the layer. The label field must be defined in the layer configuration
52
+ // Do the search using the active layer and the preferred label field for the
53
+ // layer. The label field must be defined in the layer configuration
47
54
// file/database. The code required to do this is similar to identify, except
48
55
// we only want the first qualifying feature and we will only display the
49
- // field defined as the label field in the layer configuration file/database.
50
- //
51
- // TODO: Define the label (display) field for each map layer in the map configuration file/database
56
+ // field defined as the label field in the layer configuration file/database
52
57
53
58
// Show the maptip on the canvas
54
- QString myTipText = fetchFeature ( thepLayer, theMapPosition, thepMapCanvas );
55
- mMapTipVisible = !myTipText.isEmpty ();
59
+ QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle,
60
+ backgroundColor, borderColor;
61
+
62
+ delete mWidget ;
63
+ mWidget = new QWidget ( pMapCanvas );
64
+ mWebView = new QgsWebView ( mWidget );
65
+
66
+ mWebView ->page ()->settings ()->setAttribute (
67
+ QWebSettings::DeveloperExtrasEnabled, true );
68
+ mWebView ->page ()->settings ()->setAttribute (
69
+ QWebSettings::JavascriptEnabled, true );
70
+
71
+ QHBoxLayout* layout = new QHBoxLayout;
72
+ layout->addWidget ( mWebView );
73
+
74
+ mWidget ->setSizePolicy ( QSizePolicy::Expanding, QSizePolicy::Expanding );
75
+ mWidget ->setLayout ( layout );
76
+
77
+ // assure the map tip is never larger than half the map canvas
78
+ const int MAX_WIDTH = pMapCanvas->geometry ().width () / 2 ;
79
+ const int MAX_HEIGHT = pMapCanvas->geometry ().height () / 2 ;
80
+ mWidget ->setMaximumSize ( MAX_WIDTH, MAX_HEIGHT );
81
+
82
+ // start with 0 size,
83
+ // the content will automatically make it grow up to MaximumSize
84
+ mWidget ->resize ( 0 , 0 );
85
+
86
+ backgroundColor = mWidget ->palette ().base ().color ().name ();
87
+ borderColor = mWidget ->palette ().shadow ().color ().name ();
88
+ mWidget ->setStyleSheet ( QString (
89
+ " .QWidget{"
90
+ " border: 1px solid %1;"
91
+ " background-color: %2;}" ).arg (
92
+ borderColor, backgroundColor ) );
56
93
57
- if ( mMapTipVisible )
94
+ tipText = fetchFeature ( pLayer, mapPosition, pMapCanvas );
95
+
96
+ mMapTipVisible = !tipText.isEmpty ();
97
+ if ( !mMapTipVisible )
58
98
{
59
- QToolTip::showText ( thepMapCanvas->mapToGlobal ( thePixelPosition ), myTipText, thepMapCanvas );
60
- // store the point so we can use it to clear the maptip later
61
- mLastPosition = thePixelPosition;
99
+ clear ();
100
+ return ;
101
+ }
102
+
103
+ if ( tipText == lastTipText )
104
+ {
105
+ return ;
106
+ }
107
+
108
+ bodyStyle = QString (
109
+ " background-color: %1;"
110
+ " margin: 0;" ).arg ( backgroundColor );
111
+
112
+ containerStyle = QString (
113
+ " display: inline-block;"
114
+ " margin: 0px" );
115
+
116
+ tipHtml = QString (
117
+ " <html>"
118
+ " <body style='%1'>"
119
+ " <div id='QgsWebViewContainer' style='%2'>%3</div>"
120
+ " </body>"
121
+ " </html>" ).arg ( bodyStyle, containerStyle, tipText );
122
+
123
+ mWidget ->move ( thePixelPosition.x (),
124
+ thePixelPosition.y () );
125
+
126
+ mWebView ->setHtml ( tipHtml );
127
+ lastTipText = tipText;
128
+
129
+ mWidget ->show ();
130
+
131
+ int scrollbarWidth = mWebView ->page ()->mainFrame ()->scrollBarGeometry (
132
+ Qt::Vertical ).width ();
133
+ int scrollbarHeight = mWebView ->page ()->mainFrame ()->scrollBarGeometry (
134
+ Qt::Horizontal ).height ();
135
+
136
+ if ( scrollbarWidth > 0 || scrollbarHeight > 0 )
137
+ {
138
+ // Get the content size
139
+ QWebElement container = mWebView ->page ()->mainFrame ()->findFirstElement (
140
+ " #QgsWebViewContainer" );
141
+ int width = container.geometry ().width () + 5 + scrollbarWidth;
142
+ int height = container.geometry ().height () + 5 + scrollbarHeight;
143
+
144
+ mWidget ->resize ( width, height );
62
145
}
63
146
}
64
147
65
- void QgsMapTip::clear ( QgsMapCanvas *mpMapCanvas )
148
+ void QgsMapTip::clear ( QgsMapCanvas * )
66
149
{
67
150
if ( !mMapTipVisible )
68
151
return ;
69
152
70
- // set the maptip to blank
71
- QToolTip::showText ( mpMapCanvas->mapToGlobal ( mLastPosition ), " " , mpMapCanvas );
153
+ mWebView ->setHtml ( QString () );
154
+ mWidget ->hide ();
155
+
72
156
// reset the visible flag
73
157
mMapTipVisible = false ;
74
158
}
@@ -77,7 +161,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsM
77
161
{
78
162
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
79
163
if ( !vlayer )
80
- return " " ;
164
+ return QString () ;
81
165
82
166
double searchRadius = QgsMapTool::searchRadiusMU ( mpMapCanvas );
83
167
@@ -92,7 +176,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsM
92
176
QgsFeature feature;
93
177
94
178
if ( !vlayer->getFeatures ( QgsFeatureRequest ().setFilterRect ( r ).setFlags ( QgsFeatureRequest::ExactIntersect ) ).nextFeature ( feature ) )
95
- return " " ;
179
+ return QString () ;
96
180
97
181
int idx = vlayer->fieldNameIndex ( vlayer->displayField () );
98
182
if ( idx < 0 )
@@ -108,5 +192,7 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPoint &mapPosition, QgsM
108
192
return QgsExpression::replaceExpressionText ( vlayer->displayField (), &context );
109
193
}
110
194
else
195
+ {
111
196
return feature.attribute ( idx ).toString ();
197
+ }
112
198
}
0 commit comments